Estado todos los recuentos

I have one table like below (MSSql)

Name  Status
----  -------
kmk   Success
kmk   Success
mvk   Fail
per   Success
Per   Fail
mvk   Fail

I need the query which gives result like below

Name Status  Count
---- ------- -----
kmk  Success 2
kmk  Fail    0
mvk  Success 0
mvk  Fail    2
Per  Success 1
Per  Fail    1

lo intenté con group by but i got result like below My Query is

(Select Name,[Status],COUNT([Status]) As Count From Checking group by [Status],Name)

Name Status  Count
---- ------- -----
kmk  Success 2
mvk  Fail    2
Per  Success 1
Per  Fail    1

I tried many ways can you please give me the query

preguntado el 31 de julio de 12 a las 11:07

You should make yourself familiar with GROUP BY clauses and the COUNT function. -

What have you tried? Show your query. Rewrite your question to have a better understanding of your table structure -

I tried with group by but i got output like below Name Status Res kmk Success 2 mvk Fail 2 mvk Success 1 Per Fail 1 per Success 1 -

Try doing an outer join with a table containing the two statuses ("Success", "Fail"), and use your count/group just like you did. -

Or if nothing else works, try a cartesian product of unique names with a 2-row table with your statuses. Then left outer join this cartesian product with your checking table to get the count. -

1 Respuestas

You can try the following. It works with sqlite3, and depending on your SQL engine, your mileage may vary:

select
  name,
  "Success" as status,
  sum(case when status = "Success" then 1 else 0 end) as count
from checking group by name
union
select
  name,
  "Fail" as status,
  sum (case when status = "Fail" then 1 else 0 end) as count
from checking group by name;

Result with SQLite3:

kmk|Fail|0
kmk|Success|2
mvk|Fail|2
mvk|Success|0
per|Fail|1
per|Success|1

Respondido 31 Jul 12, 22:07

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.