简体   繁体   中英

get Distinct records after Concat Clause in Mysql

i have this mysql Query

   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

what i am trying to do is that i want to fetch state name cocncat 'US' with it and then get distinct states based on rest of the query.

Edit this is the out put

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

and suggested output should be something like this

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

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

Try summing up the total discharges and grouping by the provider state.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM