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 > Hierarchy sql query

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #16 (permalink)  
Old 02-02-12, 06:46
kpeeroo kpeeroo is offline
Registered User
 
Join Date: Jul 2009
Posts: 143
Wim, thanks man, sorry for the trouble I managed to get it working with your Query slightly modified:

Code:
DECLARE @TopLevelId	INT;
SET @TopLevelId = 23;

WITH CTE AS
(SELECT id, name, parentid, id as Adam, 1 as level
FROM Category
WHERE id = @TopLevelId
	UNION ALL
SELECT Category.id, Category.name, Category.parentid, CTE.Adam, CTE.level + 1
FROM Category 
	INNER JOIN CTE ON
		Category.parentid = CTE.id
WHERE Category.id <> @TopLevelId
),
Prods AS
(SELECT productid, 
	description, 
	categoryId,
	NewId() as GUID_
FROM Products
),
Prods5 AS
(SELECT  productid, 
	description, 
	categoryId,
	ROW_NUMBER() OVER (PARTITION by categoryId ORDER BY GUID_) as RowNum
FROM Prods
)
SELECT CTE.id, CTE.name, Prods5.productid, Prods5.description
FROM CTE
	LEFT OUTER JOIN Prods5 ON
		CTE.id = Prods5.categoryId AND
		Prods5.RowNum <= 5
ORDER BY level, CTE.id
It's working fantastic now thanks so much WIM, great stuff
Reply With Quote
  #17 (permalink)  
Old 02-02-12, 06:53
kpeeroo kpeeroo is offline
Registered User
 
Join Date: Jul 2009
Posts: 143
Wim, if you permit:

1) how can anyone learn to write these complex TSQL queries, there must some kind of techniques right? any advice how to get going and understand these stuff. I know I will be back on this forum for some time but still I want to know the techniques how to derive those complex TSQL queries.

2) Can software like Crystal Reports help as it says it does that we do not need to learn or write complex TSQL because the software does all the magic for us?

Thanks.

Last edited by kpeeroo; 02-02-12 at 07:13.
Reply With Quote
  #18 (permalink)  
Old 02-02-12, 08:14
Wim Wim is offline
Registered User
 
Join Date: Nov 2004
Posts: 1,280
3 years ago I came from a DB2 world and didn't knew T-SQL.

The first two things I did was installing SQL Server 2005 on my machine and making dBforums one of my startup tabs in Firefox and started reading the posts in this forum.
Next thing I did was mimicking the things I just read on the forum, on my test machine. Reading up on Books On Line (BOL) when things were nor clear.
After that I tried to answer people's questions to sharpen my TSQL skills. And that's what I still do.
__________________
With kind regards . . . . . SQL Server 2000/2005/2008/2008 R2 Earned beers: 16
Wim
Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald Knuth
Grabel's Law: 2 is not equal to 3 -- not even for very large values of 2.
Pat Phelan's Law: 2 very definitely CAN equal 3 -- in at least two programming languages
Reply With Quote
  #19 (permalink)  
Old 02-02-12, 10:09
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by kpeeroo View Post
2) Can software like Crystal Reports help as it says it does that we do not need to learn or write complex TSQL because the software does all the magic for us?
hahahaha... thanks for the laugh, this brightened up my morning, lol
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
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