如果有多个数据点,则按帐户拆分收入数据的逻辑

时间:2014-07-27 19:01:19

标签: sql

首先关闭SQL noob,所以我的问题可能没有意义。

表1有ID和Name(我想要添加数据的表) 表2是一个包含数百万个数据点的大表,我需要从中获取收入信息。

表2也有dateid,我转换为月份和年份,我想将月份和年份汇总到四分之一年,这是我得到的。我真的需要像下面这样保留这个脚本,但稍微修改一下。

所以最后我希望按产品和季度收入。我错过了什么?

这是我的剧本

select t1.nameid,
    t1.date_id,
      -- month
      period_idtoid(t1.date_id,DATEID,MONTHOFYEAR) as month,
      -- year
  period_idtoid(t1.date_id,DATEID,YEARID) as year,
SELECT t1.month,t1.year
   CASE 
    WHEN month in (1,2,3) and year = 2012 then 'Q1 2012'
    WHEN month in (4,5,6) and year = 2012 then 'Q2 2012'
    WHEN month in (7,8,9) and year = 2012 then 'Q3 2012'
    WHEN month in (10,11,12) and year = 2012 then 'Q4 2012'
    WHEN month in (1,2,3) and year = 2013 then 'Q1 2013'
    WHEN month in (4,5,6) and year = 2013 then 'Q2 2013'
    WHEN month in (7,8,9) and year = 2013 then 'Q3 2013'
    WHEN month in (10,11,12) and year = 2013 then 'Q4 2013'
    WHEN month in (1,2,3) and year = 2013 then 'Q1 2014'
    WHEN month in (4,5,6) and year = 2013 then 'Q2 2014'

   END AS 'Quarter'

    FROM 'Big table t1'


       sum(case when product_group = 'dog food' then revenue else 0 end) as dog_food_spend,
       sum(case when product_group = 'cat food' then revenue else 0 end) as cat_food_spend,
       sum(case when product_group = 'parrot food' then revenue else 0 end) as parrot_food_spend,

   from table 2 t1 join
     table 1 t2
     on t1.nameid = t2.nameid
group by 1,2,3,4,5

3 个答案:

答案 0 :(得分:1)

假设T-SQL和SQL Server以及date_id是SQL DateTime:

SELECT product_group, 
       sum(revenue) as revenue, 
       quarter + ' ' + convert(varchar(4), year)  as quarter
FROM 
    (SELECT 
         bt.product_group, 
         sum(isnull(bt.revenue, 0)) as revenue,  
         year(r.date_id) as year, 
         month(r.date_id) as month,
         case when month(r.date_id) in (1,2,3) then 'Q1'
              when month(r.date_id) in (4,5,6) then 'Q2'
              when month(r.date_id) in (7,8,9) then 'Q3'
         else 'Q4' end as quarter

     FROM Revenue r
     LEFT JOIN BigTable bt ON r.nameid = bt.nameid
     GROUP BY bt.product_group, year(r.date_id), month(r.date_id)
    ) a
GROUP BY product_group, 
         year,
         quarter 

答案 1 :(得分:0)

我认为这可能就是你要找的东西:

select    nameid,
          year,
          case 
            when month in (1,2,3)    then concat('Q1 ',year)
            when month in (4,5,6)    then concat('Q2 ',year)
            when month in (7,8,9)    then concat('Q3 ',year)
            when month in (10,11,12) then concat('Q4 ',year)
            end as qtr,
         sum(case when product_group = 'dog food' then revenue else 0 end) as dog_food_spend,
         sum(case when product_group = 'cat food' then revenue else 0 end) as cat_food_spend,
         sum(case when product_group = 'parrot food' then revenue else 0 end) as parrot_food_spend
from     table2
where    nameid in (select nameid from table1) 
group by nameid,
         year,
         case 
            when month in (1,2,3)    then concat('Q1 ',year)
            when month in (4,5,6)    then concat('Q2 ',year)
            when month in (7,8,9)    then concat('Q3 ',year)
            when month in (10,11,12) then concat('Q4 ',year)
            end
order by nameid, year

这仅针对2014年第二季度执行以上操作:(根据您的评论)

select    nameid,
          year,
          case 
            when month in (1,2,3)    then concat('Q1 ',year)
            when month in (4,5,6)    then concat('Q2 ',year)
            when month in (7,8,9)    then concat('Q3 ',year)
            when month in (10,11,12) then concat('Q4 ',year)
            end as qtr,
         sum(case when product_group = 'dog food' then revenue else 0 end) as dog_food_spend,
         sum(case when product_group = 'cat food' then revenue else 0 end) as cat_food_spend,
         sum(case when product_group = 'parrot food' then revenue else 0 end) as parrot_food_spend
from     table2
where    nameid in (select nameid from table1) 
     and year = 2014
     and month in (4,5,6)
group by nameid,
         year,
         case 
            when month in (1,2,3)    then concat('Q1 ',year)
            when month in (4,5,6)    then concat('Q2 ',year)
            when month in (7,8,9)    then concat('Q3 ',year)
            when month in (10,11,12) then concat('Q4 ',year)
            end
order by nameid, year

编辑 -

根据您的评论,以下内容可能更接近您想要的内容:

select    nameid,
          year,
          product_group,
          sum(case when month in (1,2,3)    then revenue else 0 end) as q1
          sum(case when month in (4,5,6)    then revenue else 0 end) as q2
          sum(case when month in (7,8,9)    then revenue else 0 end) as q3
          sum(case when month in (10,11,12) then revenue else 0 end) as q4
from     table2
where    nameid in (select nameid from table1) 
group by nameid,
         year,
         product_group
order by nameid, year, product_group

答案 2 :(得分:0)

当您的主数据表包含数百万行时,您肯定希望进行预聚合:

create table #t (
     Quarter         varchar(7)
    ,ProductGroup    varchar(20)
    ,Amount          money
);

insert #t(Quarter,ProductGroup,Amount)
select
   CASE 
     when month between  1 and  3 then 'Q1 '
     when month between  4 and  6 then 'Q2 '
     when month between  7 and  9 then 'Q3 '
     when month between 10 and 12 then 'Q4 '
   end + cast(year as char(4))
   as Quarter,
   ProductGroup,
   sum(Amount) as Amount
from [Big Table]
group by
   CASE 
     when month between  1 and  3 then 'Q1 '
     when month between  4 and  6 then 'Q2 '
     when month between  7 and  9 then 'Q3 '
     when month between 10 and 12 then 'Q4 '
   end + cast(year as char(4))
   as Quarter,
   ProductGroup;

select
    PoductGroup
    ,"Q1 2012","Q2 2012","Q3 2012","Q4 2012"
    ,"Q1 2013","Q2 2013","Q3 2013","Q4 2013"
    ,"Q1 2014","Q2 2014","Q3 2014","Q4 2014"
from #t
pivot (sum(Amount) for Quarter in ("Q1 2012","Q2 2012","Q3 2012","Q4 2012"
                                  ,"Q1 2013","Q2 2013","Q3 2013","Q4 2013"
                                  ,"Q1 2014","Q2 2014","Q3 2014","Q4 2014")
group by ProductGroup;

drop table #t;