473,387 Members | 3,684 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,387 software developers and data experts.

query to find a daily value difference

1
Hi,
I m a new SQL leaner, and got my self into a difficult question,
let's say i have this table:
Expand|Select|Wrap|Line Numbers
  1. create table purchase(
  2.     datePurchase date  
  3.     itemiId int,
  4.     nbItemSold int
  5.     cstmrId int
  6. );
How can I get the daily difference of the product sold ?

Ex:
Expand|Select|Wrap|Line Numbers
  1. insert into purchase values
  2. ('2017-02-01' ,4, 'product1'),
  3. ('2017-02-01', 3, 'product2'),
  4. ('2017-02-01', 1, ' product3'),
  5. ('2017-02-02', 1, 'product1'),
  6. ('2017-02-02', 2, 'product2'),
  7. ('2017-02-02', 1, 'product3'),
  8. ('2017-02-03', 5, 'product2'), 
  9. ('2017-02-03', 1, 'product3'),
  10. ('2017-02-03', 0, 'product1');
  11.  
Query result would be (getting the difference between the number of the product sold on the D day with those sold on the D-1 day):
the final table resulting of these difference should contain this datas

2017-02-02, product1, -3
2017-02-02, product2, -1
2017-02-02, product3, 1
2017-02-03, product1, -1
2017-02-03, product2, 3
2017-02-03, product3, 0

the customer doesn't matter. i just want to find the daily difference in selling products
Mar 18 '17 #1
1 952
jforbes
1,107 Expert 1GB
You could do something like this:
Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2.   Today.ItemID
  3. , Today.dtePurchase
  4. , Today.nbItemSold
  5. , PrevDay.nbItemSold
  6. , Nz(Today.nbItemSold, 0)-Nz(PrevDay.nbItemSold, 0) AS DailyDifference
  7. FROM ItemSalesHistory AS Today
  8. LEFT JOIN ItemSalesHistory AS PrevDay
  9. ON Today.dtePurchase=PrevDay.dtePurchase+1
  10. AND Today.ItemID=PrevDay.ItemID
  11. ORDER BY Today.dtePurchase, Today.ItemID
Mar 20 '17 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: clickon | last post by:
I want to use an INSERT Query as the value for the UpdateCommand property of an SQL datasource, anyone know if this will actually work ? The reason i want to do it is bacause i am taking values...
2
by: Ros | last post by:
Peeps, I need help with trying to find a value in array of arrays. Public Module myModule Private Site_Access() As Array Property Set_SA() Get Return Site_Access End Get
1
by: umeshdchaudhari | last post by:
fist I filled value of drow down and GridView control. Now we find the databound of grid view control as per drow down value at time binding. ? How we find the value of control time of binding in...
2
by: donpro | last post by:
Hi, Does anyone know how to use DOM to find the value of a selected radio both (or if none were selected)? My web page displays a set of radio buttons - the number is dynamic so could be one...
1
by: Kenneth Jones | last post by:
How Can You Successfully Query a NULL value in a Text formatted Table Column? This is a known problem per internet articles I have read and I cannot get the query to execute properly.
2
by: Jackson Werid | last post by:
Is it posible to use only stdio.h library to find roundup value, and find all the factor of a number I am wondering how roundup is work with using a function but not cmath library, and how to...
3
by: raamay | last post by:
can anybody tell me why the following query returns null value. SELECT * FROM yjdf4_tbltenders AS a LEFT JOIN yjdf4_tblclass AS b ON a.cls = b.clid LEFT JOIN yjdf4_tblcategory AS c ON...
13
by: chintan85 | last post by:
I have a table1 id col1 col2 1 NC_001A>T 5 2 NC_001C>G 4 3 NC_001G>C 3 4 NC_001_98_G>C 1 Now, I want to have max col2 value for...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.