Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
select distinct(UserName), case when (column1 <> column2
        OR (column1 IS NULL AND column2 IS NOT NULL)
        OR (column1 IS NOT NULL AND column2 IS NULL)) then
     Count(column3)  else '0' end as Count
 from table1 where column4='test'  Group BY column1 , column2, UserName



UserName Count
john 0
mary 0
mary 1
mary 20
sean 0
sam 0

i am getting results like above but i need sum of count based on unique of username
for ex: Mary is repeated 3 time in the table but i need the result as mary:21 in sql query.
Please help me on this

thanks in advance
Posted
Updated 19-Nov-14 20:18pm
v2

You are on good way with the flag which says if column4 and column5 are different for the given row. Then you just need to sum those flags by user name.

SQL
select 
  column3 as UserName, 
  Sum(case 
    when (column4 <> column5
      OR (column4 IS NULL AND column5 IS NOT NULL)
      OR (column4 IS NOT NULL AND column5 IS NULL)) then 1 
    else 0 
  end) as Count
from table1 where column1='test' 
group by column3
 
Share this answer
 
Comments
SukirtiShetty 20-Nov-14 4:23am    
Thank you so much. It worked for me.
I'm not sure, but based on my Observation, i think you want show data for each users.
Try like this,
SQL
select UserName,sum(Count) as Count from table1 group by UserName
 
Share this answer
 
v2
Comments
SukirtiShetty 20-Nov-14 2:09am    
No i have tried but same results. i want results of count as 0 for other usernames but for mary i want 21 as count. Please help me on this
Rajesh waran 20-Nov-14 2:17am    
can u provide your full table stucture and sample data's? and pls provide your expecting output.
SukirtiShetty 20-Nov-14 3:28am    
Column1 Column2 Column3 Column4 Column5
test sample001 Mary NULL NULL
test sample002 Mary NULL NULL
test sample003 Mary NULL NULL
test sample004 Mary NULL NULL
test sample005 Mary A NULL
test sample006 Mary A A
test sample007 Mary NULL NULL
test sample008 John NULL NULL
test sample009 John NULL NULL
test sample010 John NULL NULL
test sample011 John NULL NULL
test sample012 John NULL NULL
test sample013 John NULL NULL
test sample014 John NULL NULL
test sample015 John NULL NULL
test sample016 Sam A NULL
test sample017 Sam NULL NULL
test sample018 Sam NULL NULL
test sample019 Sam NULL NULL
test sample020 Sam A NULL
test sample021 Sam NULL NULL
test sample022 Sam NULL NULL
test sample023 Sam NULL NULL
test sample024 Sam A NULL



Output will be:
UserName Count
Mary 1
John 0
Sam 3
Tomas Takac 20-Nov-14 3:43am    
You should update your question with this information. Use the "Improve question" button. BTW it seems the order of your columns is different in your sample data and your query.
Shweta N Mishra 20-Nov-14 3:42am    
on what basis you want only one count of Mary , i see its 7 and even a filter is applied on column3 then also its 2.
i think this should also work,check it.

SQL
select 
     Column3 as UserName,Count(1) as Count 
from 
    table1 
Where 
      isnull(column4,'')<>isnull(column5,'')
group by Column3
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900