Write advanced SQL Select

前端 未结 3 1366
春和景丽
春和景丽 2020-12-18 13:07

Item table:

|   Item    |   Qnty    |   ProdSched   |
|    a      |    1      |       1       |
|    b      |    2      |       1       |
|    c      |    3          


        
3条回答
  •  悲&欢浪女
    2020-12-18 13:55

    Let's hit this in two phases. First, although this is not the exact format you wanted, you can get the data you asked for as follows:

    Select item, ProdSched, max(qty)
      from Item1
     group by item,ProdSched
    

    Now, to get the data in the format you desired, one way of accomplishing it is a PIVOT table. You can cook up a pivot table in SQL Server as follows:

    Select item, [1] as ProdSched1, [2] as ProdSched2
    from ( Select Item, Qty, ProdSched
             from item1 ) x
    Pivot ( Max(qty) for ProdSched in ([1],[2]))  y
    

提交回复
热议问题