Question

I have a SQL table that has date (day), location, and value. The table is ordered by date then location. I am wanting to find out if out of 9 consecutive dates, the values are all increasing or not (so I can flag them in my eventual chart). If 9 values are increasing, on the 9th day I want it to flag in a new column. It will be a rolling 9 days throughout the entire time period. I also am trying to figure out if in a group of 3 days, 2/3 of the values are over X limit. (WECO rules, basically) Suggestions for an approach would be greatly appreciated! I'm using SQL Management Studio and/or SSIS or SSRS. Thanks!

Était-ce utile?

La solution

-- Create a table

CREATE TABLE [dbo].[Weco](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [aDate] [date] NULL,
 CONSTRAINT [PK_ID] PRIMARY KEY CLUSTERED 
(
    [ID] ASC
) 
);

-- insert data

INSERT INTO [dbo].[Weco] 
VALUES 
('2012-01-01'), ('2012-01-02'), ('2012-01-03'), ('2012-01-04');

--Run this query. This should give you some idea

SELECT
    W1.ID, W1.[aDate] , DATEDIFF (d,W1.aDate, W2.aDate)
FROM [dbo].[Weco] W1
--inner join 
LEFT OUTER JOIN
(
    SELECT
         W2.ID, 
         W2.[aDate]
    FROM [dbo].[Weco] W2
    WHERE W2.ID > (SELECT MIN (ID)FROM [dbo].[Weco])
) W2 on W1.ID = W2.ID - 1
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top