473,405 Members | 2,338 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,405 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 1265
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.