0

我有一个包含以下列的表格

Date        recd_amt    paid_amnt  counter

2013-01-01  899.00      120.00      1  
2013-01-02  6988.00     255.00      1  
2013-01-03  94.89       259.00      1  
2013-01-01  589.50      255.00      2  
2013-01-02  745.00      569.00      2  
2013-01-03  298.00      985.00      2  
2013-01-04  449.00      312.00      2  
2013-01-04  271.00      255.00      1  

我想以这种方式输出

Date              Counter-1                      Counter 2
            Recd Amt     Paid amnt          Recd amnt  Paid amnt

2013-01-01   899.00    120.00                 589.50     255.00

我试过这个 sql 但不工作。请指出我错在哪里。

select date, 
    case when unit='1' then act_h3 end as u1acth3, 
    case when unit='1' then act_gb end  as u1actgb  
from systemactivity A 
   left join (select case when unit='2' then act_h3 end as u2acth3, 
                     case when unit='2' then act_gb end  as u2actgb 
              from systemactivity)b 
       on A.date = B.date 
4

2 回答 2

1

我不会试图找出你的列名,因为它们看起来很糟糕(抱歉),但这是你想要的一般 SQL 结构

SELECT t1.date, t1.recd_amt, t1.paid_amt, t2.recd_amt, t2.paid_amt
FROM systemactivity t1
LEFT JOIN systemactivity t2
    ON t1.date = t2.date
    AND t2.counter = 2
WHERE t1.counter = 1
于 2013-10-15T11:15:12.953 回答
1

你的意思是这样的:

select sa1.date, sa1.recd_amnt, sa1.paid_amnt, sa2.recd_amnt, sa2.paid_amnt
from systemactivity sa1
join systemactivity sa2 on sa2.date = sa1.date
where sa1.counter = 1 and sa2.counter = 2;
于 2013-10-15T11:15:23.043 回答