好吧,NOK计数Sonuc柱

时间:2016-01-08 07:38:35

标签: sql sql-server-2008-r2

SELECT COUNT(Sonuc)
      ,CAST(Tarih AS Date)
      ,VardiyaAdi
      ,ReferansNo
      ,MusteriAdi
      ,KontrolEden
FROM   Kayit_MPI_Kontrolu
GROUP BY
       CAST(Tarih AS Date)
      ,VardiyaAdi
      ,ReferansNo
      ,MusteriAdi
      ,KontrolEden

我有一个查询。 我想进入Sonuc列OK和NOK值计数。我该怎么办?

2 个答案:

答案 0 :(得分:1)

      select sum( case when Sonuc = 'OK' then 1 else 0 end ) as countOK,
             sum( case when Sonuc = 'NOK' then 1 else 0 end ) as countNOK,
                   ,CAST(Tarih AS Date)
      ,VardiyaAdi
      ,ReferansNo
      ,MusteriAdi
      ,KontrolEden
FROM   Kayit_MPI_Kontrolu
GROUP BY
       CAST(Tarih AS Date)
      ,VardiyaAdi
      ,ReferansNo
      ,MusteriAdi
      ,KontrolEden

答案 1 :(得分:0)

就像这样,

SELECT SUM(CASE WHEN Sonuc = 'OK' THEN 1 ELSE 0 END) AS OK
      ,SUM(CASE WHEN Sonuc = 'NOK' THEN 1 ELSE 0 END) AS NOK
      ,CAST(Tarih AS Date)
      ,VardiyaAdi
      ,ReferansNo
      ,MusteriAdi
      ,KontrolEden
FROM   Kayit_MPI_Kontrolu
GROUP BY
       CAST(Tarih AS Date)
      ,VardiyaAdi
      ,ReferansNo
      ,MusteriAdi
      ,KontrolEden