SELECT location,
MAX(DECODE(candidate, 'x', VALUE)) x,
MAX(DECODE(candidate, 'y', VALUE)) y,
MAX(DECODE(candidate, 'z', VALUE)) z
FROM data_for_report
GROUP BY location
Main Topics
Browse All TopicsI have a table which contains rows of data that look like this:
Row Number, Location ID, Location Name, Candidate ID, Candidate Name, Votes
The select I have now is:
select * from data_for_report order by cand_id, row_num
The table has data for a fixed number of locations, and the number of votes each candidate has for that location.
I need to produce a report which produces a line for each location with the number of votes each candidate received, and this I can feed into the report.
For example, the data might look like this (3 locations, a,b, and c) and 3 candidates (x,y and z) (I'll skipe the id's in the example, just use the names)
1 a x n1
2 b x n2
3 c x n3
4 a y n4
5 b y n5
6 c y n6
7 a z n7
8 b z n8
9 c z n9
And the report should look like this in the end
x y z
a n1 n4 n7
b n2 n5 n8
c n3 n6 n9
This might look like homework, it's not - I'm an embedded developer who has been given some database work that he's not trained or qualified for. The code in in Delphi 5, and the report is using Quickreports 3.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
if "cand_id" is your x,y, z you can order by that, since it's now data in columns within a single row.
how would you expect row_num (I'm guessing that's your 1-9 first column, you didn't specify what anything was above) to be sorted in the output?
a has row_num 1, 4 and 7 in the input. how do you expect that reflected in your output?
Answers coming to fast to comment on them...
Row ID is the original order of the rows, and they should interleave.
Row Num | Place | Candidate | Votes
1 a x n1
...
In the report, the row numbers, while not printed, should remain per candidate in their original order. As you can see, the places go in row order a,b,c, a,b,c,a,b,c and the report should be according to place in the end in the incoming order of a,b,c, but only once each.
Business Accounts
Answer for Membership
by: mrjoltcolaPosted on 2009-04-27 at 07:50:44ID: 24242327
select location_name, candidate_name, sum(votes)
from data_for_report
group by location_name, candidate_name