The continuing saga of Ian trying to learn DB2-SQL
18. CASE Statements
Are these even possible:
SELECT
CASE
WHEN v.id=1 THEN 'Hello';
WHEN v.id=2 THEN ', ';
WHEN v.id=3 THEN 'world';
ELSE '!'
END CASE AS foo
FROM (VALUES 1, 2, 3, 4) AS v(id)
Desired output
foo
========
Hello
,
world
!
Another example:
SELECT
CustomerID,
SUM(
CASE
WHEN OrderValue > 1000 THEN OrderAmount;
ELSE 0
END CASE) AS TotalOfBigOrders
FROM Orders
Another example:
SELECT
i.InfractionID,
CASE
WHEN i.EmployeeID IS NOT NULL THEN (Employees.EmployeeName);
WHEN i.PatronID IS NOT NULL THEN (Patrons.PatronName);
ELSE 'Unknown Participant';
END CASE AS PersonDoingTheInfaction
FROM Infactions i
LEFT JOIN Patrons
ON i.PatronID = Patrons.PatronID
LEFT JOIN Employees
ON i.EmployeeID = Employees.EmployeeID
or another desired technique of the above:
SELECT
i.InfactionID,
CASE
WHEN i.EmployeeID IS NOT NULL THEN (
SELECT EmployeeName
FROM Employees
WHERE Employees.EmployeeID = i.EmployeeID);
WHEN i.PatronID IS NOT NULL THEN (
SELECT Patronname
FROM Patrons
WHERE Patrons.PatronsID = i.PatronID);
ELSE 'Unknown participant';
END CASE AS PersonDoingTheInfraction
FROM Infractions i
Gert van der Kooij - 22 Mar 2006 22:58 GMT
> The continuing saga of Ian trying to learn DB2-SQL
You can learn a lot reading the SQL Cookbook from Greame Birchall,
google for DB2 SQL Cookbook and check the first link.
Ian Boyd - 23 Mar 2006 15:58 GMT
> You can learn a lot reading the SQL Cookbook from Greame Birchall,
> google for DB2 SQL Cookbook and check the first link.
Yeah yeah, i downloaded it. i'm getting to it! :)
Serge Rielau - 22 Mar 2006 23:08 GMT
> The continuing saga of Ian trying to learn DB2-SQL
>
[quoted text clipped - 4 lines]
> SELECT
> CASE
WHEN v.id=1 THEN 'Hello'
WHEN v.id=2 THEN ', '
WHEN v.id=3 THEN 'world'
> ELSE '!'
END AS foo
> FROM (VALUES 1, 2, 3, 4) AS v(id)
>
[quoted text clipped - 11 lines]
> SUM(
> CASE
WHEN OrderValue > 1000 THEN OrderAmount
> ELSE 0
END ) AS TotalOfBigOrders
> FROM Orders
>
[quoted text clipped - 3 lines]
> i.InfractionID,
> CASE
WHEN i.EmployeeID IS NOT NULL THEN (Employees.EmployeeName)
WHEN i.PatronID IS NOT NULL THEN (Patrons.PatronName)
> ELSE 'Unknown Participant'
END AS PersonDoingTheInfaction
> FROM Infactions i
> LEFT JOIN Patrons
[quoted text clipped - 10 lines]
> SELECT EmployeeName
> FROM Employees
WHERE Employees.EmployeeID = i.EmployeeID)
> WHEN i.PatronID IS NOT NULL THEN (
> SELECT Patronname
> FROM Patrons
WHERE Patrons.PatronsID = i.PatronID)
ELSE 'Unknown participant'
END AS PersonDoingTheInfraction
> FROM Infractions i
Works exactly the same in Oracle 9i and *squinthard* SQL Server 2000
Cheers
Serge

Signature
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Brian Tkatch - 23 Mar 2006 15:33 GMT
> The continuing saga of Ian trying to learn DB2-SQL
That would explain quite a bit. :P
> 18. CASE Statements
>
[quoted text clipped - 16 lines]
> world
> !
Drop the semi-colon after each clause, and the word "CASE" from "END
CASE"
SELECT
CASE
WHEN v.id=1 THEN 'Hello'
WHEN v.id=2 THEN ', '
WHEN v.id=3 THEN 'world'
ELSE '!'
END AS foo
FROM (VALUES 1, 2, 3, 4) AS v(id)
Also, is this particular case you can shorten it to:
SELECT
CASE v.id
WHEN 1 THEN 'Hello'
WHEN 2 THEN ', '
WHEN 3 THEN 'world'
ELSE '!'
END AS foo
FROM (VALUES 1, 2, 3, 4) AS v(id)
B.
Ian Boyd - 23 Mar 2006 16:07 GMT
> Drop the semi-colon after each clause, and the word "CASE" from "END
> CASE"
i see my problem now. The DB2 documentation has "END CASE" and semi-colons.
i should have known that semi-colons are mandatory sometimes, optional
sometimes and forbidden sometimes.
As for the "END CASE", i have no explanation.
http://tinyurl.com/fq5vn
Serge Rielau - 23 Mar 2006 16:38 GMT
>> Drop the semi-colon after each clause, and the word "CASE" from "END
>> CASE"
[quoted text clipped - 6 lines]
>
> http://tinyurl.com/fq5vn
Because what you are using here is a CASE EXPRESSION, not a CASE STATEMENT.
CASE EXPRESSION is part of an expression and returns a scalar.
Since DB2 documentation apparently isn't good enough here is the link in
SQL Server 2005 BOL:
http://msdn2.microsoft.com/en-us/library/ms181765(SQL.90).aspx
Here it is in DB2, just in case:
http://publib.boulder.ibm.com/infocenter/db2luw/v8/topic/com.ibm.db2.udb.doc/adm
in/r0000736.htm#casexp
Cheers
Serge

Signature
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Knut Stolze - 23 Mar 2006 21:13 GMT
>> The continuing saga of Ian trying to learn DB2-SQL
>
[quoted text clipped - 32 lines]
> END AS foo
> FROM (VALUES 1, 2, 3, 4) AS v(id)
You need an "ORDER BY id" at the end. Otherwise the order of the rows is
not guaranteed.

Signature
Knut Stolze
DB2 Information Integration Development
IBM Germany
Ian Boyd - 23 Mar 2006 21:51 GMT
> You need an "ORDER BY id" at the end. Otherwise the order of the rows is
> not guaranteed.
The order of VALUES(1,2,3,4,5) is not guaranteed?
Knut Stolze - 23 Mar 2006 22:40 GMT
>> You need an "ORDER BY id" at the end. Otherwise the order of the rows is
>> not guaranteed.
>
> The order of VALUES(1,2,3,4,5) is not guaranteed?
No, of course not. The only thing that guarantees the ordering of rows in a
result set is the ORDER BY clause. Everything else is just happenstance.

Signature
Knut Stolze
DB2 Information Integration Development
IBM Germany
Serge Rielau - 24 Mar 2006 02:39 GMT
>>> You need an "ORDER BY id" at the end. Otherwise the order of the rows is
>>> not guaranteed.
>> The order of VALUES(1,2,3,4,5) is not guaranteed?
>
> No, of course not. The only thing that guarantees the ordering of rows in a
> result set is the ORDER BY clause. Everything else is just happenstance.
There must be a German gene - nurture alone cannot do this.
Knut, I beg you. 1. Semester.. keep it simple. It was just an example.
Ian, read the thread "ORDER BY in view not working" over in
c.d.ms-sqlserver. It's been raging for weeks....
And let's pretend the topic of ORDER has never come up.
Serge

Signature
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Knut Stolze - 24 Mar 2006 10:48 GMT
>>>> You need an "ORDER BY id" at the end. Otherwise the order of the rows
>>>> is not guaranteed.
[quoted text clipped - 6 lines]
> There must be a German gene - nurture alone cannot do this.
> Knut, I beg you. 1. Semester.. keep it simple. It was just an example.
You're right. My apologies.

Signature
Knut Stolze
DB2 Information Integration Development
IBM Germany
Ian Boyd - 31 Mar 2006 21:14 GMT
> Ian, read the thread "ORDER BY in view not working" over in
> c.d.ms-sqlserver. It's been raging for weeks....
> And let's pretend the topic of ORDER has never come up.
i know how sets are by definition unordered lists. And i know
that order by does not work in a view. And you cannot guarantee
the return order of rows, even if the data is physically ordered by
some column.
But i thought, that even given all that, using
VALUES (1,2,3,4,5)
might,
just might,
happen to,
even though it isn't at all required to,
but internally the way it's done probably would,
but you still can't rely on it,
return values in the order:
1
2
3
4
5
But if it never will, okay then :)