Solved

Getting a conditional counter into a SQL select statement

Posted on 2010-11-08
2
383 Views
Last Modified: 2012-05-10
I have a select statement that produces these results:

item_no             seq_no            demand                group
123                    5                      P                          0
234                   10                     null                       0
345                   15                     null                       0
456                    20                    P                          0
567                    25                    null                       0

The query currently looks like this:
select             item_no,seq_no,demand,0
from                myTable
order by         seq_no

What I want the statement to do is that whenever it sees a record where demand = "P", increment the group number so that the result set looks like this:

item_no             seq_no            demand                group
123                    5                      P                          10
234                   10                     null                       10
345                   15                     null                       10
456                    20                    P                          20
567                    25                    null                       20

(group does not need to be in increments of 10; it just needs to be unique)

I eventually need to make this into a view.

Can this be done in a view?
0
Comment
Question by:g_johnson
2 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 34086640
is seq_no the "sequential" field for the data, aka unique, and the "order by" of your query?if yes:
select t.*
   , (select count(*) from yourtable i where i.seq_no <= t.seq_no and i.demand = 'P' ) group
  from yourtable
order by seq_no

Open in new window

0
 
LVL 4

Author Closing Comment

by:g_johnson
ID: 34086747
Thank you!
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

832 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