473,396 Members | 1,891 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,396 software developers and data experts.

Difference between rows in a table

Hello All,
Please help me with the SQL Query.
The table structure is:

EffectiveDate Amount
08/31/2008 400
09/30/2008 350
10/31/2008 200
11/30/2008 500
12/31/2008 100

I want to find the difference between any two rows provided the effective date.
For example: @Month=11/30/3008 then i want the output of the stored procedure to be like
Year Month Quarter
500 300 150


i.e for year it has to display the Amount for the @Month(11/30/2008)
for month it has to display the difference of Amount for 11/30/2008 and 10/31/2008 (500-300)
for Quarter it has to display the difference of Amount for 11/30/2008 and 09/30/2008. (500-350)
Can somebody help me with the logic for this.
Nov 18 '08 #1
3 1871
ck9663
2,878 Expert 2GB
Looks like you're getting the difference between specific time periods. Did you just display 500 for 11/30/2008 because there are no other records? What if you run your application on 11/30/2009?

-- CK
Nov 18 '08 #2
The month is a parameter. It can be any date. The corresponding amount will be stored in the table.

I need the amount for the month (selected by user for example 11/30/2008),
diffrence between the amount for 11/30/2008 and 10/31/2008(amount month-amount previous month)
difference between the amount for 11/30/2008 and 09/30/2008 (amount month-amount of the quarter)
Nov 18 '08 #3
ck9663
2,878 Expert 2GB
Try this sample code:

Expand|Select|Wrap|Line Numbers
  1. declare @tablename table (EffectiveDate smalldatetime, Amount money)
  2.  
  3. insert into @tablename values('08/31/2008', 400)
  4. insert into @tablename values('09/30/2008', 350)
  5. insert into @tablename values('10/31/2008', 200)
  6. insert into @tablename values('11/30/2008', 500)
  7. insert into @tablename values('12/31/2008', 100)
  8.  
  9.  
  10. declare @inputdate as smalldatetime
  11.  
  12. set @inputdate = '11/30/2008'
  13.  
  14. select * from @tablename
  15.  
  16. select @inputdate as input, 
  17. year_value = Amount, 
  18. month_value = (select t1.Amount - t2.amount from @tablename t2 where DATEDIFF(MONTH, t2.EffectiveDate, @inputdate) = 1),
  19. quarter_value = (select t1.Amount - t2.amount from @tablename t2 where DATEDIFF(MONTH, t2.EffectiveDate, @inputdate) = 2)
  20. from @tablename t1
  21. where  @inputdate = EffectiveDate
  22.  
One catch, there should be only one row per month.

This will be your resultset:

Expand|Select|Wrap|Line Numbers
  1. EffectiveDate           Amount
  2. ----------------------- ---------------------
  3. 2008-08-31 00:00:00     400.00
  4. 2008-09-30 00:00:00     350.00
  5. 2008-10-31 00:00:00     200.00
  6. 2008-11-30 00:00:00     500.00
  7. 2008-12-31 00:00:00     100.00
  8.  
  9. input                   year_value            month_value           quarter_value
  10. ----------------------- --------------------- --------------------- ---------------------
  11. 2008-11-30 00:00:00     500.00                300.00                150.00
  12.  

-- CK
Nov 19 '08 #4

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

Similar topics

1
by: Jason | last post by:
I needed to get a list of rows from a table that is not present in another table. My problem lies in the fact that I only want SOME of the rows in table 2 used in determining existance. This...
5
by: mas | last post by:
I have a Stored Procedure (SP) that creates the data required for a report that I show on a web page. The SP does all the work and just returns back a results set that I dump in an ASP.NET...
3
by: RobG | last post by:
A little while ago I opined that do/while loops are harder to read than for loops, and therefore I preferred using for loops. However, it was pointed out that do/while has significant...
8
by: Paulo Jan | last post by:
Hi all: I have here a table with the following schema: Table "todocinetv" Column | Type | Modifiers...
5
by: BBFrost | last post by:
Win2000 ..Net 1.1 SP1 c# using Visual Studio Ok, I'm currently in a "knock down - drag out" tussle with the .Net 1.1 datagrid. I've come to realize that a 'block' of rows highlighted within...
9
by: HC | last post by:
Hello, all, I started out thinking my problems were elsewhere but as I have worked through this I have isolated my problem, currently, as a difference between MSDE and SQL Express 2005 (I'll just...
4
by: Jane T | last post by:
I appreciate how difficult it is to resolve a problem without all the information but maybe someone has come across a similar problem. I have an 'extract' table which has 1853 rows when I ask for...
3
by: bbawa1 | last post by:
Hi, I have a table which has a field ItemsReceived of type datetime. I have a grid view which has two columns. In first column i have to show the data from field ItemsReceived and in second...
3
by: Sandman | last post by:
Hi, So I read the manual where it says to use mysql_affected_rows() for everything except SELECT and SHOW, and use mysql_num_rows() for those two, which actually return a result. However, I...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
tracyyun
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.