Solved

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

Posted on 2007-11-15
9
551 Views
Last Modified: 2008-02-01
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
0
Comment
Question by:MIKE
9 Comments
 
LVL 8

Expert Comment

by:srafi78
ID: 20293109
Select ORDERID, COUNT(PAYID) FROM urTable WHERE PAYID = 2
GROUP BY ORDERID
0
 
LVL 8

Expert Comment

by:srafi78
ID: 20293114
Slight correction

Select ORDERID, COUNT(PAYID) 'NSFCOUNT' FROM urTable WHERE PAYID = 2
GROUP BY ORDERID
0
 
LVL 8

Expert Comment

by:Wikkard
ID: 20293128
try this
select orderid, count(orderid) as payid from sometable where somecriteria group by orderid
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 8

Expert Comment

by:Wikkard
ID: 20293132
lol should have checked for other answers first
0
 

Expert Comment

by:KOL999
ID: 20293141
SELECT ORDERID
           ,COUNT(*) AS NFSCOUNT
  FROM <TableName>
 WHERE PayId     = 2
 GROUP
       BY ORDERID
 ORDER
       BY ORDERID  
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 20293157
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.
0
 
LVL 17

Author Comment

by:MIKE
ID: 20293225
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


0
 
LVL 8

Expert Comment

by:srafi78
ID: 20293443
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
0
 
LVL 8

Accepted Solution

by:
srafi78 earned 500 total points
ID: 20293468
Select t1.ORDERID, COUNT(t1.PAYID) 'NSFCOUNT' FROM urTable t1 WHERE t1.PAYID = 2
AND  t1.DATE > (SELECT MAX(t2.DATE) FROM urTable t2 WHERE t2.PAYID = 1 AND t1.ORDERID = t2.ORDERID GROUP BY t2.ORDERID)
GROUP BY t1.ORDERID
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SSRS 2013 - Overlapping reports 2 21
sql server insert 12 30
Sql Server group by 10 27
SQL Query Help Top 1 and Distinct? 6 24
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to shrink a transaction log file down to a reasonable size.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question