lundi 16 mai 2016

SQL count not showing result properly

I have a simple select query

select count(status_flag) STATUS_COUNT from
inward_doc_tracking_hdr where to_user = 1279  and status_flag = 4

which display' results as

Simple SELECT

I have joined with user_mst table like below:-

SELECT COUNT (a.status_flag) counts, a.mkey, a.to_user, b.email, b.first_name + ' ' + 
b.last_name name FROM inward_doc_tracking_hdr a LEFT JOIN user_mst b 
ON a.to_user = b.mkey WHERE a.to_user = '1279' AND a.status_flag = '4' 
Group by a.mkey, a.to_user, b.email, b.first_name,b.last_name 

which displays result as

JOIN QUERY

So my issue is

why the second query is showing two rows for the same to_user whose count is 2.

I am using sql-server-2005

Aucun commentaire:

Enregistrer un commentaire