If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Microsoft SQL Server > UNION ALL, Joins and no Joins

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #16 (permalink)  
Old 03-03-08, 09:16
blindman blindman is offline
World Class Flame Warrior
 
Join Date: Jun 2003
Location: Ohio
Posts: 11,726
Sigh...
Some horses you can't even lead to water.
__________________
If it's not practically useful, then it's practically useless.

blindman
www.chess.com: "sqlblindman"
Reply With Quote
  #17 (permalink)  
Old 03-03-08, 09:45
GWilliy GWilliy is offline
Registered User
 
Join Date: Jan 2003
Location: Nottinghamshire, UK
Posts: 364
Neigh......

Wot's wrong Mr blindman - Sure my TSQL is childlike and could do with tidying up (easier with DDL) but methinks it does the job he's asking for.

Surely it's just a fairly basic correlated returning a TOP 1 using NEWID() to give a Random.

Performance does'nt seem to be a concern.

Or am I missing something

GW
__________________
"Everything should be made as simple as possible, but not simpler." - Albert Einstein
"Everything should be made as complex as possible, so I look Cleverer." - Application Developer
Reply With Quote
  #18 (permalink)  
Old 03-03-08, 10:08
GWilliy GWilliy is offline
Registered User
 
Join Date: Jan 2003
Location: Nottinghamshire, UK
Posts: 364
Thought I'd better tidy up a bit

Quote:

Select
title,description,simage from tableA
UNION ALL
Select title,description, simage from tableB
UNION ALL
Select title,description,(SELECT sImage FROM(SELECT TOP 1 NewID() x,sImage FROM tableW wi WHERE wi.id = TableC.id ORDER BY x)dt)
FROM TableC

__________________
"Everything should be made as simple as possible, but not simpler." - Albert Einstein
"Everything should be made as complex as possible, so I look Cleverer." - Application Developer

Last edited by GWilliy; 03-03-08 at 10:26.
Reply With Quote
  #19 (permalink)  
Old 03-03-08, 12:32
Nate1 Nate1 is offline
Registered User
 
Join Date: Aug 2007
Location: Auckland New Zealand
Posts: 113
Looks good, I'll give it a try tonight.
Reply With Quote
  #20 (permalink)  
Old 03-03-08, 14:41
TallCowboy0614 TallCowboy0614 is offline
Throwin' the steel to SQL
 
Join Date: Feb 2004
Location: Marina Del Rey, CA
Posts: 3,663
I think the horse is of a different color than you, gwilliy. But whatdoIknow?
__________________
aka "Paul"
Non est ei similis.

I just read "100 Things To Do Before You Die". I was surprised that "Yell for help!!" wasn't one of them
Reply With Quote
  #21 (permalink)  
Old 03-04-08, 02:08
Nate1 Nate1 is offline
Registered User
 
Join Date: Aug 2007
Location: Auckland New Zealand
Posts: 113
Thanks a lot worked very well, I inserted the typ to make defining which table the row came from easier for locating the images folder,

SELECT fThumbNail AS sImage,'L' as typ,
iListingID AS ID,iListingID AS Parent,sTitle,sDescription FROM Listing UNION ALL

SELECT sImage as sImage,'D' as typ,ParentID AS Parent,WikiID as
ID,sTitle,sDescription FROM wikidata UNION ALL

SELECT (SELECT sImage FROM (SELECT TOP 1 NewID() x,sImage
FROM
wiki_images wi WHERE wi.wikiid = wikiinfo.parentid ORDER BY x) as sImage) as sImage,'W' as typ
,wikiinfo.ParentID AS Parent,wikiinfo.WikiInfoID AS ID, stitle,sdescription FROM wikiinfo ORDER BY stitle
Reply With Quote
  #22 (permalink)  
Old 03-04-08, 06:13
GWilliy GWilliy is offline
Registered User
 
Join Date: Jan 2003
Location: Nottinghamshire, UK
Posts: 364
Your welcome Nate - Glad to help

