Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Grouping Results by ElectionName

Grouping Results by ElectionName

Scheduled Pinned Locked Moved Database
help
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    samflex
    wrote on last edited by
    #1

    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)

    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:

    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.

    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

    J M 2 Replies Last reply
    0
    • S samflex

      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)

      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:

      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.

      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

      J Offline
      J Offline
      Jorgen Andersson
      wrote on last edited by
      #2

      This can be achieved by looping through the resultset and clear the ElectionName if the Position is the same as in the previous row. But this doesn't belong to the database.

      Wrong is evil and must be defeated. - Jeff Ello[^]

      1 Reply Last reply
      0
      • S samflex

        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)

        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:

        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.

        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

        M Offline
        M Offline
        Mycroft Holmes
        wrote on last edited by
        #3

        As Jorgen has said, this is the wrong tool for the job, the database serves up the data, your UI/Report/Consumer of the data formats that data according you your requirements. So do your loop/delete in the client, not the database.

        Never underestimate the power of human stupidity RAH

        S 1 Reply Last reply
        0
        • M Mycroft Holmes

          As Jorgen has said, this is the wrong tool for the job, the database serves up the data, your UI/Report/Consumer of the data formats that data according you your requirements. So do your loop/delete in the client, not the database.

          Never underestimate the power of human stupidity RAH

          S Offline
          S Offline
          samflex
          wrote on last edited by
          #4

          I was not needing for app. The code below is what I used for my app. It is doing the grouping by electionName but I can't figure out how to count the total count for each candidate. Can you guys add that bit? I just to get the total scores for each candidateName

          WITH
          ctePreAgg AS
          (
          SELECT (ElectionName + ' - ' + CAST(ClosingDate AS VARCHAR(12))) electionName, CASE WHEN Position='Member' THEN '' ELSE Position END As Position, CASE WHEN c.CurrentOfficeHolder='Incumbent' THEN CandidateName + '('+ c.CurrentOfficeHolder + ')' ELSE CandidateName END As CandidateName , c.PositionId,COUNT(*) TotalVotes
          FROM Candidates c
          JOIN Positions p ON c.PositionId = p.PositionId
          JOIN Elections e on c.ElectionId = e.ElectionId
          WHERE c.ElectionId IN (1,2,3)
          GROUP BY Position, CandidateId, CandidateName,c.PositionId,CurrentOfficeHolder,AnswerType,ElectionName, ClosingDate
          )

          SELECT [OrgName] = CASE WHEN GROUPING(mh.PositionId) = 0 THEN MAX(mh.Position) ELSE mh.ElectionName END
          ,Names = CASE WHEN GROUPING(mh.PositionId) = 0 THEN MAX(mh.CandidateName) ELSE '' END
          ,PositionId = CASE WHEN GROUPING(mh.PositionId) = 0 THEN mh.PositionId ELSE '' END
          ,TotalCount = CASE WHEN GROUPING(mh.PositionId) = 0 THEN COUNT(*) ELSE '' END
          FROM ctePreAgg mh
          GROUP BY ElectionName,PositionId WITH ROLLUP
          HAVING GROUPING(mh.ElectionName) = 0
          ORDER BY mh.ElectionName, GROUPING(mh.PositionId) DESC, mh.PositionID;

          M 1 Reply Last reply
          0
          • S samflex

            I was not needing for app. The code below is what I used for my app. It is doing the grouping by electionName but I can't figure out how to count the total count for each candidate. Can you guys add that bit? I just to get the total scores for each candidateName

            WITH
            ctePreAgg AS
            (
            SELECT (ElectionName + ' - ' + CAST(ClosingDate AS VARCHAR(12))) electionName, CASE WHEN Position='Member' THEN '' ELSE Position END As Position, CASE WHEN c.CurrentOfficeHolder='Incumbent' THEN CandidateName + '('+ c.CurrentOfficeHolder + ')' ELSE CandidateName END As CandidateName , c.PositionId,COUNT(*) TotalVotes
            FROM Candidates c
            JOIN Positions p ON c.PositionId = p.PositionId
            JOIN Elections e on c.ElectionId = e.ElectionId
            WHERE c.ElectionId IN (1,2,3)
            GROUP BY Position, CandidateId, CandidateName,c.PositionId,CurrentOfficeHolder,AnswerType,ElectionName, ClosingDate
            )

            SELECT [OrgName] = CASE WHEN GROUPING(mh.PositionId) = 0 THEN MAX(mh.Position) ELSE mh.ElectionName END
            ,Names = CASE WHEN GROUPING(mh.PositionId) = 0 THEN MAX(mh.CandidateName) ELSE '' END
            ,PositionId = CASE WHEN GROUPING(mh.PositionId) = 0 THEN mh.PositionId ELSE '' END
            ,TotalCount = CASE WHEN GROUPING(mh.PositionId) = 0 THEN COUNT(*) ELSE '' END
            FROM ctePreAgg mh
            GROUP BY ElectionName,PositionId WITH ROLLUP
            HAVING GROUPING(mh.ElectionName) = 0
            ORDER BY mh.ElectionName, GROUPING(mh.PositionId) DESC, mh.PositionID;

            M Offline
            M Offline
            Mycroft Holmes
            wrote on last edited by
            #5

            Well you are doing exactly what we advised you not to do and you want suggestion on how to improve doing that. Getting the total/count per group IS a database thing, formatting the text is NOT. Personally I would use ROW_NUMBER and PARTITION OVER but not with a CTE.

            Never underestimate the power of human stupidity RAH

            S 1 Reply Last reply
            0
            • M Mycroft Holmes

              Well you are doing exactly what we advised you not to do and you want suggestion on how to improve doing that. Getting the total/count per group IS a database thing, formatting the text is NOT. Personally I would use ROW_NUMBER and PARTITION OVER but not with a CTE.

              Never underestimate the power of human stupidity RAH

              S Offline
              S Offline
              samflex
              wrote on last edited by
              #6

              Ok, sorry for my stupidity but I thought this is a database forum. If it is, I am only trying to figure out how to get Total Count for each candidate, not formatting request.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups