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
Valued Member
Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)

Group: General Forum Members
Points: 56 Visits: 68
Notes:
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.

--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


Attachments
Book16.xlsx (10 views, 9.00 KB)
GilaMonster
GilaMonster
SSC Guru
SSC Guru (673K reputation)SSC Guru (673K reputation)SSC Guru (673K reputation)SSC Guru (673K reputation)SSC Guru (673K reputation)SSC Guru (673K reputation)SSC Guru (673K reputation)SSC Guru (673K reputation)

Group: General Forum Members
Points: 673929 Visits: 48433
As Piet pointed out in your previous thread, people in general are not too keen on downloading excel spreadsheets. Please can you list your sample data and expected results as per http://www.sqlservercentral.com/articles/Best+Practices/61537/

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


Sandra D
Sandra D
Valued Member
Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)

Group: General Forum Members
Points: 56 Visits: 68

Obtained results

PImDateTTAabc
280042016-11-02 22:02:42P10011
280042016-12-27 21:54:26C10011
280042016-12-29 10:13:24R10011
280042016-12-29 21:39:36C10022
280042017-01-03 08:34:32R10022

Desired results

PImDateTTAabc
280042016-11-02 22:02:42P10011
280042016-12-27 21:54:26C10011
280042016-12-29 10:13:24R10011
280042016-12-29 21:39:36C10011
280042017-01-03 08:34:32R10011


GilaMonster
GilaMonster
SSC Guru
SSC Guru (673K reputation)SSC Guru (673K reputation)SSC Guru (673K reputation)SSC Guru (673K reputation)SSC Guru (673K reputation)SSC Guru (673K reputation)SSC Guru (673K reputation)SSC Guru (673K reputation)

Group: General Forum Members
Points: 673929 Visits: 48433
Table definitions please (as CREATE TABLE statement). And, if you look at the link, sample data is best as INSERT statements, so that testing queries is easy.
What is the logic behind changing the 2s in the sample data to 1?

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


Sandra D
Sandra D
Valued Member
Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)

Group: General Forum Members
Points: 56 Visits: 68

remove duplicate consecutive rows using the key PI, m and TT.

Create transactions when PI, M, TT changes based on the order of date. When TT changes a new transaction needs to be created.


GilaMonster
GilaMonster
SSC Guru
SSC Guru (673K reputation)SSC Guru (673K reputation)SSC Guru (673K reputation)SSC Guru (673K reputation)SSC Guru (673K reputation)SSC Guru (673K reputation)SSC Guru (673K reputation)SSC Guru (673K reputation)

Group: General Forum Members
Points: 673929 Visits: 48433
Table definitions and sample data please.

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


Sandra D
Sandra D
Valued Member
Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)

Group: General Forum Members
Points: 56 Visits: 68

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

CREATE TABLE [dbo].[temp](

[PI] [float] NULL,

[m] [float] NULL,

[Date] [datetime] NULL,

[TT] [nvarchar](255) NULL,

[A] [float] NULL,

[a1] [float] NULL,

[b] [float] NULL,

[c] [float] NULL,

[F9] [nvarchar](255) NULL,

[a2] [float] NULL,

[b1] [float] NULL,

[c1] [float] NULL

) ON [PRIMARY]

GO


Sandra D
Sandra D
Valued Member
Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)

Group: General Forum Members
Points: 56 Visits: 68

INSERT INTO [dbo].[temp]

([PI]

,[m]

,[Date]

,[TT]

,[A])

VALUES

(2800 ,4 ,'2016-11-02 22:02:42.000','P' ,10),

(2800 ,4 ,'2016-11-02 22:03:42.000','E' ,10),

(2800 ,4 ,'2016-11-02 22:04:42.000' ,'E' ,10),

(2800 ,4 ,'2016-12-27 21:54:26' ,'C' ,10),

(2800 ,4 ,'2016-12-29 10:13:24.000' ,'R' ,10),

(2800 ,4 ,'2016-12-29 21:39:36.000' , 'C' ,10),

(2800 ,4 ,'2017-01-03 08:34:32.000' ,'R' ,10)

GO


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