SQL - 有命令可以做到这一点吗?
我有一张表,目前是一长串设备及其销售时间的信息。我需要采用看起来像这样的表格:
Item | Time Sold
--------------------
A 05/2010
B 04/2010
C 03/2010
A 04/2010
A 05/2010
然后有一个表格,其中该项目作为第一列,日期计数作为列标题,如下所示:
Item | Count_03/2010 | Count_04/2010 | Count_05/2010
-------------------------------------------------------------
A | 0 | 1 | 2
B | 0 | 1 | 0
C | 1 | 0 | 0
有没有一种简单的方法可以做到这一点?我知道在其他语言中只有一个命令可以做到这一点,我想知道 SQL 是否有一个命令。
编辑
我的问题是有多个表,并且某些表中的月份可能与其他表中的不同。无论如何,是否可以通过列出变量然后使用这些变量在代码中创建列来编写一个适用于所有变量的脚本?如果我知道月份总是相同的,我可以写一个,但由于它们是可变的,有没有办法做到这一点。
I have a table that currently is a long list of devices and information about when they were sold. I need to take the table which would look something like this:
Item | Time Sold
--------------------
A 05/2010
B 04/2010
C 03/2010
A 04/2010
A 05/2010
And then have a table with the item as the first column, with the count of the dates being the column headers, like below:
Item | Count_03/2010 | Count_04/2010 | Count_05/2010
-------------------------------------------------------------
A | 0 | 1 | 2
B | 0 | 1 | 0
C | 1 | 0 | 0
Is there a simple way of doing this? I know in other languages there is a single command to do it, was wondering if SQL had one.
EDIT
My problem is that there is more than one table, and in some tables the months may be different than in other tables. Is there anyway to write a script that will apply to them all, by getting the variables listed and then using those to create the columns in the code? I could write one if I knew the months would always be the same, but since they are variable, is there a way of doing this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
SQL Server 2005 及更高版本有一个名为
pivot
的功能,但它仍然不是一个命令,您需要知道“销售时间”列中的值。您可以使用动态透视方法,如 Itzik Ben-Gan 在他的 Inside Microsoft SQL Server 中演示的那样2008:T-SQL 查询图书示例
SQL Server 2005 and up has something called
pivot
, but it is still not one command and you need to know the values in the Time sold column. You can use a dynamic pivot approach as demonstrated by Itzik Ben-Gan in his Inside Microsoft SQL Server 2008: T-SQL Querying bookExample
我没有测试它,但以下应该有效。我使用子查询来获得所需的结果:
I didn't test it, but the following should work. I uses subqueries to get the desired results:
在不知道要从中生成列名的值的情况下,MS-SQL 本身不提供任何以所需的表格格式显示数据的功能。
有一个 关于在 MS-SQL 中动态创建交叉表查询的精彩文章。这是一个黑客,但看起来应该可以工作。
否则,一种更灵活的方法是......
这将为您提供:
这是一种更好的数据分析格式,因为您可以编写查询来处理已知列名的数据。
例如,您可以在报告工具中使用此数据来:
要将其转换为您要求的报告格式,您需要“甚至最好使用 Crystal Reports 或 Excel 等报告工具。
Excel 和 Crystal Report 都支持数据透视表,以您请求的表格格式显示数据(而且看起来也会更好!)。
Natively MS-SQL does not provide any functionality to display data in the tabular format you want without knowing the values you want to generate the column names from.
There is a great article on dynamically creating crosstab queries in MS-SQL. It's a hack, but looks like it should work.
Otherwise a more flexible approach is to do...
This will give you:
This is a much better format to work with for data analysis, as you can write a query to handle the data as the column names are known.
For example, you can use this data in a reporting tool to:
To get it into the report format you have requested, you're better off using a reporting tool such as Crystal Reports or Excel even.
Both Excel and Crystal Report support Pivot tables for displaying your data in the tabular format you request (and it'll look a lot nicer too!).
您可以
group by
来获取每个项目的聚合结果。使用sum
和case
,您可以计算特定时间段内的项目数量。例如:如果您有多种格式的源表,请在子查询中将它们联合在一起。我将将此示例限制为仅一个范围以节省空间:
You can
group by
to get aggregated results per item. Usingsum
andcase
, you can count the number of items in a specific period. For example:If you have source tables in multiple formats, union them together in a subquery. I'll limit this example to only one range to save space: