【发布时间】: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