如何将 3 个单独的 SQL 语句组合成一组结果
How to combine 3 separate SQL statements into 1 set of results
我有一个 SQL 查询,我是 运行 3 次(3 次仅更改日期范围)并且想将结果合并为一个 table,而不是 运行 3 个不同的查询并试图在 SQL 之外加入。我试图找出每个 day/month/year 发生某事的次数。我在 SQL 服务器中 运行 这个。
我有 2 个 table;一个有交易日期,另一个有我需要的信息(InventoryNumber
table 的前 3 个字符)所以我必须加入这些 table。然后我想按库存编号的前 3 个字符分组,并在列中添加计数。
最终目标是拥有如下所示的内容:
InvNum | DayCount | MonthCount | YearCount
abc | 2 | 10 | 40
def | 0 | 2 | 6
xyz | 0 | 0 | 2
这是我对第一天的查询。这完全像我想要的那样工作。但是现在,我想在其中添加月份的计数,然后还有当年的计数。此查询与其他 2 个查询之间唯一会发生变化的是计数列名称,然后是日期。:
SELECT
LEFT(LINEITEM.InventoryNumber, 3) AS InvNum,
COUNT(*) AS DailyCount
FROM
INVOICE
INNER JOIN
LINEITEM ON INVOICE.InvoiceID = LINEITEM.InvoiceID
WHERE
InventoryNumber IS NOT Null
AND InventoryNumber != 'Misc'
AND DateCreated > '5-20-2022'
GROUP BY
LEFT(LINEITEM.InventoryNumber, 3)
ORDER BY
InvNum ASC;
我查看了其他一些与此类似的问题,但他们的查询要简单得多,我无法用我的查询复制相同的内容。
感谢任何帮助。
您需要根据 where 子句中的列和日期编写三个查询,每个查询结果在这些 table 中
只需更改每个 table
的 where 子句中的列和日期值
第一个查询结果
InvNum | DayCount
abc | 2
def | 0
xyz | 0
第二次查询结果:
InvNum | MonthCount
abc | 10
def | 2
xyz | 0
第三次查询结果:
InvNum | YearCount
abc | 40
def | 6
xyz | 2
然后将这三个 table 加入 InvNum 列
这里还不够了解,你应该考虑条件聚合
Declare @D Date='2022-05-20';
SELECT InvNum = LEFT(LINEITEM.InventoryNumber, 3)
,DayCount = sum( case when DateCreated = @D then 1 else 0 end )
,MonthCount = sum( case when month(DateCreated) = month(@D) then 1 else 0 end )
,YearCount = sum( case when year(DateCreated) = year(@D) then 1 else 0 end )
FROM INVOICE
INNER JOIN LINEITEM ON INVOICE.InvoiceID = LINEITEM.InvoiceID
WHERE InventoryNumber IS NOT Null
AND InventoryNumber <> 'Misc'
AND DateCreated >= format(@D,'yyyy-01-01')
GROUP BY LEFT(LINEITEM.InventoryNumber, 3)
ORDER BY InvNum ASC;
这可能会为您完成工作:
SELECT ISNULL(DailyQuery.InvNum, ISNULL(MonthlyQuery.InvNum, YearlyQuery.InvNum)) as InvNum,
ISNULL(DailyCount,0) as DailyCount,
ISNULL(MonthlyCount,0) as MonthlyCount,
ISNULL(YearlyCount,0) as YearlyCount
FROM
(SELECT
LEFT(LINEITEM.InventoryNumber, 3) AS InvNum,
COUNT(*) AS DailyCount
FROM
INVOICE
INNER JOIN
LINEITEM ON INVOICE.InvoiceID = LINEITEM.InvoiceID
WHERE InventoryNumber != 'Misc'
AND DateCreated > DATEADD(day, DATEDIFF(day, 0, GETDATE()-1), 0) --first hour day before
GROUP BY
LEFT(LINEITEM.InventoryNumber, 3)
) DailyQuery
FULL JOIN
(SELECT
LEFT(LINEITEM.InventoryNumber, 3) AS InvNum,
COUNT(*) AS MonthlyCount
FROM
INVOICE
INNER JOIN
LINEITEM ON INVOICE.InvoiceID = LINEITEM.InvoiceID
WHERE InventoryNumber != 'Misc'
AND DateCreated > DATEADD(month, DATEDIFF(month, 0, GETDATE()), 0) --first day of current month
GROUP BY
LEFT(LINEITEM.InventoryNumber, 3) ) MonthlyQuery ON DailyQuery.InvNum = MonthlyQuery.InvNum
FULL JOIN
(SELECT
LEFT(LINEITEM.InventoryNumber, 3) AS InvNum,
COUNT(*) AS YearlyCount
FROM
INVOICE
INNER JOIN
LINEITEM ON INVOICE.InvoiceID = LINEITEM.InvoiceID
WHERE InventoryNumber != 'Misc'
AND DateCreated > DATEADD(year, DATEDIFF(year, 0, GETDATE()), 0) --first day of current month
GROUP BY
LEFT(LINEITEM.InventoryNumber, 3) ) YearlyQuery ON MonthlyQuery.InvNum = YearlyQuery.InvNum
看来您需要具有三个不同开始日期的条件聚合。
DECLARE @D date = '2022-05-20';
DECLARE @M date = '2022-04-21';
DECLARE @Y date = '2021-05-21';
SELECT InvNum = LEFT(li.InventoryNumber, 3)
,DayCount = COUNT(CASE WHEN i.DateCreated >= @D THEN 1 END)
,MonthCount = COUNT(CASE WHEN i.DateCreated >= @M THEN 1 END)
,YearCount = COUNT(*)
FROM INVOICE i
INNER JOIN LINEITEM li ON i.InvoiceID = li.InvoiceID
WHERE li.InventoryNumber <> 'Misc'
AND i.DateCreated >= @Y
GROUP BY
LEFT(li.InventoryNumber, 3)
ORDER BY
InvNum;
Note that <> 'Misc'
also excludes nulls, and that ASC
is the default.
您还可以动态计算这些开始日期
DECLARE @D date = DATEADD(day, -1, CAST(GETDATE() AS date));
DECLARE @M date = DATEADD(month, -1, CAST(GETDATE() AS date));
DECLARE @Y date = DATEADD(year, -1, CAST(GETDATE() AS date));
我有一个 SQL 查询,我是 运行 3 次(3 次仅更改日期范围)并且想将结果合并为一个 table,而不是 运行 3 个不同的查询并试图在 SQL 之外加入。我试图找出每个 day/month/year 发生某事的次数。我在 SQL 服务器中 运行 这个。
我有 2 个 table;一个有交易日期,另一个有我需要的信息(InventoryNumber
table 的前 3 个字符)所以我必须加入这些 table。然后我想按库存编号的前 3 个字符分组,并在列中添加计数。
最终目标是拥有如下所示的内容:
InvNum | DayCount | MonthCount | YearCount
abc | 2 | 10 | 40
def | 0 | 2 | 6
xyz | 0 | 0 | 2
这是我对第一天的查询。这完全像我想要的那样工作。但是现在,我想在其中添加月份的计数,然后还有当年的计数。此查询与其他 2 个查询之间唯一会发生变化的是计数列名称,然后是日期。:
SELECT
LEFT(LINEITEM.InventoryNumber, 3) AS InvNum,
COUNT(*) AS DailyCount
FROM
INVOICE
INNER JOIN
LINEITEM ON INVOICE.InvoiceID = LINEITEM.InvoiceID
WHERE
InventoryNumber IS NOT Null
AND InventoryNumber != 'Misc'
AND DateCreated > '5-20-2022'
GROUP BY
LEFT(LINEITEM.InventoryNumber, 3)
ORDER BY
InvNum ASC;
我查看了其他一些与此类似的问题,但他们的查询要简单得多,我无法用我的查询复制相同的内容。
感谢任何帮助。
您需要根据 where 子句中的列和日期编写三个查询,每个查询结果在这些 table 中
只需更改每个 table
的 where 子句中的列和日期值第一个查询结果
InvNum | DayCount
abc | 2
def | 0
xyz | 0
第二次查询结果:
InvNum | MonthCount
abc | 10
def | 2
xyz | 0
第三次查询结果:
InvNum | YearCount
abc | 40
def | 6
xyz | 2
然后将这三个 table 加入 InvNum 列
这里还不够了解,你应该考虑条件聚合
Declare @D Date='2022-05-20';
SELECT InvNum = LEFT(LINEITEM.InventoryNumber, 3)
,DayCount = sum( case when DateCreated = @D then 1 else 0 end )
,MonthCount = sum( case when month(DateCreated) = month(@D) then 1 else 0 end )
,YearCount = sum( case when year(DateCreated) = year(@D) then 1 else 0 end )
FROM INVOICE
INNER JOIN LINEITEM ON INVOICE.InvoiceID = LINEITEM.InvoiceID
WHERE InventoryNumber IS NOT Null
AND InventoryNumber <> 'Misc'
AND DateCreated >= format(@D,'yyyy-01-01')
GROUP BY LEFT(LINEITEM.InventoryNumber, 3)
ORDER BY InvNum ASC;
这可能会为您完成工作:
SELECT ISNULL(DailyQuery.InvNum, ISNULL(MonthlyQuery.InvNum, YearlyQuery.InvNum)) as InvNum,
ISNULL(DailyCount,0) as DailyCount,
ISNULL(MonthlyCount,0) as MonthlyCount,
ISNULL(YearlyCount,0) as YearlyCount
FROM
(SELECT
LEFT(LINEITEM.InventoryNumber, 3) AS InvNum,
COUNT(*) AS DailyCount
FROM
INVOICE
INNER JOIN
LINEITEM ON INVOICE.InvoiceID = LINEITEM.InvoiceID
WHERE InventoryNumber != 'Misc'
AND DateCreated > DATEADD(day, DATEDIFF(day, 0, GETDATE()-1), 0) --first hour day before
GROUP BY
LEFT(LINEITEM.InventoryNumber, 3)
) DailyQuery
FULL JOIN
(SELECT
LEFT(LINEITEM.InventoryNumber, 3) AS InvNum,
COUNT(*) AS MonthlyCount
FROM
INVOICE
INNER JOIN
LINEITEM ON INVOICE.InvoiceID = LINEITEM.InvoiceID
WHERE InventoryNumber != 'Misc'
AND DateCreated > DATEADD(month, DATEDIFF(month, 0, GETDATE()), 0) --first day of current month
GROUP BY
LEFT(LINEITEM.InventoryNumber, 3) ) MonthlyQuery ON DailyQuery.InvNum = MonthlyQuery.InvNum
FULL JOIN
(SELECT
LEFT(LINEITEM.InventoryNumber, 3) AS InvNum,
COUNT(*) AS YearlyCount
FROM
INVOICE
INNER JOIN
LINEITEM ON INVOICE.InvoiceID = LINEITEM.InvoiceID
WHERE InventoryNumber != 'Misc'
AND DateCreated > DATEADD(year, DATEDIFF(year, 0, GETDATE()), 0) --first day of current month
GROUP BY
LEFT(LINEITEM.InventoryNumber, 3) ) YearlyQuery ON MonthlyQuery.InvNum = YearlyQuery.InvNum
看来您需要具有三个不同开始日期的条件聚合。
DECLARE @D date = '2022-05-20';
DECLARE @M date = '2022-04-21';
DECLARE @Y date = '2021-05-21';
SELECT InvNum = LEFT(li.InventoryNumber, 3)
,DayCount = COUNT(CASE WHEN i.DateCreated >= @D THEN 1 END)
,MonthCount = COUNT(CASE WHEN i.DateCreated >= @M THEN 1 END)
,YearCount = COUNT(*)
FROM INVOICE i
INNER JOIN LINEITEM li ON i.InvoiceID = li.InvoiceID
WHERE li.InventoryNumber <> 'Misc'
AND i.DateCreated >= @Y
GROUP BY
LEFT(li.InventoryNumber, 3)
ORDER BY
InvNum;
Note that
<> 'Misc'
also excludes nulls, and thatASC
is the default.
您还可以动态计算这些开始日期
DECLARE @D date = DATEADD(day, -1, CAST(GETDATE() AS date));
DECLARE @M date = DATEADD(month, -1, CAST(GETDATE() AS date));
DECLARE @Y date = DATEADD(year, -1, CAST(GETDATE() AS date));