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

Database

 
QuestionGrouping Results by ElectionName Pin
samflex19-Mar-14 10:44
samflex19-Mar-14 10:44 
Hello,

The following code returns the results we want.

However, they are being repeated.

For instance, we have ElectionName, Position, CandidateName and TotalCount.

Rather then display the results in the following format (current format)

SQL
ElectionName                    Position             Candidate Name  TotCount

Election Name 1 Mar 19 2014 	President	      Candidate Name 1	13
Election Name 1 Mar 19 2014 	President	      Candidate Name 2	22
Election Name 2 Mar 19 2014 	President	      Candidate Name 1	13
Election Name 1 Mar 19 2014 	President	      Candidate Name 2	22
Election Name 1 Mar 19 2014 	President	      Candidate Name 1	13
Election Name 1 Mar 19 2014 	President	      Candidate Name 2	22
Election Name 2 Mar 19 2014 	1ST Vice President    Candidate VP 1	27
Election Name 2 Mar 19 2014 	1ST Vice President    Candidate VP	8
Election Name 2 Mar 19 2014 	1ST Vice President    Candidate VP	27
Election Name 2 Mar 19 2014 	1ST Vice President    Candidate VP	8
Election Name 3 Mar 19 2014 	1ST Vice President    Candidate VP	27
etc
etc


We would like the results displayed in the following format:

SQL
ElectionName                    Position             Candidate Name  TotCount

Election Name 1 Mar 19 2014 	President	      Candidate Name 1	13
            		 	President	      Candidate Name 2	22
			 	President	      Candidate Name 1	13
			 	President	      Candidate Name 2	22
				President	      Candidate Name 1	13
			 	President	      Candidate Name 2	22
Election Name 1 Mar 19 2014 	1ST Vice President    Candidate VP 1	27
 				1ST Vice President    Candidate VP 1	8
				1ST Vice President    Candidate VP 1	27
 				1ST Vice President    Candidate VP 1	8
 				1ST Vice President    Candidate VP 1	27
 etc
 etc


with related positions group together with ElectioName.

Below is my code. Any help is greatly appreciated.

SQL
Select (e.ElectionName + CAST(e.ClosingDate AS VARCHAR(12))) as [ElectionName], 
p.position, 
c.candidateName,
COUNT(*) AS TotalCount
from ElectionResults er
inner join candidates c on er.candidateId = c.candidateid
inner join Positions p on er.positionId = p.positionId 
inner join Elections e on c.PositionId = c.PositionId
group by grouping Sets(e. ElectionName),p.position,p.positionid,c.candidateName, e.closingDate
order by p.PositionId

AnswerRe: Grouping Results by ElectionName Pin
Jörgen Andersson19-Mar-14 11:02
professionalJörgen Andersson19-Mar-14 11:02 
AnswerRe: Grouping Results by ElectionName Pin
Mycroft Holmes19-Mar-14 12:52
professionalMycroft Holmes19-Mar-14 12:52 
GeneralRe: Grouping Results by ElectionName Pin
samflex19-Mar-14 13:12
samflex19-Mar-14 13:12 
GeneralRe: Grouping Results by ElectionName Pin
Mycroft Holmes19-Mar-14 14:05
professionalMycroft Holmes19-Mar-14 14:05 
GeneralRe: Grouping Results by ElectionName Pin
samflex19-Mar-14 14:13
samflex19-Mar-14 14:13 
GeneralSSIS - Truncation may occur due to inserting data Pin
Smith201418-Mar-14 2:59
Smith201418-Mar-14 2:59 
AnswerRe: SSIS - Truncation may occur due to inserting data Pin
Kornfeld Eliyahu Peter18-Mar-14 3:00
professionalKornfeld Eliyahu Peter18-Mar-14 3:00 
GeneralRe: SSIS - Truncation may occur due to inserting data Pin
Smith201418-Mar-14 3:07
Smith201418-Mar-14 3:07 
AnswerRe: SSIS - Truncation may occur due to inserting data Pin
Kornfeld Eliyahu Peter18-Mar-14 3:11
professionalKornfeld Eliyahu Peter18-Mar-14 3:11 
GeneralRe: SSIS - Truncation may occur due to inserting data Pin
jschell18-Mar-14 8:44
jschell18-Mar-14 8:44 
GeneralRe: SSIS - Truncation may occur due to inserting data Pin
Mycroft Holmes18-Mar-14 12:55
professionalMycroft Holmes18-Mar-14 12:55 
QuestionSSIS - Truncation may occur due to inserting data Pin
Smith201418-Mar-14 2:54
Smith201418-Mar-14 2:54 
QuestionInserting a new record into 2 tables at the same time Pin
jkirkerx17-Mar-14 11:49
professionaljkirkerx17-Mar-14 11:49 
AnswerRe: Inserting a new record into 2 tables at the same time Pin
Mycroft Holmes17-Mar-14 19:48
professionalMycroft Holmes17-Mar-14 19:48 
GeneralRe: Inserting a new record into 2 tables at the same time Pin
jkirkerx19-Mar-14 13:21
professionaljkirkerx19-Mar-14 13:21 
GeneralRe: Inserting a new record into 2 tables at the same time Pin
Mycroft Holmes19-Mar-14 14:10
professionalMycroft Holmes19-Mar-14 14:10 
GeneralRe: Inserting a new record into 2 tables at the same time Pin
jkirkerx19-Mar-14 16:26
professionaljkirkerx19-Mar-14 16:26 
QuestionStructure Database Pin
sbangani15-Mar-14 5:29
sbangani15-Mar-14 5:29 
AnswerRe: Structure Database Pin
Richard Andrew x6415-Mar-14 10:55
professionalRichard Andrew x6415-Mar-14 10:55 
GeneralRe: Structure Database Pin
sbangani16-Mar-14 12:05
sbangani16-Mar-14 12:05 
AnswerRe: Structure Database Pin
Eddy Vluggen16-Mar-14 0:51
professionalEddy Vluggen16-Mar-14 0:51 
AnswerRe: Structure Database Pin
jschell17-Mar-14 8:37
jschell17-Mar-14 8:37 
QuestionGet Result according to comma seperated sequence Pin
Rupesh Kumar Swami15-Mar-14 4:04
Rupesh Kumar Swami15-Mar-14 4:04 
AnswerRe: Get Result according to comma seperated sequence Pin
Jörgen Andersson15-Mar-14 9:07
professionalJörgen Andersson15-Mar-14 9:07 

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.