SQL PIVOT - 数据格式查询

时间:2014-04-02 14:28:01

标签: sql sql-server tsql

我有一些来自提供商的数据,但遗憾的是他们没有真正的关系数据库。基本上我有一个参考,5列是事故日期。现在表格中可以有多个条目,因为每个框架都有一个条目。在软件中只能显示5个事故日期。

如果他们有超过5个事故,则会创建一个额外的行,因为这将是另一个框架。

下面是一个我把一个客户端放在一起的例子,它有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

我相信这将是一个SQL PIVOT,但我仍然是SQL的绿色,并且还没有在SQL中了解PIVOT。

我需要数据结束,事故日期实际上是行而不是列,例如

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

等。我可以使用与Acc_Date1,Acc_Date2等相同的方式插入到每一行的其他列,但我觉得请求帮助并尝试自己做一些工作是公平的:)

如果有人可以帮助我,我会非常感激。

此致

詹姆斯

1 个答案:

答案 0 :(得分:1)

这是你的答案:

    /*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

我希望这会对你有所帮助。

祝你好运