2014-07-02 62 views
-3

取組值我有一個表:計數的同時通過

PCdID PSID PlateNo dtime 
1  100  15  2014-03-02 07:49:38.000 
1  101  16 2014-03-05 07:49:38.000 
1  100  15 2014-03-10 07:49:38.000 
2  102 108 2014-03-11 07:49:38.000 
1  101  16 2014-03-20 07:49:38.000 
1  101  16 2014-03-13 07:49:38.000 

我想不同的價值觀和計數,所以我寫:

select t.PCdID,t.PSID,t.PlateNo from Transaction_tbl t where dtime >='2014-01-01' 
group by PCdID,PSID,PlateNo order by PSID,PlateNo 

我的輸出是:

PCdID PSID PlateNo 
    1  100  15  
    1  101  16 
    2  102  108 

但我期待:

PCdID PSID PlateNo count 
    1  100  15  2 
    1  101  16  3 
    2  102  108 1 

如何重寫我的查詢?

+3

我不敢相信,如果你想擁有計數,你還沒有嘗試過'Count(*)'。 –

回答

1

中只需添加一個COUNT(*)SELECT

select t.PCdID, t.PSID, t.PlateNo, COUNT(*) as count 
from Transaction_tbl t 
where dtime >='2014-01-01' 
group by PCdID,PSID,PlateNo 
order by PSID,PlateNo 
+0

yes..worked fine ..thanks – user3790223

+0

非常感謝您的幫助 – user3790223

0

試試這個

select t.PCdID, t.PSID, t.PlateNo, COUNT(PlateNo) as Plate Count 
from Transaction_tbl t 
where dtime >='2014-01-01' 
group by PCdID,PSID,PlateNo 
order by PSID,PlateNo 

輸出

PCdID PSID PlateNo Plate Count 

1  100  15  2 
1  101  16  3 
2  102  108 1 
0

試試這個

select t.PCdID,t.PSID,t.PlateNo , count(*) Count 
from Transaction_tbl t 
where dtime >='2014-01-01' 
group by PCdID,PSID,PlateNo 
order by PSID,PlateNo