SQL Server Over Partition

  • 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

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

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

    [float] NULL,

    [c] [float] NULL,

    [F9] [nvarchar](255) NULL,

    [a2] [float] NULL,

    [b1] [float] NULL,

    [c1] [float] NULL

    ) ON [PRIMARY]

    GO

  • 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

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply