在Mysql中获取Concat子句后获得不同的记录

时间:2013-12-31 13:03:49

标签: mysql distinct

我有这个mysql查询

   SELECT  DISTINCT CONCAT ('US-',  medicare_provider_charge_inpatient_drg100_fy2011.`Provider State`) AS `Provider State`,
                                    ROUND(medicare_provider_charge_inpatient_drg100_fy2011.`Total Discharges`, 2) AS `Total Discharges`
                                    FROM medicare_provider_charge_inpatient_drg100_fy2011
                                    WHERE medicare_provider_charge_inpatient_drg100_fy2011.`Provider Name` LIKE '%ELK REGIONAL HEALTH CENTER'
                                     limit 0,5

我想要做的是,我想用它获取状态名称cocncat“US”,然后根据查询的其余部分获取不同的状态。

编辑这是输出

  State   Total
  US-PA     18
  US-PA     16
  US-PA     27
  US-PA     39
  US-PA     42

并建议输出应该是这样的

  State   Total
  US-NY     18
  US-WD     16
  US-TX     27
  US-AZ     39
  US-CA     42

2 个答案:

答案 0 :(得分:1)

我建议您使用GROUP BY而不是DISTINCT。

答案 1 :(得分:1)

尝试按提供者状态汇总总排放量和分组。

SELECT CONCAT ('US-',  medicare_provider_charge_inpatient_drg100_fy2011.`Provider State`) AS `Provider State`,
                                sum(ROUND(medicare_provider_charge_inpatient_drg100_fy2011.`Total Discharges`, 2)) AS `Total Discharges`
                                FROM medicare_provider_charge_inpatient_drg100_fy2011
                                WHERE medicare_provider_charge_inpatient_drg100_fy2011.`Provider Name` LIKE '%ELK REGIONAL HEALTH CENTER'
GROUP BY CONCAT ('US-',  medicare_provider_charge_inpatient_drg100_fy2011.`Provider State`)
                                 limit 0,5