Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Fixing a sql query to use joins?

Author  Topic 

chuy08
Starting Member

2 Posts

Posted - 2006-10-31 : 19:16:33
I have two tables in my database, leads and mortgages. The two tables contain a mortgage_id field. The mortgage_id field in the leads table will be unique and the mortgage_id field in the mortgages tables could potentially have many relationships back to the given leads table. Currently I have a sql query like the following:

select leads.fname, leads.lname, mortage.mortgage_lender from leads, mortgage where leads.mortgage_id = mortgage.mortgage_id;

This query is working and generating most of what I want with the exception that each time a mortage_id is matched from the leads table to the mortage table it generates a new line of information. How could I contsturct a sql query so that one record from the leads table is selected and matched to any and all records in the mortage table and the query will be returned into one line of information.

The long and short of this is that I am trying to prefill a web form with the query above and put all pertinent data where it belongs using an array but the array that I am generating above will only contain the data from the first match of the mortgage_id field in the mortgage table. I am fairly certain that some sort of join will do the trick but I am a realative newb when it comes to SQL dbases. Any help appreciated.

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-10-31 : 21:02:46
[b]"one record from the leads table is selected and matched to any and all records in the mortage table and the query will be returned into one line of information"]/b]
As you said. ONE leads can have MANY mortgages. So which record of mortgages do you want to show ?



KH

Go to Top of Page

chuy08
Starting Member

2 Posts

Posted - 2006-11-01 : 00:02:43
Using the current sql query that I have I have to execute that query twice, the first time to get out the first mortgage and the second time to get out the second mortgage. What I want is one query that will get out all the mortgages and put them in one row of returns, the current sql query that I have returns each entry to a new row.

Example as follows:

Using this query:

select leads.fname, leads.lname, mortage.mortgage_lender from leads, mortgage where leads.mortgage_id = mortgage.mortgage_id;

I get this result:

john doe wellsfargo
john doe countrywide
jane doe wamu
jane doe wamu

What I would like to see is the following

john doe wellsfargo countrywide
jane doe wamu wamu

So for every record in the leads table I want all corresponding record in the mortgage table to appear all in one row. Is this a better explanation.
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-11-01 : 01:00:52
use this http://sqljunkies.com/WebLog/amachanic/archive/2004/11/10/5065.aspx to concatenate the mortgage_lender column


KH

Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-11-01 : 01:18:17
Here it is.

create table leads
(
fname varchar(10),
lname varchar(10),
mortgage_id int,
)

create table mortgage
(
mortgage_lender varchar(20),
mortgage_id int
)

insert into leads
select 'john', 'doe', 1 union all
select 'jane', 'doe', 2

insert into mortgage
select 'wellsfargo', 1 union all
select 'countrywide', 1 union all
select 'wamu', 2 union all
select 'wamu', 2
go

ALTER FUNCTION dbo.Concatenate( @mortgage_id integer )
RETURNS VARCHAR(8000)
AS
BEGIN
DECLARE @Output VARCHAR(8000)
SET @Output = ''

SELECT @Output = CASE @Output
WHEN '' THEN mortgage_lender
ELSE @Output + ', ' + mortgage_lender
END
FROM dbo.mortgage
WHERE mortgage_id = @mortgage_id
--group by mortgage_lender
ORDER BY mortgage_lender

RETURN @Output
END
go

select l.fname, l.lname, dbo.Concatenate(l.mortgage_id)
from leads l inner join mortgage m
on l.mortgage_id = m.mortgage_id
group by l.fname, l.lname, l.mortgage_id

drop table leads
drop table mortgage



KH

Go to Top of Page
   

- Advertisement -