Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 按收入和销售产品分列的销售产品交叉表数据_Mysql_Sql - Fatal编程技术网

Mysql 按收入和销售产品分列的销售产品交叉表数据

Mysql 按收入和销售产品分列的销售产品交叉表数据,mysql,sql,Mysql,Sql,为交叉列表数据获取正确频率的问题。我的预期输出如下: 我尝试用SUM语句替换COUNT语句 SUM(IF(product.product_id = 1, line_item.quantity, 0)) AS Soda, SUM(IF(product.product_id = 2, line_item.quantity, 0)) AS Liquor, SUM(IF(product.product_

为交叉列表数据获取正确频率的问题。我的预期输出如下:

我尝试用SUM语句替换COUNT语句

                 SUM(IF(product.product_id = 1, line_item.quantity, 0)) AS Soda,
                 SUM(IF(product.product_id = 2, line_item.quantity, 0)) AS Liquor,
                 SUM(IF(product.product_id = 3, line_item.quantity, 0)) AS Lemon,
                 SUM(IF(product.product_id = 4, line_item.quantity, 0)) AS Mango,
                 SUM(IF(product.product_id = 5, line_item.quantity, 0)) AS Inhaler,
                 SUM(1) AS Count

FROM line_item
JOIN product USING (product_id)
JOIN ( SELECT    0 lo,  500 hi UNION
       SELECT  501   , 1000    UNION
       SELECT 1001   , 1500    UNION
       SELECT 1501   , 2000    UNION
       SELECT 2001   , 2500 ) ranges ON (product.price * line_item.quantity) BETWEEN ranges.lo AND ranges.hi
GROUP BY ranges.lo, ranges.hi```

It is getting closer because it is distributing already the values in its ranges just that the values are not correct. I am expecting to see something like this:

[Expected Result][1]


  [1]: https://i.stack.imgur.com/YuB92.png

查看我的代码后,以下是答案:

                 SUM(product.product_id = 1) AS Soda,
                 SUM(product.product_id = 2) AS Liquor,
                 SUM(product.product_id = 3) AS Lemon,
                 SUM(product.product_id = 4) AS Mango,
                 SUM(product.product_id = 5) AS Inhaler,
                 SUM(1) AS Count

FROM line_item
JOIN product USING (product_id)
JOIN ( SELECT    0 lo,  500 hi UNION
       SELECT  501   , 1000    UNION
       SELECT 1001   , 1500    UNION
       SELECT 1501   , 2000    UNION
       SELECT 2001   , 2500 ) ranges ON product.price * line_item.quantity BETWEEN ranges.lo AND ranges.hi
GROUP BY ranges.lo, ranges.hi

我的代码似乎不起作用
-有什么特殊原因不告诉我们哪些代码不起作用?您是否收到任何错误消息?结果与您预期的不同吗?如果是的话,有什么不同?这与我的预期结果不同。我已经嵌入了Imagehttps://i.stack.imgur.com/5s71f.png. 与此相反,作为上述代码的结果,我在所有列或字段中只得到9、2、5、3和1。任何帮助都是非常重要的。以下是我的全部表格:编辑你的问题并将这些内容放在那里。然后删除这篇文章,因为它不是答案。我想我已经找到了一个方法,福帕斯先生。也许不是最好的答案,但确实有效。