Solved

SQL Query using result set for criteria

Posted on 2008-10-07
14
179 Views
Last Modified: 2012-05-05
I am trying to find a way with sql server 2005 to query a table with a demand column and a supply column, I want to search a top level demand and find the supply/s for it then I want to see if those supplies are in the demand column also and find the supplies for them and so on.  I have attached a small file that may explain it a little better.  Thanks
Sql-Table.xls
0
Comment
Question by:jeremyj54
  • 8
  • 6
14 Comments
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22662369
Here is a recursive UDF that does this, although you can write a really cool common table expression to do it.  Since I have seen it so much from my EE experts I won't claim the fame; therefore, I will find an example and post for you.

http:Q_23680666.html
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22662441
Here is one -- http:Q_23203927.html
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22662523
Here is an example with your data and UDF method -- I will leave the cte for Angel Eyes, Chapman, or Brandon -- I am sure they will drop by especially since I didn't use a CTE. :)
SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

CREATE FUNCTION [dbo].[fn_GetSupply] 

(

	-- Input parameters

	@demand int

)

RETURNS 

@TBL TABLE 

(

	-- Returned table layout

        supply int

)

AS

BEGIN

	DECLARE @currentID int  -- item currently being processed

	

        -- grab first child record

        SELECT @currentID = MIN(supply)

        FROM TABLENAME WHERE demand = @demand

 

	WHILE @currentID IS NOT NULL

		BEGIN

			-- explode downward

			INSERT INTO @TBL

            SELECT supply

            FROM dbo.fn_GetSupply(@currentID)

			

-- insert id matching input

        INSERT INTO @TBL

        SELECT supply

        FROM tablename WHERE supply = @currentID
 

			-- grab the next record

			SELECT @currentID = MIN(supply)

        FROM TABLENAME WHERE demand = @demand AND supply > @currentID

		END

	RETURN 

END 

Open in new window

0
 

Author Comment

by:jeremyj54
ID: 22663293
How do I call this function now?
0
 

Author Comment

by:jeremyj54
ID: 22663750
How do I call this with a view that I want to report in crystal from?
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22664474
SELECT * FROM dbo.fn_GetSupply(1)
0
 

Author Comment

by:jeremyj54
ID: 22665181
How do I send the variable from crystal reports to the function?
0
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22665481
SELECT * FROM dbo.fn_GetSupply(@parameter)

You just send the parameter into the dbo.fn_GetSuppy like above.  It is like calling a stored procedure.
0
 

Author Comment

by:jeremyj54
ID: 22668542
My problem is that I am trying to use the function which is working great thank you as part of a view and I want crystal reports to be able to report off this view when the user selects a certain demand id.  If I try to save the view now without a static demand Id it gives me an error "insufficient number of arguments"  is what im trying to do doable or is there another way I should be doing it?
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22669704
Do you not want to use the function directly in code?  Are you getting additional data in the view?
0
 

Author Comment

by:jeremyj54
ID: 22670068
I am getting additional data from the view, I have the function attached to another table to grab the data I need.
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22675338
Ok, you can either change the VIEW to be a procedure that takes in a parameter that you can pass along to this function OR you can use this function like any other table and JOIN it to VIEW from the report.

SELECT s.supply, v.column1, v.column2, v.column3, v.column4
FROM dbo.fn_GetSupply(@parameter) s JOIN viewName v
ON s.supply = v.supply
0
 

Author Comment

by:jeremyj54
ID: 22681629
The stored procedure worked great, one last question is it possible for the original parameter @demand to show in the table?
0
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 250 total points
ID: 22681905
Yes just alter this and change all the inserts to add in the demand id if you want this to be the demand id in the table:

(
      -- Returned table layout
        supply int
        , demand int /* ADDED THIS LINE */
)

If you want all the records to match the original demand id passed in you can do it easier, by doing this:

SELECT @parameter AS demand, s.supply, v.column1, v.column2, v.column3, v.column4
FROM dbo.fn_GetSupply(@parameter) s JOIN viewName v
ON s.supply = v.supply
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now