473,503 Members | 1,647 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

test your intermediate SQL (joins) by fixing my problem !

I have rather complicated join I can't figure out (been mostly using
trial-and-error method...). I'll just explain with words first: (table
layout below)

Users are in categories. Depending on what category they're in, they
have a different set of functions that are *possible. There's an xref
table that defines which functions they actually have.

So e.g. user 1 is in category 4, and thus *might possess functions 6,7,
and 8. In fact she only possesses 7, so there's an xref record:
user_id | function_id
1 | 7

My query needs to loop over categories, and for each user in a category
tell me which functions she qualifies for but does not possess, e.g:
user_id | function_id | result
1 | 6 | NULL
1 | 7 | 1 (or "sure thing" or whatever)
1 | 8 | NULL

i.e. if there's no record in the xref table with user_id 1 and
function_id 6, show NULL.

One thought I had would be just to have an "available" field in the xref
table, with values 'y' or 'n'. But in this case every user would need
(in this example) 3 records in the xref table, and that struck me as
inefficient.

users
------
user_id
name
category_id

categories
----------
user_id
category_name

functions
---------
function_id
category_id
function_name

xref
-----
user_id
function_id
Sep 15 '06 #1
8 3193

Matt C wrote:
I have rather complicated join I can't figure out (been mostly using
trial-and-error method...). I'll just explain with words first: (table
layout below)

Users are in categories. Depending on what category they're in, they
have a different set of functions that are *possible. There's an xref
table that defines which functions they actually have.

So e.g. user 1 is in category 4, and thus *might possess functions 6,7,
and 8. In fact she only possesses 7, so there's an xref record:
user_id | function_id
1 | 7

My query needs to loop over categories, and for each user in a category
tell me which functions she qualifies for but does not possess, e.g:
user_id | function_id | result
1 | 6 | NULL
1 | 7 | 1 (or "sure thing" or whatever)
1 | 8 | NULL

i.e. if there's no record in the xref table with user_id 1 and
function_id 6, show NULL.

One thought I had would be just to have an "available" field in the xref
table, with values 'y' or 'n'. But in this case every user would need
(in this example) 3 records in the xref table, and that struck me as
inefficient.

users
------
user_id
name
category_id

categories
----------
user_id
category_name

functions
---------
function_id
category_id
function_name

xref
-----
user_id
function_id
can functions belong to more than one category? the functions table
suggests that they can't but I just wanted to check

Sep 15 '06 #2
strawberry wrote:
Matt C wrote:
>I have rather complicated join I can't figure out (been mostly using
trial-and-error method...). I'll just explain with words first: (table
layout below)

Users are in categories. Depending on what category they're in, they
have a different set of functions that are *possible. There's an xref
table that defines which functions they actually have.

So e.g. user 1 is in category 4, and thus *might possess functions 6,7,
and 8. In fact she only possesses 7, so there's an xref record:
user_id | function_id
1 | 7

My query needs to loop over categories, and for each user in a category
tell me which functions she qualifies for but does not possess, e.g:
user_id | function_id | result
1 | 6 | NULL
1 | 7 | 1 (or "sure thing" or whatever)
1 | 8 | NULL

i.e. if there's no record in the xref table with user_id 1 and
function_id 6, show NULL.

One thought I had would be just to have an "available" field in the xref
table, with values 'y' or 'n'. But in this case every user would need
(in this example) 3 records in the xref table, and that struck me as
inefficient.

users
------
user_id
name
category_id

categories
----------
user_id
category_name

functions
---------
function_id
category_id
function_name

xref
-----
user_id
function_id

can functions belong to more than one category? the functions table
suggests that they can't but I just wanted to check
They cannot. The only many-to-many rel. is users_functions
Sep 15 '06 #3

Matt C wrote:
strawberry wrote:
Matt C wrote:
I have rather complicated join I can't figure out (been mostly using
trial-and-error method...). I'll just explain with words first: (table
layout below)

Users are in categories. Depending on what category they're in, they
have a different set of functions that are *possible. There's an xref
table that defines which functions they actually have.

So e.g. user 1 is in category 4, and thus *might possess functions 6,7,
and 8. In fact she only possesses 7, so there's an xref record:
user_id | function_id
1 | 7

My query needs to loop over categories, and for each user in a category
tell me which functions she qualifies for but does not possess, e.g:
user_id | function_id | result
1 | 6 | NULL
1 | 7 | 1 (or "sure thing" or whatever)
1 | 8 | NULL

