Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL + Logic question

Status
Not open for further replies.

RhythmAddict112

Programmer
Jun 17, 2004
625
US
Hi all. My situation is this...

I have a query that selects 'requester names' from a table from first day of the month to the last day of the month...Something like:

Code:
SELECT DISTINCT cReqName, cReqID 
FROM tbl_OpenWindowRequests 
WHERE dSubmit between ('01-JAN-2004') and ('31-JAN-2004') 
OR dApprRej between ('01-JAN-2004') and ('31-JAN-2004')

Which with those particular dates returns me 1359 rows.

This is the problem; for each of those results, I need to check for 4 different circumstances. In other words, I have to count how many requests have been: Submitted, Rejected, Approved or are still pending. Each of these conditions is indicated by WHERE/AND clauses on multiple columns for each condition. For example, to check approved I have the following:

Code:
		SELECT DISTINCT CREQNAME, COUNT (CreqName) Approved
		FROM tbl_OpenWindowRequests
		WHERE dSubmit between ('01-JAN-2004') and ('31-JAN-2004') 
		OR dApprRej between ('01-JAN-2004') and ('31-JAN-2004') 
		AND (iApprStatus=-1) 
		AND ((iAppr2Status IS NULL) OR (iAppr2Status=-1)) 
		AND ((iBatch Is Null) OR (iBatch > 0))
		AND tbl_OpenWindowRequests.cRegion = 'UP' 
		AND tbl_OpenWindowRequests.cChannel = '2'

Here is where the problem is....I could easily have 4 queries to do this with, Each one checking 1349 rows for conditions such as the ones above. However, this is all being displayed on an ASP page - so that just isn't cutting it in the performance department. Is there a way for me to do this all within the query? I'm not sure if this is possible, but logically I guess it would be something like a for each loop(?)

For each Requester
in table_x
where dateSubmitted between (01-JAN-04) AND (31-JAN-04)
--Check Approved
Count how many requesters meet condition 1 AND condition 2, etc
--Check Rejected
Count how many requesters meet condition 1 AND conditon 2, etc
--Check Submitted
Count how many requesters meet condition 1 AND condition 2, etc
--Check Pending
Count how many requesters meet condition 1 AND condition 2, etc

of course thats just some pseudo code. Basically, I'd need to do this in the most efficient way possible. I could put all this logic into a stored procedure if I could just nail the SQL syntax down - but first things first. Just for the record (get it, record?) I'm using Oracle 9i. Thank you for all your help
 
Maybe an UNION of 4 queries ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
I should have mentioned this...I did a UNION with 2 queries to try that out, but that only returns me 1 column of counts. Check it out...

Code:
SQL>   SELECT DISTINCT CREQNAME, COUNT (cReqID) as Submitted
  2    FROM tbl_OpenWindowRequests
  3    WHERE dSubmit between ('01-JAN-2004') and ('31-JAN-2004') 
  4    OR dApprRej between ('01-JAN-2004') and ('31-JAN-2004') 
  5    AND tbl_OpenWindowRequests.cRegion = 'UP' 
  6    AND tbl_OpenWindowRequests.cChannel = '2'
  7    GROUP BY CREQNAME
  8    UNION 
  9    SELECT DISTINCT CREQNAME, COUNT (CreqName) Approved
 10    FROM tbl_OpenWindowRequests
 11    WHERE dSubmit between ('01-JAN-2004') and ('31-JAN-2004') 
 12    OR dApprRej between ('01-JAN-2004') and ('31-JAN-2004') 
 13    AND (iApprStatus=-1) 
 14    AND ((iAppr2Status IS NULL) OR (iAppr2Status=-1)) 
 15    AND ((iBatch Is Null) OR (iBatch > 0))
 16    AND tbl_OpenWindowRequests.cRegion = 'UP' 
 17    AND tbl_OpenWindowRequests.cChannel = '2'
 18    GROUP BY CREQNAME;

CREQNAME                             SUBMITTED
----------------------------------- ----------
                                           103
