Click here to Skip to main content
16,017,788 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi
Excuse me. I can’t speak English well.

I have 2 tables in my database.

Master_table has these fields : product_name, receive_date, price.

Detail table (daily information) has these fields : product_name, sell_date.

I wants to get a report of products that have been sold.
Since the price of a product is different in some days, the SQL command must use related record in Master_table which the date in master table be related to date in detail table.

I can do this in ACCESS by use of word “first”, but in SQL Server I can’t.

How is it possible?

Thanks very much
Posted
Updated 21-Oct-11 22:36pm
v2

This should give you the price of the products on their sell date.
SQL
SELECT D.product_name,
       D.sell_date,
       (SELECT TOP 1 M.price
        FROM Master_table M
        WHERE M.product_name = D.Product_name AND M.receive_date >= D.sell_date
        ORDER BY M.receive_date DESC)
FROM Detail_table D
 
Share this answer
 
v3
Comments
Amir Mahfoozi 22-Oct-11 4:49am    
it seams the right solution.
SQL
select top 1 rerceive_date , price from master_table order by receive_date desc
 
Share this answer
 
SQL
Select * 
from Master_table m, Detail_table d
where m.product_name = d.product_name; 

this query is for oracle 9i.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900