Welcome to Dream.In.Code
Become an Expert!

Join 149,499 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,393 people online right now. Registration is fast and FREE... Join Now!




Conditional SQL statement for Access 2003

 
Reply to this topicStart new topic

Conditional SQL statement for Access 2003, I need help constructing conditional statement in SQL for MS Access

bamn68
28 Mar, 2007 - 10:07 PM
Post #1

New D.I.C Head
*

Joined: 28 Mar, 2007
Posts: 3


My Contributions
[font=Arial]Hi, how do i use conditional statement in sql? im using access 2003 as frontend and backend.

i would like to count the number of records in a table (ex. ENROLL) where there is only 1 instance of any value in a field (ex. STUDENT_NO) plus another condition where a given value (ex. 1) should exist in another field (ex. SECTION).

example

Table name: ENROLL

STUDENT_NO -- SECTION -FEE
16 ---------------- 1 ------ 75
16 ---------------- 2 ------ 75
17 ---------------- 1 ------ 100
18 ---------------- 2 ------ 125
19 ---------------- 1 ------ 75
19 ---------------- 2 ------ 75
19 ---------------- 3 ------ 75
20 ---------------- 1 ------ 150

Parameters:

COUNT
The number of records (count) in the table where there is only one instance of the STUDENT_NO value and the corresponding SECTION value is 1

VALUE
The sum of the FEE values in the table where there is only one instance of the STUDENT_NO
value and the corresponding SECTION value is 1


The answer should come out like this:

count = 2
value = 250

What would be the correct SQL statement for this situation that will be used in MS Access?
Thanks in advance for the help.

This post has been edited by bamn68: 28 Mar, 2007 - 10:12 PM
User is offlineProfile CardPM
+Quote Post

Trogdor
RE: Conditional SQL Statement For Access 2003
29 Mar, 2007 - 07:43 AM
Post #2

D.I.C Addict
Group Icon

Joined: 6 Oct, 2006
Posts: 549



Thanked: 4 times
Dream Kudos: 125
My Contributions
You might want to use the HAVING sql clause.
Look in the manual how it works.

User is offlineProfile CardPM
+Quote Post

bamn68
RE: Conditional SQL Statement For Access 2003
29 Mar, 2007 - 06:53 PM
Post #3

New D.I.C Head
*

Joined: 28 Mar, 2007
Posts: 3


My Contributions
I was able to get it to work with this query:

CODE
SELECT Count(*) AS SCount, Sum(FEE) AS SValue
FROM enrol
WHERE (((DCount("*","enrol","STUDENT_NO = " & [STUDENT_NO]))=1) AND ((FEE)=1));


but only when the data type for STUDENT_NO is numeric.
The data type of the field equivalent to STUDENT_NO that im working on is text and that's where i'm getting an error message.

I know this is very easy for you guys. I'm just a newbie and i'd like to get this program to work so i can pull the right report.

Any ideas how i could revise the query above if the data type of STUDENT_NO is text?

Appreciate all the help.
User is offlineProfile CardPM
+Quote Post

Trogdor
RE: Conditional SQL Statement For Access 2003
30 Mar, 2007 - 01:01 AM
Post #4

D.I.C Addict
Group Icon

Joined: 6 Oct, 2006
Posts: 549



Thanked: 4 times
Dream Kudos: 125
My Contributions
QUOTE(bamn68 @ 30 Mar, 2007 - 04:53 AM) *

I know this is very easy for you guys.


Would be, if it was SQL. But Access is not, it has very strange ways to do things, so i am afraid i can not help you further.
Again, i would retry going from the original and using a HAVING clause.
User is offlineProfile CardPM
+Quote Post

bamn68
RE: Conditional SQL Statement For Access 2003
30 Mar, 2007 - 03:47 AM
Post #5

New D.I.C Head
*

Joined: 28 Mar, 2007
Posts: 3


My Contributions
QUOTE(Trogdor @ 30 Mar, 2007 - 02:01 AM) *

QUOTE(bamn68 @ 30 Mar, 2007 - 04:53 AM) *

I know this is very easy for you guys.


Would be, if it was SQL. But Access is not, it has very strange ways to do things, so i am afraid i can not help you further.
Again, i would retry going from the original and using a HAVING clause.


I finally got the answer. I changed the DCount() into a Select Count subquery.
For the benefit of those who would like to know it, here's the statement:

CODE
SELECT Count(*) AS SCount, Sum(FEE) AS SValue
FROM Enrol
WHERE (Select Count("*") from Enrol as S where S.[STUDENT_NO] = Enrol.[STUDENT_NO])=1 AND [SECTION]=1;


It should work no matter whether [STUDENT_NO] is text or numeric.

Cheers!

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 06:23PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month