联合二案例陈述SQL

时间:2014-04-09 18:19:28

标签: c# sql wpf

CASE WHEN msdgen.user_tds_ball_wear.sequence_number = '1'
      THEN CONCAT(msdgen.user_tds_ball_wear.ball_wear_typical, ' (For 1 Hour  @  ', 
                  msdgen.user_tds_ball_wear.ball_wear_temp, '°C  @  ', 
                  msdgen.user_tds_ball_wear.ball_wear_speed, ' RPM  @  ', 
                  msdgen.user_tds_ball_wear.ball_wear_load, 'N)') 
END AS '4-Ball Wear Test #1',

CASE WHEN msdgen.user_tds_ball_wear.sequence_number = '2' 
      THEN CONCAT(msdgen.user_tds_ball_wear.ball_wear_typical, ' (For 1 Hour  @  ', 
                  msdgen.user_tds_ball_wear.ball_wear_temp, '°C  @  ', 
                  msdgen.user_tds_ball_wear.ball_wear_speed, ' RPM  @  ', 
                  msdgen.user_tds_ball_wear.ball_wear_load, 'N)') 
END AS '4-Ball Wear Test #2'

那就是我的代码我得到了以下内容

material_id              4-Ball Wear Test #1               4-Ball Wear Test #2
121               0.5 (1 Hour @ 75°C @ 1200 RPM @ 45 N)         --------
121                          ----------            0.32 (1 Hour @ 25°C @ 1200 RPM @ 2045 N)

我很想合并这些行,以便它们成为一行。有任何想法吗?这是查询许多其他内容的巨大SQL语句的一小部分。但是,除了这两列之外,每行的数据都是相同的。

1 个答案:

答案 0 :(得分:0)

使用GROUP_CONCAT()函数并在子查询中放入案例条件查询。

SELECT materialID,
       GROUP_CONCAT(CONCAT(`4-Ball Wear Test #1`,`4-Ball Wear Test #2`) separator ',')
FROM
(
SELECT materialId,
CASE WHEN msdgen.user_tds_ball_wear.sequence_number = '1'
      THEN CONCAT(msdgen.user_tds_ball_wear.ball_wear_typical, ' (For 1 Hour  @  ', 
                  msdgen.user_tds_ball_wear.ball_wear_temp, '°C  @  ', 
                  msdgen.user_tds_ball_wear.ball_wear_speed, ' RPM  @  ', 
                  msdgen.user_tds_ball_wear.ball_wear_load, 'N)') 
END AS '4-Ball Wear Test #1',

CASE WHEN msdgen.user_tds_ball_wear.sequence_number = '2' 
      THEN CONCAT(msdgen.user_tds_ball_wear.ball_wear_typical, ' (For 1 Hour  @  ', 
                  msdgen.user_tds_ball_wear.ball_wear_temp, '°C  @  ', 
                  msdgen.user_tds_ball_wear.ball_wear_speed, ' RPM  @  ', 
                  msdgen.user_tds_ball_wear.ball_wear_load, 'N)') 
END AS '4-Ball Wear Test #2'

FROM tableName
) as Z
GROUP BY Z.materialID