我正在处理一个查询,希望对行进行分组并返回groupped数据,但我的查询没有按预期工作。
我的问题-
select item, branch, packunit,packlevel,dealqty,PromotionFlag,PromotionID, PromotionEndDate, cnc,delivery, volumedeal, standard_price_scheme,
deliv_price_scheme
from
(
SELECT
`item` AS `item`,
`branch` AS `branch`,
`PackUnit` AS `PackUnit`,
`PackLevel` AS `PackLevel`,
`DealQty` AS `DealQty`,
`PromotionFlag` AS `PromotionFlag`,
`PromotionID` AS `PromotionID`,
`PromotionEndDate` AS `PromotionEndDate`,
SUM(`cnc`) AS `cnc`,
SUM(`delivery`) AS `delivery`,
SUM(`volumedeal`) AS `volumedeal`,
`standard_price_scheme` AS `standard_price_scheme`,
`deliv_price_scheme` AS `deliv_price_scheme`
FROM
(
SELECT DISTINCT
`Pricing_Today`.`item` AS `item`,
`Pricing_Today`.`branch` AS `branch`,
`Pricing_Today`.`price_scheme` AS `price_scheme`,
`Pricing_Today`.`PackUnit` AS `PackUnit`,
`Pricing_Today`.`PackLevel` AS `PackLevel`,
`Pricing_Today`.`DealQty` AS `DealQty`,
`Pricing_Today`.`PromotionFlag` AS `PromotionFlag`,
`Pricing_Today`.`PromotionID` AS `PromotionID`,
`Pricing_Today`.`PromotionEndDate` AS `PromotionEndDate`,
(CASE
WHEN (`Pricing_Today`.`PriceType` = 'C&C') THEN `Pricing_Today`.`Sell`
END) AS `cnc`,
(CASE
WHEN (`Pricing_Today`.`PriceType` = 'Delivery') THEN `Pricing_Today`.`Sell`
END) AS `delivery`,
(CASE
WHEN (`Pricing_Today`.`PriceType` = 'Volume Deal') THEN `Pricing_Today`.`Sell`
END) AS `volumedeal`,
(CASE
WHEN (`Pricing_Today`.`PriceType` = 'C&C') THEN `Pricing_Today`.`price_scheme`
END) AS `standard_price_scheme`,
(CASE
WHEN
((`Pricing_Today`.`PriceType` = 'Delivery')
OR (`Pricing_Today`.`PriceType` = 'Volume Deal'))
THEN
`Pricing_Today`.`price_scheme`
END) AS `deliv_price_scheme`
FROM
`Pricing_Today`
where item = 78867
and branch = 0
GROUP BY `Pricing_Today`.`item` , `Pricing_Today`.`PackUnit` , `Pricing_Today`.`PriceType`,`standard_price_scheme`,`deliv_price_scheme`
) as a
GROUP BY branch,`item` , `PackUnit`,`standard_price_scheme`,`deliv_price_scheme`
) as a
-- group by item, packunit
它回来了-
但是,当我按项目分组时,我得到这个-
对于cnc,它显示空值。如何消除空值并获得数字?
提前谢谢
1条答案
按热度按时间6xfqseft1#
你需要把这些
CASE
表达:这只是一个标准的pivot查询。采取行动背后的想法
MAX
的CASE
表达式是如果给定的一组记录有一个非NULL
价值,那么MAX
会正确地提取它。这是因为MAX
忽略NULL
价值观。请注意,我从您的查询中删除了反勾号,这些都不是必需的。除非确实需要,否则我尽量避免使用反勾号,因为这会使查询更难阅读。