Unix Technical Forum

SQL Date Query

This is a discussion on SQL Date Query within the SQL Server forums, part of the Microsoft SQL Server category; --> I need a query that will select the closest date. I have to tables Pricing and InventoryItem. tblInventoryItem InventoryItemID ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 05:57 PM
Josh
 
Posts: n/a
Default SQL Date Query

I need a query that will select the closest date.

I have to tables Pricing and InventoryItem.

tblInventoryItem
InventoryItemID <- Pk
Description


tblPricing
PricingID <- Pk
InventoryItemID
Price
EffectiveDate


I need to select all the current "prices" for each InventoryItem based
on the Pricing's effective date.


select top 1 * from tblPricing join tblInventoryItem on
tlbPricing.InventoryItemID = tblInventoryItem.InventoryItemID
WHERE
tblPricing.EffectiveDate <= GetDate()

This does grab the correct price for a single InventoryItem. But I
need this query run for all InventoryItem's. I probably need some
sort of subquery but I can't figure it out....

Thanks....
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 05:57 PM
David Portas
 
Posts: n/a
Default Re: SQL Date Query

In your Pricing table the natural key is presumably (inventoryitemid,
effectivedate). The following query assumes that (inventoryitemid,
effectivedate) is unique, so you should declare a unique constraint for it.

SELECT I.*, P.*
FROM tblInventoryItem AS I
JOIN
(SELECT inventoryitemid, MAX(effectivedate) AS effectivedate
FROM tblPricing
GROUP BY inventoryitemid) AS M
ON I.inventoryitemid = M.inventoryitemid
JOIN tblPricing AS P
ON P.inventoryitemid = M.inventoryitemid
AND P.effectivedate = M.effectivedate

(untested)

--
David Portas
------------
Please reply only to the newsgroup
--


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 05:57 PM
Christian Maslen
 
Posts: n/a
Default Re: SQL Date Query

Hi Josh,

Try the following (I've assumed the combo of InventoryItemID and
EffectiveDate are unique):

SELECT *
FROM tblInventoryItem i
, tblPricing p
,(SELECT InventoryItemID
, MAX(EffectiveDate) as PriceDate
FROM tblPricing
WHERE EffectiveDate <= @YourDate
GROUP BY InventoryItemID
) cp
WHERE i.InventoryItemID = cp.InventoryItemID
AND cp.InventoryItemID = p.InventoryItemID
AND p.Effective_date = cp.PriceDate;

You can achieve the same result using a correlated sub-query, but this
method works best for me across different DBs.

Christian.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-28-2008, 05:57 PM
Shervin Shapourian
 
Posts: n/a
Default Re: SQL Date Query

Hi Josh,

You need to divide your problem into two smaller and easier problems.
First, find the current effective date for each inventory item in
Pricing table. Then find the Price of each item in Pricing table where
its effective date matches the current effective date.


This view gives you current effective date for all items:

create view EffectiveDates as
select ItemID, max(EffectiveDate) as CurEffDate
from Pricing
where EffectiveDate <= GetDate()
group by ItemID


Now you can write a query using your tables and this view, like this:


select InvItem.ItemID, [Desc], Price as CurrentPrice, EffectiveDate
from InvItem, Pricing, EffectiveDates
where InvItem.ItemID = Pricing.ItemID
and Pricing.ItemID = EffectiveDates.ItemID
and Pricing.EffectiveDate = EffectiveDates.CurEffDate


Or if you don't want to have a separate view, you can just incorporate
the body of view into your query like this:

select InvItem.ItemID,
[Desc],
Price,
EffectiveDate
from InvItem,
Pricing,
(select ItemID, max(EffectiveDate) as CurEffDate
from Pricing
where EffectiveDate <= GetDate()
group by ItemID) as EffectiveDates
where InvItem.ItemID = Pricing.ItemID
and Pricing.ItemID = EffectiveDates.ItemID
and Pricing.EffectiveDate = EffectiveDates.CurEffDate

which gives you the same result. I hope it helps you.

Shervin


josh@musicsteps.com (Josh) wrote in message news:<37b0705b.0309170951.54a3a983@posting.google. com>...
> I need a query that will select the closest date.
>
> I have to tables Pricing and InventoryItem.
>
> tblInventoryItem
> InventoryItemID <- Pk
> Description
>
>
> tblPricing
> PricingID <- Pk
> InventoryItemID
> Price
> EffectiveDate
>
>
> I need to select all the current "prices" for each InventoryItem based
> on the Pricing's effective date.
>
>
> select top 1 * from tblPricing join tblInventoryItem on
> tlbPricing.InventoryItemID = tblInventoryItem.InventoryItemID
> WHERE
> tblPricing.EffectiveDate <= GetDate()
>
> This does grab the correct price for a single InventoryItem. But I
> need this query run for all InventoryItem's. I probably need some
> sort of subquery but I can't figure it out....
>
> Thanks....

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 09:39 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com