如何将多行合并为有条件的单行

时间:2019-08-08 13:27:29

标签: sql sql-server entity-framework

我需要有关如何将数据插入具有多个条件的临时表的帮助。我有3个相互链接的表,而我的问题是每个金额的 AmountIntakeVendorRequisition 中的金额值在单行中返回。因此,我需要将每个具有相同“ AmountIntakeVendorId”的金额合并到临时表的Quaters列(Q1,Q2,Q3,Q4)中。下表是我的案例。预先感谢!

金额摄入

 ------ ---------------------- --------------- ------------- 
| id   |  AmountIntakeBucketId|     CenterId  |   ProjectId |   
 ------ ---------------------- --------------- ------------- 
|  400 |                 94   |         C1000 |       P100  | 
|  401 |                 95   |         C1001 |       P101  | 
|  402 |                 96   |         C1002 |       P102  | 
|  403 |                 97   |         C1003 |       P103  | 
 ------ ---------------------- --------------- ------------- 

AmountIntakeVendor

 ------ ----------------- --------------- --------------
| id   |  AmountIntakeId |     VendorId  |   ProjectName| 
 ------ ----------------- --------------- --------------
|  411 |             400 |             1 |    Microsoft | 
|  412 |             400 |             2 |         Dell | 
|  413 |             400 |             4 |       Google | 
|  414 |             400 |             4 |       Huawei | 
 ------ ----------------- ---------------- -------------

AmountIntakeVendorRequisition

  ------ ----------------------  ---------------------------- -------------------------- ------------
| id   |  AmountIntakeVendorId|                    StartDt |                    EndDt |    Amount   |
 ------ ---------------------- ---------------------------- -------------------------- -------------
| 1771 |                  411 |    2019-04-01 00:00:00.000 |  2019-06-30 23:59:59.000 |     1000.00 |
| 1772 |                  411 |    2019-07-01 00:00:00.000 |  2019-09-31 23:59:59.000 |     1430.00 |
| 1773 |                  411 |    2019-10-01 00:00:00.000 |  2019-12-31 23:59:59.000 |     2000.00 |
| 1774 |                  412 |    2019-07-01 00:00:00.000 |  2019-09-31 23:59:59.000 |     5000.00 |
 ------  ---------------------- --------------------------- -------------------------- -------------

预期结果

 ----------- ----------------------  ------------ -------------- ------------- ------------- 
| ProjectId |          ProjectName |          Q1 |           Q2 |          Q3 |          Q4 |        
 ----------- ---------------------- ------------- -------------- ------------- ------------- 
|      P100 |            Microsoft |        NULL |      1000.00 |    1430.00  |     2000.00 |  
|      P101 |                 Dell |        NULL |      5000.00 |        NULL |        NULL |         
 ----------  ---------------------- ------------- -------------- ------------- ------------- 

我已经创建了用于插入数据的表,但是我被困在将每个数量(Q1,Q2,Q2,Q4)的'Amount'插入到@tempTable中

  DECLARE @AIBId Int = 94
  DECLARE @tempTable TABLE            
 (            
  ProjectId INT,            
  ProjectName NVARCHAR(100),              
  Q1 DECIMAL (12,2), --1
  Q2 DECIMAL (12,2), -- 4
  Q3 DECIMAL (12,2), --7
  Q4 DECIMAL (12,2)  --10    
 )       

INSERT into @tempTable
SELECT *
AI.ProjectId,
AIV.ProjectName,
Month(AISR.StartDate) AS Quater,
CASE Quater
WHEN 1 THEN AISR.Amount
WHEN IS NULL THEN NULL
END AS Q1
CASE Quater
WHEN 4 THEN AISR.Amount
WHEN IS NULL THEN NULL
END AS Q2
CASE Quater
WHEN 7 THEN AISR.Amount
WHEN IS NULL THEN NULL
END AS Q3
CASE Quater
WHEN 10 THEN AISR.Amount
WHEN IS NULL THEN NULL
END AS Q4
END 
FROM AmountIntake AI
LEFT JOIN AmountIntakeVendor AIV on AI.Id = AIV.AmountIntakeId
LEFT JOIN AmountIntakeVendorRequisition AIVR on AIV.ID = AIVR.AmountIntakeVendorId
WHERE AI.AmountIntakeBucketId = @AIBId 

SELECT * from @tempTable

1 个答案:

答案 0 :(得分:0)

您的select查询看起来不错,只需进行较小的更新--

SELECT AI.ProjectId
      ,AIV.ProjectName
      ,Month(AISR.StartDate) AS Quater
      ,CASE Month(AISR.StartDate) WHEN 1 THEN AIVR.Amount END AS Q1
      ,CASE Month(AISR.StartDate) WHEN 4 THEN AIVR.Amount END AS Q2
      ,CASE Month(AISR.StartDate) WHEN 7 THEN AISR.Amount END AS Q3
      ,CASE Month(AISR.StartDate) WHEN 10 THEN AISR.Amount END AS Q4
FROM AmountIntake AI
LEFT JOIN AmountIntakeVendor AIV on AI.Id = AIV.AmountIntakeId
LEFT JOIN AmountIntakeVendorRequisition AIVR on AIV.ID = AIVR.AmountIntakeVendorId
WHERE AI.AmountIntakeBucketId = @AIBId

CASE语句如果不符合条件,则会自动分配NULL,因此无需显式分配。