473,396 Members | 2,092 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.

Maybe something very easy?

Hi

I've got a table with the following
MonthNr
ColumnNr
RowNr
Column01
......
Column12

I want to calc the movements between this month and last month
Can this be done in 1 sql statement?
I want to select * from table where monthnr = 11 and rownr between 1 and 10
"minus" select * from table where monthnr = 10 and rownr between 1 and 10
in order to calc the movement
It can be done in a trigger and cursor, but that will just complicate matters.

Any ideas, maybe I'm just missing something very basic.
Many thanks
Jan 14 '08 #1
6 1210
deepuv04
227 Expert 100+
Hi

I've got a table with the following
MonthNr
ColumnNr
RowNr
Column01
......
Column12

I want to calc the movements between this month and last month
Can this be done in 1 sql statement?
I want to select * from table where monthnr = 11 and rownr between 1 and 10
"minus" select * from table where monthnr = 10 and rownr between 1 and 10
in order to calc the movement
It can be done in a trigger and cursor, but that will just complicate matters.

Any ideas, maybe I'm just missing something very basic.
Many thanks
Can you explain indetail (with example if possible) exactly wat you want
Jan 15 '08 #2
Can you explain indetail (with example if possible) exactly wat you want
It would basically be a table with the structure

[#Year] char(4),
[#MonthNr] char(2),
[#RowNr] decimal(3, 0),
[#Column01] decimal(20, 4),
[#Column02] decimal(20, 4) ,
.....
.....
.....


With data for MonthNr 1 to 12 for the year.
i.e.
"2007", "10", 129, 1234.00, 4321.00
"2007", "11", 129, 1222.00, 4333.00
"2007", "10", 130, .........
...
...
"2007", "10", 140, 3234.00, 1321.00
"2007", "11", 140, 3222.00, 1333.00 .....

Now line 129 up to line 140 dataset that I use on the report. The users now require movements between the tow months; which is basically dataset for the line 129 to line 140 for month 11 minus the dataset for the 10th month.

I can use a cursor to do this, but is there not a way of saying this in just one sql statement something like

select
rownr, year,month,
column1 - (select column1 from table where rownr between 129 and 140 and year = 2007 and month = 10) as column1,
(same select statement for column2 as column1)
from table where rownr between 129 and 140 and year = 2007 and month = 11

When I try and run this sort of statement, the inner select returns more that 1 record, but thats what I want to do, dataset for 11th month minus the dataset from the 10th month

Any elegant solutions?
Jan 16 '08 #3
Delerna
1,134 Expert 1GB
Having difficulty understanding what you are after, but to answer you question is there an elegant solution then the answer is almost certainly Yes


"2007", "10", 129, 1234.00, 4321.00
"2007", "11", 129, 1222.00, 4333.00
"2007", "10", 130, .........
...
...
"2007", "10", 140, 3234.00, 1321.00
"2007", "11", 140, 3222.00, 1333.00 .....

1) In your first post you said line numbers between 1 and 10 and in the last tey are 129 to 140. Does this mean that there is no real boundary for the line numbers
2) do you want to subtract the sum of value1 of all the line numers for month 11 from the sum of value1 for month 10

3) Are there other years other that 2007, if so then there is a complication when it comes to subtracting the first month of 2008 from the last month of 2007. Or are you only ever going to do subtractions within the same year period?

Sorry to say this but a clearer description of the problem would be helpful
For example what are the line numbers, what are there ranges, are the values stock figures and the line numbers represent stock at a particular time .
Jan 16 '08 #4
Hi, let me try and give all the details, but what I really want is to subtract a dataset (dataset, i.e. line 129 to line 150, for month 11, columns 1 to 12)
from the previous months dataset (i.e. month 10)

Example
Line Month Column1 Column2 Column3 Column99
129 11 9,700 4,000 4,600 6,300
130 11 7,800 8,300 1,500 3,900
131 11 4,400 4,500 6,000 7,100
132 11 500 8,600 1,900 300
133 11 6,900 5,400 9,000 2,600

MINUS

Line Month Column1 Column2 Column3 Column99
129 10 9,100 1,400 3,200 8,600
130 10 7,600 9,600 400 4,700
131 10 6,500 1,800 2,100 3,100
132 10 4,900 4,900 300 9,200
133 10 7,000 8,900 1,500 4,700

DIFFERENCE

Line Month Column1 Column2 Column3 Column99
129 - 600 2,600 1,400 -2,300
130 - 200 -1,300 1,100 -800
131 - -2,100 2,700 3,900 4,000
132 - -4,400 3,700 1,600 -8,900
133 - -100 -3,500 7,500 -2,100

Sorry about the bad formatting, but hope this helps
Thx
Jan 17 '08 #5
Delerna
1,134 Expert 1GB
Thats better, now I understand what you are after, at least I think I do
I am going to assume you are only working with one years worth of data

This is hard without data or a server (at home at the moment) so I might make some mistakes. I normaly build my queries in a step by step process.
I think you can work it out and make the necessay adjustments

SELECT a.line,a.month,a.col1-isnull(b.col1,0) as col1,a.col2-isnull(b.col2,0) as col2,.......
FROM
(SELECT line,month,col1,col2,......
FROM YourTable) a
left join
(SELECT line,month,col1,col2,......
FROM YourTable) b on a.line=b.line and a.month=b.month-1
)z

SubQuery a has every months data and joins to subQuery b to get each months (in a) last months data
I hope that last sentence makes sense and that this helps you
Jan 17 '08 #6
SELECT a.line,a.month,a.col1-isnull(b.col1,0) as col1,a.col2-isnull(b.col2,0) as col2,.......
FROM
(SELECT line,month,col1,col2,......
FROM YourTable) a
left join
(SELECT line,month,col1,col2,......
FROM YourTable) b on a.line=b.line and a.month=b.month-1
)z
Hi Delerma