i.e. if there's no record in the xref table with user_id 1 and
function_id 6, show NULL.

One thought I had would be just to have an "available" field in the xref
table, with values 'y' or 'n'. But in this case every user would need
(in this example) 3 records in the xref table, and that struck me as
inefficient.

users
------
user_id
name
category_id

categories
----------
user_id
category_name

functions
---------
function_id
category_id
function_name

xref
-----
user_id
function_id
can functions belong to more than one category? the functions table
suggests that they can't but I just wanted to check

They cannot. The only many-to-many rel. is users_functions
How about:

SELECT *
FROM (
SELECT u.user_id, u.name, u.category_id, f.function, x.function_id
FROM users u
LEFT JOIN functions f ON f.category_id = u.category_id
LEFT JOIN xref x ON x.user_id = u.user_id
AND x.function_id = f.function_id
)t1
WHERE ISNULL( t1.function_id )

Sep 16 '06 #4
strawberry wrote:
Matt C wrote:
>strawberry wrote:
>>Matt C wrote:
I have rather complicated join I can't figure out (been mostly using
trial-and-error method...). I'll just explain with words first: (table
layout below)

Users are in categories. Depending on what category they're in, they
have a different set of functions that are *possible. There's an xref
table that defines which functions they actually have.

So e.g. user 1 is in category 4, and thus *might possess functions 6,7,
and 8. In fact she only possesses 7, so there's an xref record:
user_id | function_id
1 | 7

My query needs to loop over categories, and for each user in a category
tell me which functions she qualifies for but does not possess, e.g:
user_id | function_id | result
1 | 6 | NULL
1 | 7 | 1 (or "sure thing" or whatever)
1 | 8 | NULL

i.e. if there's no record in the xref table with user_id 1 and
function_id 6, show NULL.

One thought I had would be just to have an "available" field in the xref
table, with values 'y' or 'n'. But in this case every user would need
(in this example) 3 records in the xref table, and that struck me as
inefficient.

users
------
user_id
name
category_id

categories
----------
user_id
category_name

functions
---------
function_id
category_id
function_name

xref
-----
user_id
function_id
can functions belong to more than one category? the functions table
suggests that they can't but I just wanted to check
They cannot. The only many-to-many rel. is users_functions

How about:

SELECT *
FROM (
SELECT u.user_id, u.name, u.category_id, f.function, x.function_id
FROM users u
LEFT JOIN functions f ON f.category_id = u.category_id
LEFT JOIN xref x ON x.user_id = u.user_id
AND x.function_id = f.function_id
)t1
WHERE ISNULL( t1.function_id )
Thanks, I'll let you know in a bit.
Sep 16 '06 #5
strawberry wrote:
Matt C wrote:
>strawberry wrote:
>>Matt C wrote:
I have rather complicated join I can't figure out (been mostly using
trial-and-error method...). I'll just explain with words first: (table
layout below)

Users are in categories. Depending on what category they're in, they
have a different set of functions that are *possible. There's an xref
table that defines which functions they actually have.

So e.g. user 1 is in category 4, and thus *might possess functions 6,7,
and 8. In fact she only possesses 7, so there's an xref record:
user_id | function_id
1 | 7

My query needs to loop over categories, and for each user in a category
tell me which functions she qualifies for but does not possess, e.g:
user_id | function_id | result
1 | 6 | NULL
1 | 7 | 1 (or "sure thing" or whatever)
1 | 8 | NULL

i.e. if there's no record in the xref table with user_id 1 and
function_id 6, show NULL.

One thought I had would be just to have an "available" field in the xref
table, with values 'y' or 'n'. But in this case every user would need
(in this example) 3 records in the xref table, and that struck me as
inefficient.

users
------
user_id
name
category_id

categories
----------
user_id
category_name

functions
---------
function_id
category_id
function_name

xref
-----
user_id
function_id
can functions belong to more than one category? the functions table
suggests that they can't but I just wanted to check
They cannot. The only many-to-many rel. is users_functions

How about:

SELECT *
FROM (
SELECT u.user_id, u.name, u.category_id, f.function, x.function_id
FROM users u
LEFT JOIN functions f ON f.category_id = u.category_id
LEFT JOIN xref x ON x.user_id = u.user_id
AND x.function_id = f.function_id
)t1
WHERE ISNULL( t1.function_id )
Thanks. You got a wishlist somewhere?

