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

Calculations between two different rows same Table

Hi

I am trying to solve this, I hope somebody can help me. I have a table which has sales per month per year

Table1

Column1 Month1 Month2 Month3 Month4 .... Total
----------------------------------------------------------------------------------------
Year07
Year08

and I would like to calculate the grown per month an total, so I would like to create an additional row

Column1 Month1 Month2 Month3 Month4 .... Total
----------------------------------------------------------------------------------------
Year07
Year08
Grown

where Grown is calculated as " (Second Row/First Row - 1)" this by every "Month" and Total.

Does anyone give me any idea?

Thanks.
Mar 3 '08 #1
4 1757
ck9663
2,878 Expert 2GB
Hi

I am trying to solve this, I hope somebody can help me. I have a table which has sales per month per year

Table1

Column1 Month1 Month2 Month3 Month4 .... Total
----------------------------------------------------------------------------------------
Year07
Year08

and I would like to calculate the grown per month an total, so I would like to create an additional row

Column1 Month1 Month2 Month3 Month4 .... Total
----------------------------------------------------------------------------------------
Year07
Year08
Grown

where Grown is calculated as " (Second Row/First Row - 1)" this by every "Month" and Total.

Does anyone give me any idea?

Thanks.
I'm assuming column1 is the year column. try something like this:

Expand|Select|Wrap|Line Numbers
  1. select column1, (sr.month1/month1 - 1), (sr.month2/month2 - 1)
  2. (select top 1 sr.* from YourSalesTable sr where sr.column1 > YourSalesTable.column1) 
  3. from YourSalesTable
I did not really test this code, so you have to change it as necessary. I just hope you got what am trying to say.

Good luck.

-- CK
Mar 3 '08 #2
amitpatel66
2,367 Expert 2GB
Hi

I am trying to solve this, I hope somebody can help me. I have a table which has sales per month per year

Table1

Column1 Month1 Month2 Month3 Month4 .... Total
----------------------------------------------------------------------------------------
Year07
Year08

and I would like to calculate the grown per month an total, so I would like to create an additional row

Column1 Month1 Month2 Month3 Month4 .... Total
----------------------------------------------------------------------------------------
Year07
Year08
Grown

where Grown is calculated as " (Second Row/First Row - 1)" this by every "Month" and Total.

Does anyone give me any idea?

Thanks.
Try this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT * FROM table1
  3. UNION ALL
  4. SELECT 'Grown', (t.month1 / x.month1 - 1),(t.month1 / x.month1 - 1)
  5. ,(t.month2 / x.month2 - 1),(t.month3 / x.month3 - 1),(t.month4 / x.month4 - 1)
  6. ,(t.month5 / x.month5 - 1),(t.month6 / x.month6 - 1),(t.month7 / x.month7 - 1)
  7. ,(t.month8 / x.month8 - 1),(t.month9 / x.month9 - 1),(t.month10 / x.month10 - 1)
  8. ,(t.month11 / x.month11 - 1),(t.month12 / x.month12 - 1),(t.total/x.total - 1) FROM
  9. (SELECT d.* FROM table1 d WHERE column1 = 'Year07') t
  10. (SELECT y.* FROM table1 y WHERE column1 = 'Year08') x
  11.  
  12.  
Mar 4 '08 #3
Thanks

I feel this last answer is close. I just try it but I got error because of the last SELECT after the FROM.

That is what I am looking for, I mean is it possible to add two select sentences after the FROM so they can be the two sources to calculate?????
Mar 4 '08 #4
amitpatel66
2,367 Expert 2GB
Thanks

I feel this last answer is close. I just try it but I got error because of the last SELECT after the FROM.

That is what I am looking for, I mean is it possible to add two select sentences after the FROM so they can be the two sources to calculate?????
Oops, a small syntax error. Here it goes:

Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM table1
  2. UNION ALL
  3. SELECT 'Grown', (t.month1 / x.month1 - 1),(t.month1 / x.month1 - 1)
  4. ,(t.month2 / x.month2 - 1),(t.month3 / x.month3 - 1),(t.month4 / x.month4 - 1)
  5. ,(t.month5 / x.month5 - 1),(t.month6 / x.month6 - 1),(t.month7 / x.month7 - 1)
  6. ,(t.month8 / x.month8 - 1),(t.month9 / x.month9 - 1),(t.month10 / x.month10 - 1)
  7. ,(t.month11 / x.month11 - 1),(t.month12 / x.month12 - 1),(t.total/x.total - 1) FROM
  8. (SELECT d.* FROM table1 d WHERE column1 = 'Year07') t,
  9. (SELECT y.* FROM table1 y WHERE column1 = 'Year08') x
  10.  
Mar 4 '08 #5

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

Similar topics

0
by: Jim Moseby | last post by:
I stumbled across this while trying to update a table with a timestamp type column. At the time, I didn't know that the timstamp column would update itself when a row was changed. A kind gentleman...
3
by: brian kaufmann | last post by:
Hi, I had sent this earlier, and would appreciate any suggestions on this. I need to make calculations for unemployment rate for three different data sources (A,B,C) for many countries and age...
2
by: chudson007 | last post by:
This is a very general quastion..... I regularily take data extracts from a SQL server database drop them in excel and run a macro on the data. The macro does nothing more complicated than...
24
by: clare at snyder.on.ca | last post by:
I have a SQL query I need to design to select name and email addresses for policies that are due and not renewed in a given time period. The problem is, the database keeps the information for every...
3
by: Chifo | last post by:
hello. i have a problem with a populate html table with data from table here it's the problem two querys retrieving data from table, one of querys show me a colletion of data from 6:00 am to...
3
by: acecraig100 | last post by:
I am fairly new to Javascript. I have a form that users fill out to enter an animal to exhibit at a fair. Because we have no way of knowing, how many animals a user may enter, I created a table...
18
by: Neekos | last post by:
Ok so i know you shouldn't store calculations in a table, but what if i need to? i have calculations on one form, and i want to be able to show the value of that calculation on a different form based...
1
by: Grubsy4u | last post by:
Grubsy4u Newbie 7 Posts October 5th, 2007 11:31 AM #1 Report calculations --------------------------------------------------------------------------------
9
Catalyst159
by: Catalyst159 | last post by:
I have a form which is used to calculate residential Floor Area Ratio (FAR). The form is structured into seven parts as follows: Part A: Maximum FAR and Floor Area: Part B: Gross Floor Area of...
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...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.