473,231 Members | 1,640 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 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 1260
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: netpurpose | last post by:
I need to extract data from this table to find the lowest prices of each product as of today. The product will be listed/grouped by the name only, discarding the product code - I use...
3
by: Torrent | last post by:
When Trying to Load an XSLT File with the XslTransform i got a rather annoying Exception being thrown "System.Xml.XPath.XPathException: XsltContext is needed for this query because of an unknown...
1
by: Dimitris | last post by:
Hi all. I have an access 2000 database with one table with two fields. Both fields have telephone numbers(like telephone calls).I need to make a query to count how many times one number call...
4
by: Oreo Bomb | last post by:
I have a secured database that contains a Read-Only group. This group has permissions to view reports, but cannot add, edit, or delete any DB objects. One of the reports the group needs access to...
5
by: Alicia | last post by:
Yes, but will that skip a week and group by the date for me? I basically wanted something that would do a count of the dates, then group them by their week name.. BEFORE: Resource Date ...
13
by: Sue | last post by:
I'm working on a database that keeps track of employees hired by a general contractor on a project by project basis. These employees are hired to work on a project and are then laid off either at...
0
by: rdnews | last post by:
Dear group, I got excellent help here a while back in optimizing a slow query, so I thought I'd try again with another one... I have a small table, around 3000 rows with two columns. One is a...
4
by: frizzle | last post by:
Hi there, Still building my forum. I have a certain mysql-query i just can't figure out. These are my tables (simplified): categories -> id, description forums -> id, cat_id,...
4
by: lorirobn | last post by:
Hi, I have a report displaying items that are missing from a room. I created 2 queries, the first getting the items IN the room, and the second being an "unmatched" query that references the...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

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.