GW
__________________
"Everything should be made as simple as possible, but not simpler." - Albert Einstein
"Everything should be made as complex as possible, so I look Cleverer." - Application Developer
Reply With Quote
  #23 (permalink)  
Old 03-04-08, 09:29
blindman blindman is offline
World Class Flame Warrior
 
Join Date: Jun 2003
Location: Ohio
Posts: 11,726
Okay, well one of your nested subqueries is completely superfluous. Your statement can be rewritten more simply as:
Code:
SELECT	fThumbNail AS sImage,
	'L' as typ,
	iListingID AS ID,
	iListingID AS Parent,
	sTitle,
	sDescription
FROM	Listing
UNION ALL
SELECT	sImage as sImage,
	'D' as typ,
	ParentID AS Parent,
	WikiID as ID,
	sTitle,
	sDescription
FROM	wikidata
UNION ALL
SELECT	(SELECT	TOP 1 sImage
		FROM	wiki_images wi
		WHERE	wi.wikiid = wikiinfo.parentid
		ORDER BY newid()) as sImage,
	'W' as typ,
	wikiinfo.ParentID AS Parent,
	wikiinfo.WikiInfoID AS ID,
	stitle,sdescription
FROM	wikiinfo
ORDER BY stitle
...and, though Nate is apparently uninterested in such concepts as efficiency or best practices, for the benefit of others who stumble upon this thread I'll post the proper method of coding this, which avoids bucketloads of unnecessary table scans:
Code:
SELECT	fThumbNail AS sImage,
	'L' as typ,
	iListingID AS ID,
	iListingID AS Parent,
	sTitle,
	sDescription
FROM	Listing
UNION ALL
SELECT	sImage as sImage,
	'D' as typ,
	ParentID AS Parent,
	WikiID as ID,
	sTitle,
	sDescription
FROM	wikidata
UNION ALL
SELECT	Images.sImage,
	'W' as typ,
	wikiinfo.ParentID AS Parent,
	wikiinfo.WikiInfoID AS ID,
	stitle,
	sdescription
FROM	wikiinfo
	left outer join --Images
		(SELECT	ROW_NUMBER() OVER (PARTITION BY wikiinfo.parentid ORDER BY NewID()) as ROWNUMBER,
			wiki_images.wikiid,
			wiki_images.sImage as sImage
		from	wiki_images) Images
		on wikiinfo.parentid = Images.wikiid
		and Images.ROWNUMBER = 1
ORDER BY stitle
__________________
If it's not practically useful, then it's practically useless.

blindman
www.chess.com: "sqlblindman"
Reply With Quote
  #24 (permalink)  
Old 03-04-08, 14:07
Nate1 Nate1 is offline
Registered User
 
Join Date: Aug 2007
Location: Auckland New Zealand
Posts: 113
Blindman So by superfluous you mean the three selects in the one statement is unnecessary?

Ill try the bottom query tonight also, I would like to see the difference in speed Ive seen it done in SQL2005 Management Studio where the reports are created for each query requested but could you tell me where to click so I can see the difference between these queries?
Reply With Quote
  #25 (permalink)  
Old 03-04-08, 14:57
GWilliy GWilliy is offline
Registered User
 
Join Date: Jan 2003
Location: Nottinghamshire, UK
Posts: 364
Eeeek

But

1] Ref the superfluous subquery
Code:

Msg 104, Level 15, State 1, Line 1
ORDER BY items must appear in the select list if the statement contains a UNION operator.
2] Is'nt it Best Practice to leave behind easily maintainable Code

3] Why do you think there will be table scans as the only join is in the subquery and thats via the id which is probably a clustererd PK integer.

4] Who gets to spend valuable minutes nowadays optimizing queries that probably have insignificant performance or system overheads.

I don't mean to be bolchy blindman, I know you can run rings round me with TSQL and I have a lot of respect for all you guys.

Nate I'd be interested to see how many seconds each version takes to run on average (after running each a couple of times first for queryplan caching etc.)

Now I'm gonna duck

GW
__________________
"Everything should be made as simple as possible, but not simpler." - Albert Einstein
"Everything should be made as complex as possible, so I look Cleverer." - Application Developer
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On