Subtract the second row from the first row in a Query

brandon20

Board Regular
Joined
Feb 12, 2014
Messages
203
Hi,

I was wondering how this would be possible. I come across this kind of problem very often so finding a solution would be amazing!!

I need to subtract the second row from the first row for each well in my access query.

I have data with three fields.

[Name]
[Surface]
[Depth]

This is the table I have

NameSurfaceDepth
Well1A12
Well1B21
Well1C34
Well1D40
Well2A5
Well2B15
Well2C25
Well2D20

<tbody>
</tbody>


What I need: I need to subtract the second row [Depth] from the first row [Depth] for each well in [Name]
As you see below I call this field [Thickness] =SecondRow[Depth]-FirstRow[Depth]

I also drop the row which has last or Max [Depth] for each well in [Name] because I am calculating the thickness and keeping the final row would be redundant.
(this part can be left out if it is too complicated)


NameSurfaceDepthNameSurfaceDepthThickness
Well1A12Well1B208
Well1B21Well1C309
Well1C34Well1D406
Well2A5Well2B1510
Well2B15Well2C2510
Well2C25Well2D3510

<tbody>
</tbody>

Any help is greatly appreciated!
Thanks!
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Hi,

The data in your first table contradicts the data in your second table according to your description, until you clarify (numbers don't match up), it'll be very difficult for anyone to help.
 
Last edited:
Upvote 0
Ah man!!

I dont know how that happend. I really dont like the fact that I cant just cut and paste a table from Excel into this forum.
Or at least I dont know how. I messed up the data writing it in by hand.....

Hopefully this table imports well, although they never do.
Let me know if you have any other questions,


what i have

NameSurfaceDepth
Well1A12
Well1B21
Well1C34
Well1D40
Well2A5
Well2B15
Well2C25
Well2D35

<tbody>
</tbody>



what i need

NameSurfaceDepthNameSurfaceDepthThickness
Well1A12Well1B219
Well1B21Well1C3413
Well1C34Well1D406
Well2A5Well2B1510
Well2B15Well2C2510
Well2C25Well2D3510

<tbody>
</tbody>
 
Upvote 0
Do you need help with building the entire second table?, or you already have the second table setup, and just need the formula for "Thickness" column?
 
Upvote 0
really just getting the thickness equation would help.

I just dont know a way to write an equation that would subtract the second row depth from the first row depth of for each well in [Name].

That would solve most of my problems.

Thanks.
 
Upvote 0
really just getting the thickness equation would help.

I just dont know a way to write an equation that would subtract the second row depth from the first row depth of for each well in [Name].

That would solve most of my problems.

Thanks.

Not sure if I understand what you need, because if you already have your second table setup, then it's just a matter of subtracting the H column from the K column...like =K2-H2
But I'll just give you a suggestion based only on your first table...


Excel 2010
ABCDEFGHIJKL
1NameSurfaceDepthThicknessNameSurfaceDepthNameSurfaceDepthThickness
2Well1A12 Well1A12Well1B219
3Well1B219Well1B21Well1C3413
4Well1C3413Well1C34Well1D406
5Well1D406Well2A5Well2B1510
6Well2A5Well2B15Well2C2510
7Well2B1510Well2C25Well2D3510
8Well2C2510
9Well2D3510
Sheet1
Cell Formulas
RangeFormula
L2=K2-H2
D2=IF(A2=A1,C2-C1,"")


Copy D2 formula down.
 
Last edited:
Upvote 0
Hey Thanks,

I know how to do this in excel, it is easy.

This is an access forum. I am looking for the answer in ms sql.

I have one table. with the three fields, [name],[surface],[depth]

From this one table I need to figure out how to do something similar to that excel equation.

I am not great at sql. Finding out how to create this in access is a little bit harder,
im sure I could do it in some round about way but I was just asking if there was a best practice to this kind of question because it is so basic.
 
Upvote 0

Forum statistics

Threads
1,221,828
Messages
6,162,215
Members
451,752
Latest member
freddocp

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top