由于没有Pivots或Cubes,我在MYSQL中一直绞尽脑汁。 我尝试使用Case When它部分有效,但是我似乎无法过滤它只有会员=是,尽管子查询工作过滤掉了' No'
CREATE TABLE `nvp` (
`rowid` int(11) NOT NULL AUTO_INCREMENT,
`groupid` varchar(45) DEFAULT NULL,
`name` varchar(45) DEFAULT NULL,
`value` varchar(45) DEFAULT NULL,
`created` date DEFAULT NULL,
PRIMARY KEY (`rowid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `t2`.`nvp` (`groupid`, `name`, `value`, `created`) VALUES ('1', 'Fee', '10', '2014-03-01');
INSERT INTO `t2`.`nvp` (`groupid`, `name`, `value`, `created`) VALUES ('1', 'Member', 'Yes', '2014-03-01');
INSERT INTO `t2`.`nvp` (`groupid`, `name`, `value`, `created`) VALUES ('1', 'Type', 'MLB', '2014-03-01');
INSERT INTO `t2`.`nvp` (`groupid`, `name`, `value`, `created`) VALUES ('2', 'Fee', '10', '2014-03-01');
INSERT INTO `t2`.`nvp` (`groupid`, `name`, `value`, `created`) VALUES ('2', 'Member', 'Yes', '2014-03-01');
INSERT INTO `t2`.`nvp` (`groupid`, `name`, `value`, `created`) VALUES ('2', 'Type', 'MLB', '2014-03-01');
INSERT INTO `t2`.`nvp` (`groupid`, `name`, `value`, `created`) VALUES ('3', 'Fee', '10', '2014-03-02');
INSERT INTO `t2`.`nvp` (`groupid`, `name`, `value`, `created`) VALUES ('3', 'Member', 'Yes', '2014-03-02');
INSERT INTO `t2`.`nvp` (`groupid`, `name`, `value`, `created`) VALUES ('3', 'Type', 'MLB', '2014-03-02');
INSERT INTO `t2`.`nvp` (`groupid`, `name`, `value`, `created`) VALUES ('4', 'Fee', '20', '2014-03-03');
INSERT INTO `t2`.`nvp` (`groupid`, `name`, `value`, `created`) VALUES ('4', 'Member', 'Yes', '2014-03-03');
INSERT INTO `t2`.`nvp` (`groupid`, `name`, `value`, `created`) VALUES ('4', 'Type', 'NFL', '2014-03-03');
INSERT INTO `t2`.`nvp` (`groupid`, `name`, `value`, `created`) VALUES ('5', 'Fee', '50', '2014-03-03');
INSERT INTO `t2`.`nvp` (`groupid`, `name`, `value`, `created`) VALUES ('5', 'Member', 'Yes', '2014-03-03');
INSERT INTO `t2`.`nvp` (`groupid`, `name`, `value`, `created`) VALUES ('5', 'Type', 'NBA', '2014-03-03');
INSERT INTO `t2`.`nvp` (`groupid`, `name`, `value`, `created`) VALUES ('6', 'Fee', '50', '2014-03-03');
INSERT INTO `t2`.`nvp` (`groupid`, `name`, `value`, `created`) VALUES ('6', 'Member', 'Yes', '2014-03-03');
INSERT INTO `t2`.`nvp` (`groupid`, `name`, `value`, `created`) VALUES ('6', 'Type', 'NBA', '2014-03-03');
INSERT INTO `t2`.`nvp` (`groupid`, `name`, `value`, `created`) VALUES ('7', 'Fee', '100', '2014-03-04');
INSERT INTO `t2`.`nvp` (`groupid`, `name`, `value`, `created`) VALUES ('7', 'Member', 'No', '2014-03-04');
INSERT INTO `t2`.`nvp` (`groupid`, `name`, `value`, `created`) VALUES ('7', 'Type', 'NBA', '2014-03-04');
所需的输出" Pivot"表格,过滤仅显示会员以及按日期分组的每笔费用的总和。
Date, MLB, NFL, NBA
2014-03-01, 20, 0, 0
2014-03-02, 10, 0, 0
3014-03-03, 0, 20, 50
任何人都有一些指示? 谢谢。
这是SQL尝试过:
select tbl2.created,
sum(case when tbl2.type='MLB' then fee else null end) as MLB,
sum(case when tbl2.type='NBA' then fee else null end) as NBA,
sum(case when tbl2.type='NFL' then fee else null end) as NFL
from (select tbl.* from (select
created,
sum(case when nvp.name='Fee' then value else null end) as Fee,
max(case when nvp.name='Type' then value else null end) as Type,
max(case when nvp.name='Member' then value else null end) as member
from nvp group by groupid) AS tbl where member='Yes') as tbl2 group by created;
# query to transpose row/columns, uses max where strings are required, this does filter out the No.
(select tbl.* from (select
created,
sum(case when nvp.name='Fee' then value else null end) as Fee,
max(case when nvp.name='Type' then value else null end) as Type,
max(case when nvp.name='Member' then value else null end) as member
from nvp group by groupid) AS tbl where member='Yes');
答案 0 :(得分:0)
这是执行总摘要的基本查询:
select t.created,
sum(case when t.type = 'MLB' then fee else null end) as MLB,
sum(case when t.type = 'NBA' then fee else null end) as NBA,
sum(case when t.type = 'NFL' then fee else null end) as NFL
from (select created,
sum(case when nvp.name = 'Fee' then value+0 else null end) as Fee,
max(case when nvp.name = 'Type' then value else null end) as Type,
max(case when nvp.name = 'Member' then value else null end) as member
from nvp
group by groupid
) t
group by created;
添加where member = 'Yes'
应该有效:
select t.created,
sum(case when t.type = 'MLB' then fee else null end) as MLB,
sum(case when t.type = 'NBA' then fee else null end) as NBA,
sum(case when t.type = 'NFL' then fee else null end) as NFL
from (select created,
sum(case when nvp.name = 'Fee' then value+0 else null end) as Fee,
max(case when nvp.name = 'Type' then value else null end) as Type,
max(case when nvp.name = 'Member' then value else null end) as member
from nvp
group by groupid
) t
where member = 'Yes'
group by created;
事实上,根据SQL Fiddle(参见here),确实如此。这看起来与您尝试的基本相同。