Tuesday, March 20, 2012

Can this query be simpilified

Hi,
Can this query be simpilified for performance
If (select count(id)
From dbo.TDetails
where
ID = 554 AND Name = 'SUN'
HAVING
Sum(Quantity_Shipped + Quantity_Credited) <> SUM(QUANTITY)) = 0 Then ......
.
ThanksChris,
I think we are missing a "group by" clause.
AMB
"Chris" wrote:

> Hi,
> Can this query be simpilified for performance
>
> If (select count(id)
> From dbo.TDetails
> where
> ID = 554 AND Name = 'SUN'
> HAVING
> Sum(Quantity_Shipped + Quantity_Credited) <> SUM(QUANTITY)) = 0 Then ....
..
> Thanks|||> I think we are missing a "group by" clause.
My mistake. Can you try:
if (
select
isnull(sum(Quantity_Shipped + Quantity_Credited), 0) -
isnull(sum(QUANTITY), 0)
from
dbo.TDetails
where
[ID] = 554 AND [Name] = 'SUN'
) != 0
...
AMB
"Alejandro Mesa" wrote:
> Chris,
> I think we are missing a "group by" clause.
>
> AMB
> "Chris" wrote:
>|||Hi Chris,
I would not expect any performance gain by rewriting the statement.
Personally, I would prefer the following syntax. An "Else" clause might
become faster this way. The "Then" clause will not be faster.
If NOT EXISTS (
SELECT 1
FROM dbo.TDetails
WHERE ID = 554
AND Name = 'SUN'
HAVING SUM(Quantity_Shipped + Quantity_Credited) <> SUM(Quantity)
)
Begin
.
End
Make sure you have an index on (ID, Name).
Gert-Jan
Chris wrote:
> Hi,
> Can this query be simpilified for performance
> If (select count(id)
> From dbo.TDetails
> where
> ID = 554 AND Name = 'SUN'
> HAVING
> Sum(Quantity_Shipped + Quantity_Credited) <> SUM(QUANTITY)) = 0 Then ....
..
> Thanks

No comments:

Post a Comment