简体   繁体   English

在SQL Server中透视日期

[英]Pivoting Date in SQL Server

I have the following code in SQL Server that is generating a count of users by Code and by Date : 我在SQL Server中具有以下代码,该代码通过CodeDate生成用户计数:

SELECT
    code, CONVERT(DATE, created_at) Date, COUNT(account_id) UserCount
FROM 
    Code_Table
GROUP BY
    code, CONVERT(DATE, created_at)

This just generates a count of users by each unique code and date combination. 这只是通过每个唯一的代码和日期组合来生成用户计数。 I'm attempting to use the Pivot function to get: 我正在尝试使用Pivot函数来获取:

  • the list of unique codes in the first column 第一列中的唯一代码列表
  • a column for each unique date, starting in the second column 每个唯一日期的一列,从第二列开始
  • a count of each user associated with the date-code combinations populating the table. 与填充表的日期代码组合关联的每个用户的计数。

One issue I'm running into is: I would like to set this query to update automatically daily, which will add an additional column with the most recent date each day. 我遇到的一个问题是:我想将此查询设置为每天自动更新,这将在每天的最新日期中添加一列。 The only pivots functions I've found require a declaration of the number of columns that are being created from rows. 我发现的唯一枢轴函数需要声明从行创建的列数。

I understand this would be much more easily done in Excel w/ a Pivot, but I don't currently have that option. 我知道在带有Pivot的Excel中更容易完成此操作,但是我目前没有该选项。

Any advice would be much appreciated! 任何建议将不胜感激!

You need a dynamic pivot to accomplish this. 您需要动态的枢纽才能完成此任务。 You're correct that you need an explicit column list -- so you'll query your table and actually generate SQL syntax as the result, and then execute that syntax with sp_executesql. 您正确的需要一个显式的列列表-因此您将查询表并实际生成SQL语法作为结果,然后使用sp_executesql执行该语法。

You can find that on Stack Overflow: SQL Server dynamic PIVOT query? 您可以在Stack Overflow上找到: SQL Server动态PIVOT查询?

A word of warning: This is usually not best practice. 警告:这通常不是最佳做法。 You won't be able to do any filtering or any date-related logic on this result set. 您将无法对此结果集执行任何过滤或任何与日期相关的逻辑。 Whatever front end reporting software you are using is probably where you want to implement the matrix/crosstab like behavior that you're getting from pivoting. 无论您使用哪种前端报告软件,都可能想在其中实现矩阵/交叉表,例如从旋转获得的行为。

Try using this store procedure: 尝试使用此存储过程:

CREATE PROCEDURE GetData 
    -- Add the parameters for the stored procedure here
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    DECLARE @columns varchar(MAX) = '';

    SELECT @columns = @columns + '[' + CONVERT(VARCHAR,ct.created_at) + '],'  
    FROM Code_Table ct
    GROUP BY created_at
    ORDER BY created_at ASC;

    SET @columns = SUBSTRING(@columns,1,LEN(@columns) - 1);

    DECLARE @query varchar(MAX)= 'SELECT  
                                    *
                                  FROM 
                                    (SELECT code,account_id,created_at from Code_Table) AS result
                                  PIVOT 
                                    (
                                     COUNT(account_id) FOR created_at IN ('+ @columns +')
                                    ) p'
                                  ;

    EXEC (@query)
END

I built the column's header dynamically depending of existing values of dates. 我根据日期的现有值动态构建了列的标题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM