2013-10-15 72 views
0

我有一個表中放置這樣選擇一個表兩次在MySQL獲得行數據爲列數據

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

2013-01-01 899.00 120.00     589.50  255.00 

以下幾列

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 

我想離開這個我試過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 

回答

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 
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; 
+0

是的,哈姆扎庫巴先是:) –

+0

哈,你是非常接近的第二個。 :) –