将SQL查询合并为一个

时间:2014-06-12 09:37:36

标签: sql sql-server

我有这些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'

有没有办法将这些查询合并为一个并将其作为单个查询执行?然后可以在代码中分离结果。

6 个答案:

答案 0 :(得分:2)

在这些查询之间写下UNION个关键字。

答案 1 :(得分:2)

<强>已更新 根据对一系列日期的讨论进行更新。我创建了一个SQL Fiddle for you

    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'

答案 2 :(得分:2)

;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

答案 3 :(得分:1)

使用关键字&#34; UNION ALL&#34;你的查询之间。如果查询之间的所有列都相同,这将有效。

答案 4 :(得分:1)

这取决于您是想要水平还是垂直结果。

如果你想要每行1行,你需要在每一行上加上一个类别标签来辨别它们。

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

如果您希望将所有结果放在一行上,那么就是&#34; CASE WHEN THEN&#34;声明将允许你统计它们。

答案 5 :(得分:0)

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