SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsDatabaseSQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old November 3rd, 2009, 07:06 AM
garyrobar garyrobar is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Aug 2006
Posts: 113 garyrobar User rank is Corporal (100 - 500 Reputation Level)garyrobar User rank is Corporal (100 - 500 Reputation Level)garyrobar User rank is Corporal (100 - 500 Reputation Level)garyrobar User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Day 3 h 23 m 34 sec
Reputation Power: 5
Query - General - Best way to Join "Lineitems" & "SKU Price Hist" Tables?

I am having trouble getting my head around the join for two tables. One table has lineitems (what sku was ordered, and when), and the other has the price dimension data for SKUs, over time.

"LINEITEMS" - from orders, with a sku, qty, date. EG:

Code:
LINE_DATE	SKU		QTY
----------	-------		----
2008-08-29	1000101		10
2007-05-15	1000102		4
2001-08-03	1000103		9


"SKU_PRICE" - with a sku and date. EG:

Code:
SKU         	EFF_DATE        UNIT_PRICE
----------- 	----------	----------
1000101		2005-05-04	0.07276
1000101		2008-05-10	0.091
1000101		2009-01-29	0.08596


For this example, look at SKU 1000101. It was ordered on 2008-08-29. What is the best way to join the SKU_PRICE table to get the relevent price for that sku on the date it was ordered?

I know that I should be joining on sku first, but then how to I set up the dates to get the correct join? In this example, the query should return the SKU_PRICE example row #2 above - with effective date 2008-05-10 at 9.1 cents per unit.


Here is the shell query, I just need the date part.

Code:
SELECT 
	L.LINE_DATE,
	L.SKU,
	L.QTY,
	P.UNIT_PRICE
FROM
	LINEITEMS L INNER JOIN 
	SKU_PRICE P ON 
		L.SKU = P.SKU AND
		????????????????


Thanks in advance,
G
__________________
Billiards
Digital Signage

Reply With Quote
  #2  
Old November 3rd, 2009, 07:25 AM
micky's Avatar
micky micky is offline
Couch Potato Wizard
Click here for more information. Click here for more information
 
Join Date: Jan 2005
Location: India
Posts: 12,259 micky User rank is General 18th Grade (Above 100000 Reputation Level)micky User rank is General 18th Grade (Above 100000 Reputation Level)micky User rank is General 18th Grade (Above 100000 Reputation Level)micky User rank is General 18th Grade (Above 100000 Reputation Level)micky User rank is General 18th Grade (Above 100000 Reputation Level)micky User rank is General 18th Grade (Above 100000 Reputation Level)micky User rank is General 18th Grade (Above 100000 Reputation Level)micky User rank is General 18th Grade (Above 100000 Reputation Level)micky User rank is General 18th Grade (Above 100000 Reputation Level)micky User rank is General 18th Grade (Above 100000 Reputation Level)micky User rank is General 18th Grade (Above 100000 Reputation Level)micky User rank is General 18th Grade (Above 100000 Reputation Level)micky User rank is General 18th Grade (Above 100000 Reputation Level)micky User rank is General 18th Grade (Above 100000 Reputation Level)micky User rank is General 18th Grade (Above 100000 Reputation Level)micky User rank is General 18th Grade (Above 100000 Reputation Level)  Folding Points: 1480 Folding Title: Novice Folder
Time spent in forums: 5 Months 4 Days 2 m 48 sec
Reputation Power: 2179
LINE_DATE and EFF_DATE are not same as per your examples!!
Just making a wild guess here
Code:
SELECT 
	L.LINE_DATE,
	L.SKU,
	L.QTY,
	P.UNIT_PRICE
FROM
	LINEITEMS L INNER JOIN 
	SKU_PRICE P ON 
		L.SKU = P.SKU 
        WHERE L.LINE_DATE=P.EFF_DATE
__________________
Laziness is my religion and Sunday is my God

Get the Mantra!

Reply With Quote
  #3  
Old November 3rd, 2009, 07:44 AM
garyrobar garyrobar is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Aug 2006
Posts: 113 garyrobar User rank is Corporal (100 - 500 Reputation Level)garyrobar User rank is Corporal (100 - 500 Reputation Level)garyrobar User rank is Corporal (100 - 500 Reputation Level)garyrobar User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Day 3 h 23 m 34 sec
Reputation Power: 5
I know they aren't, because I can set a price effective today, and sell that sku today, tomorrow, the next day, etc...

All skus sold from today up to the day I set the next new price should be priced by eff_date of today.

Thats why it's tricky. This is a COMMON database design so there has to be a way.

Reply With Quote
  #4  
Old November 3rd, 2009, 08:18 AM
micky's Avatar
micky micky is offline
Couch Potato Wizard
Click here for more information. Click here for more information
 
Join Date: Jan 2005
Location: India
Posts: 12,259 micky User rank is General 18th Grade (Above 100000 Reputation Level)micky User rank is General 18th Grade (Above 100000 Reputation Level)micky User rank is General 18th Grade (Above 100000 Reputation Level)micky User rank is General 18th Grade (Above 100000 Reputation Level)micky User rank is General 18th Grade (Above 100000 Reputation Level)micky User rank is General 18th Grade (Above 100000 Reputation Level)micky User rank is General 18th Grade (Above 100000 Reputation Level)micky User rank is General 18th Grade (Above 100000 Reputation Level)micky User rank is General 18th Grade (Above 100000 Reputation Level)micky User rank is General 18th Grade (Above 100000 Reputation Level)micky User rank is General 18th Grade (Above 100000 Reputation Level)micky User rank is General 18th Grade (Above 100000 Reputation Level)micky User rank is General 18th Grade (Above 100000 Reputation Level)micky User rank is General 18th Grade (Above 100000 Reputation Level)micky User rank is General 18th Grade (Above 100000 Reputation Level)micky User rank is General 18th Grade (Above 100000 Reputation Level)  Folding Points: 1480 Folding Title: Novice Folder
Time spent in forums: 5 Months 4 Days 2 m 48 sec
Reputation Power: 2179
Something on lines of
Code:
SELECT 
	L.LINE_DATE,
	L.SKU,
	L.QTY,
	P.UNIT_PRICE
