Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

The Database Forum


You are currently viewing our The Database Forum as a guest. Please register to participate.
Login



Reply
Help with SQL Query needed
Old 03-08-2005, 10:20 AM Help with SQL Query needed
RikComery's Avatar
Average Talker

Posts: 18
Location: Essex, United Kingdom
Trades: 0
Could someone please help with my sql query. I have been at it for two days now, and am now running out of time. I have two tables as below.

Table 1 (data)
ContentID----Quantity
---------------------
prd_00001------1
prd_00002------2
prd_00003------3
prd_00001------5
prd_00002------6
prd_00003------7

Table 2 (IDs)
ContentID--------Name--------Language
----------------------------------------
prd_00001-------Product 1--------UK
prd_00002-------Product 2--------FR
prd_00003-------Product 3--------UK

What i am trying to achieve is a table showing unique ContentIDs, the name for that Content ID, it's Language and a sum of Quantity.

i.e.

ContentID----Name---------Language---Quantity
------------------------------------------------
prd_00001----Product 1--------UK----------6
prd_00002----Product 2--------FR----------8
prd_00003----Product 3--------UK---------10

The SQL query i have finally given up on is
SELECT d.ContentID, c.Name, c.Language, SUM(Quantity) From data d, IDs c WHERE c.ContentID = d.ContentID GROUP BY d.ContentID

this is throwing errors about values not being part of an aggregated function. I have tried to research the use of aggregated functions, but being on a tight deadline, i have not been able to digest it properly. I now understand what aggregate functions are, but cannot seem to work out what to use when querying more than one field. To be honest, at this stage it is going straight over my head. I fully intend to research it more, but in the mean time, is anyone able to provide me with the correct solution. Also, is it possible to order on Quantity Desc. I tried with a different query, but it doesn't seem to like it when the results are grouped
RikComery is offline
Reply With Quote
View Public Profile Visit RikComery's homepage!
 
 
Register now for full access!
Old 03-08-2005, 10:30 AM
Anacrusis's Avatar
Defies a Status

Posts: 2,099
Name: Adam
Location: Colchester CT
Trades: 0
you need to group by ALL columns that are not part of an aggregate
Code:
SELECT d.ContentID, c.Name, c.Language, SUM(Quantity) 
From data d, IDs c 
WHERE c.ContentID = d.ContentID 
GROUP BY  d.ContentID, c.Name, c.Language
Anacrusis is offline
Reply With Quote
View Public Profile
 
Old 03-08-2005, 11:46 AM
RikComery's Avatar
Average Talker

Posts: 18
Location: Essex, United Kingdom
Trades: 0
Can't believe it was that simple. I spent ages trying to work this out. Still i guess that is often the way.

Thanks for your help
RikComery is offline
Reply With Quote
View Public Profile Visit RikComery's homepage!
 
Old 03-08-2005, 11:56 AM
RikComery's Avatar
Average Talker

Posts: 18
Location: Essex, United Kingdom
Trades: 0
Actually, what about the ordering? I want to order on the Quantity column in Descending order. I have tried the following, but this just changed the value of my quantities, and even then didn't place in the correct order

SELECT d.ContentID, c.Name, c.Language, SUM(Quantity) AS Quant
From data d, IDs c
WHERE c.ContentID = d.ContentID
GROUP BY d.ContentID, c.Name, c.Language
ORDER BY Quant Desc
RikComery is offline
Reply With Quote
View Public Profile Visit RikComery's homepage!
 
Old 03-08-2005, 12:21 PM
Anacrusis's Avatar
Defies a Status

Posts: 2,099
Name: Adam
Location: Colchester CT
Trades: 0
Try this:
Code:
SELECT d.ContentID, c.Name, c.Language, SUM(Quantity) 
FROM data d, IDs c 
WHERE c.ContentID = d.ContentID 
GROUP BY  d.ContentID, c.Name, c.Language
ORDER BY SUM(Quantity) DESC
Anacrusis is offline
Reply With Quote
View Public Profile
 
Old 03-10-2005, 05:03 AM
RikComery's Avatar
Average Talker

Posts: 18
Location: Essex, United Kingdom
Trades: 0
That's the one. Thanks for all of your help.
RikComery is offline
Reply With Quote
View Public Profile Visit RikComery's homepage!
 
Reply     « Reply to Help with SQL Query needed
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.47087 seconds with 12 queries