我正在尝试连接两个select语句,以在一个表中形成4列。我想要的结果将是这样的
+--------------+----------------+------------------+--------------+
| ExpectedDate | ExpectedAmount | ActualDate | ActualAmount |
+--------------+----------------+------------------+--------------+
| 01/03/2014 | 65161 | 2014-03-01 00:00 | 57 |
| 02/03/2014 | 64 | 2014-03-02 00:00 | 321651651 |
| 03/03/2014 | 8 | 2014-03-03 00:00 | 233258646 |
| 04/03/2014 | 561 | 2014-03-04 00:00 | 2321 |
| 05/03/2014 | 651651 | 2014-03-05 00:00 | 8494561 |
| 06/03/2014 | 651616131 | NULL | NULL |
| 07/03/2014 | 316548478 | NULL | NULL |
| 08/03/2014 | 646512132 | NULL | NULL |
| 10/03/2014 | 654984984 | NULL | NULL |
| 11/03/2014 | 323213218 | NULL | NULL |
| 12/03/2014 | 6464651 | NULL | NULL |
| 13/03/2014 | 313218 | NULL | NULL |
| 14/03/2014 | 849898 | NULL | NULL |
| 15/03/2014 | 3213218 | NULL | NULL |
| 16/03/2014 | 9898465 | NULL | NULL |
+--------------+----------------+------------------+--------------+
我目前有以下SQL,非常接近,但它们不是在同一行中,而是像这样拆分:
+--------------+----------------+------------------+--------------+
| ExpectedDate | ExpectedAmount | ActualDate | ActualAmount |
+--------------+----------------+------------------+--------------+
| 01/03/2014 | 65161 | NULL | NULL |
| 02/03/2014 | 64 | NULL | NULL |
| 03/03/2014 | 8 | NULL | NULL |
| 04/03/2014 | 561 | NULL | NULL |
| 05/03/2014 | 651651 | NULL | NULL |
| 06/03/2014 | 651616131 | NULL | NULL |
| 07/03/2014 | 316548478 | NULL | NULL |
| 08/03/2014 | 646512132 | NULL | NULL |
| 10/03/2014 | 654984984 | NULL | NULL |
| 11/03/2014 | 323213218 | NULL | NULL |
| 12/03/2014 | 6464651 | NULL | NULL |
| 13/03/2014 | 313218 | NULL | NULL |
| 14/03/2014 | 849898 | NULL | NULL |
| 15/03/2014 | 3213218 | NULL | NULL |
| 16/03/2014 | 9898465 | NULL | NULL |
| NULL | NULL | 2014-03-01 00:00 | 57 |
| NULL | NULL | 2014-03-02 00:00 | 321651651 |
| NULL | NULL | 2014-03-03 00:00 | 233258646 |
| NULL | NULL | 2014-03-04 00:00 | 2321 |
| NULL | NULL | 2014-03-05 00:00 | 8494561 |
+--------------+----------------+------------------+--------------+
这是SQL:
--DECLARE PERIOD
DECLARE @NextMonthDate VARCHAR(10)
DECLARE @currentMonthDate VARCHAR(10)
DECLARE @VarientVar VARCHAR(10)
DECLARE @ExpectedDateSample VARCHAR(10)
--SET PERIOD
SET @NextMonthDate = CONVERT(VARCHAR(4), DATEPART(YEAR, DATEADD(MONTH, -1, GETDATE())))+'-'+SUBSTRING(CONVERT(VARCHAR, DATEADD(MONTH, +1, GETDATE()), 112),5,2)
SET @currentMonthDate = CONVERT(VARCHAR(4), DATEPART(YEAR, GETDATE()))+'-'+SUBSTRING(CONVERT(VARCHAR, DATEADD(MONTH, +0, GETDATE()), 112),5,2)
SET @ExpectedDateSample = SUBSTRING(CONVERT(VARCHAR, DATEADD(MONTH, +0, GETDATE()), 112),5,2)+'/'+CONVERT(VARCHAR(4), DATEPART(YEAR, GETDATE()))
SELECT e.ExpectedDate, e.ExpectedAmount, a.ActualDate, a.ActualAmount
FROM (SELECT ExpectedDate, SUM(ExpectedPayment) ExpectedAmount
FROM Simplicity..V_PaymentPlan2
WHERE Period >= @currentMonthDate and Period < @NextMonthDate AND HyphenStatus != 'Unpaid' AND ExpectedDate LIKE '%'+@ExpectedDateSample+'%'
GROUP BY ExpectedDate) e
FULL JOIN
(SELECT CONVERT(VARCHAR, PaymentDate, 20) ActualDate, SUM(ActualPayment) ActualAmount
FROM Simplicity..V_PaymentPlan2
WHERE Period >= @currentMonthDate and Period < @NextMonthDate AND HyphenStatus != 'Unpaid' AND CONVERT(VARCHAR, PaymentDate, 20) LIKE '%'+@currentMonthDate+'%'
GROUP BY PaymentDate) a on a.ActualDate = e.ExpectedDate
ORDER BY e.ExpectedDate, a.ActualDate
任何想法如何让它们排成一行而不是插入NULL并将它们推下来?
我没有任何主键或外键。
答案 0 :(得分:0)
SELECT ExpectedDate,ExpectedAmount, ActualDate, ActualAmount FROM A a1
INNER JOIN A a2
ON a1.ExpectedDate = a2.ActualDate