将值增加到同一列中的特定值

时间:2015-02-08 12:36:10

标签: sql-server

以下是我的表格:

companyId   country  Year
18507       Denmark  2002
18507       Denmark  2008

我想更新年份列,其中包括从最小年到最大年份的所有年份,如下所示:

companyId   country  Year
18507       Denmark  2002
18507       Denmark  2003
18507       Denmark  2004
18507       Denmark  2005
18507       Denmark  2006
18507       Denmark  2007
18507       Denmark  2008

这些最小值和最大值对于我拥有的每个公司都是不同的。 专家请帮助我完成这项工作。

由于 Satish Kumar Peddi

3 个答案:

答案 0 :(得分:0)

这似乎可以使用CTE,但不确定它将如何对更大量的数据执行。

示例数据:

CREATE TABLE #Test
(
    [companyID] INT
    , [Country] VARCHAR(100)
    , [Year] INT
);


INSERT INTO #Test
    ([companyID], [Country], [Year])
VALUES
    (18507, 'Denmark', 2002)
    , (18507, 'Denmark', 2008)

实际查询

;WITH CTE
AS (
    SELECT T.[companyID]
        , T.[Country]
        , MIN(T.[Year]) AS CurYear
        , MIN(T.[Year]) AS MinYear
        , MAX(T.[Year]) AS MaxYear
    FROM #Test AS T
    GROUP BY T.[companyID], T.[Country]

    UNION ALL
    SELECT T.[companyID]
        , T.[Country]
        , C.CurYear + 1
        , C.MinYear
        , C.MaxYear
    FROM #Test AS T
    INNER JOIN CTE AS C
        ON C.companyID = T.companyID
        AND C.Country = T.Country
    WHERE C.CurYear < C.MaxYear
)
SELECT DISTINCT *
FROM CTE

答案 1 :(得分:0)

退房:

DECLARE @t TABLE
    (
      companyID INT ,
      country NVARCHAR(MAX) ,
      year INT
    )

INSERT  INTO @t
VALUES  ( 18507, 'Denmark', 2002 ),
        ( 18507, 'Denmark', 2008 ),
        ( 18507, 'Brasil', 2003 ),
        ( 18507, 'Brasil', 2005 ),
        ( 18508, 'Denmark', 2004 ),
        ( 18508, 'Denmark', 2007 );
WITH    cte
          AS ( SELECT   MIN(year) year ,
                        MAX(year) AS m
               FROM     @t
               UNION ALL
               SELECT   cte.year + 1 ,
                        m
               FROM     cte
               WHERE    cte.year < cte.m
             )
    SELECT  t.companyID ,
            t.country ,
            cte.year
    FROM    cte
            JOIN ( SELECT   companyID ,
                            country ,
                            MIN(year) AS mi ,
                            MAX(year) AS ma
                   FROM     @t
                   GROUP BY companyID ,
                            country
                 ) t ON cte.year BETWEEN t.mi AND t.ma
    ORDER BY t.companyID ,
            t.country ,
            cte.year

输出:

companyID   country year
18507       Brasil  2003
18507       Brasil  2004
18507       Brasil  2005
18507       Denmark 2002
18507       Denmark 2003
18507       Denmark 2004
18507       Denmark 2005
18507       Denmark 2006
18507       Denmark 2007
18507       Denmark 2008
18508       Denmark 2004
18508       Denmark 2005
18508       Denmark 2006
18508       Denmark 2007

答案 2 :(得分:0)

  

使用两个变量用于策略&amp;结束年和领导命令 -

   DECLARE @startyear int
DECLARE @endyear int
SELECT @startyear= [Year]
,      @endyear=lead([Year],1) over ( order by Year)
FROM [dbo].[Testx]
ORDER BY [Year] desc

WHILE @startyear <= @endyear-2
BEGIN
    INSERT INTO testx ( companyId, country,   year         )
    VALUES            ( 18507,     'Denmark', @startyear+1 )
    SET @startyear=@startyear+1
END
SELECT companyID
,      Country
,      Year
FROM [dbo].[Testx]
ORDER BY YEAR

enter image description here