案例:一个简单的表Inventory有100以上的信息,其中包含:
Item                   Color              Quantity
--------------------------------------------------
Table                  Blue                    124
Table                  Red                     223
Chair                  Blue                    101
Chair                  Red                    Null
Chair                  Red                    210
........
以下请使用sql或Oracle语句完成:1,按Item分类,列出各类商品的数量.
2,按Item分类,仅列出各类商品中红色多于蓝色的商品名称及差额数量.
3,按Item分类,将数据按下列方式进行统计显示:
Item           Red             Blue

解决方案 »

  1.   

    select 
    Item,
    sum(case when Color='Red' then isnull(Quantity,0) else 0 end) as Red,
    sum(case when Color='Blue' then isnull(Quantity,0) else 0 end) as Blue
    from 
    Inventory 
    group by Item
      

  2.   

    1,按Item分类,列出各类商品的数量. select item,count(*) 数量 from inventory group by item2,按Item分类,仅列出各类商品中红色多于蓝色的商品名称及差额数量. select m.item , m.数量-n.数量 差额数量 from
    (select item,count(*) 数量 from inventory where Color = 'Red' group by item) m
    (select item,count(*) 数量 from inventory where Color = 'Blue' group by item) n
    where m.item = n.item and m.数量 > n.数量
    3,按Item分类,将数据按下列方式进行统计显示: 
    Item                       Red                           Blueselect item ,
      sum(case color when 'red' then quantity else 0 end) Red,
      sum(case color when 'blue' then quantity else 0 end) Blue
    from tb
    group by item
      

  3.   

    select 
    *,Red-Blue as 红色多于蓝色的商品名称及差额数量
    from 
    (select 
    Item,
    sum(case when Color='Red' then isnull(Quantity,0) else 0 end) as Red,
    sum(case when Color='Blue' then isnull(Quantity,0) else 0 end) as Blue
    from 
    Inventory 
    group by Item)T
    where
    Red>Blue
      

  4.   


    --1,按Item分类,列出各类商品的数量.
    --2,按Item分类,仅列出各类商品中红色多于蓝色的商品名称及差额数量.
    --3,按Item分类,将数据按下列方式进行统计显示:
    --Item     Red      Blue
    declare @tb table (item varchar(10),color varchar(10),quantity int)
    insert into @tb select 'Table','Blue',124
    insert into @tb select 'Table','Red',223
    insert into @tb select 'Chair','Blue',101
    insert into @tb select 'Chair','Red',null
    insert into @tb select 'Chair','Red',210
    --1
    select item,count(item) as 'count' from @tb group by item
    --2
    select tpa.item,quantitya-quantityb as '差' from (
    select item,sum(quantity) as quantitya from @tb where color='red' group by item) tpa left join (
    select item,sum(quantity) as quantityb from @tb where color='blue' group by item)tpb 
    on tpa.item=tpb.item
    where quantitya>quantityb
    --3
    select item,
    sum(case when color='red' then quantity else 0 end) as 'red',
    sum(case when color='blue' then quantity else 0 end) as 'blue'
    from @tb
    group by item
      

  5.   


    create table inventory (item varchar(10),color varchar(10),quantity int)
    insert into inventory select 'Table','Blue',124
    insert into inventory select 'Table','Red',223
    insert into inventory select 'Chair','Blue',101
    insert into inventory select 'Chair','Red',0
    insert into inventory select 'Chair','Red',210
    insert into inventory select 'Chair','Blue',301--1、
    select item,sum(Quantity)Quantity from inventory group by item--2、
    select * from 
    (select Item,sum(case when Color='Red' then isnull(Quantity,0) else 0 end)-
    sum(case when Color='Blue' then isnull(Quantity,0) else 0 end) [差]
    from Inventory group by Item)a
    where [差]>0--3没有样式。
      

  6.   


    select 
    Item,
    sum(Quantity) as 各类商品的数量,from 
    Inventory 
    group by Item
      

  7.   

    create table tb(Item varchar(10), Color varchar(10), Quantity int)
    insert into tb values('Table', 'Blue', 124 )
    insert into tb values('Table', 'Red' , 223) 
    insert into tb values('Chair', 'Blue', 101) 
    insert into tb values('Chair', 'Red' , Null )
    insert into tb values('Chair', 'Red' , 210 )
    go--1
    select item,count(*) 数量 from tb group by item
    /*
    item       数量          
    ---------- ----------- 
    Chair      3
    Table      2(所影响的行数为 2 行)
    */--2
    select m.item , m.数量-n.数量 差额数量 from
    (select item,count(*) 数量 from tb where Color = 'Red' group by item) m,
    (select item,count(*) 数量 from tb where Color = 'Blue' group by item) n
    where m.item = n.item and m.数量 > n.数量
    /*
    item       差额数量        
    ---------- ----------- 
    Chair      1(所影响的行数为 1 行)
    */--3
    select item ,
      sum(case color when 'red' then quantity else 0 end) Red,
      sum(case color when 'blue' then quantity else 0 end) Blue
    from tb
    group by item
    /*
    item       Red         Blue        
    ---------- ----------- ----------- 
    Chair      210         101
    Table      223         124(所影响的行数为 2 行)
    */drop table tb