dbTalk Databases Forums  

Help with Query

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss Help with Query in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
FCB
 
Posts: n/a

Default Help with Query - 08-19-2011 , 03:51 PM






Have written basic queries with SQL for a while now but have been
given a project that requires me to step it up a notch. Any help would
be appreciated.

I have a table that lists customer records; one row per customer but
there can be more than one customer per account.

For example

Account Customer First Last Status
1 1 James Joyce
Active
1 2 Nora Barnacle Active
2 1 Fred Flintstone Active
3 1 Barney Rubble
Inactive
3 2 Betty Rubble Active

I'm trying to create a query that will choose the first active
customer only. Is there a way to use the min() function to loop
through each account number and choose the lowest customer per
account? Or is there another function that can be used.

Thanks again,

Dave

Reply With Quote
  #2  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: Help with Query - 08-19-2011 , 04:52 PM






FCB (dave.white19 (AT) gmail (DOT) com) writes:
Quote:
For example

Account Customer First Last Status
1 1 James Joyce
Active
1 2 Nora Barnacle Active
2 1 Fred Flintstone Active
3 1 Barney Rubble
Inactive
3 2 Betty Rubble Active

I'm trying to create a query that will choose the first active
customer only. Is there a way to use the min() function to loop
through each account number and choose the lowest customer per
account? Or is there another function that can be used.
WITH numbered AS (
SELECT Account, First, Last, Status,
rowno = row_number()
OVER (PARTITION BY Account ORDER BY Customer)
FROM tbl
)
SELECT Account, First, Last, Status
FROM tbl
WHERE rowno = 1

The row_number() function orders the rows in the query, starting on one
for each new value in the PARTITION BY clause. The WITH thing is a
CTE, Common Table Expression, which is kind of view that exists only
for the query.

By getting the rows with row number = 1, you can easily get the first
row and all that goes with it.


--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx

Reply With Quote
  #3  
Old   
FCB
 
Posts: n/a

Default Re: Help with Query - 08-20-2011 , 11:19 AM



On Aug 19, 5:52*pm, Erland Sommarskog <esq... (AT) sommarskog (DOT) se> wrote:
Quote:
FCB (dave.whit... (AT) gmail (DOT) com) writes:
For example

Account * * Customer * * *First * * *Last * * * * * * * * Status
1 * * * * * * * 1 * * * * * * * * *James * Joyce
Active
1 * * * * * * * 2 * * * * * * * * *Nora* * *Barnacle * * * * *Active
2 * * * * * * * 1 * * * * * * * * *Fred* * *Flintstone * * * * Active
3 * * * * * * * 1 * * * * * * * * *Barney*Rubble
Inactive
3 * * * * * * * 2 * * * * * * * * *Betty * *Rubble* * * * * * Active

I'm trying to create a query that will choose the first active
customer only. Is there a way to use the min() function to loop
through each account number and choose the lowest customer per
account? Or is there another function that can be used.

WITH numbered AS (
* * SELECT Account, First, Last, Status,
* * * * * *rowno = row_number()
* * * * * * * * * * OVER (PARTITION BY Account ORDER BY Customer)
* * FROM * tbl
)
SELECT Account, First, Last, Status
FROM * tbl
WHERE *rowno = 1

The row_number() function orders the rows in the query, starting on one
for each new value in the PARTITION BY clause. The WITH thing is a
CTE, Common Table Expression, which is kind of view that exists only
for the query.

By getting the rows with row number = 1, you can easily get the first
row and all that goes with it.

--
Erland Sommarskog, SQL Server MVP, esq... (AT) sommarskog (DOT) se

Links for SQL Server Books Online:
SQL 2008:http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005:http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
Erland,

Thanks very much for your help.

Dave

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.