473,594 Members | 2,768 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is there a better way to handle a conditional sum?

Bob
I need to know if there is a better way to construct this SQL statement.
(Error handling is omitted)

MS SQL Server 2000

Insert into FSSUTmp
Select a.acct_no, a.ac_nm, a.ac_type, 10, -1,
-1 * sum(CHARINDEX(c onvert(char(4), b.post_yr), @post_yr) * CHARINDEX('-',
CONVERT(char(2) , b.post_prd - @post_prd - 1)) * b.prd_trn_amt),
-1 * sum(CHARINDEX(c onvert(char(4), b.post_yr), @post_yr) * CHARINDEX('0',
CONVERT(char(1) , b.post_prd)) * b.prd_trn_amt)
FROM GLAccounts a, GLBalances b
WHERE b.cmpny_cd = a.cmpny_cd
AND b.acct_no = a.acct_no
AND a.cmpny_cd = @cmpny_cd
AND ac_ctrl_type between '200' and '219'
Group by a.acct_no, a.ac_nm, a.ac_type

The part I’m wondering about is the 2 sum sections.
The GLBalances table has following important fields:
Post_yr -- the posting year
Post_prd – the posting period
Prd_trn_amt – The beginning balances if the period is 0, or the net
transactions for periods 1 through 12.

The first sum gives the current balance as of the period @post_prd by adding
all of the periods from 0 to @post-prd
The second sum is just the beginning balance.

It is doing a conditional sum by using CHARINDEX to be 0 if the record
should not be added and 1 if it should.

There is a problem as it stands when you are looking for the balances when
the @post_prd is 9 or greater because “b.post_prd - @post_prd – 1” will
be –10 or smaller. Then the CONVERT(char(2) ….. is an error, so CHARINDEX
is 0 when it needs to be 1.

I can fix that by using SIGN and it will work fine. What I what to know, is
there a better way to populate the table, where one of the values is a
conditional sum?

This is a STORED PROCEDURE from a commercial product, so I can’t change
anything else other than the STORED PROCEDURE.
Jul 20 '05 #1
1 8638
I think CASE is what you are looking for:

INSERT INTO FSSUTmp
SELECT acct_no, ac_nm, ac_type, 10, -1,
SUM(prd_trn_amt ),
SUM(CASE post_prd WHEN 0 THEN prd_trn_amt END)
FROM GLAccounts
WHERE cmpny_cd = @cmpny_cd
AND ac_ctrl_type BETWEEN '200' AND '219'
AND post_prd <= @post_prd
AND post_yr = @post_yr
GROUP BY acct_no, ac_nm, ac_type

--
David Portas
------------
Please reply only to the newsgroup
--
Jul 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

133
8494
by: Gaurav | last post by:
http://www.sys-con.com/story/print.cfm?storyid=45250 Any comments? Thanks Gaurav
3
3368
by: valued customer | last post by:
Is there a more concise way to do something like the the desired code below? The gripe is with the try-catch syntax. It takes *way* too many lines of code to evaluate a conditional expression when zero or more parts of the conditional expression may trigger an error. In this case, the trigger is a call to a non-defined (null) object. In other words, how can you do a more simple 'try' statement
28
3430
by: Benjamin Niemann | last post by:
Hello, I've been just investigating IE conditional comments - hiding things from non-IE/Win browsers is easy, but I wanted to know, if it's possible to hide code from IE/Win browsers. I found <!> in the original MSDN documentation, but this is (although it is working) unfortunately non-validating gibberish. So I fooled around trying to find a way to make it valid. And voila: <!--><!><!-->
4
3130
by: Bradley | last post by:
I have an A2000 database in which I have a continuous form with a tick box. There is also a text box with a conditional format that is based on the expression , if it's true then change the background colour. In A2000 it works great, but in A2003 the background doesn't always change and when it does it only changes when the record looses the focus. Any way around this? Is it a bug? Or have they "improved" it?
10
28617
by: John Smith | last post by:
After reading C# documentation the Conditional attribute seemed the way to go, but after inspecting the IL it seems those methods are still there and I imagine the CLR removes them. Using #if DEBUG means the code does not even reach the IL when compiling in release mode. Is there any benefit to using the Conditional Attribute? Am I right in thinking there will small performance overhead using over #if DEBUG.
8
8473
by: Typehigh | last post by:
I have many text fields with conditional formatting applied, specifically when the condition is "Field Has Focus". Without any events associated with the fields the conditional formatting works perfectly. However, I have code that runs under the ON CLICK event. The code changes the focus to other controls, which means the conditional formatting is no longer displayed. This part makes sense to me. Here's what doesn't make sense. The last...
5
2895
by: paulo | last post by:
Can anyone please tell me how the C language interprets the following code: #include <stdio.h> int main(void) { int a = 1; int b = 10; int x = 3;
3
3242
by: damodharan | last post by:
Hello and thanks in advance. I have two table in DB2 (ver 8). table details are table one(Cust_det) has two fields (Cust_cin and cust_name). table two(cust_add_det) has cust_cin and cust_address fields. To join above two table which sql is better on perfomance point of view? and how conditional where clause works?
5
1393
by: Nitesh | last post by:
Objects of type X are basic building blocks of my application and there are a few functions which return XHandle (typedef X** XHandle). The task at hand needs to use a couple of these functions and then iterate over X objects. Because its done frequently and it separates iteration from task-at- hand'logic we decided to create iterator. Assuming the availability of functions GetXHandleSize(XHandle) and DisposeXHandle(XHandle) there are...
0
7936
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7874
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8366
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7997
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6646
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5738
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5402
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3893
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1469
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.