SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


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


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

Author
Message
Alicia Rose
Alicia Rose
Old Hand
Old Hand (380 reputation)Old Hand (380 reputation)Old Hand (380 reputation)Old Hand (380 reputation)Old Hand (380 reputation)Old Hand (380 reputation)Old Hand (380 reputation)Old Hand (380 reputation)

Group: General Forum Members
Points: 380 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
hodgy
hodgy
Hall of Fame
Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)

Group: General Forum Members
Points: 3229 Visits: 596
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

Alicia Rose
Alicia Rose
Old Hand
Old Hand (380 reputation)Old Hand (380 reputation)Old Hand (380 reputation)Old Hand (380 reputation)Old Hand (380 reputation)Old Hand (380 reputation)Old Hand (380 reputation)Old Hand (380 reputation)

Group: General Forum Members
Points: 380 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
hodgy
hodgy
Hall of Fame
Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)

Group: General Forum Members
Points: 3229 Visits: 596
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

Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search