【问题标题】:How to split q1 data into months如何将 q1 数据拆分为月份
【发布时间】:2018-09-11 06:34:57
【问题描述】:

您好,我对 sql server 有一个疑问:

在我的表中有 4 个 querter 相关的 totalamt 值可用。 在这里,我想将 q1 总和值拆分为当年的月份(4 月、5 月、6 月)和 q2 总和值到当年的月份(7 月、8 月、9 月) q3 总和值到当年的月份(Oct、Nov、Dec) 如果我运行当前月份是(jan 或 feb 或 mar ),则 q4 对当年月份(Jan,Feb,Mar)的总和值是(jan 或 feb 或 mar ),然后将年份视为当年,否则将当年的下一年。

表:

CREATE TABLE [dbo].[task](
    [Vertical] [varchar](50) NULL,
    [AccountName] [varchar](50) NULL,
    [q1] [money] NULL,
    [q2] [money] NULL,
    [q3] [money] NULL,
    [q4] [money] NULL
) ON [PRIMARY]
GO
INSERT [dbo].[task] ([Vertical], [AccountName], [q1], [q2], [q3], [q4]) VALUES (N'BFSI', N'susse', 90.0000, 15.0000, 30.0000, 6.0000)
GO
INSERT [dbo].[task] ([Vertical], [AccountName], [q1], [q2], [q3], [q4]) VALUES (N'BFSI', N'AIG', 100.0000, 50.0000, 40.0000, 60.0000)
GO

基于以上数据,我希望输出如下:

+----------+-------------+---------+-----------+------+
| vertical | accountname | reveune |   month   | year |
+----------+-------------+---------+-----------+------+
| BFSI     | AIG         | 13.3333 | December  | 2018 |
| BFSI     | AIG         | 13.3333 | November  | 2018 |
| BFSI     | AIG         | 13.3333 | October   | 2018 |
| BFSI     | AIG         | 16.6666 | August    | 2018 |
| BFSI     | AIG         | 16.6666 | July      | 2018 |
| BFSI     | AIG         | 16.6666 | September | 2018 |
| BFSI     | AIG         |   20.00 | Feburary  | 2019 |
| BFSI     | AIG         |   20.00 | January   | 2019 |
| BFSI     | AIG         |   20.00 | March     | 2019 |
| BFSI     | AIG         | 33.3333 | April     | 2018 |
| BFSI     | AIG         | 33.3333 | June      | 2018 |
| BFSI     | AIG         | 33.3333 | May       | 2018 |
| BFSI     | susse       |    2.00 | Feburary  | 2019 |
| BFSI     | susse       |    2.00 | January   | 2019 |
| BFSI     | susse       |    2.00 | March     | 2019 |
| BFSI     | susse       |    5.00 | August    | 2018 |
| BFSI     | susse       |    5.00 | July      | 2018 |
| BFSI     | susse       |    5.00 | September | 2018 |
| BFSI     | susse       |   10.00 | December  | 2018 |
| BFSI     | susse       |   10.00 | November  | 2018 |
| BFSI     | susse       |   10.00 | October   | 2018 |
| BFSI     | susse       |   30.00 | April     | 2018 |
| BFSI     | susse       |   30.00 | June      | 2018 |
| BFSI     | susse       |   30.00 | May       | 2018 |
+----------+-------------+---------+-----------+------+

我尝试如下:

select  vertical ,accountname ,[q1]/3  as reveune , 'April' as month  ,year(getdate())as year from task 
union 
select  vertical ,accountname ,[q1]/3 revenue ,'May' as month,  year(getdate())as year from task 
union
select  vertical ,accountname ,[q1]/3  as reveune , 'June' as month  ,year(getdate())as year from task 
union 
select  vertical ,accountname ,[q2]/3 revenue ,'July' as month,  year(getdate())as year from task 
union
select  vertical ,accountname ,[q2]/3 revenue ,'August' as month,  year(getdate())as year from task 
union
select  vertical ,accountname ,[q2]/3 revenue ,'September' as month,  year(getdate())as year from task 
union 
select  vertical ,accountname ,[q3]/3 revenue ,'October' as month,  year(getdate())as year from task 
union
select  vertical ,accountname ,[q3]/3 revenue ,'November' as month,  year(getdate())as year from task 
union
select  vertical ,accountname ,[q3]/3 revenue ,'December' as month,  year(getdate())as year from task 
union
select  vertical ,accountname ,[q4]/3 revenue ,'January' as month,  
case when datepart(mm,getdate())=1 then datepart(yyyy,getdate()) else  datepart(yyyy,getdate())+1 end  year from task
union 
select  vertical ,accountname ,[q4]/3 revenue ,'Feburary' as month,  
case when datepart(mm,getdate())=1 then datepart(yyyy,getdate()) else  datepart(yyyy,getdate())+1 end  year from task 
union 
select  vertical ,accountname ,[q4]/3 revenue ,'March' as month,  
case when datepart(mm,getdate())=1 then datepart(yyyy,getdate()) else  datepart(yyyy,getdate())+1 end  year from task 

上面的查询给出了预期的结果。但是它需要很长时间。你能告诉我任何替代解决方案吗 在 sql server 中实现这个任务

【问题讨论】:

    标签: sql sql-server tsql sql-server-2008 sql-server-2012


    【解决方案1】:

    您可以尝试使用cross apply 构建所需的输出:

    select Vertical, AccountName, x.v as revenue, x.m as month, x.y as year
    from [dbo].[task]
    cross apply (values 
         (q1/3, 'April'    , year(getdate()))
        ,(q1/3, 'May'      , year(getdate()))
        ,(q1/3, 'June'     , year(getdate()))
        ,(q2/3, 'July'     , year(getdate()))
        ,(q2/3, 'August'   , year(getdate()))
        ,(q2/3, 'September', year(getdate()))
        ,(q3/3, 'October'  , year(getdate()))
        ,(q3/3, 'November' , year(getdate()))
        ,(q3/3, 'December' , year(getdate()))
        ,(q4/3, 'January'  , case when datepart(mm,getdate())=1 then datepart(yyyy,getdate()) else  datepart(yyyy,getdate())+1 end) 
        ,(q4/3, 'February' , case when datepart(mm,getdate())=1 then datepart(yyyy,getdate()) else  datepart(yyyy,getdate())+1 end) 
        ,(q4/3, 'March'    , case when datepart(mm,getdate())=1 then datepart(yyyy,getdate()) else  datepart(yyyy,getdate())+1 end) 
        ) x(v,m,y)
    

    注意:我不确定月份和季度之间的关联是否真的正确,但这只是一个实施细节,您应该能够快速修复它

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-05
      • 1970-01-01
      • 2013-03-30
      • 2020-10-19
      • 2018-03-16
      • 1970-01-01
      相关资源
      最近更新 更多