Thanks for the reply, Is this query still going to work for a dataset of records. I need for lines x to y to be minus-ed from the previous months lines x to y?
Just looking at the code it seems that you are saying that it is just this month's 1 line minus last month's 1 line?
Jan 21 '08 #7

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

Similar topics

2
by: Dave Smithz | last post by:
Hello there, In summary: How to make my password protected php scripts available for use to public, without letting them do anything they want to DB. Previously a shared hosting hosted MySQL...
9
by: Dave Smithz | last post by:
Hi, Summary: Best way to divide out the HTML and PHP in some code I inherited. How can I keep the HTML files separate? Full Details: I've now been working two weeks on PHP / MySQL code I...
8
by: sebb | last post by:
I'm kind of newbie to programming, but I thought of something and I want some opinions on that. It's about a new instruction block to do some cycles. I thought about that because it's not very...
9
by: Brian Kelley | last post by:
I have been using gettext and various utilities to provide internationalization for a wxPython application and have not really been liking the process. Essentially it uses a macro-style notation...
1
by: news.verizon.net | last post by:
Hi all, I have a website created by others and sort of meshed together. Here are a couple of links... http://www.ibco.net/honours/rc/ See how the navigation bars on the left come in behind this -...
9
by: Colin McGuire | last post by:
Hi, I have an report in Microsoft Access and it displays everything in the table. One column called "DECISION" in the table has either 1,2, or 3 in it. On my report it displays 1, 2, or 3. I want...
25
by: Amarendra GODBOLE | last post by:
Hi, I am working on a legacy user space app, which has been developed entirely in C, some 15 years ago. Needless to say, it does not even partially conform to any standard. My team is in the...
3
by: Michael | last post by:
Hello, I am trying to add some kind of counter in a datatable, i use SetValue but it don't semm to work. Here is the code System.Data.DataSet dsClubs = new System.Data.DataSet(); ...
2
by: Michael C | last post by:
"John Brock" <jbrock@panix.comwrote in message news:gfvqmb$70j$2@reader1.panix.com... Perl isn't that strict a language, you might find a few things a little more difficult in dot net. That...
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...
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
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...
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.