Showing posts with label similar. Show all posts
Showing posts with label similar. Show all posts

Monday, March 26, 2012

Help with an SQL view problem

Hi,

Have hit upon a problem with an SQL view, and wondered if anyone else had come upon a similar problem.

I have 2 tables in my database:

    Categories

    Requests

The Categories table contains three fields: CategoryID, ParentID, CategoryName

My Categories table has 3 entries:

CategoryID: 1
ParentID: Null
Title: Category 1

CategoryID: 2
ParentID: 1
Title: Category 2

CategoryID: 3
ParentID: 2
Title: Category 3

The Reqests table contains the following: RequestID, CategoryID, Title etc

My Requests table also has 3 entries:

RequestID: 1
CategoryID: 1
Title: Request 1

RequestID: 2
CategoryID: 2
Title: Request 2

RequestID: 3
CategoryID: 3
Title: Request 3

The problem I am faced with is that I need to build up a select statement which can return all requests filtered by the category ID, but this also needs to include any requests whose sub categories are a sub of the master category (if that makes sense).

Am not sure how I could go about this, I'm not sure what I would need to do to be able to loop up through all the parent ID's until I reach a null value?

Any help on this would be much appreciated.

Matt

Its good that you provided sample data. Can you also provide expected output for a given categoryID of 1 or 2 so we can understand your requirement better?

|||

Here is a query to get you a listing of all top level parents and one child.

select c.CategoryID ParentCategoryID, c.Title ParentTitle , r.RequestID ParentRequestID, r.Title , cc.CategoryID ChildCategoryID, cc.CategoryTitle ChildTitle , cc.RequestID ChildRequestID, cc.ChildRequestTitlefrom Categories cleftouter join (select cx.CategoryID, cx.ParentID, cx.Title CategoryTitle , rx.RequestID, rx.Title RequestTitlefrom Categories cxleftouter join Requests rxon cx.CategoryID = rx.CategoryIDand cx.ParentIDISNOT NULL )as ccon cc.ParentID = c.CategoryIDleftouter join Requests ron c.CategoryID = r.CategoryIDwhere c.ParentIDISNULL

But I do believe that this only gets you part of the way.

|||

Hi,

This article should provide you with some helpful guidance.

http://mssqltips.com/tip.asp?tip=938

|||

Thanks for all of your help, had a look at the recursive common table expressions in SQL server 2005 and looks like I now have my solution.

Cheers,

Matt

|||

Hi,

Managed to get around my first problem using the recursive common table expressions but have now hit upon another as I want to add paging using the Row_Number and custom sorting.

Here is what I have in my stored procedure so far:

DECLARE @.sqlString nvarchar(4000)

IF @.SortExpression = ''
BEGIN
SET @.SortExpression = 'DateCreated DESC'
END

IF @.CategoryID = 0
BEGIN
WITH AllJobRequests AS
(
SELECT *, ROW_NUMBER() OVER (ORDER BY @.SortExpression) AS RowNum FROM dbo.JobRequests
)
SELECT * FROM AllJobRequests WHERE RowNum BETWEEN @.Start AND @.End ORDER BY RowNum ASC
END
ELSE
BEGIN
SET @.sqlString = 'WITH GetSubCategories (JobRequestID, CategoryID, ParentID, CategoryName, Depth, RowNum) AS
(
SELECT dbo.JobRequests.JobRequestID, dbo.JobRequests.CategoryID, dbo.Categories.ParentID, dbo.Categories.CategoryName, 0 AS Depth, ROW_NUMBER() OVER (ORDER BY dbo.JobRequests.' + @.SortExpression + ') AS RowNum
FROM dbo.Categories INNER JOIN
dbo.JobRequests ON dbo.Categories.CategoryID = dbo.JobRequests.CategoryID
WHERE dbo.Categories.CategoryID = ' + CONVERT(nvarchar(10), @.CategoryID) + '
UNION ALL
SELECT dbo.JobRequests.JobRequestID, dbo.JobRequests.CategoryID, dbo.Categories.ParentID, dbo.Categories.CategoryName, GetSubCategories.Depth + 1 AS Depth, ROW_NUMBER() OVER (ORDER BY dbo.JobRequests.' + @.SortExpression + ') AS RowNum
FROM dbo.Categories INNER JOIN
dbo.JobRequests ON dbo.Categories.CategoryID = dbo.JobRequests.CategoryID
JOIN GetSubCategories ON Categories.ParentID = GetSubCategories.CategoryID
)
SELECT DISTINCT * FROM GetSubCategories WHERE RowNum BETWEEN ' + CONVERT(nvarchar(10), @.Start) + ' AND ' + CONVERT(nvarchar(10), @.End) + ' ORDER BY RowNum ASC'
EXEC sp_executesql @.sqlString

