SQL查询结果每月安排 [英] SQL query result monthly arrangement
本文介绍了SQL查询结果每月安排的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两张桌子
(1) MonthlyTarget {SalesManCode, TargetValue};
(2) MonthlySales {SalesManCode, SaleDate, AchievedValue};
我必须进行查询以产生如下表所示的结果:
I have to make a query that produces a result like the following table:
{SalesManCode, JanTar, JanAch, FebTar, FebAch,....., DecTar, DecAch}
查询应该是什么?
推荐答案
您使用的是什么数据库?我有一个存储过程,可以简化为 Microsoft SQL Server 2005 执行这种类型的数据透视...
What database are you using? I have a stored procedure that simplifies doing that type of pivot for Microsoft SQL Server 2005 ...
如果您使用的是 SQL Server 2005:
If you are using SQL Server 2005:
运行此程序以安装 pivot_query 过程.
这是您描述的 示例,输出如下所示:
Here is an example like you described, the output looks like:
SalesManCode April_Tar April_Ach February_Tar February_Ach January_Tar January_Ach March_Tar March_Ach
------------ ------------ ------------ ------------ ------------ ------------ ------------ ------------ ------------
Bob 12000.000 9000.000 9000.000 9000.000 10000.000 11000.000 11000.000 10000.000
Sara 12000.000 9200.000 9000.000 9300.000 10000.000 11500.000 11000.000 10200.000
这篇关于SQL查询结果每月安排的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文