Sql ode>我想存储过程在这里会有所帮助。使用p_date作为参数。如果存在count1的保存值,会发生什么情况?你要合并它们吗?@zhongxiao37从查询中,每个查询都会有一条记录。它可以工作,但我想知道如何区分结果。。。哪一个是第一次查询的结果?例如,

Sql ode>我想存储过程在这里会有所帮助。使用p_date作为参数。如果存在count1的保存值,会发生什么情况?你要合并它们吗?@zhongxiao37从查询中,每个查询都会有一条记录。它可以工作,但我想知道如何区分结果。。。哪一个是第一次查询的结果?例如,,sql,sql-server,Sql,Sql Server,ode>我想存储过程在这里会有所帮助。使用p_date作为参数。如果存在count1的保存值,会发生什么情况?你要合并它们吗?@zhongxiao37从查询中,每个查询都会有一条记录。它可以工作,但我想知道如何区分结果。。。哪一个是第一次查询的结果?例如,在每个子查询中添加一个名为“type”的额外列。为每个项目提供所需的值query@Bobby你只是一针见血。注意不同的金额。。。一个是对计数进行1/0求和。另一个是求和案例和联合将有性能增益的数量?这两种方法似乎都能解决问题。@UchennaN


ode>我想
存储过程
在这里会有所帮助。使用
p_date
作为参数。如果存在
count1
的保存值,会发生什么情况?你要合并它们吗?@zhongxiao37从查询中,每个查询都会有一条记录。它可以工作,但我想知道如何区分结果。。。哪一个是第一次查询的结果?例如,在每个子查询中添加一个名为“type”的额外列。为每个项目提供所需的值query@Bobby你只是一针见血。注意不同的金额。。。一个是对计数进行1/0求和。另一个是求和
案例
联合
将有性能增益的数量?这两种方法似乎都能解决问题。@UchennaNwanyanwu我的期望是这会更快。这是使用单个聚合从表中选择数据一次,然后执行多个函数。UNIONS方法是从v_purchase表中完成单独的select语句,并将它们放在一个大烤肉串中,这意味着将每个单独语句的数据放到磁盘上。IMHO此方法会使DB做的工作更少。确认这一点的最佳方法是在查询分析器中打开实际的实验计划,并将每个计划的成本进行比较。这很有意义。您在
p_日期
上分组,如果我在做类似
的事情,p_日期在'2014-06-10'和'2014-06-12'之间
,该怎么办。我是否仍应在p_date上分组?@UchennaNwanyanwu也有可能通过在金额(可能是聚集的)和p_date上添加索引来改进whis。当您在分段上加入时选择求和值(要分段)时,这种加入条件如何工作。xxx装载金额(非求和)?每次购买都被加入到相应的“段”,然后这些结果按段进行计数和求和。@JanVanHerck+1表示比我更清洁的解决方案,很有趣地看到它如何在性能方面进行比较。
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'
    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'
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
;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
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