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 2005 Forums
 Transact-SQL (2005)
 Can I do this query?

Author  Topic 

Joshua.Zimmerman
Starting Member

3 Posts

Posted - 2010-09-27 : 11:53:38
Hello all. I know the basics of SQL, and have been using C# to manipulate one
of my queries for a while. I am wondering if the following output is possible
using only SQL. I will try to be as specific as I can.

Table1
EventID - int
EventName - varchar(50)

Table2
RequirementID - int
Requirement - varchar(50)

Table3
DataID - int
EventID - int
RequirementID - int
Who - varchar(50)

Example Data:

Table1:
1, Texas Fair
2, St Loius Parade

Table2:
1, Carnival Rides
2, Cotton Candy Stand

Table3
1, 1, 1, Bobs Carnival Rides
2, 1, 2, Saras Candy Store
3, 2, 2, Crystals Candy Surplus

Desired Output:
Grouped by Requirement on left hand column with following
columns being Events as header and who in field. Additionally,
some requirements might be filled with multiple vendors requiring
more rows. -- indicate white space.

Requirement-----------Texas Fair-----St Loius Parade
Carnival-Rides--------Bobs......--------------------
----------------------Another Vendor----------------
Cotton-Candy-Stand----Saras.....-----Crystals.......

Thanks for any help in advance.
Joshua

Vinnie881
Master Smack Fu Yak Hacker

1231 Posts

Posted - 2010-09-27 : 12:54:00
You want a pivot table. It's possible, but do a google search on T-SQL PIVOT, there will be numerous articles that explain in depth. If you still have issues re-ask if you run into issues.


Success is 10% Intelligence, 70% Determination, and 22% Stupidity.
\_/ _/ _/\_/ _/\_/ _/ _/- 881
Go to Top of Page

Ifor
Aged Yak Warrior

700 Posts

Posted - 2010-09-27 : 13:03:40
Here is an example:

-- *** Test Data ***
CREATE TABLE #Events
(
EventID int NOT NULL
,EventName varchar(50) NOT NULL
)
INSERT INTO #Events
SELECT 1, 'Texas Fair'
UNION ALL SELECT 2, 'St Loius Parade'

CREATE TABLE #Reqirements
(
RequirementID int NOT NULL
,Requirement varchar(50) NOT NULL
)
INSERT INTO #Reqirements
SELECT 1, 'Carnival Rides'
UNION ALL SELECT 2, 'Cotton Candy Stand'

CREATE TABLE #EventRequirements
(
EventID int NOT NULL
,RequirementID int NOT NULL
,Who varchar(50) NOT NULL
)
INSERT INTO #EventRequirements
SELECT 1, 1, 'Bob''s Carnival Rides'
UNION ALL SELECT 1, 2, 'Sara''s Candy Store'
UNION ALL SELECT 2, 2, 'Crystal''s Candy Surplus'
-- *** End Test Data ***


SELECT Requirement, [Texas Fair], [St Loius Parade]
FROM
(
SELECT X.Who, R.Requirement, E.EventName
FROM #EventRequirements X
JOIN #Reqirements R
ON X.RequirementID = R.RequirementID
JOIN #Events E
ON X.EventID = E.EventID
) AS P
PIVOT
(
MIN(Who)
FOR EventName IN ([Texas Fair], [St Loius Parade])
) AS pvt

Go to Top of Page

Joshua.Zimmerman
Starting Member

3 Posts

Posted - 2010-09-27 : 14:33:02
Thanks, I will give these a shot.
Go to Top of Page

Joshua.Zimmerman
Starting Member

3 Posts

Posted - 2010-09-27 : 15:19:22
Two questions.

1. Can I use a select statement to populate the FOR..IN of the
pivot?

FOR EventName IN (SELECT EventName FROM Table1)
Instead of...
FOR EventName IN ([Texas Fair], [St Loius Parade])

2. Using MIN(Who) appears to have elimated additional vendors.
Can I add multiple lines of "Who"s under the correct column if mutiple "Who"s appeared for the same requirement and eventname?

Thanks for the suggestions and I will keep searching.

Josh
Go to Top of Page
   

- Advertisement -