Click here to Skip to main content
15,887,936 members
Home / Discussions / Database
   

Database

 
AnswerRe: saving changes to sqlserver2008 Pin
uspatel8-Nov-11 19:33
professionaluspatel8-Nov-11 19:33 
AnswerNot Allow Null ? Pin
David Mujica9-Nov-11 3:16
David Mujica9-Nov-11 3:16 
QuestionLesserOf function Pin
PIEBALDconsult8-Nov-11 15:05
mvePIEBALDconsult8-Nov-11 15:05 
AnswerRe: LesserOf function Pin
Andy_L_J8-Nov-11 21:28
Andy_L_J8-Nov-11 21:28 
GeneralRe: LesserOf function Pin
PIEBALDconsult9-Nov-11 1:45
mvePIEBALDconsult9-Nov-11 1:45 
AnswerRe: LesserOf function Pin
Andy_L_J9-Nov-11 7:33
Andy_L_J9-Nov-11 7:33 
AnswerRe: LesserOf function Pin
Jörgen Andersson8-Nov-11 22:50
professionalJörgen Andersson8-Nov-11 22:50 
QuestionAccumulation with multiple conditions Pin
VentsyV8-Nov-11 9:16
VentsyV8-Nov-11 9:16 
I'm trying to sum up a column twice, each with a separate condition

For example, if we have the following table:

Visitors
--------

SQL
MeetingID     LocationID       NumVisitors
---------     ------------     -----------
1             1                3
1             1                5
1             3                7
2             1                9
2             5                3
2             5                4
4             3                10


I want to specify 2 Meeting IDs (lets say 1 & 2) and have the query return the following:

SQL
LocationID    TotalNumVisitors1     TotalNumVisitors2
----------    -----------------     -----------------
1             8                     9
3             7                     0
5             0                     7 


I tried using a full outer join on the table with itself, but that accumulates everything (both ids) together.
SQL
Select MeetingID, Sum(Visitors.NumVisitors) as NumVisitors1, Sum(Visitors1.NumVisitors) NumVisitors1
from Visitors
full outer join ...

where Visitors.MeetingId = 1 or Visitors1.MeetingId = 2
Group By ...


I then tried using sub-queries - that results in dis-joined return set - half of the columns are populated, the other half are null, then it switches

SQL
Select  Visitors.MeetingId, TotalNumVisitors = (select Sum(Visitors.NumVisitors) from Visitors where Visitors.MeetingId = 1), 
TotalNumVisitors1 = (select Sum(Visitors.NumVisitors) from Visitors where Visitors.MeetingId = 2)
where ...


SQL
LocationID    TotalNumVisitors1     TotalNumVisitors2
----------    -----------------     -----------------
1             8                     NULL
1             NULL                  9
3             7                     NULL
5             NULL                  7 


Any ideas?
AnswerRe: Accumulation with multiple conditions Pin
Jörgen Andersson8-Nov-11 10:05
professionalJörgen Andersson8-Nov-11 10:05 
GeneralRe: Accumulation with multiple conditions Pin
VentsyV8-Nov-11 11:30
VentsyV8-Nov-11 11:30 
GeneralRe: Accumulation with multiple conditions Pin
Jörgen Andersson8-Nov-11 11:54
professionalJörgen Andersson8-Nov-11 11:54 
QuestionHow can i check executing time from mssql2008? Pin
buffering836-Nov-11 19:49
buffering836-Nov-11 19:49 
AnswerRe: How can i check executing time from mssql2008? Pin
Wayne Gaylard6-Nov-11 20:04
professionalWayne Gaylard6-Nov-11 20:04 
AnswerRe: How can i check executing time from mssql2008? Pin
Corporal Agarn8-Nov-11 8:43
professionalCorporal Agarn8-Nov-11 8:43 
Questionemeregency Pin
yousefshokati1-Nov-11 18:43
yousefshokati1-Nov-11 18:43 
AnswerRe: emeregency Pin
Mycroft Holmes1-Nov-11 19:15
professionalMycroft Holmes1-Nov-11 19:15 
GeneralRe: emeregency Pin
yousefshokati1-Nov-11 20:23
yousefshokati1-Nov-11 20:23 
GeneralRe: emeregency Pin
Mycroft Holmes1-Nov-11 20:58
professionalMycroft Holmes1-Nov-11 20:58 
GeneralRe: emeregency Pin
yousefshokati1-Nov-11 21:17
yousefshokati1-Nov-11 21:17 
QuestionStored Procedure to Handle CSV Pin
Franklin Smith31-Oct-11 0:32
Franklin Smith31-Oct-11 0:32 
AnswerRe: Stored Procedure to Handle CSV Pin
Mycroft Holmes31-Oct-11 1:11
professionalMycroft Holmes31-Oct-11 1:11 
GeneralRe: Stored Procedure to Handle CSV Pin
Franklin Smith31-Oct-11 2:07
Franklin Smith31-Oct-11 2:07 
GeneralRe: Stored Procedure to Handle CSV Pin
Mycroft Holmes31-Oct-11 11:58
professionalMycroft Holmes31-Oct-11 11:58 
GeneralRe: Stored Procedure to Handle CSV Pin
Franklin Smith31-Oct-11 22:02
Franklin Smith31-Oct-11 22:02 
GeneralRe: Stored Procedure to Handle CSV Pin
Mycroft Holmes31-Oct-11 22:52
professionalMycroft Holmes31-Oct-11 22:52 

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.