January 2, 2018 at 7:58 am
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
January 2, 2018 at 8:11 am
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
January 2, 2018 at 8:29 am
Obtained results
PI | m | Date | TT | A | a | b | c |
2800 | 4 | 2016-11-02 22:02:42 | P | 10 | 0 | 1 | 1 |
2800 | 4 | 2016-12-27 21:54:26 | C | 10 | 0 | 1 | 1 |
2800 | 4 | 2016-12-29 10:13:24 | R | 10 | 0 | 1 | 1 |
2800 | 4 | 2016-12-29 21:39:36 | C | 10 | 0 | 2 | 2 |
2800 | 4 | 2017-01-03 08:34:32 | R | 10 | 0 | 2 | 2 |
Desired results
PI | m | Date | TT | A | a | b | c |
2800 | 4 | 2016-11-02 22:02:42 | P | 10 | 0 | 1 | 1 |
2800 | 4 | 2016-12-27 21:54:26 | C | 10 | 0 | 1 | 1 |
2800 | 4 | 2016-12-29 10:13:24 | R | 10 | 0 | 1 | 1 |
2800 | 4 | 2016-12-29 21:39:36 | C | 10 | 0 | 1 | 1 |
2800 | 4 | 2017-01-03 08:34:32 | R | 10 | 0 | 1 | 1 |
January 2, 2018 at 8:32 am
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
January 2, 2018 at 8:59 am
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.
January 2, 2018 at 9:52 am
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
January 2, 2018 at 10:44 am
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
January 2, 2018 at 11:16 am
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