Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Problem With Joins

Author  Topic 

curry
Starting Member

14 Posts

Posted - 2008-11-05 : 08:51:47
[code]SELECT productid, description, rangename, min(tbl_media.media_name) as prod_image from products
inner join range on products.range = range.rangeid
left outer join tbl_media on products.productid = tbl_media.product_id
where range=227
group by products.productid, products.range, products.description, range.rangename[/code]

The above statement gives me everything I want apart from the image name it pulls from tbl_media is the first one alphabetically where as I need the first one via the unique ID in tbl_media. I've tried different joins and played with sub queries a little but without success.

Thanks in advance


Matt

gedwards
Starting Member

19 Posts

Posted - 2008-11-05 : 13:16:23
Can you post a script to generate the tables and some sample data? Just a small set.
And for your 227 value what you are seeing and what you expect to see?
I can see where you're getting the first by alpha, but first via the unique ID in the tbl_media isn't clear.
Could be the first record, or the lowest unique value.
Thanks, Greg E

Greg E
Go to Top of Page

jcelko
Esteemed SQL Purist

547 Posts

Posted - 2008-11-05 : 14:03:20
"A problem well stated is a problem half solved." -- Charles F. Kettering

Please post DDL, so that people do not have to guess what the keys, constraints, Declarative Referential Integrity, data types, etc. in your schema are. If you know how, follow ISO-11179 data element naming conventions and formatting rules. Temporal data should use ISO-8601 formats. Code should be in Standard SQL as much as possible and not local dialect.

Sample data is also a good idea, along with clear specifications. It is very hard to debug code when you do not let us see it. If you want to learn how to ask a question on a Newsgroup, look at: http://www.catb.org/~esr/faqs/smart-questions.html

I am confused by the sentence "the unique ID in tbl_media" because there is no such thing as a magical universal identifier in RDBMS and unless this deals with furniture, the "tbl-" is both silly and a violation of ISO-11179 rules.


--CELKO--
Joe Celko, SQL Guru
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2008-11-05 : 15:34:43
Not tested because of missing sample data etc.
But try this:
;with my_cte_media as 
(select
product_id,
media_name as prod_image,
row_number() over (partition by product_id order by product_id,media_unique_id) as rownum
from tbl_media)
SELECT
productid,
description,
rangename,
min(cte.prod_image) as prod_image
from products
inner join range
on products.range = range.rangeid
left outer join my_cte_media as cte
on products.productid = cte.product_id and cte.rownum = 1
where range=227
group by products.productid, products.range, products.description, range.rangename


Webfred


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

curry
Starting Member

14 Posts

Posted - 2008-11-06 : 03:49:39
Sorry if I've broken any rules, this was not my intention.

It seems I've made another mistake as I've posted this in SQL Server 2005 and it should be in SQL Server 2000, sorry but can a mod move this for me please?

I'm not doing very well with this thread.
Go to Top of Page

curry
Starting Member

14 Posts

Posted - 2008-11-06 : 04:14:34
Sample Data

My original SQL statement

Gives This

Product_id---Description---RangeName---Prod_Image
575---Product Description1---RangeName 44---1_445
576---Product Description2---RangeName 44---446
577---Product Description3---RangeName 44---447



SELECT * from tbl_media where product_id=575

Gives This

Media_id---Product_id---media_ext---media_name
100534---575---jpg---445(1)
100541---575---jpg---445_zoom2
100563---575---jpg---445_zoom_37631
100835---575---jpg---1_445


You’ll see that my original SQL Statement returns 1_445 from the media table (tbl_media) as it is the first alphabetically but I need to return 445(1) as that is the first in the table via the Media_id (This is the unique ID I mentioned in my original post)
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-11-06 : 04:45:56
[code]SELECT Media_ID,
Product_ID,
Media_Ext,
Media_Name
FROM (
SELECT Media_ID,
Product_ID,
Media_Ext,
Media_Name,
ROW_NUMBER() OVER (ORDER BY Media_ID) AS recID
FROM tbl_Media
WHERE Product_ID = 575
) AS d
WHERE recID = 1[/code]


E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-11-06 : 04:48:22
[code]SELECT p.Product_id,p.Description,r.RangeName,m.media_name
FROM tbl_Product p
inner join range r on p.range = r.rangeid
INNER JOIN tbl_media m
ON m.Product_ID=p.Product_id
INNER JOIN (SELECT Product_id,MIN(Media_Id) AS First
FROM tbl_Media
GROUP BY Product_Id)m1
ON m1.Product_id=m.Product_id
AND m1.First=m.Media_Id[/code]
Go to Top of Page

curry
Starting Member

14 Posts

Posted - 2008-11-06 : 05:09:41
quote:
Originally posted by Peso

SELECT	Media_ID,
Product_ID,
Media_Ext,
Media_Name
FROM (
SELECT Media_ID,
Product_ID,
Media_Ext,
Media_Name,
ROW_NUMBER() OVER (ORDER BY Media_ID) AS recID
FROM tbl_Media
WHERE Product_ID = 575
) AS d
WHERE recID = 1



E 12°55'05.63"
N 56°04'39.26"




"'ROW_NUMBER' is not a recognized function name."

Is this a feature of SQL Server 2005? I mistakenly posted this in 2005 and it should have been in 2000 :(.
Go to Top of Page

curry
Starting Member

14 Posts

Posted - 2008-11-06 : 05:13:01
quote:
Originally posted by visakh16

SELECT p.Product_id,p.Description,r.RangeName,m.media_name
FROM tbl_Product p
inner join range r on p.range = r.rangeid
INNER JOIN tbl_media m
ON m.Product_ID=p.Product_id
INNER JOIN (SELECT Product_id,MIN(Media_Id) AS First
FROM tbl_Media
GROUP BY Product_Id)m1
ON m1.Product_id=m.Product_id
AND m1.First=m.Media_Id




I am 99% sure I can make that work.

You where the one that helped me last time, thank you sir.
Go to Top of Page

curry
Starting Member

14 Posts

Posted - 2008-11-11 : 08:52:54
quote:
Originally posted by jcelko

"A problem well stated is a problem half solved." -- Charles F. Kettering

Please post DDL, so that people do not have to guess what the keys, constraints, Declarative Referential Integrity, data types, etc. in your schema are. If you know how, follow ISO-11179 data element naming conventions and formatting rules. Temporal data should use ISO-8601 formats. Code should be in Standard SQL as much as possible and not local dialect.

Sample data is also a good idea, along with clear specifications. It is very hard to debug code when you do not let us see it. If you want to learn how to ask a question on a Newsgroup, look at: http://www.catb.org/~esr/faqs/smart-questions.html

I am confused by the sentence "the unique ID in tbl_media" because there is no such thing as a magical universal identifier in RDBMS and unless this deals with furniture, the "tbl-" is both silly and a violation of ISO-11179 rules.


--CELKO--
Joe Celko, SQL Guru




I have taken some time to consider your response Mr Celko and frankly I feel the nature of it was not necessary. Clearly I'm not as skilled as you when we are talking about SQL which is why I asked the question.

I am sure that I have skills in areas where you are not as skilled as me, judging by your web site (http://www.celko.com) I'd say that maybe my skills in coding web sites to current accessibility standards may well be one of them. The following link http://www.w3.org/WAI/ might be of some assistance to you.

I have no wish (or time) to enter in to an argument with you but I feel it only fair that I am allowed the opportunity to comment on your sarcasm.
Go to Top of Page
   

- Advertisement -