使用每个日期列下具有多个值的数据透视表SQL服务器的每日销售报表

时间:2017-06-12 09:48:56

标签: sql-server pivot-table

我必须从网站向管理员发送每周报告。每周报告看起来像下图。 ATV (Average Transaction Value) and UPT (Units per transaction)

表格模式和示例数据如下所示

    SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Sale](
    [Id] [nvarchar](128) NOT NULL,
    [UserId] [nvarchar](128) NOT NULL,
    [StoreId] [nvarchar](128) NOT NULL,
    [SaleAmount] [decimal](18, 2) NOT NULL,
    [CreatedDate] [datetime] NOT NULL,
    [Note] [nvarchar](max) NULL,
    [ATV] [decimal](18, 2) NOT NULL DEFAULT ((0)),
    [UPT] [decimal](18, 2) NOT NULL DEFAULT ((0)),
 CONSTRAINT [PK_dbo.Sale] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO
/****** Object:  Table [dbo].[Store]    Script Date: 12-Jun-17 2:20:36 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Store](
    [Id] [nvarchar](128) NOT NULL,
    [Title] [nvarchar](1000) NOT NULL,
    [Address1] [nvarchar](100) NULL,
    [Address2] [nvarchar](100) NULL,
    [Address3] [nvarchar](100) NULL,
    [City] [nvarchar](100) NULL,
    [State] [nvarchar](100) NULL,
    [Country] [nvarchar](100) NULL,
    [ZipCode] [nvarchar](100) NULL,
    [Telephone1] [nvarchar](100) NULL,
    [Telephone2] [nvarchar](100) NULL,
    [Telephone3] [nvarchar](100) NULL,
    [Location] [geography] NULL,
    [IsActive] [bit] NOT NULL DEFAULT ((0)),
    [Rank] [int] NOT NULL DEFAULT ((0)),
 CONSTRAINT [PK_dbo.Store] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO
INSERT [dbo].[Sale] ([Id], [UserId], [StoreId], [SaleAmount], [CreatedDate], [Note], [ATV], [UPT]) VALUES (N'11', N'9d5626d9-a71e-4116-bae5-4e6d736986c3', N'c71c82aa-2ad0-4599-b6d2-17a21a7b98da', CAST(9000.00 AS Decimal(18, 2)), CAST(N'2017-06-04 10:50:04.997' AS DateTime), N'5th', CAST(16.00 AS Decimal(18, 2)), CAST(27.00 AS Decimal(18, 2)))
GO
INSERT [dbo].[Sale] ([Id], [UserId], [StoreId], [SaleAmount], [CreatedDate], [Note], [ATV], [UPT]) VALUES (N'12', N'9d5626d9-a71e-4116-bae5-4e6d736986c3', N'c71c82aa-2ad0-4599-b6d2-17a21a7b98da', CAST(8000.00 AS Decimal(18, 2)), CAST(N'2017-06-05 10:50:04.997' AS DateTime), N'4th', CAST(15.00 AS Decimal(18, 2)), CAST(26.00 AS Decimal(18, 2)))
GO
INSERT [dbo].[Sale] ([Id], [UserId], [StoreId], [SaleAmount], [CreatedDate], [Note], [ATV], [UPT]) VALUES (N'13', N'9d5626d9-a71e-4116-bae5-4e6d736986c3', N'c71c82aa-2ad0-4599-b6d2-17a21a7b98da', CAST(7000.00 AS Decimal(18, 2)), CAST(N'2017-06-06 10:50:04.997' AS DateTime), N'3rd', CAST(14.00 AS Decimal(18, 2)), CAST(25.00 AS Decimal(18, 2)))
GO
INSERT [dbo].[Sale] ([Id], [UserId], [StoreId], [SaleAmount], [CreatedDate], [Note], [ATV], [UPT]) VALUES (N'14', N'9d5626d9-a71e-4116-bae5-4e6d736986c3', N'c71c82aa-2ad0-4599-b6d2-17a21a7b98da', CAST(6000.00 AS Decimal(18, 2)), CAST(N'2017-06-07 10:50:04.997' AS DateTime), N'second', CAST(13.00 AS Decimal(18, 2)), CAST(24.00 AS Decimal(18, 2)))
GO
INSERT [dbo].[Sale] ([Id], [UserId], [StoreId], [SaleAmount], [CreatedDate], [Note], [ATV], [UPT]) VALUES (N'15', N'9d5626d9-a71e-4116-bae5-4e6d736986c3', N'c71c82aa-2ad0-4599-b6d2-17a21a7b98da', CAST(5000.00 AS Decimal(18, 2)), CAST(N'2017-06-08 10:50:04.997' AS DateTime), N'TEST', CAST(12.00 AS Decimal(18, 2)), CAST(23.00 AS Decimal(18, 2)))
GO
INSERT [dbo].[Sale] ([Id], [UserId], [StoreId], [SaleAmount], [CreatedDate], [Note], [ATV], [UPT]) VALUES (N'16', N'9d5626d9-a71e-4116-bae5-4e6d736986c3', N'c71c82aa-2ad0-4599-b6d2-17a21a7b98da', CAST(10000.00 AS Decimal(18, 2)), CAST(N'2017-06-09 10:50:04.997' AS DateTime), N'6th', CAST(17.00 AS Decimal(18, 2)), CAST(28.00 AS Decimal(18, 2)))
GO
INSERT [dbo].[Sale] ([Id], [UserId], [StoreId], [SaleAmount], [CreatedDate], [Note], [ATV], [UPT]) VALUES (N'17', N'9d5626d9-a71e-4116-bae5-4e6d736986c3', N'c71c82aa-2ad0-4599-b6d2-17a21a7b98da', CAST(888.00 AS Decimal(18, 2)), CAST(N'2017-06-10 10:43:49.693' AS DateTime), N'test test2', CAST(12.00 AS Decimal(18, 2)), CAST(13.00 AS Decimal(18, 2)))
GO
INSERT [dbo].[Store] ([Id], [Title], [Address1], [Address2], [Address3], [City], [State], [Country], [ZipCode], [Telephone1], [Telephone2], [Telephone3], [Location], [IsActive], [Rank]) VALUES (N'67586972-6694-444a-9426-a0e6e4066015', N'store 3', N'1', N'Unnamed Road', NULL, N'Richmond', N'England', N'United Kingdom', N'DL11 6RR', NULL, NULL, NULL, NULL, 1, 3)
GO
INSERT [dbo].[Store] ([Id], [Title], [Address1], [Address2], [Address3], [City], [State], [Country], [ZipCode], [Telephone1], [Telephone2], [Telephone3], [Location], [IsActive], [Rank]) VALUES (N'68ece12f-c3e7-4705-88eb-2a0da2d7c1cc', N'Test Product', N'tesadf', N'323', N'asdf', N'sdf23', N'234', N'United Kingdom', N'234', NULL, NULL, NULL, NULL, 1, 4)
GO
INSERT [dbo].[Store] ([Id], [Title], [Address1], [Address2], [Address3], [City], [State], [Country], [ZipCode], [Telephone1], [Telephone2], [Telephone3], [Location], [IsActive], [Rank]) VALUES (N'c71c82aa-2ad0-4599-b6d2-17a21a7b98da', N'Store 1', N'Greenhillstairs', NULL, NULL, N'Moffat', N'Scotland', N'United Kingdom', N'DG10 9SP', NULL, NULL, NULL, 0xE6100000010C5278753348B04B406FEE5465841C0CC0, 1, 1)
GO
INSERT [dbo].[Store] ([Id], [Title], [Address1], [Address2], [Address3], [City], [State], [Country], [ZipCode], [Telephone1], [Telephone2], [Telephone3], [Location], [IsActive], [Rank]) VALUES (N'f26f67f1-e8f7-44a4-aaac-e861d18d54f3', N'Store 2', N'Lythe Fell Road', NULL, NULL, NULL, N'England', N'United Kingdom', NULL, NULL, NULL, NULL, 0xE6100000010C640D72721A054B40729472ADACD303C0, 1, 2)
GO

枢轴查询下方

DECLARE @DynamicPivotQuery AS NVARCHAR(MAX)
DECLARE @ColumnName AS NVARCHAR(MAX)
declare @fromDate  AS NVARCHAR(MAX)
set @fromDate=CAST('2017-06-04 10:50:04.997' AS DATE)

SELECT CAST(CreatedDate AS DATE) [Date],  sum(saleamount)  [SaleTotal], ATV,UPT 
    INTO #PivotSalesData2  
FROM sale
where createddate>='2017-06-04 10:50:04.997'
GROUP BY CAST(Createddate AS DATE),storeid,ATV,UPT

--Get distinct values of the PIVOT Column 
SELECT @ColumnName= ISNULL(@ColumnName + ',','') 
       + QUOTENAME([Date])
FROM #PivotSalesData2

print @columnName

--Prepare the PIVOT query using the dynamic 
SET @DynamicPivotQuery = 
  N'SELECT * from (SELECT (select Title from store where id=storeid ) storeid, CAST(CreatedDate AS DATE) [Date], 
             sum(atv) [atv]  
      FROM Sale
      where createddate>=@fromDate
      GROUP BY CAST(CreatedDate AS DATE),storeid,atv) AS DailyData
    PIVOT( sum([atv])
          FOR [Date] IN (' + @ColumnName + ')) AS PVTTable'

print @DynamicPivotQuery
--Execute the Dynamic Pivot Query
EXEC sp_executesql @DynamicPivotQuery , N'@fromDate nvarchar(max)', @fromDate = @fromDate

生成以下输出 enter image description here

但我需要在每个日期列下的ATV和UPT列值

1 个答案:

答案 0 :(得分:0)

我已经使用您的样本数据进行了测试。

让我知道你正在寻找的最终输出,你输出和输入应该串联。

SET @DynamicPivotQuery = 
  N'
      SELECT  * from 
      (select UserId,sum( [ATV])atv

         ,DATENAME(dw,CreatedDate) createddate

  FROM #Sale
 group by userid,createddate
  ) AS DailyData
PIVOT( sum(atv)
      FOR  createddate IN ('+@ColumnName+')
      )  AS PVTTable

union ALL
SELECT  * from 
      (select UserId,sum( [UPT])[UPT]

         ,DATENAME(dw,CreatedDate) createddate

  FROM #Sale
 group by userid,createddate
  ) AS DailyData
PIVOT( sum(upt)
      FOR  createddate IN ('+@ColumnName+')
      )  AS PVTTable

'
print @DynamicPivotQuery
exec (@DynamicPivotQuery)