MySQL Forums
Forum List  »  Newbie

Use a SQL query to create column names for another query
Posted by: Mark Warburton
Date: August 17, 2015 08:31AM

Hi,

If I have user created content in one table, BirdSpecies:
ID BirdName
1  Hawk
2  Sparrow
3  Duck
4  Goose
5  LBJ

and I have collected data in another table BirdData:
ID  LocationID  BirdID    Numbirds
1   2           3         1
2   3           2         5
3   2           1         2
4   7           4         1
5   9           4         4
6   11          3         5
7   5           5         10

I want to generate a view for the totals as follows:
Hawk  Sparrow  Duck  Goose  LBJ
2     5        6     5      10

I need to use the BirdName data to create the column names in the view because I want to use this view data in subsequent queries. Ordinarily, I would use SELECT ... AS ..., but the column names are dynamic so I cannot hard code this in this example.

Is there a native SQL statement that will allow this output or does it have to be done elsewhere in a loop?

Thanks in advance.

Regards
Mark

Options: ReplyQuote


Subject
Written By
Posted
Use a SQL query to create column names for another query
August 17, 2015 08:31AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.