473,884 Members | 2,397 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

query to find a daily value difference

1 New Member
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 ?

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');
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 968
1,107 Recognized Expert Top Contributor
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

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 from tablea and binding them to controls, whose values i want to insert into tableb. I am using a detailsview control to do the databinding. If the detailsview is in edit mode then it displays the data from table A in the bound testbox controls...
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
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 GridView control ? - Umesh
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 or 100; I do this within a loop. At the moment, I'm giving each one the same name and ID - not sure if giving the same ID is correct or not. By default, no radio buttons
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.
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 find all the factor of a number by onlt stdio.h too.
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 a.wcategory = c.catid LEFT JOIN (yjdf4_tblclient AS d JOIN yjdf4_tblclscategory AS e ON d.clcatid = e.clsid ) ON a.clientagency = d.cid LEFT JOIN yjdf4_tblpaper AS f ON a.paper = f.pid LIMIT 0 , 30
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 partially duplicate col1 value
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.