FROM
	LINEITEMS L INNER JOIN 
	SKU_PRICE P ON 
	L.SKU = P.SKU 
        WHERE P.EFF_DATE<=L.LINE_DATE Order By P.EFF_DATE

Reply With Quote
  #5  
Old November 3rd, 2009, 08:21 AM
garyrobar garyrobar is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Aug 2006
Posts: 113 garyrobar User rank is Corporal (100 - 500 Reputation Level)garyrobar User rank is Corporal (100 - 500 Reputation Level)garyrobar User rank is Corporal (100 - 500 Reputation Level)garyrobar User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Day 3 h 23 m 34 sec
Reputation Power: 5
heh, still doesn't work. Of course, it should return 1 price for a given date.

In your example, if there were previous price changes, it would return a row for each of those.

Reply With Quote
  #6  
Old November 3rd, 2009, 08:24 AM
micky's Avatar
micky micky is offline
Couch Potato Wizard
Click here for more information. Click here for more information
 
Join Date: Jan 2005
Location: India
Posts: 12,259 micky User rank is General 18th Grade (Above 100000 Reputation Level)micky User rank is General 18th Grade (Above 100000 Reputation Level)micky User rank is General 18th Grade (Above 100000 Reputation Level)micky User rank is General 18th Grade (Above 100000 Reputation Level)micky User rank is General 18th Grade (Above 100000 Reputation Level)micky User rank is General 18th Grade (Above 100000 Reputation Level)micky User rank is General 18th Grade (Above 100000 Reputation Level)micky User rank is General 18th Grade (Above 100000 Reputation Level)micky User rank is General 18th Grade (Above 100000 Reputation Level)micky User rank is General 18th Grade (Above 100000 Reputation Level)micky User rank is General 18th Grade (Above 100000 Reputation Level)micky User rank is General 18th Grade (Above 100000 Reputation Level)micky User rank is General 18th Grade (Above 100000 Reputation Level)micky User rank is General 18th Grade (Above 100000 Reputation Level)micky User rank is General 18th Grade (Above 100000 Reputation Level)micky User rank is General 18th Grade (Above 100000 Reputation Level)  Folding Points: 1480 Folding Title: Novice Folder
Time spent in forums: 5 Months 4 Days 2 m 48 sec
Reputation Power: 2179
ya i know

Do you have any primary key field in prices table?

Reply With Quote
  #7  
Old November 3rd, 2009, 09:52 AM
garyrobar garyrobar is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Aug 2006
Posts: 113 garyrobar User rank is Corporal (100 - 500 Reputation Level)garyrobar User rank is Corporal (100 - 500 Reputation Level)garyrobar User rank is Corporal (100 - 500 Reputation Level)garyrobar User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Day 3 h 23 m 34 sec
Reputation Power: 5
there is a row_key field - just a incremented integer, but composite key is the date field + sku

Reply With Quote
  #8  
Old November 4th, 2009, 03:18 AM
micky's Avatar
micky micky is offline
Couch Potato Wizard
Click here for more information. Click here for more information
 
Join Date: Jan 2005
Location: India
Posts: 12,259 micky User rank is General 18th Grade (Above 100000 Reputation Level)micky User rank is General 18th Grade (Above 100000 Reputation Level)micky User rank is General 18th Grade (Above 100000 Reputation Level)micky User rank is General 18th Grade (Above 100000 Reputation Level)micky User rank is General 18th Grade (Above 100000 Reputation Level)micky User rank is General 18th Grade (Above 100000 Reputation Level)micky User rank is General 18th Grade (Above 100000 Reputation Level)micky User rank is General 18th Grade (Above 100000 Reputation Level)micky User rank is General 18th Grade (Above 100000 Reputation Level)micky User rank is General 18th Grade (Above 100000 Reputation Level)micky User rank is General 18th Grade (Above 100000 Reputation Level)micky User rank is General 18th Grade (Above 100000 Reputation Level)micky User rank is General 18th Grade (Above 100000 Reputation Level)micky User rank is General 18th Grade (Above 100000 Reputation Level)micky User rank is General 18th Grade (Above 100000 Reputation Level)micky User rank is General 18th Grade (Above 100000 Reputation Level)  Folding Points: 1480 Folding Title: Novice Folder
Time spent in forums: 5 Months 4 Days 2 m 48 sec
Reputation Power: 2179
What if we try it like this, as we just need 1 record, assuming database is MS SQL Server
Code:
SELECT TOP 1
	L.LINE_DATE,
	L.SKU,
	L.QTY,
	P.UNIT_PRICE
FROM
	LINEITEMS L INNER JOIN 
	SKU_PRICE P ON 
	L.SKU = P.SKU 
        WHERE P.EFF_DATE<=L.LINE_DATE Order By P.EFF_DATE

Reply With Quote
Reply

Viewing: ASP Free ForumsDatabaseSQL Development > Query - General - Best way to Join "Lineitems" & "SKU Price Hist" Tables?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump





 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek