471,055 Members | 2,037 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,055 software developers and data experts.

SQL query help: Need to get TOP in group where 1st is X times > than 2nd..

Win XP, Access 02:

I've got a largish table with three (relevant) fields; Item, PIN &
Incid.

What I'd like to accomplish in one SQL query (I can do it in a couple
but it's ugly. Also,the app is VBA free at present and I'd prefer to
keep it that way if poss.) is to select the Item & PIN combination
which has the highest incidence for each individual Item, but *only*
if that combination's Incid is 3 times higher than the second highest
PIN's Incid. Have been bashing away for hours at this without joy. Any
help would be much appreciated.
cheers
alvey
Nov 12 '05 #1
3 1197
DFS
Alvey,

Give this a try:

SELECT (A.Item & A.PIN) AS ItemPin, A.Incident
FROM
Table1 A INNER JOIN
(SELECT Item, Max(Incident) AS MaxIncident FROM Table1 GROUP BY Item) B
ON A.Item = B.Item AND A.Incident = B.MaxIncident
WHERE (A.Item & A.Pin) IN
(
SELECT (A.Item & A.PIN) AS ItemPin
FROM Table1 A INNER JOIN Table1 B
ON (A.Item = B.Item) AND (A.Incident >= 3 * B.Incident)
);

"Alvey Sidecast" <al************@yahoo.com> wrote in message
news:ed**************************@posting.google.c om...
Win XP, Access 02:

I've got a largish table with three (relevant) fields; Item, PIN &
Incid.

What I'd like to accomplish in one SQL query (I can do it in a couple
but it's ugly. Also,the app is VBA free at present and I'd prefer to
keep it that way if poss.) is to select the Item & PIN combination
which has the highest incidence for each individual Item, but *only*
if that combination's Incid is 3 times higher than the second highest
PIN's Incid. Have been bashing away for hours at this without joy. Any
help would be much appreciated.
cheers
alvey

Nov 12 '05 #2
Hi Alvey,

How about posting any DDL constraints and some sample data in the
tables and then the expected output.

The actual SQL DDL and some INSERT statements would be great.

It really does cut down down on the bog work anyone taking the time to
answer your SQL question has to do, but the problem sounds interesting
so I'll give it a go.

Here's a go...

SELECT AlveysTable.Item, AlveysTable.Pin, AlveysTable.Incid
FROM AlveysTable
WHERE (AlveysTable.Incid/3) >
(SELECT DISTINCT AT.Incid
FROM AlveysTable AS AT
WHERE (AT.Incid=
(SELECT Max(AT3.Incid) AS MaxOfIncid
FROM AlveysTable AS AT3
WHERE AT3.Incid<
(SELECT max(Incid)
FROM AlveysTable as AT2
WHERE AT2.Item = AT3.Item)
AND AT3.Item = AT.Item
GROUP BY AT3.Item)) AND AT.Item =
AlveysTable.Item)

Given

ID Item Pin Incid
1 a 1234 4
2 a 1233 5
3 a 1232 88
4 b 1222 3
5 b 3333 4
6 b 3333 3
7 c 2345 2
8 c 4345 7
9 c 5433 22

as the base data returns

Item Pin Incid
a 1232 88
c 5433 22

inserting

10 c 6666 20

Returns only

Item Pin Incid
a 1232 88

However not sure if this is the desired output...

Regards,

Peter

al************@yahoo.com (Alvey Sidecast) wrote in message news:<ed**************************@posting.google. com>...
Win XP, Access 02:

I've got a largish table with three (relevant) fields; Item, PIN &
Incid.

What I'd like to accomplish in one SQL query (I can do it in a couple
but it's ugly. Also,the app is VBA free at present and I'd prefer to
keep it that way if poss.) is to select the Item & PIN combination
which has the highest incidence for each individual Item, but *only*
if that combination's Incid is 3 times higher than the second highest
PIN's Incid. Have been bashing away for hours at this without joy. Any
help would be much appreciated.
cheers
alvey

Nov 12 '05 #3
Thanks lads. Nice stuff!
Nov 12 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by netpurpose | last post: by
1 post views Thread by Dimitris | last post: by
4 posts views Thread by Oreo Bomb | last post: by
5 posts views Thread by Alicia | last post: by
reply views Thread by rdnews | last post: by
4 posts views Thread by frizzle | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.