Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

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 Expand / Collapse
Author
Message
Posted Tuesday, August 12, 2008 9:10 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, March 28, 2011 12:49 AM
Points: 32, Visits: 172
Hi,

I have a table like :
ID AvgTime MaxTime Device
1 5000 9000 D1
1 8000 10000 D1
2 2800 3000 D1
2 2000 2500 D2
2 3000 3200 D1
2 2700 2900 D2
3 200 300 D1
3 230 280 D1
3 320 330 D1

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 :
ID D1 D2
1 8000 0
2 6200 5300
3 230 0


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
Post #551554
Posted Wednesday, August 13, 2008 8:15 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Friday, June 20, 2014 4:09 AM
Points: 1,865, Visits: 590
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
Post #551929
Posted Wednesday, August 13, 2008 2:50 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, March 28, 2011 12:49 AM
Points: 32, Visits: 172
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
Post #552293
Posted Thursday, August 14, 2008 2:38 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Friday, June 20, 2014 4:09 AM
Points: 1,865, Visits: 590
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
Post #552488
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse