gpt4 book ai didi

sql - MySql 多次计算同一列

转载 作者:可可西里 更新时间:2023-11-01 08:00:47 24 4
gpt4 key购买 nike

SELECT COUNT(w.invoiceId) AS '10 Days' FROM tbl_Invoice w
WHERE w.invoiceId NOT IN(
SELECT inv.invoiceId FROM tbl_InvoiceAllocation inv)
AND w.invoiceDate < DATE_SUB(curdate(), INTERVAL 10 DAY)

它工作正常并返回日期超过 10 天前的所有发票的计数。我现在想在同一查询中返回超过 20 天和 100 天的发票计数。所以理想情况下,查询会返回如下内容:

10 Days    20 Days    100 Days

350 280 90

最佳答案

将COUNT改为SUM,将指定的WHERE子句作为CASE语句,真值为1,假值为0

有点像

SELECT  SUM( CASE WHEN w.invoiceDate < DATE_SUB(curdate(), INTERVAL 10 DAY) THEN 1 ELSE 0 END) AS '10 Days',
SUM( CASE WHEN w.invoiceDate < DATE_SUB(curdate(), INTERVAL 20 DAY) THEN 1 ELSE 0 END) AS '20 Days'
FROM tbl_Invoice w
WHERE w.invoiceId NOT IN(
SELECT inv.invoiceId
FROM tbl_InvoiceAllocation inv)
AND w.invoiceDate < DATE_SUB(curdate(), INTERVAL 20 DAY)

关于sql - MySql 多次计算同一列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4254720/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com