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

Using Variables inplace of nested Queries

Hi Gang

I am working on fixing a corrupted database (A yearly proc did not handel the conversion from year to year and now I have to fix the resulting lines in the DB). Most of my fixes I have been able to adjust with simply SET statements or nested queries. To fix one field I need to either make a very complex nested query or find a way to set variables to queries. But first I have to check to confirm that each query is not null.

Please tell me if this is even possible. (or is there a way to initialize to a 0 value and then not subsitute if NULL?)

Here is an example of what I am trying to do:

DECLARE @q4TotRebate int, @sumRebate int, @q4TotRed int, @sumRedeemed int

SET @q4TotRebate = (Select n.TotalRebateAmount
FROM tblSummary as n
WHERE t.AccountNumber = n.AccountNumber
AND Quarter = '2006/Q4')
SET @sumRebate = (SELECT SUM(b.TransactionAmount)
FROM tblTransactionDetail as b
WHERE t.AccountNumber = b.AccountNumber
AND b.TransactionType = 'P'
and b.TransactionDate > '12/31/06'
AND b.TransactionDate < '01/06/07')
SET @q4TotRed = (Select SUM(g.QtrTotalRebateRedeemed)
FROM tblSummary as g
WHERE g.AccountNumber = t.AccountNumber
And Quarter < '2007/Q1')
SET @sumRedeemed = (SELECT SUM(m.TransactionAmount)
FROM tblTransactionDetail as m
WHERE t.AccountNumber = m.AccountNumber
AND m.TransactionType = 'R'
AND m.TransactionDate > '12/31/06'
AND m.TransactionDate < '01/06/07')

SET @q4TotRebate =
CASE
WHEN @q4TotRebate IS Null
THEN 0
WHEN @q4TotRebate IS NOT NULL
THEN @q4TotRebate
END

SET @sumRebate =
CASE
WHEN @sumRebate IS NULL
THEN 0
WHEN @sumRebate IS NOT NULL
THEN @sumRebate
END

SET @q4TotRed =
CASE
WHEN @q4TotRed is NULL
THEN 0
WHEN @q4TotRed IS NOT NULL
THEN @q4TotRed
END

SET @sumRedeemed =
CASE
WHEN @sumRedeemed IS NULL
THEN 0
WHEN @sumRedeemed IS NOT NULL
THEN @sumRedeemed
END

From tblTransactionSummary as t


Update tblTransactionSummary

Set TotalRebate =
CASE
WHEN ((@q4TotRebate + @sumRebate)-(@q4TotRed + @sumRedeemed)) < 0
THEN (TotalRebate - ((@q4TotRebate + @sumRebate)-(@q4TotRed + @sumRedeemed)))
WHEN ((@q4TotRebate + @sumRebate)-(@q4TotRed + @sumRedeemed)) > 0
THEN TotalRebate
END
Jan 22 '07 #1
6 2797
iburyak
1,017 Expert 512MB
Try this:

[PHP]DECLARE @q4TotRebate int, @sumRebate int, @q4TotRed int, @sumRedeemed int

SET @q4TotRebate = ISNULL((Select n.TotalRebateAmount
FROM tblSummary as n
WHERE t.AccountNumber = n.AccountNumber
AND Quarter = '2006/Q4'),0)

SET @sumRebate = ISNULL((SELECT SUM(b.TransactionAmount)
FROM tblTransactionDetail as b
WHERE t.AccountNumber = b.AccountNumber
AND b.TransactionType = 'P'
and b.TransactionDate > '12/31/06'
AND b.TransactionDate < '01/06/07'), 0)

SET @q4TotRed = ISNULL((Select SUM(g.QtrTotalRebateRedeemed)
FROM tblSummary as g
WHERE g.AccountNumber = t.AccountNumber
And Quarter < '2007/Q1'),0)

SET @sumRedeemed = ISNULL((SELECT SUM(m.TransactionAmount)
FROM tblTransactionDetail as m
WHERE t.AccountNumber = m.AccountNumber
AND m.TransactionType = 'R'
AND m.TransactionDate > '12/31/06'
AND m.TransactionDate < '01/06/07'),0)


Update tblTransactionSummary

Set TotalRebate =
CASE
WHEN ((@q4TotRebate + @sumRebate)-(@q4TotRed + @sumRedeemed)) < 0
THEN (TotalRebate - ((@q4TotRebate + @sumRebate)-(@q4TotRed + @sumRedeemed)))
WHEN ((@q4TotRebate + @sumRebate)-(@q4TotRed + @sumRedeemed)) > 0
THEN TotalRebate
END[/PHP]
Jan 22 '07 #2
This is great, Thanks!

The only problem I am now having (and I had before) is where do I place the "FROM tblTransactionSummary as t" statement? I need to make sure that the account numbers used in the child queries "b, m, n, r" match that of the parent query "t".

Normally the FROM statement goes after the SELECT (or in this case SET) statement. But if I put it at the end, then in initiallizing the variables it does not yet initialized the "t".

The error message is as follows:

"Server: Msg 107, Level, 16 State 2, Line 3
The column prefix 't' does not match with a table name or alias name used in the query"

Please help.
Jan 23 '07 #3
ok, I was setting the variables early- I think... (it now works, but now 0 rows are affected- may be a logical operator)


The code needs to appear as follows:


DECLARE @q4TotRebate int, @sumRebate int, @q4TotRed int, @sumRedeemed int

Update tblTransactionSummary

Set TotalRebate =
CASE
WHEN ((@q4TotRebate + @sumRebate)-(@q4TotRed + @sumRedeemed)) < 0
THEN (TotalRebate - ((@q4TotRebate + @sumRebate)-(@q4TotRed + @sumRedeemed)))
WHEN ((@q4TotRebate + @sumRebate)-(@q4TotRed + @sumRedeemed)) > 0
THEN TotalRebate
END

From tblTransactionSummary as t
WHERE

@q4TotRebate = ISNULL((Select n.TotalRebateAmount
FROM tblSummary as n
WHERE t.AccountNumber = n.AccountNumber
AND Quarter = '2006/Q4'),0)
AND
@sumRebate = ISNULL((SELECT SUM(b.TransactionAmount)
FROM tblTransactionDetail as b
WHERE t.AccountNumber = b.AccountNumber
AND b.TransactionType = 'P'
and b.TransactionDate > '12/31/06'
AND b.TransactionDate < '01/06/07'), 0)
AND
@q4TotRed =ISNULL((Select SUM(g.QtrRebateRedeemed)
FROM tblSummary as g
WHERE g.AccountNumber = t.AccountNumber
And Quarter < '2007/Q1'),0)
AND
@sumRedeemed = ISNULL((SELECT SUM(m.TransactionAmount)
FROM tblTransactionDetail as m
WHERE t.AccountNumber = m.AccountNumber
AND m.TransactionType = 'R'
AND m.TransactionDate > '12/31/06'
AND m.TransactionDate < '01/06/07'),0)
Jan 23 '07 #4
iburyak
1,017 Expert 512MB
Your update should look like this. I might made some mistakes with () and you should fix it. But main idea is to replace variables with actual statements.


[PHP]Update tblTransactionSummary

