Showing posts with label resolve. Show all posts
Showing posts with label resolve. Show all posts

Friday, March 30, 2012

Query problems - Group By and Latest date

Hi all,

hopefully someone can suggest the best way of implementing the problem i am trying to resolve. We have a table which contains rows relating to tests run on our product. This table is populated from an SSIS job which parses CSV files.

There are multiple rows per serial number relating to multiple tests. The only tests i am interested in are the ones with an ID of T120. Here is the query i have so far which should make it a little easier to explain:

SELECT [SerialNumber]
,Param1
,[TimeStamp]
FROM [Build Efficiency System].[dbo].[SSIS_SCANNERDATA_TBL]
WHERE Test = 'T120'
GROUP BY SerialNumber, Param1, [TimeStamp]
ORDER BY SerialNumber

What i have above is fine to a point. The problem i am encountering is that in test T120 it specifies a part which can be be one of about 6 in field Param1. If during testing there is a problem with the part then it is replaced and the test run a second time up until the whole product passes the test. The query above returns all instances of replacements so i may have the out put as follows:

SerialNumber Param1 TimeStamp
0 Part1 15/03/07
0 Part2 15/03/07
0 Part2 16/03/07
0 Part3 15/03/03

What i really need is to only list the last part that is installed, hence the one with the latest timestamp:

SerialNumber Param1 TimeStamp

0 Part1 15/03/07

0 Part2 16/03/07

0 Part3 15/03/03

Can someone please help me to alter the above query so that it will show only those Param1 fields that have the latest date for each part.

Many thanks in advance,

Grant

This should do the trick:

SELECT [SerialNumber]
,Param1
,MAX([TimeStamp])
FROM [Build Efficiency System].[dbo].[SSIS_SCANNERDATA_TBL]
WHERE Test = 'T120'
GROUP BY SerialNumber, Param1
ORDER BY SerialNumber

