sql分組相關

2021-08-31 04:37:11 字數 1190 閱讀 1711

/*  create table inventory (

item  varchar2(100),

color varchar2(100),

quantity number(10)

)*//*

insert into inventory values('table','blue',124);

insert into inventory values('table','red',223);

insert into inventory values('chair','blue',101);

insert into inventory values('chair','red',null);

insert into inventory values('chair','red',210); */

1、紅色商品的數量總和

select  color, sum(nvl(quantity,0)) from inventory a where a.color='red' group by a.color;

2、按item分類,列出各類商品的數量

select  item, sum(nvl(quantity,0)) from inventory a group by a.item;

3、按item分類,僅列出各類商品中紅色多於藍色的商品名稱及差額數量

select  a.item,  nvl(a.quantity,0)-nvl(b.quantity,0) from inventory a, inventory b

where nvl(a.quantity,0)>nvl(b.quantity,0)  and  a.color='red'

and b.color='blue' and a.item=b.item ;

4、按item分類,將資料按下列方式進行統計顯示

item  red  blue

select item , sum(nvl(red,0)) as red , sum(nvl(blue,0)) as blue from (select item,decode(color,'red',quantity,null) as red  ,decode(color,'blue',quantity,null) as blue 

from  inventory) a

group by  a.item  order by item desc

SQL分組排序

不得不承認,我腦子梗塞的很嚴重。表 gcc bloginfo blogid uid blogtitle createtime issketch checkright 1 1 hello 2010 04 01 2 1 2 1 hello2 2010 04 02 2 1 3 1 hello3 2010 ...

sql分組排名

資料庫teradata 班級科目成績表 create multiset table pd portal.aaa no fallback no before journal,no after journal,checksum default banji varchar 40 character set...

SQL分組統計

全國各省份人口數排前三的城市 思路 先按照省份分組,再組內按照人口數排序取前三 mysql set num 0 class select from select p.num if class province,num 1 1 as rank,class p.province as pclass fr...