to apply different logic for each ID and as per the logic and a single row from each ID(group) would be the result set

  • Hi,

    I have a table like :

    IDAvgTimeMaxTimeDevice

    150009000D1

    1800010000D1

    228003000D1

    220002500D2

    230003200D1

    227002900D2

    3200300D1

    3230280D1

    3320330D1

    I need to apply different logic to each of the Ids, for ex:

    If ID =1, then My Result should be the AvgTime from second row of ID = 1

    If ID = 2, then My Result should be the Sum(MaxTime) for Devices D1 and D2 seperately

    If ID = 3, then My Result should be the AvgTime from first row of ID = 1

    and then i need it pivoted on Devices D1 and D2

    My My Resultset should look like :

    IDD1D2

    180000

    262005300

    32300

    The script for creation and insertion of table for convenience:

    CREATE TABLE [dbo].[TestPerf](

    [ID] [int] NULL,

    [AvgTime] [int] NULL,

    [MaxTime] [int] NULL,

    [Device] [nvarchar](50) )

    Insert into dbo.TestPerf

    values (1, 5000, 9000, 'D1')

    Insert into dbo.TestPerf

    values (1, 8000, 10000, 'D1')

    Insert into dbo.TestPerf

    values (2, 2800, 3000, 'D1')

    Insert into dbo.TestPerf

    values (2, 2000, 2500, 'D2')

    Insert into dbo.TestPerf

    values (2, 3000, 3200, 'D1')

    Insert into dbo.TestPerf

    values (2, 2700, 2900, 'D2')

    Insert into dbo.TestPerf

    values (3, 200, 300, 'D1')

    Insert into dbo.TestPerf

    values (3, 230, 280, 'D1')

    Insert into dbo.TestPerf

    values (3, 320, 330, 'D1')

    Please suggest on the best approach to apply business logic to each of the iDs in a table and then Pivot them based on Devices

    Alicia Rose

  • Do you have access to SSIS? You could manage all of what you require in a single data flow task.

    Tom

    Life: it twists and turns like a twisty turny thing

  • Hi Tom,

    I know to create a basic SSIS package.

    I am not sure though without using some script how would i would get the required Result.

    Please let me know the steps in SSIS to achieve this.

    Alicia Rose

  • i need to ask a couple of questions to clarify:

    1. you mention you need to get row 2 from ID 1 and row 1 from ID 2. what is the order of the data supposed to be? will the AvgTime column being ascending? because if there is no ordering specified (either by a clustered index or an order by clause) you can't guarantee that the second row you want is the second row you get

    2. ID 3 row 1: in your final output you have the AvgTime of device D1 as 230, but in the source table the first row has a value of 200. Which is correct?

    3. Do you know the number of devices before hand? Is this number fixed?

    After looking at this again, an SSIS package maybe over-egging the pudding. It could be possible to do this with plain T-SQL after all. Would this run on a SQL2005 box?

    Tom

    Life: it twists and turns like a twisty turny thing

Viewing 4 posts - 1 through 3 (of 3 total)

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