SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Combine related row from 2 tables into single row


Combine related row from 2 tables into single row

Author
Message
madhavsinghk
madhavsinghk
Grasshopper
Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)

Group: General Forum Members
Points: 24 Visits: 28
Hi,

I have two tables Projects and FundingSources

How can i create a summary with each project per row with funding sources added?


Projects:
ProjectID, Name, Status, StartDt, EndDt
R101, "Rail project", "NotStarted", "08/21/13", "08/21/14"
A201, "Aviation project", "Started", "06/01/13", "06/30/14"
B301, "BikeTransit project","Started"03/01/13", "03/30/14""

FundingSources:
ID, ProjectID, Agency, Amount, Approved
1, R101, XYZ, $30,000, Y
2, R101, ABC, $50,000, N
3, A201, LML, $100,000, N


Result:
ProjectID, Name , Status ,Agency1 ,Amount1 ,Agency2 , Amount2,
R101 ,"Rail project" , NotStarted ,XYZ ,$30,000 ,ABC ,$50,000
A201 ,"Aviation project" , Started ,LML ,$100,000 , ,
B301 ,"BikeTransit " ,Started , , , ,


--Create Table Projects
CREATE TABLE [dbo].[PROJECTS](
[id] [int] NOT NULL,
[projectid] [nvarchar](50) NOT NULL,
[name] [nvarchar](50) NOT NULL,
[status] [nvarchar](50) NOT NULL,
[startdt] [datetime2](7) NOT NULL,
[enddt] [datetime2](7) NULL,
CONSTRAINT [PK_PROJECTS] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

-- create ProjectFunding table
CREATE TABLE [dbo].[ProjectFunding](
[id] [int] NOT NULL,
[projectid] [nvarchar](50) NOT NULL,
[agency] [nvarchar](50) NOT NULL,
[amount] [nvarchar](25) NOT NULL,
[approved] [nvarchar](5) NOT NULL,
CONSTRAINT [PK_ProjectFunding] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]


--insert data projects
insert into Projects values(1, 'R101', 'Rail project', 'NotStarted', '08/21/13', '08/21/14');
insert into Projects values(2, 'A201', 'Aviation project', 'Started', '06/01/13', '06/30/14');
insert into Projects values(3, 'B301', 'BikeTransit project','Started', '03/01/13', '03/30/14');

--insert data project funding
insert into ProjectFunding values(1,'R101', 'XYZ', '$30,000', 'Y');
insert into ProjectFunding values(2,'R101', 'ABC', '$50,000', 'N');
insert into ProjectFunding values(3,'A201', 'LML', '$100,000', 'N');



Appreciate any input.
Snigdha Vartak
Snigdha Vartak
Say Hey Kid
Say Hey Kid (704 reputation)Say Hey Kid (704 reputation)Say Hey Kid (704 reputation)Say Hey Kid (704 reputation)Say Hey Kid (704 reputation)Say Hey Kid (704 reputation)Say Hey Kid (704 reputation)Say Hey Kid (704 reputation)

Group: General Forum Members
Points: 704 Visits: 247
Try Pivot Query
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)

Group: General Forum Members
Points: 217146 Visits: 41991
What do you want to do if there are more than 2 funding sources?

Also, since you're new, have a look at the first link in my signature line below. You're almost there with what you posted. If you take it just one level higher, people will usually be a lot more helpful when it comes to code.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
super48
super48
Mr or Mrs. 500
Mr or Mrs. 500 (503 reputation)Mr or Mrs. 500 (503 reputation)Mr or Mrs. 500 (503 reputation)Mr or Mrs. 500 (503 reputation)Mr or Mrs. 500 (503 reputation)Mr or Mrs. 500 (503 reputation)Mr or Mrs. 500 (503 reputation)Mr or Mrs. 500 (503 reputation)

Group: General Forum Members
Points: 503 Visits: 530
ROUGH and lengthy query if you have more records ..
using some other method i think you can make it short
SELECT A.P,A.AGENCY1,A.AMOUNT1,B.AGENCY2,B.AMOUNT2 FROM

(select PROJECTID AS P, AGENCY AS AGENCY1,AMOUNT AS AMOUNT1
FROM [FundingSources]
WHERE AGENCY='XYZ') A

JOIN

(select PROJECTID AS P,AGENCY AS AGENCY2,AMOUNT AS AMOUNT2
FROM [FundingSources]
WHERE AGENCY='ABC')B ON(A.P=B.P)
RandomEvent
RandomEvent
SSCommitted
SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)

Group: General Forum Members
Points: 1545 Visits: 528
create a select statement for the funding table using a rownumber field so each project has a number based on the Project ID.

Then do a straight select statement joining the 2 tables with a left outer join based on the ID field and in the select add a case statement that corresponds to all the possible numbers within the rownumber.

Jason.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search