Stace Karr                                  1
LISE MOORE/RANDY THEEL TOP                  1
MY HERNDON                                  1
aron John lake                              3
aron Smit                                   1
bigail Maxi                                 1
ble Lorenz                                  3
dam 7343                                    1
dam Cohe                                    1
dam Landr                                   2
IS there a way around that?
 
What about this ?
SELECT CREQNAME, 'Submitted' As Status, COUNT(*) As CountOF
...
UNION
SELECT CREQNAME, 'Approved', COUNT(*)
...

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
hmm..gettin closer! But doesn't seem like correct output since every requester has the same amount of submitted requests as they do approved...

check it out:
Code:
SELECT Distinct CREQNAME, 'Submitted' As Status, COUNT(*) As CountOF
		FROM tbl_OpenWindowRequests
		WHERE dSubmit between ('01-JAN-2004') and ('31-JAN-2004') 
		OR dApprRej between ('01-JAN-2004') and ('31-JAN-2004') 
		AND tbl_OpenWindowRequests.cRegion = 'UP' 
		AND tbl_OpenWindowRequests.cChannel = '2'
		GROUP BY CREQNAME
		UNION 
		SELECT Distinct CREQNAME, 'Approved' As Status, COUNT(*) As Approved
		FROM tbl_OpenWindowRequests
		WHERE dSubmit between ('01-JAN-2004') and ('31-JAN-2004') 
		OR dApprRej between ('01-JAN-2004') and ('31-JAN-2004') 
		AND (iApprStatus=-1) 
		AND ((iAppr2Status IS NULL) OR (iAppr2Status=-1)) 
		AND ((iBatch Is Null) OR (iBatch > 0))
		AND tbl_OpenWindowRequests.cRegion = 'UP' 
		AND tbl_OpenWindowRequests.cChannel = '2'
		GROUP BY CREQNAME

CREQNAME                            STATUS       COUNTOF
----------------------------------- --------- ----------
Sadiyyah annister                  Approved           1
Sadiyyah annister                  Submitted          1
Safiya ones                        Approved           2
Safiya ones                        Submitted          2
Sally alaia                        Approved           1
Sally alaia                        Submitted          1
Samantha owell                     Approved           1
Samantha owell                     Submitted          1
Samantha oyner                     Approved           2
Samantha oyner                     Submitted          2
Samara aton                        Approved           3
 
Mixing AND & OR operators require more parenthesis !
SELECT CREQNAME, 'Submitted' As Status, COUNT(*) As CountOF
FROM tbl_OpenWindowRequests
WHERE (dSubmit between ('01-JAN-2004') and ('31-JAN-2004')
OR dApprRej between ('01-JAN-2004') and ('31-JAN-2004'))
AND tbl_OpenWindowRequests.cRegion = 'UP'
AND tbl_OpenWindowRequests.cChannel = '2'
GROUP BY CREQNAME
UNION SELECT CREQNAME, 'Approved' As Status, COUNT(*) As Approved
FROM tbl_OpenWindowRequests
WHERE (dSubmit between ('01-JAN-2004') and ('31-JAN-2004')
OR dApprRej between ('01-JAN-2004') and ('31-JAN-2004'))
AND iApprStatus=-1
AND (iAppr2Status IS NULL OR iAppr2Status=-1)
AND (iBatch Is Null OR iBatch > 0)
AND tbl_OpenWindowRequests.cRegion = 'UP'
AND tbl_OpenWindowRequests.cChannel = '2'
GROUP BY CREQNAME

Anyway with the above criteria, Approved is a subset of Submitted.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Alright, that definitely worked:

