Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 problem with null

Author  Topic 

starwin
Starting Member

3 Posts

Posted - 2010-05-05 : 08:15:23
I need to find a way to tell the bold part of this query to sum results, but I've run into a problem I can't find my way around. I've got records in the Contracts table that have matching Deposit table records, and have a value for AMDNEWCONT. However, some of the records in the Contract table have no record at all in the Deposit table for their AMDNEWCONT value. Some do, some don't. For the accounts that have both a CONTRACT value and an AMDNEWCONT value, the ones that do not have any records in the Deposit table are giving me funny results (blank PYPRIN values when they actually exist in the table).

It looks to me that the query can't reconcile the fact that there are no values for part of the equation, which I understand, but I can't figure out how to get around it... Any help?

case when Contracts.AMDNEWCONT is null then (select Sum(isNull(Deposits.PYPRIN, 0)) from CO1.DEPOSIT Deposits where Deposits.CONTRACT = Contracts.CONTRACT) else (select Sum(isNull(Deposits.PYPRIN, 0)) from CO1.DEPOSIT Deposits where Deposits.CONTRACT = Contracts.CONTRACT) + (select Sum(isNull(Deposits.PYPRIN, 0)) from CO1.DEPOSIT Deposits where Deposits.CONTRACT = Contracts.AMDNEWCONT) end as PrinPaid,

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2010-05-05 : 08:21:17
MAybe this:

case
when Contracts.AMDNEWCONT is null then (
select Sum(isNull(Deposits.PYPRIN, 0))
from CO1.DEPOSIT Deposits
where Deposits.CONTRACT = Contracts.CONTRACT
)

else ISNULL(
(
select Sum(isNull(Deposits.PYPRIN, 0))
from CO1.DEPOSIT Deposits
where Deposits.CONTRACT = Contracts.CONTRACT
)
, 0 )
+ (
select Sum(isNull(Deposits.PYPRIN, 0))
from CO1.DEPOSIT Deposits
where Deposits.CONTRACT = Contracts.AMDNEWCONT
)

end as PrinPaid,

It's hard to say base don the info you've given. If this doesn't help try providing some sample data and table structure.

This is a pretty horrible way to do things. I think you may be able to rewrite your query using derived tables.

Feel free to post the full query.


Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

starwin
Starting Member

3 Posts

Posted - 2010-05-05 : 08:31:15
I agree that there is probably a much better way to do this, but I'm still very new to query writing and learning. Here's the full query:

select
Contracts.AMDNEWCONT as NewAmdContractNumber,
Contracts.CONTRACT as ContractNumber,
Customers.FULLNAME as CustFullName,
rtrim(Inventory.CarYear) + ' ' + rtrim(Inventory.CarMake) + ' ' + rtrim(Inventory.CarModel) as Yr_Make_Model,
TaxOwe.TAXABLE as TaxableAmount,
isNull(Contracts.DOWNPAY, 0) + isNull(Contracts.PICKUP, 0) as DownPayment,
case when Contracts.AMDNEWCONT is null then (select Sum(isNull(Deposits.PYPRIN, 0)) from CO1.DEPOSIT Deposits where Deposits.CONTRACT = Contracts.CONTRACT) else (select Sum(isNull(Deposits.PYPRIN, 0)) from CO1.DEPOSIT Deposits where Deposits.CONTRACT = Contracts.CONTRACT) + (select Sum(isNull(Deposits.PYPRIN, 0)) from CO1.DEPOSIT Deposits where Deposits.CONTRACT = Contracts.AMDNEWCONT) end as PrinPaid,
TaxOwe.STATE as TaxSTATE,
Contracts.DATESOLD as DateSold,
ContractPayoffWriteoff.POWRDATE as WriteoffDate
from
CO1.CONINFO Contracts
left join
CUSTOMER Customers on Customers.CustCode = Contracts.CUSTCODE1
left join
CO1.INVEN Inventory on Contracts.STOCKNO = Inventory.STOCKNO
left join
CO1.CONWRITE ContractPayoffWriteoff on Contracts.CONTRACT = ContractPayoffWriteoff.CONTRACT
left join
CO1.TAXOWED TaxOwe on TaxOwe.CONTRACT = Contracts.CONTRACT
left join
CO1.DEPOSIT Deposits on Deposits.CONTRACT = Contracts.CONTRACT
where
TaxOwe.STATE = 'TN' and
Contracts.DATESOLD between '01/01/2009' and '12/31/2009' and
TaxOwe.CODE = 'STATE' and
ContractPayoffWriteoff.CONTRACT is not null

