473,804 Members | 4,223 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

VERY Complex stored procedure... I'm lost. Help?

3 New Member
I don't even know if this is possible - I'm still fairly new to stored procedures.

I have 4 tables I need to compile data from:
saved_searches
saved_homes
printed_homes
member_info

saved_searches, saved_homes and printed_homes each track the date they were created.

I need to grab all the users from member_info who have had activity in saved_searches, saved_homes and printed_homes between now and X date, along with how many of each (sum(id)) they have performed from saved_searches, saved_homes and printed_homes.

I need to order the results by who has had the most activity in the specified date range, meaning the sum of the sum(id) from the searches going into saved_searches, saved_homes and printed_homes.

The results also have to be restricted to a field in member_info matched to an additional parameter.

Ok... that's a mouthful.

Here's the concept so far:

The following stored procedure would be created for saved_searches, saved_homes and printed_homes, so I'll just show one as the three tables are pretty similar:

@workingWithID
@maxDate
SELECT sum(id), username FROM saved_searches WHERE username IN
(SELECT username FROM member_info WHERE workingWithID = @workingWithID)
AND searchDate > @maxDate

Now, to get all the member info of those with recent activity, I would have this stored procedure that would call the (above) stored procedures for saved_searches, saved_homes and printed_homes:

@workingWithID
@maxDate
SELECT * FROM member_info WHERE username IN
(sp_savedSearch (@workingWithID ,@maxDate))
AND username IN
(sp_savedHomes( @workingWithID, @maxDate))
AND username IN
(sp_printedHome s(@workingWithI D,@maxDate))


HOWEVER, this concept wouldn't return the sums generated by the smaller stored procedures....

I am truly lost here - can anyone assist me in making sense of how to accomplish this?

Thank you in advance
-=Patrick=-
Jul 12 '07 #1
2 5303
Infide
28 New Member
I don't even know if this is possible - I'm still fairly new to stored procedures.

I have 4 tables I need to compile data from:
saved_searches
saved_homes
printed_homes
member_info

saved_searches, saved_homes and printed_homes each track the date they were created.

I need to grab all the users from member_info who have had activity in saved_searches, saved_homes and printed_homes between now and X date, along with how many of each (sum(id)) they have performed from saved_searches, saved_homes and printed_homes.

I need to order the results by who has had the most activity in the specified date range, meaning the sum of the sum(id) from the searches going into saved_searches, saved_homes and printed_homes.

The results also have to be restricted to a field in member_info matched to an additional parameter.

Ok... that's a mouthful.

Here's the concept so far:

The following stored procedure would be created for saved_searches, saved_homes and printed_homes, so I'll just show one as the three tables are pretty similar:

@workingWithID
@maxDate
SELECT sum(id), username FROM saved_searches WHERE username IN
(SELECT username FROM member_info WHERE workingWithID = @workingWithID)
AND searchDate > @maxDate

Now, to get all the member info of those with recent activity, I would have this stored procedure that would call the (above) stored procedures for saved_searches, saved_homes and printed_homes:

@workingWithID
@maxDate
SELECT * FROM member_info WHERE username IN
(sp_savedSearch (@workingWithID ,@maxDate))
AND username IN
(sp_savedHomes( @workingWithID, @maxDate))
AND username IN
(sp_printedHome s(@workingWithI D,@maxDate))


HOWEVER, this concept wouldn't return the sums generated by the smaller stored procedures....

I am truly lost here - can anyone assist me in making sense of how to accomplish this?

Thank you in advance
-=Patrick=-
I do not believe you can use stored procedures in this way, at least not in sql server. If you create functions you could call them in the way you are trying to call the stored procs.
Jul 12 '07 #2
trickyidiot
3 New Member
UPDATE

After extensive reading I have changed my approach...

I'm selecting the sums grouped by usernames from each of the three tables and storing them in temp tables but I'm stuck on something I;ve never dopne before - a union select statement

What I hope to achieve with this select statement is a full count of all activity per member, and from there I should be able to loop through those results and grab the member info, the full sum of all activity, and each separate sum for each type of activity... I just don't know how to do that within a stored procedure...


Here's what I have so far - please help!!!

CREATE PROCEDURE dbo.memberRecen tActivity
@workingWithID int(4)
@maxDate datetime

AS

-- Variables --
DECLARE @savedSearches nvarchar(4000),
@savedHomes nvarchar(4000),
@printedHomes nvarchar(4000),
@combinedSums nvarchar(4000)

-- Set SAVED SEARCHEs --
SET @savedSearches = (SELECT sum(id), username FROM endeavor_search WHERE searchDate > @maxDate AND username IN (SELECT username FROM endeavor_member Info WHERE workingWith = @workingWithID) GROUP BY username)
IF @savedSearches = ' ' SET @savedSearches = ' '

