我有两张桌子。
表格1
CREATE TABLE ##MedianTemp1 (
ProductID INT NOT NULL,
CountryCodeID INT NOT NULL,
Y1977 INT NULL,
Y1978 INT NULL,
Y1979 INT NULL PRIMARY KEY CLUSTERED (ProductID,CountryCodeID))
INSERT INTO ##MedianTemp1 VALUES
(1, 100, 0, 0, 0),
(2, 100, 0, 0, 0),
(3, 100, 0, 0, 0),
(4, 100, 0, 0, 0),
(5, 100, 1, 0, 0),
(6, 100, 1, 1, 0),
(7, 100, 1, 1, 1),
(8, 100, 1, 1, 1),
(9, 100, 1, 1, 1),
(1, 101, 0, 0, 0),
(2, 101, 0, 0, 0),
(3, 101, 0, 0, 0),
(4, 101, 1, 0, 0),
(5, 101, 1, 1, 0),
(6, 101, 1, 1, 1),
(7, 101, 1, 1, 1),
(8, 101, 1, 1, 1),
(9, 101, 1, 1, 1)
表 2
CREATE TABLE ##MedianTemp2 (
ProductID INT NOT NULL,
CountryCodeID INT NOT NULL,
Y1977 FLOAT(53) NULL,
Y1978 FLOAT(53) NULL,
Y1979 FLOAT(53) NULL PRIMARY KEY CLUSTERED (ProductID,CountryCodeID))
INSERT INTO ##MedianTemp2 VALUES
(1, 100, 5, 4, 1),
(2, 100, 6, 3, 2),
(3, 100, 1, 2, 4),
(4, 100, 2, 1, 5),
(5, 100, 6, 2, 6),
(6, 100, 7, 3, 7),
(7, 100, 1, 2, 3),
(8, 100, 7, 1, 8),
(9, 100, 9, 6, 9),
(1, 101, 5, 4, 1),
(2, 101, 6, 3, 2),
(3, 101, 1, 2, 4),
(4, 101, 2, 1, 5),
(5, 101, 6, 2, 6),
(6, 101, 7, 3, 7),
(7, 101, 1, 2, 3),
(8, 101, 7, 1, 8),
(9, 101, 9, 6, 9)
我需要创建这个:
##MedianResult (
ProductID INT NOT NULL,
CountryCodeID INT NOT NULL,
Y1977 FLOAT(53) NULL,
Y1978 FLOAT(53) NULL,
Y1979 FLOAT(53) NULL
PRIMARY KEY CLUSTERED (ProductID,CountryCodeID))
ProductID
查询应该为每一年和每个 CountryID(来自表)找到唯一一个##medianTemp1
将具有 value'0'
并且ProductID+1
将具有1
.
对于这个成立productID
(每年和每个国家会有所不同),我们应该使用##MedianTemp2
表格中的值应用一个公式。Sum(ProductID, ProductID+1, ProductID+2)
.
我知道我的描述有点奇怪......但不知道如何更好地描述我的问题。