[英]SQL PIVOT - Data Formatting Query
I have some data from a provider where unfortunately they don't have a true relational db. 我从提供者那里得到一些数据,不幸的是他们没有真正的关系数据库。 Basically I have a Reference, and 5 columns that are accident dates. 基本上,我有一个参考,还有5列是事故日期。 Now there can be multiple entries in the table because each 'frame' in the software can only show 5 accident dates. 现在,表中可以有多个条目,因为软件中的每个“框架”只能显示5个事故日期。
If they have more than 5 accidents then an additional row is created as that will be another frame. 如果他们发生了5次以上的事故,则将创建另一行,因为这将是另一帧。
Below is an example I have put together of a client that has 3 accident frames with 2 x frames with 5 accidents and 1 frame with 1. 下面是一个示例,我将一个具有3个事故帧的客户端与2个具有5个事故的帧和1个具有1个帧的帧组合在一起。
B@ PolRef@ Acc_date1 Acc_date2 Acc_date3 Acc_date4 Acc_date5
----------- ---------- ----------------------- ----------------------- ----------------------- ----------------------- -----------------------
0 LALX05PC01 2006-07-14 00:00:00.000 NULL NULL NULL NULL
0 LALX05PC01 2005-11-01 00:00:00.000 2007-06-07 00:00:00.000 2007-09-15 00:00:00.000 2007-09-16 00:00:00.000 2007-09-17 00:00:00.000
0 LALX05PC01 2005-11-01 00:00:00.000 2007-06-07 00:00:00.000 2007-06-08 00:00:00.000 2007-06-09 00:00:00.000 2007-06-10 00:00:00.000
I believe it will be a SQL PIVOT but I am still green with SQL and haven't got my head around PIVOT in SQL yet. 我相信它将是一个SQL PIVOT,但是我对SQL仍然持绿色态度,并且还没有对SQL中的PIVOT有所了解。
I need the data to end up that the Accident Dates are actually rows instead of columns for example 我需要数据来证明事故日期实际上是行而不是列
B@ PolRef@ AccidentDate
----------- ---------- -----------------------
0 LALX05PC01 2006-07-14 00:00:00.000
0 LALX05PC01 2005-11-01 00:00:00.000
0 LALX05PC01 2007-06-07 00:00:00.000
0 LALX05PC01 2007-09-15 00:00:00.000
Etc. there are additional columns I could do with inserting onto each line that are in the same fashion as Acc_Date1, Acc_Date2 etc but I feel it fair to ask for assistance and attempt to actually do some work myself :) 等等,我可以按照与Acc_Date1,Acc_Date2等相同的方式在每行上插入其他列,但是我觉得寻求帮助并尝试自己做一些工作很公平:)
If anyone can help me I would greatly appreciate it. 如果有人可以帮助我,我将不胜感激。
Regards, 问候,
James 詹姆士
Here's your answer : 这是您的答案:
/*Part 1 : Create a table test for exemple to test the Table-valued Function */
IF OBJECT_ID('Temp') IS NOT NULL
DROP TABLE Temp
SELECT *
INTO Temp
FROM(
SELECT 'James' Name, CONVERT(DATE,'2012-01-01') AS Date_1, ('Desc 1 2012-01-01') AS Desc_1, CONVERT(DATE,'2012-01-03') AS Date_2,('Desc 2 2012-01-03') AS Desc_2, CONVERT(DATE,'2012-01-04') AS Date_3,('Desc 3 2012-01-04') AS Desc_3 UNION ALL
SELECT 'James' Name, CONVERT(DATE,'2012-01-03') AS Date_1, ('Desc 1 2012-01-03') AS Desc_1,NULL AS Date_2,NULL AS Desc_2, CONVERT(DATE,'2012-01-02') AS Date_3,('Desc 3 2012-01- 02') AS Desc_3 UNION ALL
SELECT 'James' Name, CONVERT(DATE,'2012-01-07') AS Date_1, ('Desc 1 2012-01-07') AS Desc_1, NULL AS Date_2,NULL AS Desc_2, NULL AS Date_3,NULL AS Desc_3 UNION ALL
SELECT 'James' Name, CONVERT(DATE,'2012-02-01') AS Date_1, ('Desc 1 2012-02-01') AS Desc_1, NULL AS Date_2,NULL AS Desc_2, NULL AS Date_3,NULL AS Desc_3 UNION ALL
SELECT 'Youssef' Name, CONVERT(DATE,'2012-01-01') AS Date_1, ('Desc 1 2012-01-01') AS Desc_1, NULL AS Date_2,NULL AS Desc_2, CONVERT(DATE,'2012-01-04') AS Date_3,('Desc 3 2012-01- 04') AS Desc_3 UNION ALL
SELECT 'James' Name, CONVERT(DATE,'2012-01-12') AS Date_1, ('Desc 1 2012-01-12') AS Desc_1, CONVERT(DATE,'2012-01-04') AS Date_2,('Desc 2 2012-01-04') AS Desc_2, CONVERT(DATE,'2012-01-04') AS Date_3,('Desc 3 2012-01-04') AS Desc_3 UNION ALL
SELECT 'Youssef' Name, CONVERT(DATE,'2012-01-01') AS Date_1, ('Desc 1 2012-01-01') AS Desc_1, CONVERT(DATE,'2012-01-03') AS Date_2,('Desc 2 2012-01-03') AS Desc_2, CONVERT (DATE,'2013-01-01') AS Date_3,('Desc 3 2013-01-01') AS Desc_3
)AS T
GO
/* Part 2 : Creating the function that will allow us to create a DataSet of dates of accidents related to the Person */
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo]. [FN_GetAccidentDates]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[FN_GetAccidentDates]
GO
CREATE FUNCTION [dbo].[FN_GetAccidentDates](@p_name NVARCHAR(50))
RETURNS @table_result TABLE ([Dates] DATETIME,[Desciption] NVARCHAR(255))
AS
BEGIN
INSERT INTO @table_result([Dates], [Desciption])
SELECT vDate, vDesciption
FROM
(SELECT DISTINCT Date_1 AS vDate, Desc_1 AS vDesciption FROM Temp WHERE Name = @p_name
UNION
SELECT DISTINCT Date_2 AS vDate, Desc_2 AS vDesciption FROM Temp WHERE Name = @p_name
UNION
SELECT DISTINCT Date_3 AS vDate, Desc_3 AS vDesciption FROM Temp WHERE Name = @p_name
) AS T
WHERE
vDate IS NOT NULL
RETURN
END
GO
/*Part 3 Test the function and get the List Of dates */
SELECT
T.Name
,FN.Dates
,FN.[Desciption]
FROM
(SELECT DISTINCT Name FROM Temp) AS T
CROSS APPLY
[dbo].[FN_GetAccidentDates](T.Name) FN
I hope this will help you. 我希望这能帮到您。
Good Luck 祝好运
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.