473,387 Members | 1,493 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,387 software developers and data experts.

Problem with GROUP, UNION and COUNT...

I am writing a query to create a tree menu, it pulls from a table of music "tracks". In this database there are four "sub_genre" fields for each track, and I need to create a list of all used sub_genres and how many tracks are in each.

The list will look like this:
Expand|Select|Wrap|Line Numbers
  1. Acid Jazz (50)
  2. Big Beat (1)
  3. Breakbeat (75)
  4. Chill (27)
  5. Dance (12)
  6. Drum & Bass/Jungle (12)
  7. Hard Rock/Metal (1)
  8. House (2)
  9. ...
  10.  
I'm close to having it done, but it is not grouping them properly. Here is my query:
Expand|Select|Wrap|Line Numbers
  1. SELECT genre, 1_subgenre as sub, COUNT(title) FROM `tracks` WHERE  `genre` = 'Urban/Hip' GROUP BY `1_subgenre` 
  2.  
  3. UNION 
  4.  
  5. SELECT genre, 2_subgenre as sub, COUNT(title) FROM `tracks` WHERE `genre` = 'Urban/Hip' GROUP BY `2_subgenre` 
  6.  
  7. UNION 
  8.  
  9. SELECT genre, 3_subgenre as sub, COUNT(title) FROM `tracks` WHERE `genre` = 'Urban/Hip' GROUP BY `3_subgenre` 
  10.  
  11. UNION 
  12.  
  13. SELECT genre, 4_subgenre as sub, COUNT(title) FROM `tracks` WHERE `genre` = 'Urban/Hip' GROUP BY `4_subgenre`
  14.  
ANY help would be greatly appreciated!!!

-Ben
Jul 19 '08 #1
2 2718
bump... sorry, this is really important.
Jul 22 '08 #2
Atli
5,058 Expert 4TB
Hi.

I'm not getting the whole sub_genre thing.
Could you post the table structure?

I was thinking that you had added a genre column for each track, and then a sub_genre column to narrow it further. But that doesn't really make sense with 4 sub_genre columns.

Unless it is mean to be a tree-view, like, say:
Expand|Select|Wrap|Line Numbers
  1. Genre
  2. |->Sub_Genre1
  3. |  |->Sub_Genre2
  4. |  |  |->Sub_Genre3
  5. |  |  |  |->Sub_Genre4
  6.  
In which case your approach is very limited.
That would require a separate table for each genre, where each row is linked to another row.

Am I even close?
Jul 23 '08 #3

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

Similar topics

6
by: Stephen Miller | last post by:
Firstly, sorry for the long post, but I've included a fair bit of sample data. Im doing a comparision of 10yr Bond prices and CPI adjustments, with an 18 week moving average of the CPI. I'm...
4
by: Jaidev Paruchuri | last post by:
I have a table(work_order) with time as varchar(5). The values in table looks like this work_order_id rtim 1 08:15 2 08:45 3 10:13 4 ...
6
by: Bill | last post by:
I've created this: SELECT c.ProjectID, Count(c.ID) as 'Registrants', Count(dt.Hits) as 'Submissions' FROM CME_TBL c JOIN (SELECT ProjectID, Count(*) as Hits FROM CME_TBL
3
by: Abhi | last post by:
Hi! I am wondering if this query is possible somehow: I have a table with many fields that all can have a value from 1 to 5. if I wanna see the count of each value from one field, then this...
1
by: Dámaso Velázquez Álvarez | last post by:
I have: SELECT x, count(x) FROM table1 WHERE ... UNION ALL SELECT x, count(x) FROM table2
4
by: google | last post by:
Hi Hope someone can help me with this - have been staring at this problem all day, and with the cold I've got, just don't seem to be able to figure it out! I've got two tables, here with some...
15
by: edouard.spooner | last post by:
Hi, I have a tricky SQL query problem that I'm having probs with. I have a table which resembles something like this Date | Price1 | Price2 | Price3 01 Jan 2006 | 100 | 100 | 100 02 Jan...
1
by: Arayeshi | last post by:
Hi all, I have a two tables with same structure in DB2 Ver 6 on OS/390 1- CurrPay(CustomerId, BnkId, BranchCode, PayDate, Amount) for Current Period Customers Payments 2- PayHist(CustomerId,...
0
by: RSN | last post by:
I hav a query that uses a union of 16 tables to get some counts and i need to get the end result as a Group by. This is inside a sub-query that has the count(*) ans the attribute for the group-by in...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.