473,408 Members | 1,601 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,408 software developers and data experts.

problem with Subquery

mfr
hi,

I've following table:
+----+-------------+----------+-------+-------+
| id | productName | provider | items | price |
+----+-------------+----------+-------+-------+
| 1 | product x | gmt | 3 | 10 |
| 2 | product x | gnt | 0 | 8 |
| 3 | product x | gat | 7 | 9 |
| 4 | product y | gat | 4 | 10 |
| 5 | product y | gnt | 2 | 7 |
+----+-------------+----------+-------+-------+

I've products from diffrent delivers and I'd like to write the query
which tell me for each product which price is lowest (and have some
items on store - items>0).
So I'd like to have the query which return me the following result:

+----+-------------+----------+-------+-------+
| id | productName | provider | items | price |
+----+-------------+----------+-------+-------+
| 3 | product x | gat | 7 | 9 |
| 5 | product y | gnt | 2 | 7 |
+----+-------------+----------+-------+-------+

I've used quite a lots of combinations group by, subquery, min... but
without success. Please, have you got any idea?

Regards,
Michal

Aug 19 '06 #1
3 2157

mfr wrote:
hi,

I've following table:
+----+-------------+----------+-------+-------+
| id | productName | provider | items | price |
+----+-------------+----------+-------+-------+
| 1 | product x | gmt | 3 | 10 |
| 2 | product x | gnt | 0 | 8 |
| 3 | product x | gat | 7 | 9 |
| 4 | product y | gat | 4 | 10 |
| 5 | product y | gnt | 2 | 7 |
+----+-------------+----------+-------+-------+

I've products from diffrent delivers and I'd like to write the query
which tell me for each product which price is lowest (and have some
items on store - items>0).
So I'd like to have the query which return me the following result:

+----+-------------+----------+-------+-------+
| id | productName | provider | items | price |
+----+-------------+----------+-------+-------+
| 3 | product x | gat | 7 | 9 |
| 5 | product y | gnt | 2 | 7 |
+----+-------------+----------+-------+-------+

I've used quite a lots of combinations group by, subquery, min... but
without success. Please, have you got any idea?

Regards,
Michal
It ain't pretty (by which I mean someone will come along in a second
with a much more elegant solution):

SELECT t1.*
FROM (SELECT * FROM table1 WHERE items 0) t1
LEFT JOIN (SELECT * FROM table1 WHERE items 0) t2 ON t1.id <t2.id
AND t1.productName = t2.productName
AND t1.price t2.price
WHERE t2.price IS NULL;

Aug 19 '06 #2
mfr

strawberry wrote:
mfr wrote:
hi,

I've following table:
+----+-------------+----------+-------+-------+
| id | productName | provider | items | price |
+----+-------------+----------+-------+-------+
| 1 | product x | gmt | 3 | 10 |
| 2 | product x | gnt | 0 | 8 |
| 3 | product x | gat | 7 | 9 |
| 4 | product y | gat | 4 | 10 |
| 5 | product y | gnt | 2 | 7 |
+----+-------------+----------+-------+-------+

I've products from diffrent delivers and I'd like to write the query
which tell me for each product which price is lowest (and have some
items on store - items>0).
So I'd like to have the query which return me the following result:

+----+-------------+----------+-------+-------+
| id | productName | provider | items | price |
+----+-------------+----------+-------+-------+
| 3 | product x | gat | 7 | 9 |
| 5 | product y | gnt | 2 | 7 |
+----+-------------+----------+-------+-------+

I've used quite a lots of combinations group by, subquery, min... but
without success. Please, have you got any idea?

Regards,
Michal

It ain't pretty (by which I mean someone will come along in a second
with a much more elegant solution):

SELECT t1.*
FROM (SELECT * FROM table1 WHERE items 0) t1
LEFT JOIN (SELECT * FROM table1 WHERE items 0) t2 ON t1.id <t2.id
AND t1.productName = t2.productName
AND t1.price t2.price
WHERE t2.price IS NULL;

Hi,

thanx! it's huge, but it's working ;-) Any other ideas how to make it
faster?
I'll test it tomorrow, and let you know then about performance.

Regards,
Michal

Aug 20 '06 #3
strawberry wrote:
It ain't pretty (by which I mean someone will come along in a second
with a much more elegant solution):

SELECT t1.*
FROM (SELECT * FROM table1 WHERE items 0) t1
LEFT JOIN (SELECT * FROM table1 WHERE items 0) t2 ON t1.id <t2.id
AND t1.productName = t2.productName
AND t1.price t2.price
WHERE t2.price IS NULL;
Here's something that is slightly leaner, has no subqueries, and might
perform a bit better.

SELECT t1.*
FROM tablename AS t1
LEFT JOIN tablename AS t2 ON (t1.productName = t2.productName
AND t2.items 0 AND t1.price t2.price)
WHERE t1.items 0 AND t2.id IS NULL

Make sure to use EXPLAIN to figure out if it's doing table-scans, and
add indexes appropriately to fix that.

Regards,
Bill K.
Aug 20 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Joerg Ammann | last post by:
hi, os: aix 4.3.3 DB2: version 7 FP3 we are using a federated DB setup, datasource and fed-Db are both V7FP3 (in fact they are on the same server) and are having massiv performance problems....
15
by: Hemant Shah | last post by:
Folks, We have an SQL statement that was coded in an application many years ago (starting with DB V2 I think). When I upgraded to UDB 8.2, the optimizer does not use optimal path to access the...
8
by: Andrew McNab | last post by:
Hi folks, I have a problem with an MS Access SQL query which is being used in an Access Report, and am wondering if anyone can help. Basically, my query (shown below) gets some records from a...
1
by: Andrew McNab | last post by:
Hi folks, I have a problem with an MS Access SQL query which is being used in an Access Report, and am wondering if anyone can help. Basically, my query (shown below) gets some records from a...
21
by: mollyf | last post by:
I'm creating a query, which I want to use in code in my VB.NET app. This query produces the correct results when executed in Access: SELECT tblEncounters.EncounterBeginDT, Query11.RID,...
2
by: reap76 | last post by:
I am running the following query: if (select IntExternalAccountID from ExternalAccount) = (select IntExternalAccountID from InternalAccount) select * from InternalAccount where AccountPurpose=2 ...
12
by: info | last post by:
The following query works fine in access and does exactly what I want it to do however I get a syntax error when I port it over to SQL Server 2000. ------------- UPDATE OrdersProducts INNER...
13
by: ThePrinceIsRight | last post by:
I have a problem with using a subquery in MS Access. The purpose of the sub-query is to create a list of people who have had doctor exams in the past 6 months and exclude them from the main query....
5
by: Anne | last post by:
Hello! Here is the statement in question: --STATEMENT A SELECT * FROM dbo.myTable WHERE colX in (SELECT colX FROM dbo.sourceTable) The problem with Statement A is that 'colX' does not exist...
1
by: mipo1984 | last post by:
I have subquery into a principal query, and i need the subquery return me only the last row of all results, but i can`t use "order by <field> desc " in the subquery because this return me an error,...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.