Domanda

I have a complete database system that is primarily used with it's own frontend software. This year our character generator only accepts XML data feeds, and I have found that I can query the database and create an XML file.

The database is spread across about a dozen tables. I can get all the information I want to air from a 2 table query, but am afraid I will need 3.

My first few queries bring up a RACE, each race has 2 candidates, but when I run the query it shows a record for each candidate (times 2). So I have 2 rows titled RACE NUMBER 101. Each row has BOTH candidates in it, their votes (which are related by their unique candidate #, and is in another table), precints reporting, vote percentage..etc.

I am trying to get one race within one ROW. One row, that will show the race title, both candidates, their votes, their vote percents and the precincts reporting.

The query I have written is:

SELECT * 
FROM dbo.[RACE] 
INNER JOIN dbo.[RACE CANDIDATES] ON [RACE].[race number] = [RACE CANDIDATES].[race number] 

This gives me a complete overview of all fields I am able to query, but they are showing 2 rows from the same RACE#. Also, it shows the candidate's unique # along with their # of votes. But again, these are all in 2 rows. The only 2 differences I can see in the rows queried is the candidate's unique ID and their vote number.

I would like to get both candidate's names, number of votes and voter percentage in ONE single row, so I can make it an XML PATH, in a single block. One race, all race elements under one XML category.

It is driving me nuts. It is very hard to ask a question like this, especially on a subject like this. I have the database saved off as a *.bak file that I have been able to load on 3 different machines now, so I have that available if anyone would like to take a look. There is no confidential data within it, so I have no problem offering it up.

I have about 30 hours SOLID into this. I see hope, but then I end up back where I started.

Usually, I would just send the data line by line to the character generator serially, but our graphics system has changed since the last primary election.

If anyone could give me an idea of what the right questions to ask would be I would appreciate it. This of course is for work and the whole weight of this data feed is on my shoulders, I am reaching out to where ever I can with a broken limb.

Thanks for reading this.

È stato utile?

Soluzione

If I understand correctly, you are getting one row for each candidate in the race. You need to pivot the data.

Here is one way, that puts the one with the most votes first:

SELECT r.*, Winner, WinningVotes, Loser, LosingVotes
FROM dbo.[RACE] r inner join
     (select rc.[race number],
             max(case when seqnum = 1 then CandidateNumber end) as Winner,
             max(case when seqnum = 1 then Votes end) as WinningVotes,
             max(case when seqnum = 2 then CandidateNumber end) as Loser,
             max(case when seqnum = 2 then Votes end) as LosingVotes
      from (select rc.*,
                   row_number() over (partition by rc.[race number] order by votes desc) as seqnum
            from dbo.[RACE CANDIDATES] rc
           ) rc
      group by rc.[race number]
     ) rc
     on r.[race number] = rc.[race number] ;

The function row_number() is a window function. It will assign the value of 1 to the candidate with the most votes and 2 to the second candidate.

Altri suggerimenti

If all the fields of your rows are the same (except id and number of votes), you can group them by, in example, the raceName (or another field that you want). Something like this, but if you don't provide the schema I can't help you more:

SELECT r.raceName, rc.*
  FROM race r
  INNER JOIN race_candidates rc
          ON race.race_number= race_candidates.race_number
GROUP BY r.raceName
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top