求和colum_x相同的列

时间:2014-03-15 03:10:37

标签: mysql

我正在使用以下MYSQL来检索数据:

 SELECT `units`, `developer_proceeds` * `units` AS `developer_proceeds`, `customer_currency`, `country_code` FROM daily_raw WHERE `begin_date` = "2014-03-13" 

我得到以下结果:

     INSERT INTO `daily_raw` (`units`, `units`, `customer_currency`, `country_code`) VALUES
(2.00, 0.0000, 'USD', 'CO'),
(1.00, 0.0000, 'MXN', 'MX'),
(5.00, 0.0000, 'USD', 'US'),
(11.00, 0.0000, 'USD', 'AR'),
(19.00, 0.0000, 'MXN', 'MX'),
(1.00, 4.2000, 'USD', 'PE'),
(9.00, 0.0000, 'USD', 'PE'),
(2.00, 8.4000, 'USD', 'AR'),
(1.00, 0.0000, 'USD', 'US'),
(18.00, 0.0000, 'EUR', 'ES'),
(1.00, 4.2000, 'USD', 'US');

如何将数据分组到同一country_code?

E.g。

 (2.00, 0.0000, 'USD', 'CO'),
    (20.00, 0.0000, 'MXN', 'MX'),
    (7.00, 4.2000, 'USD', 'US'),
    (10.00, 4.2000, 'USD', 'PE'),
    (13.00, 8.4000, 'USD', 'AR'),
    (18.00, 0.0000, 'EUR', 'ES'),

3 个答案:

答案 0 :(得分:0)

选择计算的总和并按其余部分分组:

SELECT
    sum(`units`) as units,
    sum(`developer_proceeds` * `units`) AS `developer_proceeds`,
    `customer_currency`,
    `country_code`
FROM daily_raw
WHERE `begin_date` = "2014-03-13" 
GROUP BY 3, 4 -- FYI, these are column numbers

如果要汇总一行中国家/地区的所有内容,请从select和group by子句中删除第1列和第3列

答案 1 :(得分:0)

您将使用group by。如果我正确理解你在做什么:

SELECT sum(units), sum(developer_proceeds` * `units) AS `developer_proceeds`,
       `customer_currency`, `country_code`
FROM daily_raw
WHERE `begin_date` = '2014-03-13'
GROUP BY country_code, customer_currency;

答案 2 :(得分:0)

试试这个

SELECT sum(units), sum(developer_proceeds` * `units) AS `developer_proceeds`,
       `customer_currency`, `country_code`
FROM daily_raw
WHERE `begin_date` = '2014-03-13'
GROUP BY country_code, customer_currency;