嗨,我对sql server有一个疑问:
我表中的有4个与querter相关的totalamt值可用。 在这里我想将q1总和值拆分为当年的月份(4月,5月,6月), q2总和值到当年的月份(7月,8月,9月) q3总和值表示当年的月份(十月,十一月,十二月) 如果我运行当前月份为(jan或feb或mar),则q4的总和值为当前年份的月份(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中实现此任务
答案 0 :(得分:0)
您可以尝试使用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)
注意:我不确定月份和季度之间的关联是否正确,但这只是一个实现性的细节,您应该能够快速解决它