[英]Combining SQL queries into one
我有这些SQL
查询:
select count(*) as count1, sum(amount) as amount1 from v_purchase where amount >= 100 and
amount < 100000 and p_date = '2014-06-12'
select count(*) as count1, sum(amount) as amount1 from v_purchase where amount >= 100000 and amount < 250000 and p_date = '2014-06-12'
select count(*) as count1, sum(amount) as amount1 from v_purchase where amount >= 250000 and amount < 500000 and p_date = '2014-06-12'
select count(*) as count1, sum(amount) as amount1 from v_purchase where amount >= 500000 and amount < 1000000 and p_date = '2014-06-12'
select count(*) as count1, sum(amount) as amount1 from v_purchase where amount >= 1000000 and amount < 2500000 and p_date = '2014-06-12'
select count(*) as count1, sum(amount) as amount1 from v_purchase where amount >= 2500000 and amount < 5000000 and p_date = '2014-06-12'
select count(*) as count1, sum(amount) as amount1 from v_purchase where amount >= 5000000 and amount < 10000000 and p_date = '2014-06-12'
select count(*) as count1, sum(amount) as amount1 from v_purchase where amount >= 10000000 p_date = '2014-06-12'
有没有办法将这些查询合并为一个并将其作为单个查询执行? 然后可以在代码中分离结果。
在这些查询之间写入UNION
关键字。
修订更新基础上讨论的日期范围。 我为你创建了一个SQL小提琴
select
SUM(Case When amount >= 100 and amount < 100000 Then 1 else 0 End) as band1Count,
SUM(Case When amount >= 100000 and amount < 250000 Then 1 else 0 End) as band2Count,
SUM(Case When amount >= 250000 and amount < 500000 Then 1 else 0 End) as band3Count,
SUM(Case When amount >= 500000 and amount < 1000000 Then 1 else 0 End) as band4Count,
SUM(Case When amount >= 1000000 and amount < 2500000 Then 1 else 0 End) as band5Count,
...
SUM(Case When amount >= 100 and amount < 100000 Then amount else 0 End) as band1Sum,
SUM(Case When amount >= 100000 and amount < 250000 Then amount else 0 End) as band2Sum,
SUM(Case When amount >= 250000 and amount < 500000 Then amount else 0 End) as band3Sum,
SUM(Case When amount >= 500000 and amount < 1000000 Then amount else 0 End) as band4Sum,
SUM(Case When amount >= 1000000 and amount < 2500000 Then amount else 0 End) as band5Sum,
...
from v_purchase
where p_date between '2014-06-10' and '2014-06-12'
;WITH Segments AS
(
SELECT 100 AS MinAmount, 100000 As MaxAmount
UNION ALL SELECT 100000, 250000
UNION ALL SELECT 250000, 500000
-- etc
)
SELECT
Segments.MinAmount,
Segments.MaxAmount,
COUNT(*) AS [Count],
SUM(v_purchase.amount) AS [Sum]
FROM
v_purchase
INNER JOIN Segments
ON Segments.MinAmount <= v_purchase.amount
AND Segments.MaxAmount > v_purchase.amount
WHERE
v_purchase.p_date = '2014-06-12'
GROUP BY
Segments.MinAmount,
Segments.MaxAmount
ORDER BY
Segments.MinAmount
在查询之间使用关键字“UNION ALL”。 如果查询之间的所有列都相同,这将起作用。
这取决于您是想要水平还是垂直结果。
如果您想要每行一行,则需要在每行上放置一个类别标签以区分它们。
DECLARE @p_date DATETIME = '2014-06-12'
SELECT '100 => 100000' AS Category, count(*) AS count1, SUM(amount) AS amount1 FROM v_purchase WHERE amount >= 100 AND amount < 100000 AND p_date = @p_date
UNION
SELECT '100000 => 2500000' AS Category, count(*) AS count1, SUM(amount) AS amount1 FROM v_purchase WHERE amount >= 100000 AND amount < 250000 AND p_date = @p_date
UNION
SELECT '250000 => 500000' AS Category, count(*) AS count1, SUM(amount) AS amount1 FROM v_purchase WHERE amount >= 250000 AND amount < 500000 AND p_date = @p_date
UNION
SELECT '500000 => 1000000' AS Category, count(*) AS count1, SUM(amount) AS amount1 FROM v_purchase WHERE amount >= 500000 AND amount < 1000000 AND p_date = @p_date
UNION
SELECT '1000000 => 2500000' AS Category, count(*) AS count1, SUM(amount) AS amount1 FROM v_purchase WHERE amount >= 1000000 AND amount < 2500000 AND p_date = @p_date
UNION
SELECT '2500000 => 5000000' AS Category, count(*) AS count1, SUM(amount) AS amount1 FROM v_purchase WHERE amount >= 2500000 AND amount < 5000000 AND p_date = @p_date
UNION
SELECT '5000000 => 10000000' AS Category, count(*) AS count1, SUM(amount) AS amount1 FROM v_purchase WHERE amount >= 5000000 AND amount < 10000000 AND p_date = @p_date
UNION
SELECT '> 10000000 ' AS Category, count(*) AS count1, SUM(amount) AS amount1 FROM v_purchase WHERE amount >= 10000000 AND p_date = @p_date
如果您希望将所有结果放在一行上,那么“CASE WHEN THEN”语句将允许您计算它们。
declare @ids table(idx int identity(1,1), min_amount int, max_amount int)
declare @results table(min_amount int, max_amount int, count1 int, amount1 int)
insert into @ids (min_amount, max_amount)
select 100, 100000 union
select 100000, 250000 union
select 250000, 500000 union
select 500000, 1000000 union
select 1000000, 2500000 union
select 2500000, 5000000 union
select 5000000, 10000000 union
select 10000000, 99999999
declare @i int
declare @cnt int
declare @min_amount int
declare @max_amount int
select @i = min(idx) - 1, @cnt = max(idx) from @ids
while @i < @cnt
begin
select @i = @i + 1
select @min_amount = min_amount from @ids where idx = @i
select @max_amount = max_amount from @ids where idx = @i
insert into @results(min_amount, max_amount, count1, amount1)
select
@min_amount,
@max_amount,
count(*) as count1,
sum(amount) as amount1
from v_purchase
where amount >= @min_amount and amount < @max_amount and p_date = '2014-06-12'
end
select min_amount, max_amount, count1, amount1
from @results
order by min_amount asc
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.