473,398 Members | 2,088 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,398 software developers and data experts.

How can I get all records for both tables with the latest begin date if exists?

Itemlookup table
Field names : index_id (primary key), itemno, description.
It has a child table, which is ItemPriceHistory table
The relationship to the child table is one (parent table)-to-many
(child table). - It is possible to have no child record for some rows
in the parent table.

ItemPriceHistory table
Field names: index_id (primary key), itemlookupID (foreign key of the
Itemlookup table), date begin, price
It is a child table of the itemlookup table.

How can I get all records for both tables with the latest begin date if
exists?
I also need to show the records in the parent table if there is no
related record in the child table.

Please help

Jun 15 '06 #1
4 1846

TGEAR wrote:
Itemlookup table
Field names : index_id (primary key), itemno, description.
It has a child table, which is ItemPriceHistory table
The relationship to the child table is one (parent table)-to-many
(child table). - It is possible to have no child record for some rows
in the parent table.

ItemPriceHistory table
Field names: index_id (primary key), itemlookupID (foreign key of the
Itemlookup table), date begin, price
It is a child table of the itemlookup table.

How can I get all records for both tables with the latest begin date if
exists?
I also need to show the records in the parent table if there is no
related record in the child table.

Please help


select i.index_id, i.itemno, i.description, x.lastBeginDate
from Itemlookup i
left outer join (select ih.itemlookupID, max(ih.beginDate)
lastBeginDate
from ItemPriceHistory ih
group by ih.itemlookupID) x
on i.index_id = x.itemlookupID

Jun 15 '06 #2
thank you, I also need to show the price value and I added the column
in your stmt, but i think i did wrong and I got this message "Column
'ih.price' is invalid in the select list because it is not contained in
either an aggregate function or the GROUP BY clause."

ZeldorBlat wrote:
TGEAR wrote:
Itemlookup table
Field names : index_id (primary key), itemno, description.
It has a child table, which is ItemPriceHistory table
The relationship to the child table is one (parent table)-to-many
(child table). - It is possible to have no child record for some rows
in the parent table.

ItemPriceHistory table
Field names: index_id (primary key), itemlookupID (foreign key of the
Itemlookup table), date begin, price
It is a child table of the itemlookup table.

How can I get all records for both tables with the latest begin date if
exists?
I also need to show the records in the parent table if there is no
related record in the child table.

Please help


select i.index_id, i.itemno, i.description, x.lastBeginDate
from Itemlookup i
left outer join (select ih.itemlookupID, max(ih.beginDate)
lastBeginDate
from ItemPriceHistory ih
group by ih.itemlookupID) x
on i.index_id = x.itemlookupID


Jun 15 '06 #3

TGEAR wrote:
thank you, I also need to show the price value and I added the column
in your stmt, but i think i did wrong and I got this message "Column
'ih.price' is invalid in the select list because it is not contained in
either an aggregate function or the GROUP BY clause."

ZeldorBlat wrote:
TGEAR wrote:
Itemlookup table
Field names : index_id (primary key), itemno, description.
It has a child table, which is ItemPriceHistory table
The relationship to the child table is one (parent table)-to-many
(child table). - It is possible to have no child record for some rows
in the parent table.

ItemPriceHistory table
Field names: index_id (primary key), itemlookupID (foreign key of the
Itemlookup table), date begin, price
It is a child table of the itemlookup table.

How can I get all records for both tables with the latest begin date if
exists?
I also need to show the records in the parent table if there is no
related record in the child table.

Please help


select i.index_id, i.itemno, i.description, x.lastBeginDate
from Itemlookup i
left outer join (select ih.itemlookupID, max(ih.beginDate)
lastBeginDate
from ItemPriceHistory ih
group by ih.itemlookupID) x
on i.index_id = x.itemlookupID


You can do the following, but it won't work (correctly) if the most
recent beginDate occurs twice for a single item. It would be easier to
simply store the current price of the item in the ItemLookup table.

select i.index_id, i.itemno, i.description, ih2.price, x.lastBeginDate
from Itemlookup i
left outer join (select ih.itemlookupID, max(ih.beginDate)
lastBeginDate
from ItemPriceHistory ih
group by ih.itemlookupID) x
on i.index_id = x.itemlookupID
left outer join ItemPriceHistory ih2
on (x.itemlookupID = ih2.itemlookupID
and x.lastBeginDate = ih2.beginDate)

Jun 15 '06 #4
On 15 Jun 2006 10:24:08 -0700, TGEAR wrote:

(snip)
Please help


Hi TGEAR,

I just replied to this question in the original thread. Let me know if
anything is stil unclear!

--
Hugo Kornelis, SQL Server MVP
Jun 15 '06 #5

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

Similar topics

1
by: Patrizio | last post by:
Hi All, I've the following table with a PK defined on an IDENTITY column (INSERT_SEQ): CREATE TABLE MYDATA ( MID NUMERIC(19,0) NOT NULL, MYVALUE FLOAT NOT NULL, TIMEKEY ...
5
by: Alex | last post by:
Hi, I have two tables , A and B where table B has a foreign key constraint to table A. I want to delete all records in table A that are older than a certain date that are not referenced by...
2
by: Scott Baird | last post by:
HELP!!! I've got a DB with two tables (relationships with a autonumber). The database works just fine, but I've got a report I simply can't make work. I've made a query that uses data from...
3
by: MostlyH2O | last post by:
Hi Folks, I have a query that joins 3 tables. One of the tables (SalaryData) has data where there may be duplicate records with different dates. Of those duplicate records, I want the query to...
6
by: davegb | last post by:
I've searched here and in the help screens, but I can't find the answer to a very simple question. How do I create a specific link between 2 records in 2 different tables? I know how to use the...
24
by: clare at snyder.on.ca | last post by:
I have a SQL query I need to design to select name and email addresses for policies that are due and not renewed in a given time period. The problem is, the database keeps the information for every...
11
by: kaisersose1995 | last post by:
Hi, I've got an import procedure working, using a standard import specification to import a .csv file into a temporary table. The problem i'm having is that i have 4 different sets of borrower...
3
ebs57
by: ebs57 | last post by:
I am looking for some basic help in understanding and setting up table relationships in Access. I've created one table called PROJECTS and it has the field JobNo which I've declared as the key...
11
by: shriil | last post by:
Hi I have this database that calculates and stores the incentive amount earned by employees of a particular department. Each record is entered by entering the Date, Shift (morn, eve, or night)...
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: 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
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
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
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
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.