Click here to Skip to main content
15,902,817 members
Home / Discussions / Database
   

Database

 
GeneralRe: How to run exe file using xp_cmdshell Pin
Krushna Sahu30-Mar-09 2:55
Krushna Sahu30-Mar-09 2:55 
QuestionSQL Server Memory Usage Problem Pin
VikashGohil29-Mar-09 22:16
VikashGohil29-Mar-09 22:16 
AnswerRe: SQL Server Memory Usage Problem Pin
Lambin6-Apr-09 18:24
Lambin6-Apr-09 18:24 
QuestionService Account - Account Name Pin
sawerr29-Mar-09 6:57
sawerr29-Mar-09 6:57 
Questionsql noob question on COUNT Pin
Eytukan29-Mar-09 4:12
Eytukan29-Mar-09 4:12 
AnswerRe: sql noob question on COUNT Pin
ed575629-Mar-09 4:24
ed575629-Mar-09 4:24 
GeneralRe: sql noob question on COUNT Pin
Eytukan29-Mar-09 4:29
Eytukan29-Mar-09 4:29 
GeneralRe: sql noob question on COUNT Pin
ed575629-Mar-09 6:54
ed575629-Mar-09 6:54 
OK, you want to return a 0 if there are no scores over 20. That is trickier.

This will return the count of Bob's scores over 20:

Select userid,username,count(*) as [count] from temptable where userid=1 and score>20 group by userid


This union query will return that same result and also a dummy record with a 0 count:

Select userid,username,count(*) as ct from temptable as a where userid=1 and score>20 group by userid, username 
union 
Select top 1 userid,username,0 as ct from temptable as b where userid=1


One would think then that using the MAX function in a sub-select would get your desired result:

select userid,username,max(ct) as  
from 
 (Select userid,username,count(*) as ct from temptable as a where userid=1 and score>20 group by userid, username 
union 
 Select top 1 userid,username,0 as ct from temptable as b where userid=1) 
group by userid,username 


But I find that in T-SQL for some reason it throws an error. However, if I first define a view based on that union, then this works:

select userid,username,max(ct) from view_c as Over20 group by userid, username

GeneralRe: sql noob question on COUNT Pin
Eytukan29-Mar-09 7:00
Eytukan29-Mar-09 7:00 
GeneralRe: sql noob question on COUNT Pin
ed575629-Mar-09 16:45
ed575629-Mar-09 16:45 
GeneralRe: sql noob question on COUNT Pin
Eytukan29-Mar-09 19:18
Eytukan29-Mar-09 19:18 
GeneralRe: sql noob question on COUNT Pin
Eytukan29-Mar-09 6:16
Eytukan29-Mar-09 6:16 
GeneralRe: sql noob question on COUNT Pin
Eytukan29-Mar-09 6:48
Eytukan29-Mar-09 6:48 
AnswerRe: sql noob question on COUNT Pin
Jörgen Andersson29-Mar-09 8:48
professionalJörgen Andersson29-Mar-09 8:48 
GeneralRe: sql noob question on COUNT Pin
Eytukan29-Mar-09 19:18
Eytukan29-Mar-09 19:18 
AnswerRe: sql noob question on COUNT Pin
anup keshari30-Mar-09 5:04
anup keshari30-Mar-09 5:04 
GeneralRe: sql noob question on COUNT Pin
Eytukan30-Mar-09 5:08
Eytukan30-Mar-09 5:08 
GeneralRe: sql noob question on COUNT Pin
anup keshari30-Mar-09 5:28
anup keshari30-Mar-09 5:28 
AnswerRe: sql noob question on COUNT Pin
i.j.russell30-Mar-09 10:48
i.j.russell30-Mar-09 10:48 
QuestionQuery (find max from two table resultant row should be single in sql 2005) Pin
azad yadav28-Mar-09 21:43
azad yadav28-Mar-09 21:43 
AnswerRe: Query (find max from two table resultant row should be single in sql 2005) Pin
Reza Raad28-Mar-09 21:48
Reza Raad28-Mar-09 21:48 
AnswerRe: Query (find max from two table resultant row should be single in sql 2005) Pin
anup keshari30-Mar-09 5:11
anup keshari30-Mar-09 5:11 
QuestionStoring and retriving unicode characters in Oracle Pin
Saleem Tahiri28-Mar-09 21:25
Saleem Tahiri28-Mar-09 21:25 
AnswerRe: Storing and retriving unicode characters in Oracle Pin
Jörgen Andersson29-Mar-09 7:59
professionalJörgen Andersson29-Mar-09 7:59 
QuestionBacking up data from a Sql Server Table Pin
xbiplav28-Mar-09 16:20
xbiplav28-Mar-09 16:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.