MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesMS SQL 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 Dev Shed Forums Sponsor:
  #1  
Old January 14th, 2013, 01:44 PM
jdbrau03 jdbrau03 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 3 jdbrau03 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 24 m 32 sec
Reputation Power: 0
Question Issue pulling data from table

I am using MSSQL server 2008 r2 and I am having trouble pulling data from a inventory price history table. The price history table is laid out as follows. Store # ITem # date price changed price
The store # and Item # form the primary key for the table. I have been trying to figure out a way to get the last two price updates to generate a query to show if the price has changed by more than 25%. My issue is how to get those last two price changes. If anyone can help me with how to separate out those last two for each store # item # combo I would greatly appreciate it

Reply With Quote
  #2  
Old January 14th, 2013, 05:58 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,376 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 7 h 35 m 43 sec
Reputation Power: 4140
Quote:
Originally Posted by jdbrau03
The store # and Item # form the primary key for the table.
no, they don't

if they did, you could only have one row per store# and item#, and you wouldn't be asking for the last two
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Reply With Quote
  #3  
Old January 15th, 2013, 08:58 AM
jdbrau03 jdbrau03 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 3 jdbrau03 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 24 m 32 sec
Reputation Power: 0
You are correct, thank you for catching my error. It would be the those two and the datetime feild as to when the record was changed
Quote:
Originally Posted by r937
no, they don't

if they did, you could only have one row per store# and item#, and you wouldn't be asking for the last two

Reply With Quote
  #4  
Old January 18th, 2013, 05:21 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,376 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 7 h 35 m 43 sec
Reputation Power: 4140
Code:
SELECT store_no
     , item_no
     , date_price_changed
     , price
  FROM price_history AS t
 WHERE ( SELECT COUNT(*) 
           FROM price_history  
          WHERE store_no = t.store_no
            AND item_no = t.item_no
            AND date_price_changed > t.date_price_changed ) < 2
Comments on this post
jdbrau03 agrees!

Reply With Quote
  #5  
Old January 18th, 2013, 08:12 AM
jdbrau03 jdbrau03 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 3 jdbrau03 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 24 m 32 sec
Reputation Power: 0
Thumbs up Right on!

That is much more straight forward and faster than what I had come up with. Thank you very much for the help and teaching me a few things as well
Quote:
Originally Posted by r937
Code:
SELECT store_no
     , item_no
     , date_price_changed
     , price
  FROM price_history AS t
 WHERE ( SELECT COUNT(*) 
           FROM price_history  
          WHERE store_no = t.store_no
            AND item_no = t.item_no
            AND date_price_changed > t.date_price_changed ) < 2

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Issue pulling data from table

Developer Shed Advertisers and Affiliates



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

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


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap