473,486 Members | 2,181 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

SQL's equivalent of CASE WHEN ... END CASE

Hi,

Here's what I'd like to do: table (Orders) has fields of Processed,
Paid (both of boolean yes|no) etc, I want to return a count of of
Total number of Orders, Number of Processed vs. Number of unProcessed
(processed value = 0), and Number of Paid vs. Number of unPaid (paid
value = 0), I can easily write a query for SQL Server with Case when
.... end case statement,
I just learned that Access (don't know above which version it
supports) has something similar SELECT CASE ... END CASE, but the code
like this won't do the job,

Select count(*) as OrderTotal,
Select Case(processed)
Case = 1 select processed as pYES
Case = 0 select processed as pNO
End Select
,
count(pYes) as processedYES, count(pNO) as processedNO
From Orders o, OrderDetail od
Where o.orderID = od.orderID
....

A variant like this

Select count(*) as OrderTotal,
Case(processed)
Case = 1 select processed as pYES
Case = 0 select processed as pNO
End Case
,
count(pYes) as processedYES, count(pNO) as processedNO
From Orders o, OrderDetail od
Where o.orderID = od.orderID
....

won't work neither. So, how exactly does SELECT CASE ... END SELECT
work in Access?

TIA
Nov 12 '05 #1
5 4300
DaaaDaaa wrote:
Hi,

Here's what I'd like to do: table (Orders) has fields of Processed,
Paid (both of boolean yes|no) etc, I want to return a count of of
Total number of Orders, Number of Processed vs. Number of unProcessed
(processed value = 0), and Number of Paid vs. Number of unPaid (paid
value = 0), I can easily write a query for SQL Server with Case when
... end case statement,
I just learned that Access (don't know above which version it
supports) has something similar SELECT CASE ... END CASE, but the code
like this won't do the job,

Select count(*) as OrderTotal,
Select Case(processed)
Case = 1 select processed as pYES
Case = 0 select processed as pNO
End Select
,
count(pYes) as processedYES, count(pNO) as processedNO
From Orders o, OrderDetail od
Where o.orderID = od.orderID
...

A variant like this

Select count(*) as OrderTotal,
Case(processed)
Case = 1 select processed as pYES
Case = 0 select processed as pNO
End Case
,
count(pYes) as processedYES, count(pNO) as processedNO
From Orders o, OrderDetail od
Where o.orderID = od.orderID
...

won't work neither. So, how exactly does SELECT CASE ... END SELECT
work in Access?

TIA


I might do something like:
IIf([BooleanField],1,0) As YesItIs
IIf(Not [BooleanField],1,0) As NoItsNot

You could also do
Abs(True) As YesItIs
Abs(Not False) As NoItsNot

Count isn't the best in this situation. I use Sum.

Nov 12 '05 #2
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

You could use the IIf() & COUNT() functions like this:

SELECT
COUNT(IIf(processed=1,1) As Processed,
COUNT(IIf(processed=0,1) As Not_Processed
FROM ...

Count() doesn't count NULL values. Using the syntax IIf(expression,true
evaluation) will return NULL when the expression evaluates to false.
Therefore, the surrounding Count() will only count when the expression
evaluates to true.

--
MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)

-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBQJF0qYechKqOuFEgEQJ0dgCfZhPTPp4UYxtq77afq1zmh/kKYkIAnjYq
l9h1e+dMYwnh/BjCV47SbqLp
=ybru
-----END PGP SIGNATURE-----
DaaaDaaa wrote:
Hi,

Here's what I'd like to do: table (Orders) has fields of Processed,
Paid (both of boolean yes|no) etc, I want to return a count of of
Total number of Orders, Number of Processed vs. Number of unProcessed
(processed value = 0), and Number of Paid vs. Number of unPaid (paid
value = 0), I can easily write a query for SQL Server with Case when
... end case statement,
I just learned that Access (don't know above which version it
supports) has something similar SELECT CASE ... END CASE, but the code
like this won't do the job,

Select count(*) as OrderTotal,
Select Case(processed)
Case = 1 select processed as pYES
Case = 0 select processed as pNO
End Select
,
count(pYes) as processedYES, count(pNO) as processedNO
From Orders o, OrderDetail od
Where o.orderID = od.orderID
...

A variant like this

Select count(*) as OrderTotal,
Case(processed)
Case = 1 select processed as pYES
Case = 0 select processed as pNO
End Case
,
count(pYes) as processedYES, count(pNO) as processedNO
From Orders o, OrderDetail od
Where o.orderID = od.orderID
...

won't work neither. So, how exactly does SELECT CASE ... END SELECT
work in Access?

TIA


Nov 12 '05 #3
Goto Access Help and look up the IIF function (Immediate If).

Select IIF(Processed = 0, Processed, somethingelse) As Processed from
tbl1

You can also nest the IIF statements. Jet sql doesn't have as much
functionality as Transact Sql. But the workaround for the limitations
are to use either Jet Sql Functions (like IIF) or use VBA to make up
UDF's that you can embed in your Jet Sql Statements (UDF's in VBA way
easier than in Sql Server).

Rich

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 12 '05 #4
For multiple cases, the functional equivalent are the
SWITCH and CHOOSE functions.

select SWITCH(0,[p0],1,[p1],2,[p2],8,[pEight]) as processed

(david)

"DaaaDaaa" <da**@rock.com> wrote in message
news:21*************************@posting.google.co m...
Hi,

Here's what I'd like to do: table (Orders) has fields of Processed,
Paid (both of boolean yes|no) etc, I want to return a count of of
Total number of Orders, Number of Processed vs. Number of unProcessed
(processed value = 0), and Number of Paid vs. Number of unPaid (paid
value = 0), I can easily write a query for SQL Server with Case when
... end case statement,
I just learned that Access (don't know above which version it
supports) has something similar SELECT CASE ... END CASE, but the code
like this won't do the job,

Select count(*) as OrderTotal,
Select Case(processed)
Case = 1 select processed as pYES
Case = 0 select processed as pNO
End Select
,
count(pYes) as processedYES, count(pNO) as processedNO
From Orders o, OrderDetail od
Where o.orderID = od.orderID
...

A variant like this

Select count(*) as OrderTotal,
Case(processed)
Case = 1 select processed as pYES
Case = 0 select processed as pNO
End Case
,
count(pYes) as processedYES, count(pNO) as processedNO
From Orders o, OrderDetail od
Where o.orderID = od.orderID
...

won't work neither. So, how exactly does SELECT CASE ... END SELECT
work in Access?

TIA

Nov 12 '05 #5
Salad, it's delicious :) = it works, and everyone else, thanks.

Salad <oi*@vinegar.com> wrote in message news:<HW******************@newsread1.news.pas.eart hlink.net>...
DaaaDaaa wrote:
Hi,

Here's what I'd like to do: table (Orders) has fields of Processed,
Paid (both of boolean yes|no) etc, I want to return a count of of
Total number of Orders, Number of Processed vs. Number of unProcessed
(processed value = 0), and Number of Paid vs. Number of unPaid (paid
value = 0), I can easily write a query for SQL Server with Case when
... end case statement,
I just learned that Access (don't know above which version it
supports) has something similar SELECT CASE ... END CASE, but the code
like this won't do the job,

Select count(*) as OrderTotal,
Select Case(processed)
Case = 1 select processed as pYES
Case = 0 select processed as pNO
End Select
,
count(pYes) as processedYES, count(pNO) as processedNO
From Orders o, OrderDetail od
Where o.orderID = od.orderID
...

A variant like this

Select count(*) as OrderTotal,
Case(processed)
Case = 1 select processed as pYES
Case = 0 select processed as pNO
End Case
,
count(pYes) as processedYES, count(pNO) as processedNO
From Orders o, OrderDetail od
Where o.orderID = od.orderID
...

won't work neither. So, how exactly does SELECT CASE ... END SELECT
work in Access?

TIA


I might do something like:
IIf([BooleanField],1,0) As YesItIs
IIf(Not [BooleanField],1,0) As NoItsNot

You could also do
Abs(True) As YesItIs
Abs(Not False) As NoItsNot

Count isn't the best in this situation. I use Sum.

Nov 12 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
1699
by: MC | last post by:
I'm trying to use ASP pages generated in Frontpage to update an SQL server database. I can view information from the database but cannot update, I just get a message saying an error has occured. ...
13
3018
by: Jeager | last post by:
Why is it, Microsoft manage to write operating systems and office applications with every bell and whistle facility known to man. Yet, even after years and years of development they still cannot...
10
111243
by: N | last post by:
What is the function in SQL that works like DECODE in Oracle?" Thanks, N
10
1889
by: JMorrell | last post by:
First post to this community so am not sure if this is the correct place. Here goes. I have a MS Access db that keeps track of employees sick and annual leave balances. In it, I have a report,...
10
4416
by: Jean-David Beyer | last post by:
I have some programs running on Red Hat Linux 7.3 working with IBM DB2 V6.1 (with all the FixPacks) on my old machine. I have just installed IBM DB2 V8.1 on this (new) machine running Red Hat...
5
2455
by: DaaaDaaa | last post by:
Hi, Here's what I'd like to do: table (Orders) has fields of Processed, Paid (both of boolean yes|no) etc, I want to return a count of of Total number of Orders, Number of Processed vs. Number...
3
8359
by: Will Chamberlain | last post by:
I have looked around and found the equivalent for IIF (Access) to be a SELECT CASE in SQL. I have tried this with no success. I am also looking for the equivalent of MAX and have had no luck. The...
138
59061
by: Ian Boyd | last post by:
i've been thrown into a pit with DB2 and have to start writing things such as tables, indexes, stored procedures, triggers, etc. The online reference is only so helpful. The two pdf manuals are...
7
7353
by: chettiar | last post by:
Hi, I am trying to migrate SQL Server into DB2. In SQL Server there is code like: SET DATEFIRST 6 SELECT DATEPART(WEEK, '2006-01-31') What could be the equivalent DB2 code for this.
0
6967
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7132
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7180
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6846
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5439
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4870
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4564
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3076
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3071
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.