Set TotalRebate =
CASE
WHEN ((ISNULL((Select n.TotalRebateAmount FROM tblSummary as n
WHERE t.AccountNumber = n.AccountNumber AND Quarter = '2006/Q4'),0) +
ISNULL((SELECT SUM(b.TransactionAmount) FROM tblTransactionDetail as b
WHERE t.AccountNumber = b.AccountNumber AND b.TransactionType = 'P' and b.TransactionDate > '12/31/06'
AND b.TransactionDate < '01/06/07'), 0)
)-
(ISNULL((Select SUM(g.QtrRebateRedeemed) FROM tblSummary as g
WHERE g.AccountNumber = t.AccountNumber And Quarter < '2007/Q1'),0) +
ISNULL((SELECT SUM(m.TransactionAmount) FROM tblTransactionDetail as m
WHERE t.AccountNumber = m.AccountNumber AND m.TransactionType = 'R' AND m.TransactionDate > '12/31/06'
AND m.TransactionDate < '01/06/07'),0))
) < 0
THEN (TotalRebate - ((ISNULL((Select n.TotalRebateAmount FROM tblSummary as n
WHERE t.AccountNumber = n.AccountNumber AND Quarter = '2006/Q4'),0) +
ISNULL((SELECT SUM(b.TransactionAmount) FROM tblTransactionDetail as b
WHERE t.AccountNumber = b.AccountNumber AND b.TransactionType = 'P' and b.TransactionDate > '12/31/06'
AND b.TransactionDate < '01/06/07'), 0))-
(ISNULL((Select SUM(g.QtrRebateRedeemed) FROM tblSummary as g
WHERE g.AccountNumber = t.AccountNumber And Quarter < '2007/Q1'),0) +
ISNULL((SELECT SUM(m.TransactionAmount) FROM tblTransactionDetail as m
WHERE t.AccountNumber = m.AccountNumber AND m.TransactionType = 'R' AND m.TransactionDate > '12/31/06'
AND m.TransactionDate < '01/06/07'),0))
)
)
WHEN ((ISNULL((Select n.TotalRebateAmount FROM tblSummary as n
WHERE t.AccountNumber = n.AccountNumber AND Quarter = '2006/Q4'),0) +
ISNULL((SELECT SUM(b.TransactionAmount) FROM tblTransactionDetail as b
WHERE t.AccountNumber = b.AccountNumber AND b.TransactionType = 'P' and b.TransactionDate > '12/31/06'
AND b.TransactionDate < '01/06/07'), 0))-

(ISNULL((Select SUM(g.QtrRebateRedeemed) FROM tblSummary as g
WHERE g.AccountNumber = t.AccountNumber And Quarter < '2007/Q1'),0) +
@sumRedeemed)
) > 0
THEN TotalRebate
END

From tblTransactionSummary as t[/PHP]

Good Luck.
Jan 23 '07 #5
With a little tweeking of the (), this works PERFECT. Thanks so much!
Jan 23 '07 #6
iburyak
1,017 Expert 512MB
I saw it had a problem and couldn't fix it because didn't have data to update and test ... :)
Jan 23 '07 #7

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

Similar topics

12
by: Tim Daneliuk | last post by:
I am a bit confused. I was under the impression that: class foo(object): x = 0 y = 1 means that x and y are variables shared by all instances of a class. But when I run this against two...
3
by: Michael Sparks | last post by:
Hi, I'm posting a link to this since I hope it's of interest to people here :) I've written up the talk I gave at ACCU Python UK on the Kamaelia Framework, and it's been published as a BBC...
17
by: beliavsky | last post by:
Many of my C++ programs have the line using namespace std; but the "Accelerated C++" book of Koenig and Moo has many examples where the library names are included one at a time, for example ...
5
by: ahokdac-sql | last post by:
Hi, I'm adapting access queries to sql server and I have difficulties with the following pattern : query1 : SELECT * FROM Query2 WHERE A=@param1 query 2: SELECT * FROM Table2 WHERE B=@param2 ...
9
by: Joel Moore | last post by:
I'm a little confused here. If I have the following: Public ClassA Friend varA As Integer Private varB As Integer Private ClassB Public Sub MethodA() ' How can I access varA and varB here?...
2
by: Roy | last post by:
Hey all, Here's a small VB codeblock that connects to a database and uses 2 SQL queries then forms a relation for a master/detail view on the aspx side: Private Sub Binddata(ByVal name As...
8
by: Roland Hall | last post by:
In Access you use "*" + + "*", + can be replaced with & Calling a parameterized query in Access requires % be used in place of *, however, all that I have read show dynamic SQL passed to Access: ...
0
by: Roger | last post by:
I changed my classic reorgs to inplace reorg on my siebel database last weekend and ran into the log space getting full issue . This is one of the command on my script : reorg table SIEBEL.ASSET...
0
debasisdas
by: debasisdas | last post by:
Using Subqueries ================== The sub query is often referred to as a nested SELECT, Sub - SELECT, or inner SELECT statement. The sub query executes once before the main query. The...
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
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,...
0
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...
0
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...

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.