简体   繁体   中英

Combining SQL queries into one

I have these SQL queries:

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'

Is there a way of combining these queries into one and execute it as a single query? The results can then be later separated in a code.

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

UPDATED Update based on discussion for a range of dates. I have created a 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'

;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

Use the keywords "UNION ALL" between your queries. This will work if all of your columns are the same between queries.

It depends on whether you want your results horizontally or vertically.

If you want 1 row, per result, you'll need to put a category label on each row to discern between them.

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

If you want all the results on a single row, then a "CASE WHEN THEN" statement would allow you to tally them up.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM