SQL Server Group按列并每周连接一次

时间:2014-08-03 22:37:31

标签: sql sql-server database tsql

我有以下结果集,其中显示了公司名称和车辆租赁日期。一家公司可以租用多辆汽车。

NAME            MAKE    MODEL       STARTDATE   ENDDATE
Drive By        FORD    Fiesta      2014-08-04  2014-08-07
Drive By        FORD    Focus       2014-08-06  2014-08-19
Drive By        FORD    Mondeo      2014-08-18  2014-08-20
Drive By        AUDI    A4          2014-08-18  2014-08-20
Drive By        AUDI    A5          2014-08-18  2014-08-20
Hit n Run       AUDI    A3          2014-08-04  2014-08-26
Hit n Run       BMW     3 Series    2014-08-13  2014-08-15
Hit n Run       BMW     5 Series    2014-08-18  2014-08-20
Hit n Run       FORD    Fiesta      2014-08-18  2014-08-20
Hit n Run       FORD    Focus       2014-08-18  2014-08-20
Hit n Run       FORD    Mondeo      2014-08-22  2014-08-24
Super Stuntmen  AUDI    A4          2014-08-04  2014-08-15
Super Stuntmen  BMW     3 Series    2014-08-04  2014-08-08
Super Stuntmen  BMW     5 Series    2014-08-04  2014-08-08
Super Stuntmen  AUDI    A5          2014-08-11  2014-08-18

我希望为客户连接每周Make下的模型,结果是:

NAME            FORD Week1          AUDI Week2  BMW Week3                   FORD Week2      AUDI Week2  BMW Week2   FORD Week3              AUDI Week3  BMW Week3
Drive By        Fiesta, Focus                                               Focus                                   Focus, Mondeo           A4, A5
Hit n Run                           A3                                                      A3          3 Series    Fiesta, Focus, Mondeo   A3          5 Series
Super Stuntmen                      A4          3 Series, 5 Series                          A4, A5                                          A5

感谢任何帮助或指导。

DECLARE @Cars TABLE
(
  CAR_ID int, 
  MODEL varchar(255),
  MAKE varchar(255)
);

INSERT INTO @Cars (CAR_ID, MODEL, MAKE)
VALUES (1, 'A3', 'AUDI')

INSERT INTO @Cars (CAR_ID, MODEL, MAKE)
VALUES (2, 'A4', 'AUDI')

INSERT INTO @Cars (CAR_ID, MODEL, MAKE)
VALUES (3, 'A5', 'AUDI')

INSERT INTO @Cars (CAR_ID, MODEL, MAKE)
VALUES (4, '3 Series', 'BMW')

INSERT INTO @Cars (CAR_ID, MODEL, MAKE)
VALUES (5, '5 Series', 'BMW')

INSERT INTO @Cars (CAR_ID, MODEL, MAKE)
VALUES (6, 'Fiesta', 'FORD')

INSERT INTO @Cars (CAR_ID, MODEL, MAKE)
VALUES (7, 'Focus', 'FORD')

INSERT INTO @Cars (CAR_ID, MODEL, MAKE)
VALUES (8, 'Mondeo', 'FORD')

DECLARE @Company TABLE
(
  COMPANY_ID int, 
  NAME varchar(255)
);

INSERT INTO @Company (COMPANY_ID, NAME)
VALUES (1, 'Super Stuntmen')

INSERT INTO @Company (COMPANY_ID, NAME)
VALUES (2, 'Drive By')

INSERT INTO @Company (COMPANY_ID, NAME)
VALUES (3, 'Hit n Run')

DECLARE @Hire TABLE
(
  HIRE_ID int, 
  CAR_ID int,
  COMPANY_ID int,
  STARTDATE datetime,
  ENDDATE datetime
);

INSERT INTO @Hire (HIRE_ID, COMPANY_ID, CAR_ID, STARTDATE, ENDDATE)
VALUES (1, 1, 2, '2014-08-04', '2014-08-15')

INSERT INTO @Hire (HIRE_ID, COMPANY_ID, CAR_ID, STARTDATE, ENDDATE)
VALUES (2, 1, 3, '2014-08-06', '2014-08-18')

INSERT INTO @Hire (HIRE_ID, COMPANY_ID, CAR_ID, STARTDATE, ENDDATE)
VALUES (3, 1, 4, '2014-08-04', '2014-08-08')

INSERT INTO @Hire (HIRE_ID, COMPANY_ID, CAR_ID, STARTDATE, ENDDATE)
VALUES (4, 1, 5, '2014-08-04', '2014-08-08')

INSERT INTO @Hire (HIRE_ID, COMPANY_ID, CAR_ID, STARTDATE, ENDDATE)
VALUES (5, 2, 6, '2014-08-04', '2014-08-07')

INSERT INTO @Hire (HIRE_ID, COMPANY_ID, CAR_ID, STARTDATE, ENDDATE)
VALUES (6, 2, 7, '2014-08-11', '2014-08-19')

INSERT INTO @Hire (HIRE_ID, COMPANY_ID, CAR_ID, STARTDATE, ENDDATE)
VALUES (7, 2, 8, '2014-08-18', '2014-08-20')

INSERT INTO @Hire (HIRE_ID, COMPANY_ID, CAR_ID, STARTDATE, ENDDATE)
VALUES (8, 2, 2, '2014-08-18', '2014-08-20')

INSERT INTO @Hire (HIRE_ID, COMPANY_ID, CAR_ID, STARTDATE, ENDDATE)
VALUES (9, 2, 3, '2014-08-18', '2014-08-20')

INSERT INTO @Hire (HIRE_ID, COMPANY_ID, CAR_ID, STARTDATE, ENDDATE)
VALUES (10, 3, 1, '2014-08-04', '2014-08-26')

INSERT INTO @Hire (HIRE_ID, COMPANY_ID, CAR_ID, STARTDATE, ENDDATE)
VALUES (11, 3, 4, '2014-08-13', '2014-08-15')

INSERT INTO @Hire (HIRE_ID, COMPANY_ID, CAR_ID, STARTDATE, ENDDATE)
VALUES (12, 3, 5, '2014-08-18', '2014-08-20')

INSERT INTO @Hire (HIRE_ID, COMPANY_ID, CAR_ID, STARTDATE, ENDDATE)
VALUES (13, 3, 6, '2014-08-18', '2014-08-20')

INSERT INTO @Hire (HIRE_ID, COMPANY_ID, CAR_ID, STARTDATE, ENDDATE)
VALUES (14, 3, 7, '2014-08-18', '2014-08-20')

INSERT INTO @Hire (HIRE_ID, COMPANY_ID, CAR_ID, STARTDATE, ENDDATE)
VALUES (15, 3, 8, '2014-08-22', '2014-08-24')

select c.NAME, v.MAKE, v.MODEL, h.STARTDATE, h.ENDDATE 
from @Hire h
inner join @Company c on c.COMPANY_ID = h.COMPANY_ID
inner join @Cars v on v.CAR_ID = h.CAR_ID
order by c.NAME, h.STARTDATE

1 个答案:

答案 0 :(得分:1)

希望这有帮助

测试日期:

IF OBJECT_ID('TEMPDB..##TestData') > 0
    BEGIN
       DROP TABLE ##TestData
    END
GO

