Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL Query using result set for criteria

Posted on 2008-10-07
14
Medium Priority
?
211 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 60

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 60

Expert Comment

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

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 60

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
 
LVL 60

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 60

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 60

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 60

Accepted Solution

by:
Kevin Cross earned 1000 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

916 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