Solved

SQL Query using result set for criteria

Posted on 2008-10-07
14
184 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
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…

729 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