-1
我一直在MYSQL中討論這個問題,因爲沒有Pivots或Cubes。 我一直在使用案例在審判,它的工作原理部分然而我似乎無法對其進行過濾,只有會員=是,儘管子查詢工作,篩選出「否」名稱值列表對列
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');
所需的輸出「透視」表,已過濾,只顯示會員以及收取的每筆費用的總和,按日期分組。
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');
請顯示您嘗試的查詢。 –
試試看怎麼樣?只是谷歌的例子,並嘗試自己。 –
@戈登Linoff我添加了我的SQL到帖子,感謝您的期待。 – TSage