Discussion:
another query question
(too old to reply)
mcnewsxp
2012-07-16 22:19:48 UTC
Permalink
how to select a value called EVENT_NAME from a table then if that value equals 'GROUP SESSION' you must select a value called GROUP_NAME from another table which will then be used as EVENT_NAME all in the same query/stored proc?

tia,
mcnewsxp
Bob Barrows
2012-07-16 23:37:56 UTC
Permalink
Post by mcnewsxp
how to select a value called EVENT_NAME from a table then if that
value equals 'GROUP SESSION' you must select a value called
GROUP_NAME from another table which will then be used as EVENT_NAME
all in the same query/stored proc?
That question makes no sense. Show us a few rows of sample data, fllowed by
the results you want. Don't describe it again until you SHOW it to us.
mcnewsxp
2012-07-17 00:45:39 UTC
Permalink
join [event] to [event_type]
[event] table joins [event_type] on [event_type_id]
[event_type] table has [event_name]
[event_name] value might equal "GROUP SESSION"
when it does then the [event_name] needs to come from the [groups] table
[groups] table contains [GROUP_NAME]

i am trying to build a row that has
EVENT_ID, EVENT_NAME, EVENT_BEGIN
Bob Barrows
2012-07-17 01:08:18 UTC
Permalink
Post by mcnewsxp
join [event] to [event_type]
[event] table joins [event_type] on [event_type_id]
[event_type] table has [event_name]
[event_name] value might equal "GROUP SESSION"
when it does then the [event_name] needs to come from the [groups]
table [groups] table contains [GROUP_NAME]
i am trying to build a row that has
EVENT_ID, EVENT_NAME, EVENT_BEGIN
I just barely get the gist this time. It would have been so much easier if
you had shown sample data as I requested.
However, if I do understand this correctly, my suggestion is to use a CASE
expression.

SELECT
CASE event_name
WHEN "GROUP SESSION" THEN group_name
ELSE event_name END AS EVENT_NAME
FROM ...
Erland Sommarskog
2012-07-17 09:09:36 UTC
Permalink
Post by mcnewsxp
join [event] to [event_type]
[event] table joins [event_type] on [event_type_id]
[event_type] table has [event_name]
[event_name] value might equal "GROUP SESSION"
when it does then the [event_name] needs to come from the [groups] table
[groups] table contains [GROUP_NAME]
i am trying to build a row that has
EVENT_ID, EVENT_NAME, EVENT_BEGIN
CASE WHEN event_type.event_name = 'GROUP SESSION'
THEN groups.GROUP_NAME
ELSE event_type.event_name
END
--
Erland Sommarskog, SQL Server MVP, ***@sommarskog.se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
mcnewsxp
2012-07-17 19:00:19 UTC
Permalink
> join [event] to [event_type]
> [event] table joins [event_type] on [event_type_id]
> [event_type] table has [event_name]
> [event_name] value might equal "GROUP SESSION"
> when it does then the [event_name] needs to come from the [groups] table
> [groups] table contains [GROUP_NAME]
>
> i am trying to build a row that has
> EVENT_ID, EVENT_NAME, EVENT_BEGIN
CASE WHEN event_type.event_name = 'GROUP SESSION'
THEN groups.GROUP_NAME
ELSE event_type.event_name
END
--
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
thanks erland. worked perfectly.

Continue reading on narkive:
Loading...