-- Create Temporary Local Table for saved searches --
DECLARE @svdSrchTable TABLE (sum int(4), username varchar(50))
INSERT INTO @svdSrchTable SELECT value FROM dbo.fnc_split(@ savedSearches, ',')


-- Set SAVED HOMES --
SET @savedHomes = (SELECT sum(id), username FROM endeavor_mlsSav ed WHERE mlsDate > @maxDate AND username IN (SELECT username FROM endeavor_member Info WHERE workingWith = @workingWithID) GROUP BY username)
IF @savedHomes = ' ' SET @savedHomes = ' '

-- Create Temporary Local Table for saved homes --
DECLARE @svdHomeTable TABLE (sum int(4), username varchar(50))
INSERT INTO @svdHomeTable SELECT value FROM dbo.fnc_split(@ savedHomes, ',')


-- Set PRINTED HOMES --
SET @printedHomes = (SELECT sum(id), username FROM endeavor_mlsPri nt WHERE mlsDate > @maxDate AND username IN (SELECT username FROM endeavor_member Info WHERE workingWith = @workingWithID) GROUP BY username)
IF @printedHomes = ' ' SET @printedHomes = ' '

-- Create Temporary Local Table for printed homes --
DECLARE @prntHomeTable TABLE (sum int(4), username varchar(50))
INSERT INTO @prntHomeTable SELECT value FROM dbo.fnc_split(@ printedHomes, ',')


-- Set COMBINED SUMS --
SET @combinedSums = (SELECT sum(id) as totalSum, username from @svdSrchTable UNION SELECT sum(id) as totalSum, username from @svdHomeTable UNION SELECT sum(id) as totalSum, username from @prntHomeTable GROUP BY username ORDER BY totalSum)
Jul 12 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
1925
by: Peter Rilling | last post by:
To minimize database calls, I would like to have a single stored procedure return all the data necessary to display on my page. This information is related from several different tables and what I would like to do is to match the information loaded into a DataSet with the information in the database. For instance, suppose that I have a Customer table and an Order table. I would like to return a single customer and all their orders in a...
1
1511
by: Marcel Boscher | last post by:
Hey guys, i got a question that bothers me quite a while, been looking everywhere but found no answer My idea is to automate this procedure for better performance... Can anybody in here tell me if its possible to auto create a 4 digit randomly code 1st digit alpha 2nd -4th numeric that gets looked up in the db if its there new code is generated
0
2200
by: dumbledad | last post by:
Hi All, I'm having trouble moving from a query that calls a MySQL 5 stored procedure to a query that calls a simple SQL INSERT. Here's the relevant part of the code that calls the stored procedure: $query = sprintf( "CALL AddSearchTerm('%s', %d)", $massaged_search_term, count($_SERVER) ); $result = mysqli_query($link, $query); if($result)
3
35386
by: Jack Turnbull | last post by:
Hi, Am new to Stored Procedures and am lost how to achieve the following. I have this table:- CREATE TABLE . ( IDENTITY (1, 1) NOT NULL , NULL , (50) COLLATE Latin1_General_CI_AS NULL , (100) COLLATE Latin1_General_CI_AS NULL ) ON
2
1449
by: vncntj | last post by:
I'm trying to passing the Session value back to the store procedure, but I'm having difficulty understanding stored procedures and parameters. What I'm trying to accomplish is to have the Session variable passed through as a parameter to the Stored Procedure where I can test to see if any records exist? if no record exist then we can move to the next page. thanks,
9
2471
by: Frawls | last post by:
Hi I Am am having problems with a stored Procedure that i wrote. Basically whats happening is that the Stored procedure Runs fine when i EXECUTE it in SQL Query analyzer. But when i debug through the application in Visual Studio .NET 2003 the application an exception when it executes the query.
0
1738
by: Bruno Barberi Gnecco | last post by:
I'm using stored procedures in a PHP site, with mysql. I'm having problems with stored procedures. Of the two I'm using, one works all the time, consistently. The other works correctly, but the next query returns "Lost connection to MySQL server during query" and subsequently: "MySQL server has gone away". I'm using MDB2 to access the database. I tried changing the driver from 'mysql' to 'mysqli', with little success. Currently, about...
0
1402
by: esmith2112 | last post by:
Is there any way to retain the permissions previously granted on a stored procedure through an explicit "GRANT EXECUTE" statement, after recompiling said procedure? We are using group permissions for SP execution. Every time a developer rebuilds a procedure, it appears that it is internally dropped and recreated. It must during the "drop" phase that all previous permissions are getting lost. I've searched for options in the development...
5
1987
by: william.david.anderson | last post by:
Hi there, I have a newbie stored procedures performance question that I'm hoping someone can answer. It seems like one of the benefits of using SQL stored procedures is that they can reduce the network traffic between the client and server, resulting in a performance gain. I'm wondering, though, if there's a performance penalty because it could be harder to reuse prepared statements with them.
0
9579
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10577
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10320
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10077
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7620
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5521
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4299
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3820
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.