troubleshooting Question

SQL Command (Sub select statement?)

Avatar of LeifHurst
LeifHurst asked on
Microsoft SQL ServerDB Reporting ToolsMicrosoft SQL Server 2008
3 Comments1 Solution476 ViewsLast Modified:
I'm still a novice with SQL queries but I'm needing assistance with a query that is giving me some trouble for a report.

Here's some sample data

Customer | Transaction | TranDate | InvoiceAmt
1 NBS 01/01/2011 1000
1 UPD 02/01/2011 500
1 UPD 03/01/2011 100
1 UPD 04/01/2011 200
1 UPD 05/01/2011 100
2 RWL 01/01/2011 800
2 UPD 02/01/2011 350
2 CNL 03/01/2011 400
3 RWL 01/01/2011 500
3 NBS 02/01/2011 500

What I'm needing to do is to query all rows where the customers with the first appearance or oldest Transactions = NBS. I don't need the NBS row by itself, I need the resultant set where the first row is equal to NBS and then all other rows for that customer as well.

In the example below the query would return all rows for customer #1 only because the oldest entry is NBS.

Can I do this with a "sub select" statement in my query (not sure if this is the right term)...

(this is rough but just to convey the idea of what I'm trying to get)
Select Customer, Transaction,  TranDate, InvoiceAmt
From Customer_Table
       Select First(transaction) = 'NBS'
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 3 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros