473,397 Members | 2,028 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

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 2453
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
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. ...
2
by: Dmitry Bond. | last post by:
Hello All. Currently we are porting some software from NSK (HP Hon-Stop) SQL to Oracle and I faced with the following problem... The SQL statement: select * from BOM where ordnr = :1 where...
13
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
by: N | last post by:
What is the function in SQL that works like DECODE in Oracle?" Thanks, N
10
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
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
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
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
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
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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
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
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,...
0
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...

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.