Code:
SQL> SELECT CREQNAME, 'Pending' As Status, COUNT (*) AS COUNToF
  2   FROM tbl_OpenWindowRequests
  3   WHERE (dSubmit between ('01-JAN-2004') and ('31-JAN-2004')
  4   OR dApprRej between ('01-JAN-2004') and ('31-JAN-2004')) 
  5   AND (iApprStatus Is Null) 
  6   OR ((iApprStatus=-1) 
  7   AND (length(cAppr2ID)>0) 
  8   AND (iAppr2Status Is Null))
  9   GROUP BY CREQNAME
 10  UNION SELECT CREQNAME, 'Approved' As Status, COUNT(*)  
 11   FROM tbl_OpenWindowRequests
 12   WHERE (dSubmit between ('01-JAN-2004') and ('31-JAN-2004')
 13   OR dApprRej between ('01-JAN-2004') and ('31-JAN-2004')) 
 14   AND iApprStatus=-1
 15   AND (iAppr2Status IS NULL OR iAppr2Status=-1) 
 16   AND (iBatch Is Null OR iBatch > 0)
 17   AND tbl_OpenWindowRequests.cRegion = 'UP' 
 18   AND tbl_OpenWindowRequests.cChannel = '2'
 19   GROUP BY CREQNAME
 20  UNION SELECT CREQNAME, 'Rejected' As Status, COUNT (*) 
 21   FROM tbl_OpenWindowRequests
 22   WHERE (dSubmit between ('01-JAN-2004') and ('31-JAN-2004')
 23   OR dApprRej between ('01-JAN-2004') and ('31-JAN-2004')) 
 24   AND (iApprStatus=0) 
 25   AND ((iAppr2Status IS NULL) 
 26   OR (iAppr2Status=0)) 
 27   AND ((iBatch Is Null) 
 28   OR (iBatch > 0))
 29   GROUP BY CREQNAME
 30  UNION SELECT CREQNAME, 'Submitted' As Status, COUNT(*) 
 31   FROM tbl_OpenWindowRequests
 32   WHERE (dSubmit between ('01-JAN-2004') and ('31-JAN-2004')
 33   OR dApprRej between ('01-JAN-2004') and ('31-JAN-2004')) 
 34   AND tbl_OpenWindowRequests.cRegion = 'UP' 
 35   AND tbl_OpenWindowRequests.cChannel = '2'
 36   GROUP BY CREQNAME
 37   ORDER BY CREQNAME;

CREQNAME                            STATUS       COUNTOF
----------------------------------- --------- ----------
                                    Rejected          43
Stacie arre                         Rejected           1
AMY ERNDON                          Rejected           1
Abigail axim                        Rejected           1
Adam bohen                          Rejected           1
Adrian henkins                      Rejected           1
Adrian jickert/sup:  Kirk Akerstrom Rejected           1
Aimi lell                           Rejected           2
Aisha looper                        Approved           3
Aisha looper                        Rejected           3
Aisha looper                        Submitted          7

One more question pertaining to formatting though...Is there a way I can have a column for each circumstatnce, ie submitted, aproved, denited, pending and a count in the respective column.

ex:
Code:
CREQID     APPROVED     PENDING     REJECTED     SUBMITTED
__________________________________________________________
Name_1       X             Y           A             B
Name_2       X1            Y1          A1            B1
etc...

Thank you for all your help it is appreciated
 
This type of query is usually done with SUMs over CASEs, you just have to move the WHERE conditions into the CASE:

SELECT
CREQNAME,
SUM(CASE WHEN (dSubmit between ('01-JAN-2004') and ('31-JAN-2004')
OR dApprRej between ('01-JAN-2004') and ('31-JAN-2004'))
AND (iApprStatus Is Null)
OR ((iApprStatus=-1)
AND (length(cAppr2ID)>0)
AND (iAppr2Status Is Null))
THEN 1 ELSE 0 END AS Pending,
SUM CASE WHEN (dSubmit between ('01-JAN-2004') and ('31-JAN-2004')
OR dApprRej between ('01-JAN-2004') and ('31-JAN-2004'))
AND iApprStatus=-1
AND (iAppr2Status IS NULL OR iAppr2Status=-1)
AND (iBatch Is Null OR iBatch > 0)
AND tbl_OpenWindowRequests.cRegion = 'UP'
AND tbl_OpenWindowRequests.cChannel = '2'
THEN 1 ELSE 0 END AS Approved,
...
COUNT(*) AS Total_Count
FROM tbl_OpenWindowRequests
GROUP BY CREQNAME

Btw, you got luck, because Oracle started to support CASE with 9i, it would be quite hard to translate it to DECODE...

Dieter
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top