我想把所有的行都卷成一个。我已经尝试了MAX Case,但是在这种情况下有一个非数字字符串,并且它不能用于汇总数据。我试图找到其他的例子,但不能这样,我要求你的帮助。提前致谢
我有两个具有不同数据的字段但都具有相同的控制编号,我需要将控制编号的所有数据汇总到一行。
SELECT CNTL_NUM
,TRANS_TYP
,CASE WHEN SEQ_NUM = '02' THEN SUBSTRING(TRANS_INFO,28,9) END AS 'SSN'
,CASE WHEN SEQ_NUM = '11' THEN SUBSTRING(TRANS_INFO,46,2) END AS 'IPS_STATE'
FROM LVJPROD.IPS_LNA
WHERE TRANS_TYP = 'TA'
GROUP BY CNTL_NUM, TRANS_TYP,SEQ_NUM,TRANS_INFO
order by CNTL_NUM
我希望从下面的屏幕截图中将所有6条线路卷起来
答案 0 :(得分:0)
您可以再次对它们进行分组。问题在于case语句操作数据。因此,您需要将这些视为子查询,并在外部查询中进行分组,MAX
应该可以正常工作
WITH yourtable
AS (SELECT cntl_num,
trans_typ,
CASE
WHEN seq_num = '02' THEN Substring(trans_info, 28, 9)
END AS ssn,
CASE
WHEN seq_num = '11' THEN Substring(trans_info, 46, 2)
END AS ips_state
FROM lvjprod.ips_lna
WHERE trans_typ = 'TA'
GROUP BY cntl_num,
trans_typ,
seq_num,
trans_info)
SELECT cntl_num,
trans_typ,
Max(ssn),
Max(ips_state)
FROM yourtable
GROUP BY cntl_num,
trans_typ
ORDER BY cntl_num;
OR
SELECT cntl_num,
trans_typ,
Max(ssn),
Max(ips_state)
FROM (SELECT cntl_num,
trans_typ,
CASE
WHEN seq_num = '02' THEN Substring(trans_info, 28, 9)
END AS ssn,
CASE
WHEN seq_num = '11' THEN Substring(trans_info, 46, 2)
END AS ips_state
FROM lvjprod.ips_lna
WHERE trans_typ = 'TA'
GROUP BY cntl_num,
trans_typ,
seq_num,
trans_info) a
GROUP BY cntl_num,
trans_typ
ORDER BY cntl_num;