You only need to take the max of your timestamp field (and remove it from the group by). The group by all fields is a bit extreme in the previous query (you could use the distinct keyword instead if you had apparent duplicate rows (i.e. replaced the part 3 times in a day).

|||Thats sorted it.
I wasn't as far of the mark in the first place as i'd thought. Thank's very much for the assistance, its much appreciated.

Cheers,

Grant|||Hi, apologies but i need one more piece of advice on this subject.

If i want to include a column with a serial number of the part that has been replaced, how would i do that. As soon as i add it, it needs to be part of an aggregate function or the group by clause. When it becomes part of the group by clause it then duplicates the part again.

Any ideas?

Thanks,

Grant|||

Is the serial number of the part in the same table - if so presumably it is different for each time that part is replaced. You can use a nested query to get that - however it will run into a problem if there are multiple records with the same date. As it stands at the moment you could not distinuish between them.

If you had records:

SerialNumber Param1 TimeStamp Part_SN
0 Part1 15/03/07 1234
0 Part2 15/03/07 1235
0 Part2 16/03/07 1236
0 Part2 16/03/07 1237
0 Part3 15/03/03 1238

How would you know which of the two Part2 items fitted on 16 Mar to give the serial number of? If there are additional fields to determine this then we need to use them

If this does not arise then the query below should serve (substitute correct fieldname for Part_SN):

SELECT B.[SerialNumber]
,B.Param1
,B.[TimeStamp]
,B.Part_SN
FROM (
SELECT [SerialNumber]
,Param1
,MAX([TimeStamp]) AS TimeStamp
FROM [Build Efficiency System].[dbo].[SSIS_SCANNERDATA_TBL]
WHERE Test = 'T120'
GROUP BY SerialNumber, Param1
) A
INNER JOIN [Build Efficiency System].[dbo].[SSIS_SCANNERDATA_TBL] B
ON (A.[SerialNumber] = B.[SerialNumber]) AND
(A.Param1 = B.Param1) AND
(A.[TimeStamp] = B.[TimeStamp]) AND
(B.Test = 'T120')
ORDER BY B.[SerialNumber]

If this is a problem then you will get multiple records in that case - one for each serial number. If the TimeStamp is a datetime which includes the time of the replacement then this will not be an issue (as long as the required serial number is the last record.

|||Thanks,

That is exactly what i wanted to do. Works like a charm.

Grantsql

Friday, March 23, 2012

Query Plan Differences

Hello all,
I have an interesting problem that I hope some of you can help me resolve.
We have a Production Server (PS) that uses Transactional Replication to send
data to a Reporting Server (RS). We have 3 horizontally partitioned tables
(based upon year) that are accessed via a view. On top of the year view we
have another view that grabs MAX values based upon an ID Code (not an
identity column). A query using the MAX view returns in 10 - 15 seconds on
the PS. The same query returns in 40 minutes on the RS. The keys, indexes
and server configuration are basically identical on both machines. However,
when I compare the execution plans, they are completely different. Both
machines are running W2K AS sp3 with SQL Ent Ed sp4, but there are some
differences:
PS: 6 - 700Mhz procs, 6GB RAM
RS: 6 - 550Mhx procs, 2.75GB RAM
Both machines are Proliant 8000's with a 5300 Array Controller that has 1
channel used for the OS (RAID1) and another channel used for the PageFile
(RAID1). They also have an HBA that connects them to RAID5 (data) arrays
and RAID0+1 (Tlogs) arrays on an HP EMA12000 SAN. While I would expect the
query to run faster on PS, I would not expect a different query plan. I
have tried re-indexing the RS, verified statistics and turning off
parallelism. No luck. Any suggestions would be greatly appreciated.
Thanks in advance.i would try updating statistics with full scan on each
system.
i am assuming statistics are not replicated, and that each
system will maintain statistics as necessary with the
default sampling.
while the sampling percentages are the same, there is no
guarantee that the sampled values are the same,
hence a fullscan is required to generate matching
statistics
>--Original Message--
>Hello all,
>I have an interesting problem that I hope some of you can
help me resolve.
>We have a Production Server (PS) that uses Transactional
Replication to send
>data to a Reporting Server (RS). We have 3 horizontally
partitioned tables
>(based upon year) that are accessed via a view. On top
of the year view we
>have another view that grabs MAX values based upon an ID
Code (not an
>identity column). A query using the MAX view returns in
10 - 15 seconds on
>the PS. The same query returns in 40 minutes on the RS.
The keys, indexes
>and server configuration are basically identical on both
machines. However,
>when I compare the execution plans, they are completely
different. Both
>machines are running W2K AS sp3 with SQL Ent Ed sp4, but
there are some
>differences:
>PS: 6 - 700Mhz procs, 6GB RAM
>RS: 6 - 550Mhx procs, 2.75GB RAM
>Both machines are Proliant 8000's with a 5300 Array
Controller that has 1
>channel used for the OS (RAID1) and another channel used
for the PageFile
>(RAID1). They also have an HBA that connects them to
RAID5 (data) arrays
>and RAID0+1 (Tlogs) arrays on an HP EMA12000 SAN. While
I would expect the
>query to run faster on PS, I would not expect a different
query plan. I
>have tried re-indexing the RS, verified statistics and
turning off
>parallelism. No luck. Any suggestions would be greatly
appreciated.
>Thanks in advance.
>
>.
>|||I have already tried that. Unfortunately, it did not change the results.
"joe chang" <anonymous@.discussions.microsoft.com> wrote in message
news:053901c39f67$b0c42bc0$a501280a@.phx.gbl...
> i would try updating statistics with full scan on each
> system.
> i am assuming statistics are not replicated, and that each
> system will maintain statistics as necessary with the
> default sampling.
> while the sampling percentages are the same, there is no
> guarantee that the sampled values are the same,
> hence a fullscan is required to generate matching
> statistics
> >--Original Message--
> >Hello all,
> >I have an interesting problem that I hope some of you can
> help me resolve.
> >We have a Production Server (PS) that uses Transactional
> Replication to send
> >data to a Reporting Server (RS). We have 3 horizontally
> partitioned tables
> >(based upon year) that are accessed via a view. On top
> of the year view we
> >have another view that grabs MAX values based upon an ID
> Code (not an
> >identity column). A query using the MAX view returns in
> 10 - 15 seconds on
> >the PS. The same query returns in 40 minutes on the RS.
> The keys, indexes
> >and server configuration are basically identical on both
> machines. However,
> >when I compare the execution plans, they are completely
> different. Both
> >machines are running W2K AS sp3 with SQL Ent Ed sp4, but
> there are some
> >differences:
> >
> >PS: 6 - 700Mhz procs, 6GB RAM
> >RS: 6 - 550Mhx procs, 2.75GB RAM
> >
> >Both machines are Proliant 8000's with a 5300 Array
> Controller that has 1
> >channel used for the OS (RAID1) and another channel used
> for the PageFile
> >(RAID1). They also have an HBA that connects them to
> RAID5 (data) arrays
> >and RAID0+1 (Tlogs) arrays on an HP EMA12000 SAN. While
> I would expect the
> >query to run faster on PS, I would not expect a different
> query plan. I
> >have tried re-indexing the RS, verified statistics and
> turning off
> >parallelism. No luck. Any suggestions would be greatly
> appreciated.
> >Thanks in advance.
> >
> >
> >.
> >