473,382 Members | 1,147 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Transpose Query Output

rpnew
188 100+
Hi All,

It feels so good to be back on Bytes after almsot 2-3 years.. anyways here is my quesion. I would like to transpose my query output.

here are the details..


*** Tables
GDCA
gdcid,gdcid,gdaid,age


GDA
gdaid,name


*** Queries

SELECT
gda.id,age
FROM
gdca
JOIN gda ON (gda.id = gdca.gdaid)
WHERE gdca.gdcid IN(3573,184)
ORDER BY gdca.id

*** Output

"id" "age"
"1" "0"
"1" "11"
"2" "0"
"2" "30"
"3" "0"
"3" "1"
"4" "0"
"4" "1"
"5" "0"
"5" "1"

*** I want it something like

1 2 3 4 5
0 0 0 0 0
11 30 0 0 0

I tried using IF() clause or even CONCAT and then SUBSTRING_INDEX, however not getting the required output.

Any pointer is appreciated.

Thanks and Regards,
RP
Nov 15 '11 #1
13 4035
rpnew
188 100+
Any suggestions for above.. i need to complete one report?
Nov 15 '11 #2
Rabbit
12,516 Expert Mod 8TB
I have no idea what you're trying to do. I don't see how you get to your results from your sample data.
Nov 15 '11 #3
rpnew
188 100+
Okay.. let me explain a bit more..

I suppose output is not much difficult to understand.. now my requirement is to change column into rows..

right now query output is

id" "age"
"1" "0"
"1" "11"
"2" "0"
"2" "30"
"3" "0"
"3" "1"
"4" "0"
"4" "1"
"5" "0"
"5" "1"

Two columns one is for ID and another is for AGE.. now my requirement is something like this...
ID 1 2 3 4 5
AGE 0 0 0 0 0
AGE 11 30 0 0 0

In normal output if you see there are two rows for each ID. e.g. ID 1 has two values 0 and 11. In my requirement ID(s) should be column and related AGE should be rows, hence for ID 1 there would be two rows one containing value 0 and the other 11

@Rabbit

Thanks for the reply.. I think now the requirement should be clear..

Regards,
RP
Nov 15 '11 #4
Rabbit
12,516 Expert Mod 8TB
For 3, 4, and 5, I still don't see why you have 0/0 in your output as opposed to 0/1.
Nov 15 '11 #5
rpnew
188 100+
Hi Rabbit,

Yeah.. sorry my mistake.... you are right.. Required ouput is something like this..

ID 1 2 3 4 5
AGE 0 0 0 0 0
AGE 11 30 1 1 1

Regards,
RP
Nov 15 '11 #6
Rabbit
12,516 Expert Mod 8TB
I don't know how you're grouping the records by row. I don't see anything in the data that indicates all the 0 records should be grouped together.
Nov 15 '11 #7
rpnew
188 100+
Hi Rabbit,

let me put my ouput in another way...


"id" "age"
----------
"1" "0"
"2" "0"
"3" "0"
"4" "0"
"5" "0"
"1" "11"
"2" "30"
"3" "1"
"4" "1"
"5" "1"

And requirement is ...

ID 1 | 2 | 3 | 4 | 5
--------------------------
AGE | 0 | 0 | 0 | 0 | 0
--------------------------
AGE | 11 | 30 | 1 | 1 | 1

I have tried searching on google.. I got some suggestions for useing GROUP_CONCAT and STRING_INDEX. However did not get the exact output..
Nov 15 '11 #8
Rabbit
12,516 Expert Mod 8TB
I still don't see what indicates that all the 0 records should be grouped together.
Nov 15 '11 #9
rpnew
188 100+
Hi,

Let say.. first entries for each IDs should be first row... second entries for each IDs should be second row.. third entries of each IDs should be third row.. and so on..

Regards,
RP
Nov 15 '11 #10
Rabbit
12,516 Expert Mod 8TB
MySQL has no way to distinguish the order in which a row was entered. It has to be user defined and I don't see a field in your post that defines the order.
Nov 15 '11 #11
rpnew
188 100+
Hi Rabbit,

Agreed.. that there is now way to check the order in which the rows were entered but thats not the issue. We can for example sort the output by ID and use the output.

BTW, I got the output which i wanted by using GROUP_CONCAT() and then SUBSTRING_INDEX() however just wanted to know.. how much will this affect performance of qeury. Although, i dont see much issue there but just to get some idea..

Regards,
RP
Nov 16 '11 #12
Rabbit
12,516 Expert Mod 8TB
GROUP_CONCAT and SUBSTRING_INDEX is fine for small amounts of records (a few thousand) but if you have a lot of records (hundreds of thousand), then you may want to think about using a cursor instead. But then again, fast and slow is different for different people.
Nov 16 '11 #13
rpnew
188 100+
Okay.. but so far it looks okay.. The report will go into testing now so will see once it is tested.. and thanks for the reply once again..

Regards,
RP
Nov 16 '11 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Todd | last post by:
Hello, I'm am creating a CSV text import file for another application from my Access database. The other application requires a leading and following space surrounding a hyphen in the field...
0
by: JM | last post by:
How can I store following query output to multi dimensional array or something better? I will be using the output for print queue information reference later in program. ObjectQuery...
0
by: PW | last post by:
I am trying to create a crosstab report in ASP. I'm using ASP/VBScript and Access database. I used Access to create the SQL query, then copied the SQL and embedded it in my ASP like this ... ...
8
by: GeorgeSmiley | last post by:
Does anyone know of a way, via VBA, to set the screen position of query results to a particular top, left position? I've glanced at API techniques but cannot find exactly what will do the trick....
1
by: Phil | last post by:
Is it possible to swap rows and columns in select query output so that each record's data is displayed in a column? I want to collect data each day and display it in a query with each day's date...
1
by: katieking | last post by:
I would like a field in a query to contain the name of the query that is producing the data, in order to maintain transparency of data flows when the data is copied into Excel. Is there any way of...
1
by: RamyaKarri | last post by:
i get the output of the query as Jan 0.1 Feb 1 MAR 2 .. I Want to view data in the following format Jan Feb Mar
2
by: erbrose | last post by:
Hello All! Hoping some folks could help me optimize and or choose the best route to do this process. First off, here is what I am trying to achieve. I have a (fairly large) table of ~34million rows...
0
by: dolphin123 | last post by:
Hi, How can i redirect the output of an sql select query to a tab delimited text file and to an excel file using perl script. I use , system("sqlplus / \@$my_folder/qry.sql"); to call...
45
by: dizzydangler | last post by:
Hi, I'm new to access (2007, running on XP), but I've gotten some great help from this site on my project so far. However, the more I know, the more complex the problems seem to become.... I'm...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.