INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Need help subtracting Money of Category from two different dates

Need help subtracting Money of Category from two different dates

(OP)
I have data saved everynight like so
There are 3 columns: date, category, money.
Date1 Cat1 ExtCost1
Date2
Cat2 ExtCost2
2016-11-10 Base Material 36526.31 2016-11-11 Base Material 36522.12
2016-11-10 Encasing Parts 34523.33 2016-11-11 Encasing Parts 34345.32
I want to be able to choose two dates for the same category and have it give the difference of the money. So I would like to see:
Base Material: 36526.31 - 36522.12 = 34.88
Encasing Parts: 34523.33 - 34345.32 = 178.01

CODE

SELECT --[RMI_ID]
      CAST(i1.[DateAdded] as Date) As Date1 ,i1.[Category] as Cat1,i1.[Extended Cost] as ExtCost1,
	  CAST(i2.[DateAdded] as Date) as Date2,i2.[Category] as Cat2,i2.[Extended Cost] as ExtCost2
  FROM [FreedomSIReports].[dbo].[RMInventoryByCategory] i1
  inner join [FreedomSIReports].[dbo].[RMInventoryByCategory] i2 on i2.[Category] = i1.[Category]
  Where CAST([i1.DateAdded] as Date) = '2016-11-14' and CAST([i2.DateAdded] as Date) = '2016-11-13'
  order by i1.dateAdded desc 

TIA

RE: Need help subtracting Money of Category from two different dates

(OP)
Dooh, sorry I see I put the i1 [i1.DateAdded] in the wrong place it should be i1.[DateAdded].
my bad

CODE

SELECT --[RMI_ID]
      CAST(i1.[DateAdded] as Date) As Date1, i1.[Category] as Cat1, i1.[Extended Cost] as ExtCost1,
	  CAST(i2.[DateAdded] as Date) as Date2, i2.[Category] as Cat2, i2.[Extended Cost] as ExtCost2,i1.[Extended Cost]-i2.[Extended Cost] as Thedifference
  FROM [FreedomSIReports].[dbo].[RMInventoryByCategory] i1
  inner join [FreedomSIReports].[dbo].[RMInventoryByCategory] i2 on i2.[Category] = i1.[Category]
  Where CAST(i1.[DateAdded] as Date) = '2016-11-14' and CAST(i2.[DateAdded] as Date) = '2016-11-13'
  order by i1.dateAdded desc 

RE: Need help subtracting Money of Category from two different dates

Is this how your data looks like?

Date1      Cat1           ExtCost1 Date2      Cat2           ExtCost2
2016-11-10 Base Material  36526.31 2016-11-11 Base Material  36522.12
2016-11-10 Encasing Parts 34523.33 2016-11-11 Encasing Parts 34345.32 

Have fun.

---- Andy

There is a great need for a sarcasm font.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close