同一查询和行中的多个Where条件

时间:2017-10-10 09:26:36

标签: sql

我有一个包含如下数据的表格(有更多的ITEMID,每个都有MODULESTYPE 0,1和2的值

ITEMID  MODULETYPE  UNITID
E-MA0860S   0   base
E-MA0860S   1   purch
E-MA0860S   2   sales

我想做的是将其分成一行

ITEMID  Base UOM    PUOM    SUOM
E-MA0860S   base    purch   sales

2 个答案:

答案 0 :(得分:0)

尝试一些这样的想法

SELECT ItemID, 
       MIN(CASE WHEN MODULETYPE=0 THEN UNITID END) AS [Base UOM]
       MIN(CASE WHEN MODULETYPE=1 THEN UNITID END) AS [PUOM]   
       MIN(CASE WHEN MODULETYPE=2 THEN UNITID END) AS [SUOM]
FROM TableName
GROUP BY ItemID

答案 1 :(得分:0)

你可以使用自我加入

  select  a.ITEMID,  a.UNITID as UOM, b.UNITID AS  PUOM , c.UNITID as SUOM
  from my_table a
  inner join my_table b on a.ITEMID =  b.ITEMID and b.MODULETYPE=1
  inner join my_table c on a.ITEMID =  c.ITEMID and c.MODULETYPE=2
  where a.MODULETYPE = 0