将具有不同 WHERE 子句的多个 SELECT 语句的结果组合到一个查询中
Combining results from multiple SELECT statements with different WHERE clauses into one query
我有一个 table,其中有一列有 15 个不同的名称 (TotalName),其中两个名称是 flowOne 和 flowTwo。还有一列是时间戳 (t_stamp) 和值。每次将新值添加到 table 时,它都会加上时间戳以及它的来源。我正在尝试创建一个查询,该查询将为我提供 15 个不同名称中的每一个的列以及月份的总数和月份编号。我已经尝试了其中两个名称,但它们最终出现在不同的行中。我想让它在一个月内全部显示在同一行。
SELECT YEAR(t_stamp) as 'Year', MONTH(t_stamp) as 'Month', sum(FlowTotals.FlowTotal) as 'flowOne', '' as 'flowTwo'
FROM DataBase.dbo.FlowTotals
WHERE FlowTotals.TotalName = 'flowOne'
and year(t_stamp) = year(getDate())
Group By Year(t_stamp), Month(t_stamp)
UNION
SELECT YEAR(t_stamp) as 'Year', MONTH(t_stamp) as 'Month', '' as 'flowOne', sum(FlowTotals.FlowTotal) as 'flowTwo'
FROM DataBase.dbo.FlowTotals
WHERE FlowTotals.TotalName = 'flowTwo'
and year(t_stamp) = year(getDate())
Group By Year(t_stamp), Month(t_stamp)
按月排序
这样就出来了
Year Month totalOne totalTwo
2019 1 123456 0
2019 1 0 789123
2019 2 456789 0
2019 2 0 345678
我希望第 1 个月有一行,第 2 个月有一行,该行的所有值等等。
我知道部分问题是我的 Select 语句中有 '' 作为 'flowOne,但这是我能想到的分成 2 个不同列的唯一方法。每行的总和按预期工作。
而不是 union
你的两个查询,join
它们在 q1.year = q2.year and q1.month = q2.month
。
一种简单的方法并且对您的查询几乎没有改变,将您的 select 包装为一个 cte,然后将此 cte 用于 group by
year
和 month
并得到 sum(FlowOne)
和 sum(FlowTwo)
,像这样(我用 0 替换了空字符串,所以总和有效,因为它们都是数字。转换只是为了防止 SQL 在内部转换它是 bit 而不是 int:
with cte as(
SELECT YEAR(t_stamp) as 'Year', MONTH(t_stamp) as 'Month', sum(FlowTotals.FlowTotal) as 'flowOne', cast(0 as int) as 'flowTwo'
FROM DataBase.dbo.FlowTotals
WHERE FlowTotals.TotalName = 'flowOne'
and year(t_stamp) = year(getDate())
Group By Year(t_stamp), Month(t_stamp)
UNION
SELECT YEAR(t_stamp) as 'Year', MONTH(t_stamp) as 'Month', cast(0 as int) as 'flowOne', sum(FlowTotals.FlowTotal) as 'flowTwo'
FROM DataBase.dbo.FlowTotals
WHERE FlowTotals.TotalName = 'flowTwo'
and year(t_stamp) = year(getDate())
Group By Year(t_stamp), Month(t_stamp)
)
select c.Year, c.Month, sum(c.FlowOne), sum(c.FlowTwo)
from cte as c
group by c.Year, c.Month
我有一个 table,其中有一列有 15 个不同的名称 (TotalName),其中两个名称是 flowOne 和 flowTwo。还有一列是时间戳 (t_stamp) 和值。每次将新值添加到 table 时,它都会加上时间戳以及它的来源。我正在尝试创建一个查询,该查询将为我提供 15 个不同名称中的每一个的列以及月份的总数和月份编号。我已经尝试了其中两个名称,但它们最终出现在不同的行中。我想让它在一个月内全部显示在同一行。
SELECT YEAR(t_stamp) as 'Year', MONTH(t_stamp) as 'Month', sum(FlowTotals.FlowTotal) as 'flowOne', '' as 'flowTwo'
FROM DataBase.dbo.FlowTotals
WHERE FlowTotals.TotalName = 'flowOne'
and year(t_stamp) = year(getDate())
Group By Year(t_stamp), Month(t_stamp)
UNION
SELECT YEAR(t_stamp) as 'Year', MONTH(t_stamp) as 'Month', '' as 'flowOne', sum(FlowTotals.FlowTotal) as 'flowTwo'
FROM DataBase.dbo.FlowTotals
WHERE FlowTotals.TotalName = 'flowTwo'
and year(t_stamp) = year(getDate())
Group By Year(t_stamp), Month(t_stamp)
按月排序
这样就出来了
Year Month totalOne totalTwo
2019 1 123456 0
2019 1 0 789123
2019 2 456789 0
2019 2 0 345678
我希望第 1 个月有一行,第 2 个月有一行,该行的所有值等等。
我知道部分问题是我的 Select 语句中有 '' 作为 'flowOne,但这是我能想到的分成 2 个不同列的唯一方法。每行的总和按预期工作。
而不是 union
你的两个查询,join
它们在 q1.year = q2.year and q1.month = q2.month
。
一种简单的方法并且对您的查询几乎没有改变,将您的 select 包装为一个 cte,然后将此 cte 用于 group by
year
和 month
并得到 sum(FlowOne)
和 sum(FlowTwo)
,像这样(我用 0 替换了空字符串,所以总和有效,因为它们都是数字。转换只是为了防止 SQL 在内部转换它是 bit 而不是 int:
with cte as(
SELECT YEAR(t_stamp) as 'Year', MONTH(t_stamp) as 'Month', sum(FlowTotals.FlowTotal) as 'flowOne', cast(0 as int) as 'flowTwo'
FROM DataBase.dbo.FlowTotals
WHERE FlowTotals.TotalName = 'flowOne'
and year(t_stamp) = year(getDate())
Group By Year(t_stamp), Month(t_stamp)
UNION
SELECT YEAR(t_stamp) as 'Year', MONTH(t_stamp) as 'Month', cast(0 as int) as 'flowOne', sum(FlowTotals.FlowTotal) as 'flowTwo'
FROM DataBase.dbo.FlowTotals
WHERE FlowTotals.TotalName = 'flowTwo'
and year(t_stamp) = year(getDate())
Group By Year(t_stamp), Month(t_stamp)
)
select c.Year, c.Month, sum(c.FlowOne), sum(c.FlowTwo)
from cte as c
group by c.Year, c.Month