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

help with a difficult query

Hi all,
in short here it is my problem:
I have to monitor the prices of a product sold by different shops and I need
a query to retrieve a list of shops with the associated CURRENT price of the
product.
The structure is modelled by 2 tables in a 1 to n relation:

SHOPS
id counter
name text
address text

&

PRICES
id counter
price value
date text
idShop numeric

How can I obtain a result of this kind:
idShop name address price date
with all recorded shops showing the most recent date and price?

I was only able to retrieve the current date with the query:
SELECT B.IDSHOP,MAX(B.DATE) AS LU, A.NAME, A.ADDRESS FROM PRICES B LEFT
OUTER JOIN SHOPS A ON B.IDSHOP=A.ID GROUP BY IDSHOP, A.NAME, A.ADDRESS
but if I try to add to the select the price, the group by clause gives
(correctly) a result not suitable for my aims.
I also tried with subqueries but I had the same results as above.
Any hint?
TIA
Claudio
Jan 11 '06 #1
1 1282

"Claudio Magno" <ma************@libero.it> wrote in message
news:43**********************@reader2.news.tin.it. ..
Hi all,
in short here it is my problem:
I have to monitor the prices of a product sold by different shops and I
need a query to retrieve a list of shops with the associated CURRENT price
of the product.
The structure is modelled by 2 tables in a 1 to n relation:

SHOPS
id counter
name text
address text

&

PRICES
id counter
price value
date text
idShop numeric

How can I obtain a result of this kind:
idShop name address price date
with all recorded shops showing the most recent date and price?

I was only able to retrieve the current date with the query:
SELECT B.IDSHOP,MAX(B.DATE) AS LU, A.NAME, A.ADDRESS FROM PRICES B LEFT
OUTER JOIN SHOPS A ON B.IDSHOP=A.ID GROUP BY IDSHOP, A.NAME, A.ADDRESS
but if I try to add to the select the price, the group by clause gives
(correctly) a result not suitable for my aims.
I also tried with subqueries but I had the same results as above.
Any hint?
TIA
Claudio

Your structure hints that each shop only sells one product - I guess this is
OK. However, you should not use 'name' and 'date' for field names - they
are reserved words and this can cause you trouble. Change them to
shop_name, price_date, or whatever.
The other point to check is the join. Do you need:
Only shops with at least one price record
All shops regardless of whether they have any price records
A list of all the latest prices regardless of if there is a matching shop
(should not be the case if the database is set up correctly)
The following should give you option 1:

SELECT s.id, s.name, s.address, p.price, p.date
FROM SHOPS AS s INNER JOIN PRICES AS p
ON s.id=p.idshop
WHERE p.id IN
(SELECT TOP 1 id FROM PRICES
WHERE prices.idshop=s.id
ORDER BY PRICES.date DESC, PRICES.id)
Jan 11 '06 #2

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

Similar topics

9
by: majsen | last post by:
Hi, I have problem running this query. It will time out for me... My database are small just about 200 members. I have a site for swaping appartments (rental). my query should look for match in...
3
by: AMD Desktop | last post by:
Here is the table: CREATE TABLE ( IDENTITY (1, 1) NOT NULL , NOT NULL , NOT NULL , NOT NULL CONSTRAINT DEFAULT (0), NOT NULL , NOT NULL ) ON
28
by: stu_gots | last post by:
I have been losing sleep over this puzzle, and I'm convinced my train of thought is heading in the wrong direction. It is difficult to explain my circumstances, so I will present an identical...
1
by: rsarath | last post by:
Hello, I have the following setup and I would appreciate any help in improving the performance of the query. BigTable: Column1 (indexed) Column2 (indexed) Column3 (no index) Column4 (no...
14
by:  | last post by:
having a spot of trouble writing this one. if you are so inclined and have a moment, i'd really appreciate your insight. i have what amounts to a purchase order type of setup...a descriptive...
8
by: Xu, Wei | last post by:
Hi, I have wrote the following sql sentence.Do you have comments to improve the performance.I have created all the indexed. But it's still very slow.Thanks The primary key is proj_ID and...
2
by: schoultzy | last post by:
Hello Everyone, This is probably a simple fix so be kind when/if you reply. The query below retrieves information for individuals based on a column named ATTRIB_DEF, and assorted other columns;...
4
by: yanjie.ma | last post by:
Hi, I've got a two part question on table and form design (sorry for the length but it takes a bit to explain). Our sales department uses a look-up table to help the them select the best...
4
by: n | last post by:
Hello! Here is a problem I hope you can point me to a solution. It Problem: A teacher needs to know which lesson to teach. A school has a curriculum with 26 lessons, A-Z. For a given class,...
5
by: Justin | last post by:
Here's my XML: <?xml version="1.0" ?> <AppMode Type="Network"> <CurrentFolder Path="c:\tabs"> <Tabs> <FilePath>tabs\Justin.tab</FilePath> <FilePath>tabs\Julie.tab</FilePath> *****There could...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
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...

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.