Link to home
Start Free TrialLog in
Avatar of Marcus Aurelius
Marcus AureliusFlag for United States of America

asked on

How to count data in a column and display a text status comment..?

Experts:

I have this in my table:

ORDERID    PAYID  
1000              1
1000              2
1000              2
1000              2
1001              1
1001              2
1001              2
1002              2
1003              2
1003              2

The 1's represent APPROVED,....the 2's mean NSF or ....non sufficient funds. We didn't get paid.....

I want to achieve this:

ORDERID     NSFCOUNT
1000                    3
1001                    2
1002                    1
1003                    2

How can I do this...?
Thanks
MikeV
Avatar of srafi78
srafi78
Flag of United States of America image

Select ORDERID, COUNT(PAYID) FROM urTable WHERE PAYID = 2
GROUP BY ORDERID
Slight correction

Select ORDERID, COUNT(PAYID) 'NSFCOUNT' FROM urTable WHERE PAYID = 2
GROUP BY ORDERID
try this
select orderid, count(orderid) as payid from sometable where somecriteria group by orderid
lol should have checked for other answers first
Avatar of KOL999
KOL999

SELECT ORDERID
           ,COUNT(*) AS NFSCOUNT
  FROM <TableName>
 WHERE PayId     = 2
 GROUP
       BY ORDERID
 ORDER
       BY ORDERID  
Avatar of Scott Pletcher
It depends -- if you don't need to show a zero count, that is, you want to list them *only* those rows that had at least one NSF, then you can use srafi78's method.
Avatar of Marcus Aurelius

ASKER

SORRY.....I "over-simplified" it too much.........see below:

I have this in my table:

ORDERID    PAYID      DATE
1000              1           01/01/07
1000              2           01/02/07
1000              1           01/03/07
1000              2           01/04/07
1000              2           01/05/07
1000              2           01/06/07
1001              1           01/01/07
1001              2           01/02/07
1001              2           01/03/07
1001              1           01/04/07
1001              2           01/05/07
1001              2           01/06/07
1002              2           01/01/07
1003              2           01/01/07
1003              2           01/02/07
1003              1           01/03/07
1003              1           01/04/07
1003              2           01/05/07

The 1's represent APPROVED,....the 2's mean NSF or ....non sufficient funds. We didn't get paid.....

I'm only concerned with the LATEST or MOST RECENT "2's"...I need to be able to COUNT the 2's starting fromt he most RECENT one BACKWARDS....up to the LAST PAYMENT RECIEVED. We will be calling customers based on the Number of NSF's received so I only need to count the most recent series.

I want to achieve this:

ORDERID     NSFCOUNT
1000                    3
1001                    2
1002                    1
1003                    1

Hope it makes sense..the key is that IF they make a payment to us...represented by a '1' then this would reset their NSF COUNT. We are only concerned about their LATEST batch or series of NSF's and being able to COUNT them up.

THANK YOU for your expertise!!

MikeV


Select ORDERID, COUNT(PAYID) 'NSFCOUNT' FROM urTable t1 WHERE PAYID = 2
AND  t1.DATE > (SELECT MAX(DATE) FROM urTable WHERE PAYID = 1 AND t1.ORDERID = t2.ORDERID GROUP BY ORDERID)
GROUP BY ORDERID
ASKER CERTIFIED SOLUTION
Avatar of srafi78
srafi78
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial