SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


SQL Server Over Partition


SQL Server Over Partition

Author
Message
Sandra D
Sandra D
SSC-Enthusiastic
SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)

Group: General Forum Members
Points: 120 Visits: 68

I have added the code that was missing. Column A is not a dollar figure but a limit payed out. When column A is 0 it indicates a limit removed or a limit not removed. In the example given there is no negative limits.
The calculations are flags when to select records. when column b and c are 1 select the record. In essence select the first record when the TT value changes. In the example given the record is not being selected because the value is not reset to 1. this occurred because there are 2 TT: C within the PI: 2800 and m: 4. In the example given there are no consecutive TT and therefore all records need to be selected. A value is cancelled by a TT: C. A value is charged by TT: P, R.




sum (case when A <> 0 then 1  else 0 end) 

over (PARTITION BY A.PI,A.M, TT )

order by A.date) as b




--Counts zero records for column A
select A.PI, A.m, A.Date, TT, A.A
sum (case when A.A <> 0 then 0 else 1 end) --counts zero0
Over (PARTITION BY A.PI, A.m, A.TT order by A.Date) as a,

--Counts records with values in Column A
--The counts need to reset when TT changes.
sum (case when A.A <> 0 then 1 else 0 end)
Over (PARTITION BY A.PI, A.m, A.TT order by A.Date) as b,


--Counts records with values and determines if the record is a negative transaction
--The only difference in column c is when A has a 0. it needs to determine if a negative --transaction or not a negative transaction
sum (case when A.A <> 0 then 1 when A.A Issue is = 0 and TT = 'E' then 1 else 0 end)

Over (PARTITION BY A.PI, A.m, A.TT order by A.Date) as c

In the spreadsheet I need the columns j,k,l getting f,g,h note if tt changes set to 1


Attachments
Book16.xlsx (19 views, 9.00 KB)
pietlinden
pietlinden
SSC-Forever
SSC-Forever (49K reputation)SSC-Forever (49K reputation)SSC-Forever (49K reputation)SSC-Forever (49K reputation)SSC-Forever (49K reputation)SSC-Forever (49K reputation)SSC-Forever (49K reputation)SSC-Forever (49K reputation)

Group: General Forum Members
Points: 49695 Visits: 16182
Welcome aboard!
since you appear to be new here, you owe it to yourself to read this article:

Forum Etiquette: How to post data/code on a forum to get the best help

Help us help you by including all the information we need to help - the article explains how to do that. Many people here are leery of downloading documents to replicate your setup... so post T-SQL here instead. Then we can help you.

Thanks!



Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search