SQL Question

Status
Not open for further replies.

JHS

Ars Tribunus Militum
2,742
Subscriptor++
Let's say I've got three tables -

<div style="margin-left:20px">
Requests:

RequestID

RequestName



Categories:

CategoryID

CategoryName



Requests_Categories:

RequestID

CategoryID

</div>


It's easy to write a query that joins the tables and returns a row for each Request/Category relationship. Is

it possible, however, to create a view or query that will return one record for each request with a column for

each category?


So if you had five categories (A - E), and a request fell into three of those categories, the result set would

look something like this:

<table>
<tr

style="font-weight:bold"><td>RequestID</td><td>RequestName</td><td>A</td><td>B</td><td>C</td><td>D</td><td>E</t

d></tr>
<tr><Td>1</td><td>MyRequest</td><td>Y</td><td>N</td><td>Y</td><td>Y</td><td>N</td></tr>
</table>


Ideally the query would dynamically create the columns for categories. So if another category was added the

query wouldn't have to be changed.


Any help would be appreciated.</td>
 

Aeirould

Ars Tribunus Angusticlavius
7,535
Subscriptor
In straight SQL I can't think of a way to make it dynamic, but you could use the Case construction to do it with known values, sorta like:

Select y.RequestID, y.RequestName, Max(tA) As A, Max(tB) As B From
(Select RequestID, (Case CategoryID When 'A' Then 'Y' Else 'N' end) As tA, (Case CategoryID When 'B' Then 'Y' Else 'N' end) as tB From Requests_Categories) As z Inner Join Requests as y on y.RequestID = z.RequestID Group By y.RequestID

IIRC, 'Y' will be "Max" over 'N' since it's a "higher" letter.
 

Malyk

Ars Tribunus Angusticlavius
8,558
Subscriptor
yes you can do that.

Try something like

select request.*
'Category1' = (select name from category where category_id = request_category.category_id)
'Category2' = (select name from category where category_id = request_category.category_id)
'Category3' = ...
inner join request_category on request.request_id = request_category.request_id

Something like that.

Actually, I think that will give you the opposite of what you want. Try switching the two tables (request and category) around in the above code and give it a try.

Mike

PS. I'm in a hurry at the moment, but I definately know how to do this. If the above doesn't work let me know and I'll figure it out after my meeting. (A 4:30 meeting...WTF were they thinking?????)
 

Handor

Ars Tribunus Militum
2,513
Malyk: That's the easy, but not sufficient, way to do it. That will pull the query at a single snapshot of the category list, but if he adds a new category to the category table, it will not be automatically reflected in the query. It sounds like he's not willing to use an ASP-solution which first grabs the category list and then loops in order to build SQL like your example demonstrates, so I was thinking I'd construct a T-SQL solution that he could use as a stored procedure when I get a moment. I did something exactly like this once, so I'm fairly confident I can do it when I get a little time, but since I'm lazy I think I'll just dig out the one I did when I can.
 
Status
Not open for further replies.