SELECT a.*
INTO ##TestData
FROM(SELECT 'Drive By' AS NAME,
          'FORD' AS MAKE,
          'Fiesta' AS MODEL,
          CAST('2014-08-04' AS DATE)AS STARTDATE,
          CAST('2014-08-07' AS DATE)AS ENDDATE
    UNION ALL
    SELECT 'Drive By',
          'FORD',
          'Focus',
          '2014-08-06',
          '2014-08-19'
    UNION ALL
    SELECT 'Drive By',
          'FORD',
          'Mondeo',
          '2014-08-18',
          '2014-08-20'
    UNION ALL
    SELECT 'Drive By',
          'AUDI',
          'A4',
          '2014-08-18',
          '2014-08-20'
    UNION ALL
    SELECT 'Drive By',
          'AUDI',
          'A5',
          '2014-08-18',
          '2014-08-20'
    UNION ALL
    SELECT 'Hit n Run',
          'AUDI',
          'A3',
          '2014-08-04',
          '2014-08-26'
    UNION ALL
    SELECT 'Hit n Run',
          'BMW',
          '3 Series',
          '2014-08-13',
          '2014-08-15'
    UNION ALL
    SELECT 'Hit n Run',
          'BMW',
          '5 Series',
          '2014-08-18',
          '2014-08-20'
    UNION ALL
    SELECT 'Hit n Run',
          'FORD',
          'Fiesta',
          '2014-08-18',
          '2014-08-20'
    UNION ALL
    SELECT 'Hit n Run',
          'FORD',
          'Focus',
          '2014-08-18',
          '2014-08-20'
    UNION ALL
    SELECT 'Hit n Run',
          'FORD',
          'Mondeo',
          '2014-08-22',
          '2014-08-24'
    UNION ALL
    SELECT 'Super Stuntmen',
          'AUDI',
          'A4',
          '2014-08-04',
          '2014-08-15'
    UNION ALL
    SELECT 'Super Stuntmen',
          'BMW',
          '3 Series',
          '2014-08-04',
          '2014-08-08'
    UNION ALL
    SELECT 'Super Stuntmen',
          'BMW',
          '5 Series',
          '2014-08-04',
          '2014-08-08'
    UNION ALL
    SELECT 'Super Stuntmen',
          'AUDI',
          'A5',
          '2014-08-11',
          '2014-08-18')AS a;

<强>查询:

;WITH cye_WeekofMonth
    AS (
    SELECT NAME,
         MAKE,
         MODEL,
         'WEEK ' + CAST(DATEPART(day, DATEDIFF(day, 0, STARTDATE) / 7 * 7) / 7 + 1 AS VARCHAR(1)) AS WeekofMonth
         ,
         DATEPART(day, DATEDIFF(day, 0, STARTDATE) / 7 * 7) / 7 + 1 AS test,
         STARTDATE
    FROM ##TestData
    )
, cte_PrePivot
    AS (
    SELECT MIN(c.NAME) AS NAME,
         MIN(c.MAKE) AS MAKE,
         STUFF((SELECT ', ' + sq.MODEL
               FROM cye_WeekofMonth AS sq
               WHERE sq.NAME = c.NAME
                AND sq.Make = c.Make
                AND sq.WeekofMonth = c.WeekofMonth
               FOR XML PATH('')), 1, 2, '') AS MODEL,
         MIN(c.WeekofMonth) AS WeekofMonth
    FROM cye_WeekofMonth AS c
    GROUP BY c.NAME,
           c.MAKE,
           c.WeekofMonth
    )
    SELECT *
    FROM(SELECT NAME,
             MODEL,
             MAKE + ' ' + WeekofMonth AS MakeWeek
        FROM cte_PrePivot)AS a PIVOT(MIN(MODEL)FOR MakeWeek IN([FORD WEEK 1],
                                                    [FORD WEEK 2],
                                                    [FORD WEEK 3],
                                                    [FORD WEEK 4],
                                                    [AUDI WEEK 1],
                                                    [AUDI WEEK 2],
                                                    [AUDI WEEK 3],
                                                    [AUDI WEEK 4],
                                                    [BMW WEEK 1],
                                                    [BMW WEEK 2],
                                                    [BMW WEEK 3],
                                                    [BMW WEEK 4]))AS bs

<强>清理:

IF OBJECT_ID('TEMPDB..##TestData') > 0
    BEGIN
       DROP TABLE ##TestData
    END
GO