我在下面有SLQ选择一个日期,但我想看到彼此相邻的两个日期。我怎么能这样做?
Delete From Variance_Analysis
INSERT INTO Variance_Analysis
SELECT PIDs.Product_Group,
PIDs.PID,
PIDs.PID_Name,
SUM(MaturityValue) Current_Balance,
AsOfDate as Current_Date
FROM [TBL_PIDs] AS PIDs INNER JOIN RAW_DATA_HIST AS RawData
ON PIDs.PID = RawData.PID
Where AsOfDate = '2017-10-31'
GROUP BY Product_Group,
PIDs.PID,
PIDs.PID_Name,
AsOfDate
正如您所知,我清除了表格,然后插入一些ID和一个日期(同时汇总帐户余额)。我需要另外一个日期并总结各自的余额。然后,我想找到绝对差异和相对差异,分成3个字段。我正在使用SQL Server 2008。
-- Step1:
CREATE TABLE test_table (
Product_Group varchar(255),
PID varchar(10),
PID_Name varchar(255),
maturityValue decimal(18,2),
AsofDate date,
Portfolio varchar(255)
);
-- Step2:
INSERT INTO test_table (Product_Group,PID,PID_Name,maturityValue,AsofDate,Portfolio)
VALUES ('Invt','I.B.7','Investments',1258.69,'9/29/2017','Holding')
INSERT INTO test_table (Product_Group,PID,PID_Name,maturityValue,AsofDate,Portfolio)
VALUES ('Asse','A.Q.7','Assets',1263.28,'9/29/2017','Holding')
INSERT INTO test_table (Product_Group,PID,PID_Name,maturityValue,AsofDate,Portfolio)
VALUES ('Forx','X.S.7','Foreign_Exchange',1267.89,'9/29/2017','Holding')
INSERT INTO test_table (Product_Group,PID,PID_Name,maturityValue,AsofDate,Portfolio)
VALUES ('Forx','X.S.7','Foreign_Exchange',1272.51,'9/29/2017','Holding')
INSERT INTO test_table (Product_Group,PID,PID_Name,maturityValue,AsofDate,Portfolio)
VALUES ('Forx','X.S.7','Foreign_Exchange',1277.15,'9/29/2017','Holding')
INSERT INTO test_table (Product_Group,PID,PID_Name,maturityValue,AsofDate,Portfolio)
VALUES ('Govt','G.G.7','Government',1281.81,'9/29/2017','Passing')
INSERT INTO test_table (Product_Group,PID,PID_Name,maturityValue,AsofDate,Portfolio)
VALUES ('Govt','G.G.7','Government',1286.48,'9/29/2017','Passing')
INSERT INTO test_table (Product_Group,PID,PID_Name,maturityValue,AsofDate,Portfolio)
VALUES ('Invt','I.S.7','Investments',1359.3852,'10/31/2017','Passing')
INSERT INTO test_table (Product_Group,PID,PID_Name,maturityValue,AsofDate,Portfolio)
VALUES ('Asse','I.S.7','Assets',1364.3424,'10/31/2017','Holding')
INSERT INTO test_table (Product_Group,PID,PID_Name,maturityValue,AsofDate,Portfolio)
VALUES ('Forx','I.S.7','Foreign_Exchange',1331.2845,'10/31/2017','Holding')
INSERT INTO test_table (Product_Group,PID,PID_Name,maturityValue,AsofDate,Portfolio)
VALUES ('Forx','I.S.7','Foreign_Exchange',1336.1355,'10/31/2017','Holding')
INSERT INTO test_table (Product_Group,PID,PID_Name,maturityValue,AsofDate,Portfolio)
VALUES ('Forx','I.S.7','Foreign_Exchange',1341.0075,'10/31/2017','Holding')
INSERT INTO test_table (Product_Group,PID,PID_Name,maturityValue,AsofDate,Portfolio)
VALUES ('Govt','I.S.7','Government',1435.6272,'10/31/2017','Holding')
INSERT INTO test_table (Product_Group,PID,PID_Name,maturityValue,AsofDate,Portfolio)
VALUES ('Govt','I.S.7','Government',1518.0464,'10/31/2017','Passing')
在:
Product_Group PID PID_Name maturityValue AsofDate Portfolio
Invt I.B.7 Investments 1258.69 9/29/2017 Holding
Asst A.Q.7 Assets 1263.28 9/29/2017 Holding
Forx X.S.7 Foreign_Exchange 1267.89 9/29/2017 Holding
Forx X.S.7 Foreign_Exchange 1272.51 9/29/2017 Holding
Forx X.S.7 Foreign_Exchange 1277.15 9/29/2017 Holding
Govt G.G.7 Government 1281.81 9/29/2017 Passing
Govt G.G.7 Government 1286.48 9/29/2017 Passing
Invt I.B.7 Investments 1359.3852 10/31/2017 Passing
Asst A.Q.7 Assets 1364.3424 10/31/2017 Holding
Forx X.S.7 Foreign_Exchange 1331.2845 10/31/2017 Holding
Forx X.S.7 Foreign_Exchange 1336.1355 10/31/2017 Holding
Forx X.S.7 Foreign_Exchange 1341.0075 10/31/2017 Holding
Govt G.G.7 Government 1435.6272 10/31/2017 Holding
Govt G.G.7 Government 1518.0464 10/31/2017 Passing
后:
答案 0 :(得分:0)
这适合我。
Delete From Variance_Analysis
INSERT INTO Variance_Analysis
SELECT PIDs.Product_Group,
PIDs.PID,
PIDs.PID_Name,
SUM(CASE WHEN AsOfDate = '2017-10-31' THEN MaturityValue ELSE 0 END) AS Current_Balance,
SUM(CASE WHEN AsOfDate = '2017-09-29' THEN MaturityValue ELSE 0 END) AS Prior_Balance
FROM [TBL_PIDs] AS PIDs INNER JOIN TBL_FR2052A_RAW_DATA_HIST AS RawData
ON PIDs.PID = RawData.PID
GROUP BY PIDs.Product_Group,
PIDs.PID,
PIDs.PID_Name