473,407 Members | 2,546 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,407 software developers and data experts.

Problem with joins in SQL.

Hi,

I'm new to SQL trying to use a base that was set up by another firm.

I can't get it to do what I want and I don't know if it's due to my
ignorance or bad table design ...

I have three tables

Sales

containing ProdID and Amounts

Products

containing ProdID and ProdTypeID and
ProdSubTypeID

Product-types

containing ProdTypeID and ProdTypeIDLabel and

ProdSubTypeID and ProdSubTypeIDLabel

I have to do some stats in Excel or in Crystal

totalling Amounts

grouped by ProductTypeLabel and

grouped by ProductSubTypeLabel.

Here's what I can do...

------------------------------

Using ProdTypeID, I can join Product-Types to Products,

and

using ProdID, I can join Products to Sales

and get a sum of amounts grouped by ProductTypeLabel. OK.

Also,

Using ProdTypeID, I can join Product-Types to Products,

and

using ProdID, I can join Products to Sales

and get a sum of amounts grouped by ProductSubTypeLabel. OK.

Here's what I can't do...

------------------------------

Get both (i.e. ProductTypeLabel and ProductSubTypeLabel) in the same report
and the correct amounts.

I have found a solution but it involves modifying the tables.

I create a new field with the Type and SubType concatenated in Products and
also in Product-Types (ProductTypeGlobal) and then join on that.

So,

Using ProductTypeGlobal, I can join Product-Types to Products,

and

using ProdID, I can join Products to Sales

and get a sum of amounts

grouped by ProductTypeLabel

AND

grouped by ProductSubTypeLabel

OK.

Is there some way to create the correct joins in SQL without creating the
new field?

Thanks

SQL Newbie.




Jun 1 '08 #1
2 1393
It is unclear for your description what are the correct relations between
entities in your tables, but here is something to try:

SELECT T.ProdTypeIDLabel,
T.ProdSubTypeIDLabel,
SUM(Amount) AS amount
FROM ProductTypes AS T
JOIN Products AS P
ON T.ProdTypeID = P.ProdTypeID
AND T.ProdSubTypeID = P.ProdSubTypeID
JOIN Sales AS S
ON S.ProdID = P.ProdID
GROUP BY T.ProdTypeIDLabel, T.ProdSubTypeIDLabel;

HTH,

Plamen Ratchev
http://www.SQLStudio.com

Jun 1 '08 #2
Thanks,
will give this a blast on Monday.

Sql N.

"Plamen Ratchev" <Pl****@SQLStudio.coma écrit dans le message de news:
YY******************************@speakeasy.net...
It is unclear for your description what are the correct relations between
entities in your tables, but here is something to try:

SELECT T.ProdTypeIDLabel,
T.ProdSubTypeIDLabel,
SUM(Amount) AS amount
FROM ProductTypes AS T
JOIN Products AS P
ON T.ProdTypeID = P.ProdTypeID
AND T.ProdSubTypeID = P.ProdSubTypeID
JOIN Sales AS S
ON S.ProdID = P.ProdID
GROUP BY T.ProdTypeIDLabel, T.ProdSubTypeIDLabel;

HTH,

Plamen Ratchev
http://www.SQLStudio.com

Jun 2 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Marek Kotowski | last post by:
In MySQL online documentation there are some examples with multi-tables left joins. But all of them are like this (taken from the documentation): SELECT ... FROM table1 LEFT JOIN table2 on...
1
by: Prem | last post by:
Hi All Database Gurus, I am trying to write code which will produce all the possible valid queries, given tables and join information for tables. Right now i am just trying to construct all the...
2
by: Keith | last post by:
I am having a problem creating a many-to-many-to-many type relationship. It works fine, but when I create a view to query it and test it, it does not generate the results I expected. Below...
8
by: Matt | last post by:
Hello I have to tables ar and arb, ar holds articles and a swedish description, arb holds descriptions in other languages. I want to retreive all articles that match a criteria from ar and...
3
by: Il Khan | last post by:
Hi, I have an asp application running on a dozen of winXP machines configured as kiosks. This application stores its data on a MS access db. I have the need to make some reports extracting data...
4
by: michaelnewport | last post by:
Greetings, I like to write my inner joins as below, but someone at work tells me its not as 'performant' as using the 'inner join' statement. Is this true ? Is there a better way to write it...
1
by: imranpariyani | last post by:
Hi i have a severe performance problem with one of my views which has 6 to 8 joins .. any help will be appreciated.. the view is: CREATE OR REPLACE VIEW thsn.trade_view AS SELECT...
22
by: MP | last post by:
vb6,ado,mdb,win2k i pass the sql string to the .Execute method on the open connection to Table_Name(const) db table fwiw (the connection opened via class wrapper:) msConnString = "Data Source="...
1
by: Ana RM | last post by:
Mark.Powell@eds.com (Mark D Powell) wrote in message news:<2687bb95.0308010642.1fc4ff1f@posting.google.com>... Hi Mark, Thanks por answer me. I do not think it is important thw warehouse...
36
by: TC | last post by:
I've used Access for many years. Several times, I've encountered a bug which I refer to as the "Vanishing Joins" bug. When it happens, joins vanish randomly from queries. More specifically, all...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.