如果满足条件,则SQL将多行合并为一行

时间:2011-04-28 16:21:18

标签: sql sql-server-2008

我是新手,这也不是我的数据。我们正在尝试将多条记录拉入一条记录中以查询结果。

为简化表格和数据量,以下两个表格就足够了:

表1

PRVDR_NUM | RPT_REC_NUM  
013          1234
014          4567

表2

RPT_REC_NUM |  WKSHT_CD | LINE_NUM | ITM_VAL_NUM
1234             f1         3           30
1234             f1         9           3
1234             e2       100           100
4567             f1         3           20
4567             f1         9           8
4567             e2       100           100

第一部分是获取WKSHT_CD f1和LINE_NUM 3的ITM_NUM_CAL介于25和50之间的所有记录,ITM_NUM_VAL是收入。

SELECT      r.PRVDR_NUM as Provider, 
            r.RPT_REC_NUM as 'Report Record', 
            n.ITM_VAL_NUM as Revenue

FROM        Table 1 r
        left outer join Table 2 n on 
        r.RPT_REC_NUM = n.RPT_REC_NUM

WHERE (n.WKSHT_CD = 'f1') and (n.LINE_NUM = '3')
        and (n.ITM_VAL_NUM > 25) and (n.ITM_VAL_NUM < 50)
        and left(r.PRVDR_NUM, 2) in ('01','04','11','18','25','26','34','44','49')

所以我们的结果是:

Provider |  Report Record | Revenue 
 013           1234         30    

但我们也希望能够为WKSHT_CD f1和LINE_NUM 9提取相应的ITM_VAL_NUM,我们称之为成本。

所以结果应该是:

Provider |  Report Record | Revenue | Cost
 013           1234          30         3

提前非常感谢。

修改

我相信这是我在一些机动和添加新变量CLMN_NUM之后寻找的最终查询,这是表2中的另一列。

SELECT r.PRVDR_NUM as Provider, 
       r.RPT_REC_NUM as 'Report Record', 
       n.ITM_VAL_NUM as Revenue,
       n2.ITM_VAL_NUM as 'Cost',
       n3.ITM_VAL_NUM as 'Visits'

FROM table1 r
    LEFT OUTER JOIN table2 n on r.RPT_REC_NUM = n.RPT_REC_NUM
    LEFT OUTER JOIN table2 n2 on n.WKSHT_CD = n2.WKSHT_CD
    and n.RPT_REC_NUM = n2.RPT_REC_NUM and n2.LINE_NUM = 9
    LEFT OUTER JOIN table2 n3 on r.RPT_REC_NUM = n3.RPT_REC_NUM 
    and n3.WKSHT_CD = 'e2' and n3.LINE_NUM = 100 and n3.CLMN_NUM = xxxx  

WHERE (n.WKSHT_CD = 'f1') 
AND (n.LINE_NUM = '3')
AND (n.ITM_VAL_NUM > 25) 
AND (n.ITM_VAL_NUM < 50)
AND left(r.PRVDR_NUM, 2) in ('01','04','11','18','25','26','34','44','49')

3 个答案:

答案 0 :(得分:0)

有几种方式:

子查询

SELECT r.PRVDR_NUM as Provider, 
       r.RPT_REC_NUM as 'Report Record', 
       n.ITM_VAL_NUM as Revenue
       (SELECT ITM_VAL_NUM 
        FROM Table2 
        WHERE WKSHT_CD = n.WKSHT_CD 
        AND LINE_NUM = 9) AS Cost
FROM Table1 r
    LEFT OUTER JOIN Table2 n on r.RPT_REC_NUM = n.RPT_REC_NUM
WHERE (n.WKSHT_CD = 'f1') 
AND (n.LINE_NUM = '3')
AND (n.ITM_VAL_NUM > 25) 
AND (n.ITM_VAL_NUM < 50)
AND left(r.PRVDR_NUM, 2) in ('01','04','11','18','25','26','34','44','49')

另一个JOIN

SELECT r.PRVDR_NUM as Provider, 
       r.RPT_REC_NUM as 'Report Record', 
       n.ITM_VAL_NUM as Revenue
       n2.Cost
FROM Table1 r
    LEFT OUTER JOIN Table2 n on r.RPT_REC_NUM = n.RPT_REC_NUM
    LEFT OUTER JOIN Table2 n2 on n.WKSHT_CD = n2.WKSHT_CD
        AND LINE_NUM = 9
WHERE (n.WKSHT_CD = 'f1') 
AND (n.LINE_NUM = '3')
AND (n.ITM_VAL_NUM > 25) 
AND (n.ITM_VAL_NUM < 50)
AND left(r.PRVDR_NUM, 2) in ('01','04','11','18','25','26','34','44','49')

答案 1 :(得分:0)

您可以在相关列上执行CASE / WHEN,但在您的位置同时允许LINE_NUM的BOTH值...如果没有一侧,另一侧或两者的记录,则使用COALESCE将阻止NULL值收入/成本分别。您可以对要包含的任何其他“LINE_NUM”值使用相同的考虑因素。

SELECT
      r.PRVDR_NUM as Provider,
      r.RPT_REC_NUM as 'Report Record',
      COALESCE( CASE WHEN n.LINE_NUM = 3 THEN n.ITM_VAL_NUM ELSE 0 END, 0 ) as Revenue,
      COALESCE( CASE WHEN n.LINE_NUM = 9 THEN n.ITM_VAL_NUM ELSE 0 END, 0 ) as Costs
   from
      Table_1 r
         left join Table_2 n
            on r.RPT_REC_NUM = n.RPT_REC_NUM
            AND n.WKSHT_CD = 'f1'
            AND n.LINE_NUM in ( '3', '9')
            AND n.ITM_VAL_NUM between 25 and 50
   where
      left(r.PRVDR_NUM, 2) in ('01','04','11','18','25','26','34','44','49') 

答案 2 :(得分:0)

SELECT
  Provider        = r.PRVDR_NUM,
  [Report Record] = r.RPT_REC_NUM,
  Revenue         = MIN(CASE n.LINE_NUM WHEN '3' THEN n.ITM_VAL_NUM),
  Cost            = MIN(CASE n.LINE_NUM WHEN '9' THEN n.ITM_VAL_NUM)
FROM Table1 r
  INNER JOIN Table2 n ON r.RPT_REC_NUM = n.RPT_REC_NUM
WHERE n.WKSHT_CD = 'f1' AND n.LINE_NUM IN ('3', '9')
  AND n.ITM_VAL_NUM > 25 AND n.ITM_VAL_NUM < 50
  AND LEFT(r.PRVDR_NUM, 2) IN ('01','04','11','18','25','26','34','44','49')
GROUP BY
  r.PRVDR_NUM,
  r.RPT_REC_NUM