This works to an extent apart from the sorting, have a look at the following output gained (the last bold characters are the RowNum order)

2 13 12 Central Heating 1 1
7 12 NULL Plumbing 0 1
6 12 NULL Plumbing 0 2
5 12 NULL Plumbing 0 3
4 12 NULL Plumbing 0 4
3 12 NULL Plumbing 0 5

The results are good in that they return both the output for the master category "Plumbing" and then the child category "Central Heating", but what would I now need to do to change this stored procedure to ensure I order the columns correctly?

Thanks,

Matt

Friday, February 24, 2012

Help retrieving values from 2 similar stored procs

I created one stored proc, then copied it to create another similar stored proc with just some filtering changes in the second. Now I want to obtain the results from both like this:
ProjFee ProjGross DailyRunRate Var1 InHouse1 InHouse2 GrossGoal Group Name PostedAmount
---------
Row# 1 from current stored proc
Row # 2 from called stored proc
so something like this I should get in the end for example when both results are combined:
ProjFee ProjGross DailyRunRate Var1 InHouse1 InHouse2 GrossGoal Group Name PostedAmount
---------
100000 33455 200 300 345555 4455555 5666666 Arizona 56000
103400 22455 900 700 777555 3333555 5444666 Illinois 660000

The first stored proc (CurrentMonthCollections_AZ) attempts to include the results of the second stored proc at the end (CurrentMonthCollections_IL) by calling it and inserting the fields into a temp table.
I was told by Angel to instead convert the second stored proc into a UDF...but having trouble with this.
What is the best approach to get the desired 2 rows back that I need in that fasion?
Here is the code for the 2 stored procs (very long so I will post as as links):
http://www.webfound.net/CurrentMonthCollections_AZ.txt
http://www.webfound.net/CurrentMonthCollections_IL.txt
Look at the end of CurrentMonthCollections_AZ.txt to see where I'm stuck in trying to select the results (ProjFee ProjGross DailyRunRate Var1 InHouse1 InHouse2 GrossGoal Var1 PostedAmount
) from both stored procs.
I don't think UNION is what I want because it will combine stuff...I just want 2 separated rows

"I don't think UNION is what I want because it will combine stuff...I just want 2 separated rows"

Union is what you want:

select 1
union
select 2

returns


1
2

It seems to me that you could parameterize the procedures and get back the two rows that you want, or at least have one procedure and pass in the state you are interested in...that is a lot of proc to deal with though...

|||what about union all ?|||

The difference between the 2 is that union removes duplicates and union all does not

union all is also faster because of that

select 1
union
select 2
union
select 2


select 1
union all
select 2
union all
select 2

Denis the SQL Menace

http://sqlservercode.blogspot.com/

|||

Sorry, I should have been clearer:

UNION ALL doesn't eliminate duplicates, UNION does.

UNION ALL is probably the more correct way to go in this case, but it won't really make a difference with such a small set.

|||in this case, there are no dups so it doesn't matter I guess then.|||so I thought UNION puts it all in one row? I must be wrong...