简体   繁体   中英

SQL count number of rows in unique combinations of key value columns

Given the following MySQL table structure:

CREATE TABLE `order_params`( `order_id` BIGINT(30) NOT NULL, 
              `key` VARCHAR(50) NOT NULL, `value` VARCHAR(255) NOT NULL ); 

And this data:

INSERT INTO `order_params` (`order_id`, `key`, `value`) VALUES ('1', 'browser', 'Firefox'); 
INSERT INTO `order_params` (`order_id`, `key`, `value`) VALUES ('1', 'os', 'Windows'); 
INSERT INTO `order_params` (`order_id`, `key`, `value`) VALUES ('2', 'browser', 'Firefox'); 
INSERT INTO `order_params` (`order_id`, `key`, `value`) VALUES ('2', 'os', 'Windows'); 
INSERT INTO `order_params` (`order_id`, `key`, `value`) VALUES ('3', 'browser', 'Firefox'); 
INSERT INTO `order_params` (`order_id`, `key`, `value`) VALUES ('3', 'os', 'OSX'); 
INSERT INTO `order_params` (`order_id`, `key`, `value`) VALUES ('4', 'browser', 'Safari'); 
INSERT INTO `order_params` (`order_id`, `key`, `value`) VALUES ('4', 'os', 'OSX'); 
INSERT INTO `order_params` (`order_id`, `key`, `value`) VALUES ('5', 'browser', 'Safari'); 
INSERT INTO `order_params` (`order_id`, `key`, `value`) VALUES ('5', 'os', 'OSX'); 
INSERT INTO `order_params` (`order_id`, `key`, `value`) VALUES ('5', 'version', '5'); 

How do I get the following results?

browser Firefox os Windows              2
browser Firefox os OSX                  1
browser Safari os OSX                   1
browser Safari os OSX version 5         1

The numbers on the right are the count of records that match the unique key/value combinations. Is this even possible?

OK, updating to show that I have tried this:

SELECT CONCAT(`key`, `value`), COUNT(*)
FROM order_params 
GROUP BY `order_id`, `key`, `value`;

And this is the result:

browserFirefox  1
osWindows       1
browserFirefox  1
osWindows       1
browserFirefox  1
osOS X          1
browserSafari   1
osOS X          1

I've also tried this:

SELECT `key`, `value`, COUNT(*)
FROM order_params 
GROUP BY `key`, `value`;

Which produces this:

browser Firefox  3
browser Safari   1
os OS X          2
os Windows       2

Obviously, neither of these is the desired result.

One approach is two stages of aggregation

select browser, os, version, count(*)
from (select order_id,
             max(case when `key` = 'browser' then `value` end) as browser,
             max(case when `key` = 'os' then `value` end) as os,
             max(case when `key` = 'version' then `value` end) as version
      from order_params op
      group by order_id
     ) p
group by browser, os, version

If you actually want the string that you have, you can concat things together:

select concat(coalesce(concat('browser ', browser), ''),
              coalesce(concat('os ', os), ''),
              coalesce(concat('version ', version), ''), count(*)
from (select order_id,
             max(case when `key` = 'browser' then `value` end) as browser,
             max(case when `key` = 'os' then `value` end) as os,
             max(case when `key` = 'version' then `value` end) as version
      from order_params op
      group by order_id
     ) p
group by os, browser, version

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