参加小组时的价值计数

时间:2014-07-02 10:56:48

标签: sql sql-server

我有一张桌子:

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 个答案:

答案 0 :(得分: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

答案 1 :(得分: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

答案 2 :(得分: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