I had to change
WHERE ISNULL( t1.function_id )
to simple
ORDER by user_id

to get what I wanted, which is a sorted set of all matching functions,
with NULLs when "unavailable". But joins are perfect.
Sep 17 '06 #6

Matt C wrote:
strawberry wrote:
Matt C wrote:
strawberry wrote:
Matt C wrote:
I have rather complicated join I can't figure out (been mostly using
trial-and-error method...). I'll just explain with words first: (table
layout below)

Users are in categories. Depending on what category they're in, they
have a different set of functions that are *possible. There's an xref
table that defines which functions they actually have.

So e.g. user 1 is in category 4, and thus *might possess functions 6,7,
and 8. In fact she only possesses 7, so there's an xref record:
user_id | function_id
1 | 7

My query needs to loop over categories, and for each user in a category
tell me which functions she qualifies for but does not possess, e.g:
user_id | function_id | result
1 | 6 | NULL
1 | 7 | 1 (or "sure thing" or whatever)
1 | 8 | NULL

i.e. if there's no record in the xref table with user_id 1 and
function_id 6, show NULL.

One thought I had would be just to have an "available" field in the xref
table, with values 'y' or 'n'. But in this case every user would need
(in this example) 3 records in the xref table, and that struck me as
inefficient.

users
------
user_id
name
category_id

categories
----------
user_id
category_name

functions
---------
function_id
category_id
function_name

xref
-----
user_id
function_id
can functions belong to more than one category? the functions table
suggests that they can't but I just wanted to check

They cannot. The only many-to-many rel. is users_functions
How about:

SELECT *
FROM (
SELECT u.user_id, u.name, u.category_id, f.function, x.function_id
FROM users u
LEFT JOIN functions f ON f.category_id = u.category_id
LEFT JOIN xref x ON x.user_id = u.user_id
AND x.function_id = f.function_id
)t1
WHERE ISNULL( t1.function_id )

Thanks. You got a wishlist somewhere?

I had to change
WHERE ISNULL( t1.function_id )
to simple
ORDER by user_id

to get what I wanted, which is a sorted set of all matching functions,
with NULLs when "unavailable". But joins are perfect.
ha - i just enjoy the challenge but, since you ask:

http://www.amazon.co.uk/gp/registry/1D7R707SQRFSD

anyway, glad if it helped

Sep 17 '06 #7
strawberry wrote:
Matt C wrote:
>strawberry wrote:
>>Matt C wrote:
strawberry wrote:
Matt C wrote:
>I have rather complicated join I can't figure out (been mostly using
>trial-and-error method...). I'll just explain with words first: (table
>layout below)
>>
>Users are in categories. Depending on what category they're in, they
>have a different set of functions that are *possible. There's an xref
>table that defines which functions they actually have.
>>
>So e.g. user 1 is in category 4, and thus *might possess functions 6,7,
>and 8. In fact she only possesses 7, so there's an xref record:
>user_id | function_id
>1 | 7
>>
>My query needs to loop over categories, and for each user in a category
>tell me which functions she qualifies for but does not possess, e.g:
>user_id | function_id | result
>1 | 6 | NULL
>1 | 7 | 1 (or "sure thing" or whatever)
>1 | 8 | NULL
>>
>i.e. if there's no record in the xref table with user_id 1 and
>function_id 6, show NULL.
>>
>One thought I had would be just to have an "available" field in the xref
>table, with values 'y' or 'n'. But in this case every user would need
>(in this example) 3 records in the xref table, and that struck me as
>inefficient.
>>
>users
>------
>user_id
>name
>category_id
>>
>categories
>----------
>user_id
>category_name
>>
>functions
>---------
>function_id
>category_id
>function_name
>>
>xref
>-----
>user_id
>function_id
can functions belong to more than one category? the functions table
suggests that they can't but I just wanted to check
>
They cannot. The only many-to-many rel. is users_functions
How about:

SELECT *
FROM (
SELECT u.user_id, u.name, u.category_id, f.function, x.function_id
FROM users u
LEFT JOIN functions f ON f.category_id = u.category_id
LEFT JOIN xref x ON x.user_id = u.user_id
AND x.function_id = f.function_id
)t1
WHERE ISNULL( t1.function_id )
Thanks. You got a wishlist somewhere?

I had to change
WHERE ISNULL( t1.function_id )
to simple
ORDER by user_id

to get what I wanted, which is a sorted set of all matching functions,
with NULLs when "unavailable". But joins are perfect.

ha - i just enjoy the challenge but, since you ask:

http://www.amazon.co.uk/gp/registry/1D7R707SQRFSD

anyway, glad if it helped
Ukrainian tractors? I couldn't resist that one. You're weird, dude.
Sep 18 '06 #8

Matt C wrote:
strawberry wrote:
Matt C wrote:
strawberry wrote:
Matt C wrote:
strawberry wrote:
Matt C wrote:
I have rather complicated join I can't figure out (been mostly using
trial-and-error method...). I'll just explain with words first: (table
layout below)
>
Users are in categories. Depending on what category they're in, they
have a different set of functions that are *possible. There's an xref
table that defines which functions they actually have.
>
So e.g. user 1 is in category 4, and thus *might possess functions 6,7,
and 8. In fact she only possesses 7, so there's an xref record:
user_id | function_id
1 | 7
>
My query needs to loop over categories, and for each user in a category
tell me which functions she qualifies for but does not possess, e.g:
user_id | function_id | result
1 | 6 | NULL
1 | 7 | 1 (or "sure thing" or whatever)
1 | 8 | NULL
>
i.e. if there's no record in the xref table with user_id 1 and
function_id 6, show NULL.
>
One thought I had would be just to have an "available" field in the xref
table, with values 'y' or 'n'. But in this case every user would need
(in this example) 3 records in the xref table, and that struck me as
inefficient.
>
users
------
user_id
name
category_id
>
categories
----------
user_id
category_name
>
functions
---------
function_id
category_id
function_name
>
xref
-----
user_id
function_id
can functions belong to more than one category? the functions table
suggests that they can't but I just wanted to check

They cannot. The only many-to-many rel. is users_functions
How about:

SELECT *
FROM (
SELECT u.user_id, u.name, u.category_id, f.function, x.function_id
FROM users u
LEFT JOIN functions f ON f.category_id = u.category_id
LEFT JOIN xref x ON x.user_id = u.user_id
AND x.function_id = f.function_id
)t1
WHERE ISNULL( t1.function_id )

Thanks. You got a wishlist somewhere?

I had to change
WHERE ISNULL( t1.function_id )
to simple
ORDER by user_id

to get what I wanted, which is a sorted set of all matching functions,
with NULLs when "unavailable". But joins are perfect.
ha - i just enjoy the challenge but, since you ask:

http://www.amazon.co.uk/gp/registry/1D7R707SQRFSD

anyway, glad if it helped

Ukrainian tractors? I couldn't resist that one. You're weird, dude.
:-)

You know, in years to come digital archaeologists may struggle to
follow the thread of this conversation.

Thank you, it's very thoughtful.

Sep 19 '06 #9

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

Similar topics

3
6402
by: Prem | last post by:
Hi, I am having many problems with inner join. my first problem is : 1) I want to know the precedance while evaluating query with multiple joins. eg. select Employees.FirstName,...
2
5471
by: jklimek | last post by:
In May of this year I graduated from Penn State with a BS in IST (Information Sciences and Technology). Right after graduation I got a database programming job with a company that uses Delphi 6 and...
10
2250
by: John | last post by:
Ok, I posted in here a few days ago about a problem with apostrophes in text fields and I tried a few of the suggestions and now I'm in so deep I looking at scraping the whole thing because now I...
7
31537
by: Steve | last post by:
I have a SQL query I'm invoking via VB6 & ADO 2.8, that requires three "Left Outer Joins" in order to return every transaction for a specific set of criteria. Using three "Left Outer Joins"...
18
1840
by: Scott David Daniels | last post by:
There has been a bit of discussion about a way of providing test cases in a test suite that _should_ work but don't. One of the rules has been the test suite should be runnable and silent at every...
1
1617
by: Mark Asbach | last post by:
Hi pythonians, I'm one of the maintainers of an open source image processing toolkit (OpenCV) and responsible for parts of the autotools setup. The package mainly consists of four shared...
1
2840
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...
9
11908
by: shapper | last post by:
Hello, I am used to SQL but I am starting to use LINQ. How can I create Left, Right and Inner joins in LINQ? How to distinguish the different joins? Here is a great SQL example:...
36
2460
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
7201
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7278
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
7328
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...
0
7456
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
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3166
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3153
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
734
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
379
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.