It's probably fairly confusing without the full table structure, but the only part that is giving me problems is when the query is trying to add the bolded area. The specific accounts that return blank results are the ones with no records in the Deposit table for their AMDNEWCONT value.
Go to Top of Page

Ifor
Aged Yak Warrior

700 Posts

Posted - 2010-05-05 : 09:35:18
You would probably find it easier to read by left joining to a derived table (inline view) or CTE.

;WITH cte
AS
(
SELECT De.CONTRACT, SUM(De.PYPRIN) AS PYPRIN
FROM CO1.DEPOSIT De
GROUP BY De.CONTRACT
)
SELECT
Co.AMDNEWCONT AS NewAmdContractNumber,
Co.CONTRACT AS ContractNumber,
Cu.FULLNAME AS CustFullName,
RTRIM(I.CarYear) + ' ' + RTRIM(I.CarMake) + ' ' + RTRIM(I.CarModel) AS Yr_Make_Model,
T.TAXABLE AS TaxableAmount,
ISNULL(Co.DOWNPAY, 0) + ISNULL(Co.PICKUP, 0) AS DownPayment,
ISNULL(C1.PYPRIN, 0) + ISNULL(C2.PYPRIN, 0) AS PrinPaid,
T.STATE AS TaxSTATE,
Co.DATESOLD AS DateSold,
Co.POWRDATE as WriteoffDate
FROM CO1.CONINFO Co
-- no point in checking for W.CONTRACT IS NOT NULL in WHERE clause
-- just make inner join.
JOIN CO1.CONWRITE W
ON Co.CONTRACT = W.CONTRACT
LEFT JOIN CO1.TAXOWED T
ON Co.CONTRACT = T.CONTRACT
-- If leave these in WHERE, the effect will be the same as an inner join
-- If want inner join, just remove the LEFT.
AND T.STATE = 'TN'
AND T.CODE = 'STATE'
LEFT JOIN CUSTOMER Cu
ON Co.CUSTCODE1 = Cu.CustCode
LEFT JOIN CO1.INVEN I
ON Co.STOCKNO = I.STOCKNO
LEFT JOIN cte C1
ON Co.CONTRACT = C1.CONTRACT
LEFT JOIN cte C2
ON Co.AMDNEWCONT = C2.CONTRACT
WHERE Co.DATESOLD >= '20090101'
AND Co.DATESOLD < '20100101'
Go to Top of Page

starwin
Starting Member

3 Posts

Posted - 2010-05-05 : 11:11:12
Thanks for the suggestions guys. As I said, I'm still very new to this, so it's a painful process trying to pick things up on the fly. I modified the original query as follows to get the result I needed:

case when Contracts.AMDNEWCONT is null then (select Sum(isNull(Deposits.PYPRIN, 0)) from CO1.DEPOSIT Deposits where Deposits.CONTRACT = Contracts.CONTRACT) else isNull((select Sum(isNull(Deposits.PYPRIN, 0)) from CO1.DEPOSIT Deposits where Deposits.CONTRACT = Contracts.CONTRACT), 0) + isNull((select Sum(isNull(Deposits.PYPRIN, 0)) from CO1.DEPOSIT Deposits where Deposits.CONTRACT = Contracts.AMDNEWCONT), 0) end as PrinPaid,

The isNull for the entire subquery looks like it took care of the issue, as I'm now getting the correct results. I'm sure that your other suggestions will help going forward as well, as I'm sure that what I wrote isn't nearly as efficient as it should be.
Go to Top of Page
   

- Advertisement -