Showing posts with label producing. Show all posts
Showing posts with label producing. Show all posts

Friday, March 30, 2012

Query producing Dups

I swear I had this fixed but when I looked again this morning, I noticed that this query is producing dup records in the results. Something is not right with my query here:

SELECT DISTINCT m.customer,
c.name,
c.customer,
(SELECT Top 1 fd.Fee1 FROM FeeScheduleDetails fd
where c.feeSchedule = fd.code)
AS FeeSchedule,
m.Branch,
CASE WHEN ph.batchtype = 'PUR' OR ph.batchtype = 'PAR' OR ph.batchtype = 'PCR' OR ph.batchtype = 'DUR' OR ph.batchtype = 'DAR' Then
(-ph.totalpaid + ph.ForwardeeFee)
WHEN ph.batchtype = 'PU' OR ph.batchtype = 'PC' OR ph.batchtype = 'PA' OR ph.batchtype = 'DC' OR ph.batchtype = 'DA' Then
(ph.totalpaid + ph.ForwardeeFee)
END AS [Posted Amount],
ph.systemmonth,
ph.systemyear,
ph.datepaid,
ph.totalpaid,
ph.batchtype,
m.desk,
0 AS [NewCC_Amount],
0 AS [OldCC_Amount],
0 AS [NewPDC_Amount],
0 AS [OldPDC_Amount],
'In-House' AS Type,
1 AS Active,
m.number,
0 AS CC,
0 AS PDC,
m.original,
CONVERT(money, ph.OverPaidAmt),
0,
0,
'',
0,
0,
dc.OnHoldDate,
pd.OnHold,
(SELECT TotalPostingDays from TotalPostingDays),
(SELECT CurrentPostingDAy from CurrentPostingDay)
FROM dbo.Master m (NOLOCK)
INNER JOIN dbo.payhistory ph ON m.number = ph.number
LEFT JOIN dbo.DebtorCreditCards dc ON dc.number = m.number
LEFT JOIN dbo.pdc pd ON pd.number = m.number
INNER JOIN dbo.Customer c ON c.Customer = m.Customer
WHERE ph.systemmonth = datepart(mm, getdate()) AND ph.systemyear = datepart(yy, getdate())
AND ph.batchtype <> 'DA'
AND ph.batchtype <> 'DAR'
ORDER BY m.customer

Output:

Check out 00001, I am getting 2 $200, only should be getting one:

0000001 AD 0000001 25 00001 144.34 2 2006 2006-02-10 00:00:00.000 144.34 PU C0159 0 0 0 0 In-House 1 1259 0 0 5144.34 0.00 0 0 0 0 NULL NULL 20 12
0000001 AD 0000001 25 00001 200.00 2 2006 2006-02-06 00:00:00.000 200.00 PU C0101 0 0 0 0 In-House 1 1186 0 0 12067.88 0.00 0 0 0 0 NULL NULL 20 12
0000001 AD 0000001 25 00001 200.00 2 2006 2006-02-06 00:00:00.000 200.00 PU C0101 0 0 0 0 In-House 1 1186 0 0 12067.88 0.00 0 0 0 0 NULL 2005-04-05 00:00:00.000 20 12
0000002 MB 0000002 25 00001 -2500.00 2 2006 2006-01-31 00:00:00.000 2500.00 PUR C0137 0 0 0 0 In-House 1 713617 0 0 5131.47 0.00 0 0 0 0 NULL NULL 20 12
0000002 MB 0000002 25 00001 -1800.00 2 2006 2006-01-30 00:00:00.000 1800.00 PUR C0130 0 0 0 0 In-House 1 604096 0 0 2362.20 0.00 0 0 0 0 NULL NULL 20 12
0000002 MB 0000002 25 00001 -1000.00 2 2006 2006-01-31 00:00:00.000 1000.00 PUR C0136 0 0 0 0 In-House 1 572560 0 0 10651.37 0.00 0 0 0 0 NULL NULL 20 12
0000002 MB 0000002 25 00001 -565.00 2 2006 2006-01-30 00:00:00.000 565.00 PUR C0136 0 0 0 0 In-House 1 671991 0 0 19471.85 0.00 0 0 0 0 NULL NULL 20 12
0000002 MB 0000002 25 00001 -500.00 2 2006 2006-01-30 00:00:00.000 500.00 PUR C0137 0 0 0 0 In-House 1 685926 0 0 14825.85 0.00 0 0 0 0 NULL NULL 20 12
0000002 MB 0000002 25 00001 -478.00 2 2006 2006-01-31 00:00:00.000 478.00 PUR C0136 0 0 0 0 In-House 1 713497 0 0 4788.80 0.00 0 0 0 0 NULL NULL 20 12
0000002 MB 0000002 25 00001 -478.00 2 2006 2006-01-31 00:00:00.000 478.00 PUR C0136 0 0 0 0 In-House 1 713497 0 0 4788.80 0.00 0 0 0 0 NULL 2006-02-09 17:33:02.360 20 12
0000002 MB 0000002 25 00001 -411.94 2 2006 2006-01-19 00:00:00.000 411.94 PUR C0137 0 0 0 0 In-House 1 604705 0 0 9886.63 0.00 0 0 0 0 NULL NULL 20 12
0000002 MB 0000002 25 00001 -350.00 2 2006 2006-01-30 00:00:00.000 350.00 PUR C0137 0 0 0 0 In-House 1 558059 0 0 7040.92 0.00 0 0 0 0 NULL NULL 20 12
0000002 MB 0000002 25 00001 -328.61 2 2006 2006-02-09 00:00:00.000 328.61 PUR C0137 0 0 0 0 In-House 1 713542 0 0 6572.34 0.00 0 0 0 0 NULL NULL 20 12
0000002 MB 0000002 25 00001 -300.00 2 2006 2006-01-19 00:00:00.000 300.00 PUR C0136 0 0 0 0 In-House 1 662978 0 0 12041.96 0.00 0 0 0 0 NULL NULL 20 12

Can you provide some DB schema information, as it is hard to solve from that query alone. Usually you get duplicates because you are either missing one or more columns in your joins, or you have too many columns in your group by clause. Make sure all you joins use the Primary key column(s).

|||Did that help, or do you need more advice?

Query producing duplicate results

I swear I had this fixed but when I looked again this morning, I
noticed that this query is producing dup records in the results.
Something is not right with my query here:
SELECT DISTINCT m.customer,
c.name,
c.customer,
(SELECT Top 1 fd.Fee1 FROM FeeScheduleDetails fd
where c.feeSchedule = fd.code)
AS FeeSchedule,
m.Branch,
CASE WHEN ph.batchtype = 'PUR' OR ph.batchtype = 'PAR' OR
ph.batchtype = 'PCR' OR ph.batchtype = 'DUR' OR ph.batchtype = 'DAR'
Then
(-ph.totalpaid + ph.ForwardeeFee)
WHEN ph.batchtype = 'PU' OR ph.batchtype = 'PC' OR
ph.batchtype = 'PA' OR ph.batchtype = 'DC' OR ph.batchtype = 'DA' Then
(ph.totalpaid + ph.ForwardeeFee)
END AS [Posted Amount],
ph.systemmonth,
ph.systemyear,
ph.datepaid,
ph.totalpaid,
ph.batchtype,
m.desk,
0 AS [NewCC_Amount],
0 AS [OldCC_Amount],
0 AS [NewPDC_Amount],
0 AS [OldPDC_Amount],
'In-House' AS Type,
1 AS Active,
m.number,
0 AS CC,
0 AS PDC,
m.original,
CONVERT(money, ph.OverPaidAmt),
0,
0,
'',
0,
0,
dc.OnHoldDate,
pd.OnHold,
(SELECT TotalPostingDays from TotalPostingDays),
(SELECT CurrentPostingDAy from CurrentPostingDay)
FROM dbo.Master m (NOLOCK)
INNER JOIN dbo.payhistory ph ON m.number = ph.number
LEFT JOIN dbo.DebtorCreditCards dc ON dc.number = m.number
LEFT JOIN dbo.pdc pd ON pd.number = m.number
INNER JOIN dbo.Customer c ON c.Customer = m.Customer
WHERE ph.systemmonth = datepart(mm, getdate()) AND ph.systemyear =
datepart(yy, getdate())
AND ph.batchtype <> 'DA'
AND ph.batchtype <> 'DAR'
ORDER BY m.customer
Output:
Check out 00001, I am getting 2 $200, only should be getting one:
0000001 AD 0000001 25 00001 144.34 2 2006
2006-02-10 00:00:00.000 144.34 PU C0159 0 0 0
0 In-House 1 1259 0 0 5144.34 0.00
0 0 0 0 NULL NULL 20 12
0000001 AD 0000001 25 00001 200.00 2 2006
2006-02-06 00:00:00.000 200.00 PU C0101 0 0 0
0 In-House 1 1186 0 0 12067.88 0.00
0 0 0 0 NULL NULL 20 12
0000001 AD 0000001 25 00001 200.00 2 2006
2006-02-06 00:00:00.000 200.00 PU C0101 0 0 0
0 In-House 1 1186 0 0 12067.88 0.00
0 0 0 0 NULL 2005-04-05 00:00:00.000 20
12
0000002 MB 0000002 25 00001 -2500.00 2 2006
2006-01-31 00:00:00.000 2500.00 PUR C0137 0 0
0 0 In-House 1 713617 0 0 5131.47
0.00 0 0 0 0 NULL NULL 20 12
0000002 MB 0000002 25 00001 -1800.00 2 2006
2006-01-30 00:00:00.000 1800.00 PUR C0130 0 0
0 0 In-House 1 604096 0 0 2362.20
0.00 0 0 0 0 NULL NULL 20 12
0000002 MB 0000002 25 00001 -1000.00 2 2006
2006-01-31 00:00:00.000 1000.00 PUR C0136 0 0
0 0 In-House 1 572560 0 0 10651.37
0.00 0 0 0 0 NULL NULL 20 12
0000002 MB 0000002 25 00001 -565.00 2 2006
2006-01-30 00:00:00.000 565.00 PUR C0136 0 0
0 0 In-House 1 671991 0 0 19471.85 0.00
0 0 0 0 NULL NULL 20 12
0000002 MB 0000002 25 00001 -500.00 2 2006
2006-01-30 00:00:00.000 500.00 PUR C0137 0 0
0 0 In-House 1 685926 0 0 14825.85 0.00
0 0 0 0 NULL NULL 20 12
0000002 MB 0000002 25 00001 -478.00 2 2006
2006-01-31 00:00:00.000 478.00 PUR C0136 0 0
0 0 In-House 1 713497 0 0 4788.80 0.00
0 0 0 0 NULL NULL 20 12
0000002 MB 0000002 25 00001 -478.00 2 2006
2006-01-31 00:00:00.000 478.00 PUR C0136 0 0
0 0 In-House 1 713497 0 0 4788.80 0.00
0 0 0 0 NULL 2006-02-09 17:33:02.360 20
12
0000002 MB 0000002 25 00001 -411.94 2 2006
2006-01-19 00:00:00.000 411.94 PUR C0137 0 0
0 0 In-House 1 604705 0 0 9886.63 0.00
0 0 0 0 NULL NULL 20 12
0000002 MB 0000002 25 00001 -350.00 2 2006
2006-01-30 00:00:00.000 350.00 PUR C0137 0 0
0 0 In-House 1 558059 0 0 7040.92 0.00
0 0 0 0 NULL NULL 20 12
0000002 MB 0000002 25 00001 -328.61 2 2006
2006-02-09 00:00:00.000 328.61 PUR C0137 0 0
0 0 In-House 1 713542 0 0 6572.34 0.00
0 0 0 0 NULL NULL 20 12
0000002 MB 0000002 25 00001 -300.00 2 2006
2006-01-19 00:00:00.000 300.00 PUR C0136 0 0
0 0 In-House 1 662978 0 0 12041.96 0.00
0 0 0 0 NULL NULL 20 12Destinct will select distinct rows, and your rows are all distinct. Check
the last few columns in your results the date is null in one but not in the
other. If one column returns different results, then it will return a
seperate row. Duplicates, when using distinct, are rows where every column
has the same value.
"dba123" <d_schinkel@.hotmail.com> wrote in message
news:1140102940.858179.314400@.f14g2000cwb.googlegroups.com...
> I swear I had this fixed but when I looked again this morning, I
> noticed that this query is producing dup records in the results.
> Something is not right with my query here:
> SELECT DISTINCT m.customer,
> c.name,
> c.customer,
> (SELECT Top 1 fd.Fee1 FROM FeeScheduleDetails fd
> where c.feeSchedule = fd.code)
> AS FeeSchedule,
> m.Branch,
> CASE WHEN ph.batchtype = 'PUR' OR ph.batchtype = 'PAR' OR
> ph.batchtype = 'PCR' OR ph.batchtype = 'DUR' OR ph.batchtype = 'DAR'
> Then
> (-ph.totalpaid + ph.ForwardeeFee)
> WHEN ph.batchtype = 'PU' OR ph.batchtype = 'PC' OR
> ph.batchtype = 'PA' OR ph.batchtype = 'DC' OR ph.batchtype = 'DA' Then
> (ph.totalpaid + ph.ForwardeeFee)
> END AS [Posted Amount],
> ph.systemmonth,
> ph.systemyear,
> ph.datepaid,
> ph.totalpaid,
> ph.batchtype,
> m.desk,
> 0 AS [NewCC_Amount],
> 0 AS [OldCC_Amount],
> 0 AS [NewPDC_Amount],
> 0 AS [OldPDC_Amount],
> 'In-House' AS Type,
> 1 AS Active,
> m.number,
> 0 AS CC,
> 0 AS PDC,
> m.original,
> CONVERT(money, ph.OverPaidAmt),
> 0,
> 0,
> '',
> 0,
> 0,
> dc.OnHoldDate,
> pd.OnHold,
> (SELECT TotalPostingDays from TotalPostingDays),
> (SELECT CurrentPostingDAy from CurrentPostingDay)
> FROM dbo.Master m (NOLOCK)
> INNER JOIN dbo.payhistory ph ON m.number = ph.number
> LEFT JOIN dbo.DebtorCreditCards dc ON dc.number = m.number
> LEFT JOIN dbo.pdc pd ON pd.number = m.number
> INNER JOIN dbo.Customer c ON c.Customer = m.Customer
> WHERE ph.systemmonth = datepart(mm, getdate()) AND ph.systemyear =
> datepart(yy, getdate())
> AND ph.batchtype <> 'DA'
> AND ph.batchtype <> 'DAR'
> ORDER BY m.customer
> Output:
> Check out 00001, I am getting 2 $200, only should be getting one:
> 0000001 AD 0000001 25 00001 144.34 2 2006
> 2006-02-10 00:00:00.000 144.34 PU C0159 0 0 0
> 0 In-House 1 1259 0 0 5144.34 0.00
> 0 0 0 0 NULL NULL 20 12
> 0000001 AD 0000001 25 00001 200.00 2 2006
> 2006-02-06 00:00:00.000 200.00 PU C0101 0 0 0
> 0 In-House 1 1186 0 0 12067.88 0.00
> 0 0 0 0 NULL NULL 20 12
> 0000001 AD 0000001 25 00001 200.00 2 2006
> 2006-02-06 00:00:00.000 200.00 PU C0101 0 0 0
> 0 In-House 1 1186 0 0 12067.88 0.00
> 0 0 0 0 NULL 2005-04-05 00:00:00.000 20
> 12
> 0000002 MB 0000002 25 00001 -2500.00 2 2006
> 2006-01-31 00:00:00.000 2500.00 PUR C0137 0 0
> 0 0 In-House 1 713617 0 0 5131.47
> 0.00 0 0 0 0 NULL NULL 20 12
> 0000002 MB 0000002 25 00001 -1800.00 2 2006
> 2006-01-30 00:00:00.000 1800.00 PUR C0130 0 0
> 0 0 In-House 1 604096 0 0 2362.20
> 0.00 0 0 0 0 NULL NULL 20 12
> 0000002 MB 0000002 25 00001 -1000.00 2 2006
> 2006-01-31 00:00:00.000 1000.00 PUR C0136 0 0
> 0 0 In-House 1 572560 0 0 10651.37
> 0.00 0 0 0 0 NULL NULL 20 12
> 0000002 MB 0000002 25 00001 -565.00 2 2006
> 2006-01-30 00:00:00.000 565.00 PUR C0136 0 0
> 0 0 In-House 1 671991 0 0 19471.85 0.00
> 0 0 0 0 NULL NULL 20 12
> 0000002 MB 0000002 25 00001 -500.00 2 2006
> 2006-01-30 00:00:00.000 500.00 PUR C0137 0 0
> 0 0 In-House 1 685926 0 0 14825.85 0.00
> 0 0 0 0 NULL NULL 20 12
> 0000002 MB 0000002 25 00001 -478.00 2 2006
> 2006-01-31 00:00:00.000 478.00 PUR C0136 0 0
> 0 0 In-House 1 713497 0 0 4788.80 0.00
> 0 0 0 0 NULL NULL 20 12
> 0000002 MB 0000002 25 00001 -478.00 2 2006
> 2006-01-31 00:00:00.000 478.00 PUR C0136 0 0
> 0 0 In-House 1 713497 0 0 4788.80 0.00
> 0 0 0 0 NULL 2006-02-09 17:33:02.360 20
> 12
> 0000002 MB 0000002 25 00001 -411.94 2 2006
> 2006-01-19 00:00:00.000 411.94 PUR C0137 0 0
> 0 0 In-House 1 604705 0 0 9886.63 0.00
> 0 0 0 0 NULL NULL 20 12
> 0000002 MB 0000002 25 00001 -350.00 2 2006
> 2006-01-30 00:00:00.000 350.00 PUR C0137 0 0
> 0 0 In-House 1 558059 0 0 7040.92 0.00
> 0 0 0 0 NULL NULL 20 12
> 0000002 MB 0000002 25 00001 -328.61 2 2006
> 2006-02-09 00:00:00.000 328.61 PUR C0137 0 0
> 0 0 In-House 1 713542 0 0 6572.34 0.00
> 0 0 0 0 NULL NULL 20 12
> 0000002 MB 0000002 25 00001 -300.00 2 2006
> 2006-01-19 00:00:00.000 300.00 PUR C0136 0 0
> 0 0 In-House 1 662978 0 0 12041.96 0.00
> 0 0 0 0 NULL NULL 20 12
>|||It's impossible to answer your question without seeing the table DDL and
sample data.
ML
http://milambda.blogspot.com/

Friday, March 9, 2012

Query Optimization NOT IN vs LEFT JOIN

I've read in many places that using "NOT IN" results in worse
performance. I've got two queries producing the same result:
SELECT WS.*
FROM
WHSLocations WS
INNER JOIN WHSLocations20060428D WHD
ON WS.LocationID = WHD.LocationID
WHERE WS.LocationID NOT IN (SELECT LocationID FROM Repository)
SELECT WS.*
FROM
WHSLocations WS
INNER JOIN WHSLocations20060428D WHD
ON WS.LocationID = WHD.LocationID
LEFT JOIN Repository R
ON WS.LocationID = R.LocationID
WHERE R.LocationID IS NULL
The second one actually takes just a bit slower than the first. Under
which circumstances should I expect a performance gain if any?
I appreciate your time reading my post,
Thank you,
Igor
*** Sent via Developersdex http://www.examnotes.net ***Have you tired this
SELECT WS.*
FROM
WHSLocations WS
INNER JOIN WHSLocations20060428D WHD
ON WS.LocationID = WHD.LocationID
WHERE NOT EXISTS (SELECT LocationID FROM Repository Where LocationID =
WS.LocationID )
Also WHERE WS.LocationID NOT IN (SELECT LocationID FROM Repository)
will return nothing if you have NULL values
Denis the SQL Menace
http://sqlservercode.blogspot.com/|||mEmENT0m0RI,
Compare also the version using "NOT EXISTS".
SELECT WS.*
FROM
(
select a.*
from WHSLocations as a
WHERE
NOT EXISTS (SELECT * FROM Repository as r where r.LocationID = a.LocationID)
) as WS
INNER JOIN
WHSLocations20060428D WHD
ON WS.LocationID = WHD.LocationID
go
Remember to clean the proc cache and the data buffer before executing each
statement (do not do it on production).
dbcc freeproccache
dbcc dropcleanbuffers
AMB
"mEmENT0m0RI" wrote:

> I've read in many places that using "NOT IN" results in worse
> performance. I've got two queries producing the same result:
>
> SELECT WS.*
> FROM
> WHSLocations WS
> INNER JOIN WHSLocations20060428D WHD
> ON WS.LocationID = WHD.LocationID
> WHERE WS.LocationID NOT IN (SELECT LocationID FROM Repository)
>
> SELECT WS.*
> FROM
> WHSLocations WS
> INNER JOIN WHSLocations20060428D WHD
> ON WS.LocationID = WHD.LocationID
> LEFT JOIN Repository R
> ON WS.LocationID = R.LocationID
> WHERE R.LocationID IS NULL
>
> The second one actually takes just a bit slower than the first. Under
> which circumstances should I expect a performance gain if any?
> I appreciate your time reading my post,
> Thank you,
> Igor
>
> *** Sent via Developersdex http://www.examnotes.net ***
>|||"Alejandro Mesa" <AlejandroMesa@.discussions.microsoft.com> wrote in message
news:7C075150-2839-4CC2-A023-8C3DB5AEEA00@.microsoft.com...
> mEmENT0m0RI,
> Compare also the version using "NOT EXISTS".
> SELECT WS.*
> FROM
> (
> select a.*
> from WHSLocations as a
> WHERE
> NOT EXISTS (SELECT * FROM Repository as r where r.LocationID =
> a.LocationID)
> ) as WS
> INNER JOIN
> WHSLocations20060428D WHD
> ON WS.LocationID = WHD.LocationID
> go
> Remember to clean the proc cache and the data buffer before executing each
> statement (do not do it on production).
> dbcc freeproccache
> dbcc dropcleanbuffers
>
You should not use DBCC DROPCLEANBUFFERS to test the relative performance of
queries. When you do, you skew the results in favor of whichever query
accesses the fewest number of distinct pages. For instance a nested loops
join (or bookmark lookup) might read the same page over and over, whereas a
clustered index scan (or table scan) would read each page once. With the
page cache flushed the loop join might look faster even though it generates
more reads, and is therefore actually worse.
Instead just set
set statistics io on
and compare the total number of reads.
David|||I was comparing the performance by putting all three queries into same
batch and checking "Query cost (realtive to the batch)" values from the
execution plan window. I'm not sure though if that would be a fair way
to compare...
So, now I have three queries:
dbcc freeproccache
dbcc dropcleanbuffers
SET STATISTICS IO ON
SELECT WS.*
FROM
WHSLocations WS
INNER JOIN WHSLocations20060428D WHD
ON WS.LocationID = WHD.LocationID
WHERE NOT EXISTS (SELECT LocationID FROM Repository WHERE LocationID =
WS.LocationID)
SELECT WS.*
FROM
WHSLocations WS
INNER JOIN WHSLocations20060428D WHD
ON WS.LocationID = WHD.LocationID
WHERE WS.LocationID NOT IN (SELECT LocationID FROM Repository)
SELECT WS.*
FROM
WHSLocations WS
INNER JOIN WHSLocations20060428D WHD
ON WS.LocationID = WHD.LocationID
LEFT JOIN Repository R
ON WS.LocationID = R.LocationID
WHERE R.LocationID IS NULL
Query Costs Relative to the batch:
33.33
33.33
33.34
IO STATISTICS:
(750 row(s) affected)
Table 'Repository'. Scan count 1, logical reads 67, physical reads 0,
read-ahead reads 848.
Table 'WHSLocations'. Scan count 1, logical reads 581, physical reads 0,
read-ahead reads 1283.
Table 'WHSLocations20060428D'. Scan count 1, logical reads 10, physical
reads 0, read-ahead reads 0.
(750 row(s) affected)
Table 'Repository'. Scan count 1, logical reads 67, physical reads 0,
read-ahead reads 0.
Table 'WHSLocations'. Scan count 1, logical reads 581, physical reads 0,
read-ahead reads 0.
Table 'WHSLocations20060428D'. Scan count 1, logical reads 10, physical
reads 0, read-ahead reads 0.
(750 row(s) affected)
Table 'Repository'. Scan count 1, logical reads 67, physical reads 0,
read-ahead reads 0.
Table 'WHSLocations'. Scan count 1, logical reads 581, physical reads 0,
read-ahead reads 0.
Table 'WHSLocations20060428D'. Scan count 1, logical reads 10, physical
reads 0, read-ahead reads 0.
*** Sent via Developersdex http://www.examnotes.net ***|||See
http://groups.google.nl/group/micro...bb?dmode=source
Gert-Jan
mEmENT0m0RI wrote:
> I've read in many places that using "NOT IN" results in worse
> performance. I've got two queries producing the same result:
> SELECT WS.*
> FROM
> WHSLocations WS
> INNER JOIN WHSLocations20060428D WHD
> ON WS.LocationID = WHD.LocationID
> WHERE WS.LocationID NOT IN (SELECT LocationID FROM Repository)
> SELECT WS.*
> FROM
> WHSLocations WS
> INNER JOIN WHSLocations20060428D WHD
> ON WS.LocationID = WHD.LocationID
> LEFT JOIN Repository R
> ON WS.LocationID = R.LocationID
> WHERE R.LocationID IS NULL
> The second one actually takes just a bit slower than the first. Under
> which circumstances should I expect a performance gain if any?
> I appreciate your time reading my post,
> Thank you,
> Igor
> *** Sent via Developersdex http://www.examnotes.net ***|||>I was comparing the performance by putting all three queries into same
> batch and checking "Query cost (realtive to the batch)" values from the
> execution plan window.
Execution plan is far from the whole story.|||David Browne (davidbaxterbrowne no potted meat@.hotmail.com) writes:
> You should not use DBCC DROPCLEANBUFFERS to test the relative
> performance of queries.
And most of all, you should not run that command on a production
machine!

> When you do, you skew the results in favor of whichever query accesses
> the fewest number of distinct pages. For instance a nested loops join
> (or bookmark lookup) might read the same page over and over, whereas a
> clustered index scan (or table scan) would read each page once. With
> the page cache flushed the loop join might look faster even though it
> generates more reads, and is therefore actually worse.
> Instead just set
> set statistics io on
> and compare the total number of reads.
Actually, neither that is accurate, as this does not account for building
hash tables, sorting etc. The best is wallclock time with getdate().
Obviously this requires a server that is not occupied with other things.
As for caching, it is important to test under the same presumptions. That
is, either flush the cache each time, or make sure all data is in cache
before you start testing.
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx|||mEmENT0m0RI wrote:
> I've read in many places that using "NOT IN" results in worse
> performance. I've got two queries producing the same result:
>
> SELECT WS.*
> FROM
> WHSLocations WS
> INNER JOIN WHSLocations20060428D WHD
> ON WS.LocationID = WHD.LocationID
> WHERE WS.LocationID NOT IN (SELECT LocationID FROM Repository)
>
> SELECT WS.*
> FROM
> WHSLocations WS
> INNER JOIN WHSLocations20060428D WHD
> ON WS.LocationID = WHD.LocationID
> LEFT JOIN Repository R
> ON WS.LocationID = R.LocationID
> WHERE R.LocationID IS NULL
>
> The second one actually takes just a bit slower than the first. Under
> which circumstances should I expect a performance gain if any?
I think that since the subquery in your first query is not co-related,
it only needs to be executed once, whereas in your second query the
Repository table needs to be accessed for every row in the WHSLocations
table.
I suspect that if the subquery was co-related (e.g. NOT IN (SELECT
LocationID FROM Repository where Repository.LocationGroup =
WS.LocationGroup) ) then the LEFT OUTER JOIN would be quicker.
Kris|||Seems you got the same plan for all three queries.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"mEmENT0m0RI" <nospam@.devdex.com> wrote in message news:epbpuVWbGHA.628@.TK2MSFTNGP04.phx.gb
l...
>I was comparing the performance by putting all three queries into same
> batch and checking "Query cost (realtive to the batch)" values from the
> execution plan window. I'm not sure though if that would be a fair way
> to compare...
> So, now I have three queries:
> dbcc freeproccache
> dbcc dropcleanbuffers
> SET STATISTICS IO ON
> SELECT WS.*
> FROM
> WHSLocations WS
> INNER JOIN WHSLocations20060428D WHD
> ON WS.LocationID = WHD.LocationID
> WHERE NOT EXISTS (SELECT LocationID FROM Repository WHERE LocationID =
> WS.LocationID)
>
> SELECT WS.*
> FROM
> WHSLocations WS
> INNER JOIN WHSLocations20060428D WHD
> ON WS.LocationID = WHD.LocationID
> WHERE WS.LocationID NOT IN (SELECT LocationID FROM Repository)
>
> SELECT WS.*
> FROM
> WHSLocations WS
> INNER JOIN WHSLocations20060428D WHD
> ON WS.LocationID = WHD.LocationID
> LEFT JOIN Repository R
> ON WS.LocationID = R.LocationID
> WHERE R.LocationID IS NULL
>
> Query Costs Relative to the batch:
> 33.33
> 33.33
> 33.34
>
> IO STATISTICS:
> (750 row(s) affected)
> Table 'Repository'. Scan count 1, logical reads 67, physical reads 0,
> read-ahead reads 848.
> Table 'WHSLocations'. Scan count 1, logical reads 581, physical reads 0,
> read-ahead reads 1283.
> Table 'WHSLocations20060428D'. Scan count 1, logical reads 10, physical
> reads 0, read-ahead reads 0.
> (750 row(s) affected)
> Table 'Repository'. Scan count 1, logical reads 67, physical reads 0,
> read-ahead reads 0.
> Table 'WHSLocations'. Scan count 1, logical reads 581, physical reads 0,
> read-ahead reads 0.
> Table 'WHSLocations20060428D'. Scan count 1, logical reads 10, physical
> reads 0, read-ahead reads 0.
> (750 row(s) affected)
> Table 'Repository'. Scan count 1, logical reads 67, physical reads 0,
> read-ahead reads 0.
> Table 'WHSLocations'. Scan count 1, logical reads 581, physical reads 0,
> read-ahead reads 0.
> Table 'WHSLocations20060428D'. Scan count 1, logical reads 10, physical
> reads 0, read-ahead reads 0.
>
> *** Sent via Developersdex http://www.examnotes.net ***

Saturday, February 25, 2012

Query not producing correct results and Group By Error

This is what I need in my results:
Current Balance and Account Number should be retrieved where
Current Balance = Sum of rmstranamt - (sum of rmstranamt where rmstrancde is 10)

The Query below gives me a couple of problems

1) It's not producing the correct Current Balance

2) I keep getting these errors with Groupy by:

Column 'rf10.rmstranamt10' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

SELECT rm.rmsacctnum AS [Rms Acct Num],

(sum(rf.rmstranamt) - rf10.rmstranamt10) AS [Current Balance]

FROM RMASTER rm

INNER JOIN

(

SELECT RMSFILENUM, SUM(rmstranamt) AS rmstranamt10

FROM RFINANL

WHERE RMSTRANCDE = '10'

GROUP BY RMSFILENUM

) AS rf10 ON rf10.RMSFILENUM = rm.RMSFILENUM

INNER JOIN

(

SELECT RMSFILENUM, SUM(rmstranamt) AS rmstranamt

FROM RFINANL

GROUP BY RMSFILENUM

) AS rf ON rf.RMSFILENUM = rm.RMSFILENUM

GROUP BY rm.rmsacctnum

The error message is correct as you would have to enclose the rf10.rmstranamnt10 inside a sum() to allow for the aggregation. I always recheck the SQL with an english translation of what I am trying to do. Your descriptive phrase shows the secondary sum, but the T-SQL does not have it.

I hope this helps,

|||

I did as you said and added the sum. I am still getting a Current Balance of 0.00 for an account # I checked when I know for a fact there are a total of 2 positive rmstranamt records in RFINANL with a RMSTRANCDE of 10 so I should have the sume of those in my results for that account number but it's 0.00

This query shows why

SELECT rm.rmsacctnum AS [Rms Acct Num],

sum(rf.rmstranamt) as Sum_rstranamt,

sum(rf10.rmstranamt10) as Sum_rmstranamt_10,

(sum(rf.rmstranamt) - sum(rf10.rmstranamt10)) AS [Current Balance]

FROM RMASTER rm

INNER JOIN

(

SELECT RMSFILENUM, SUM(rmstranamt) AS rmstranamt10

FROM RFINANL

WHERE RMSTRANCDE = '10'

GROUP BY RMSFILENUM

) AS rf10 ON rf10.RMSFILENUM = rm.RMSFILENUM

INNER JOIN

(

SELECT RMSFILENUM, SUM(rmstranamt) AS rmstranamt

FROM RFINANL

GROUP BY RMSFILENUM

) AS rf ON rf.RMSFILENUM = rm.RMSFILENUM

AND rm.rmsacctnum = '4264287999172303'

GROUP BY rm.rmsacctnum

results:

4264287999172303 28789.50 28789.50 0.00

Sum_rstranamt should not be 28789.50 because there are 2 records for that account:

SELECT rm.rmsacctnum AS [Rms Acct Num],

rf.rmstranamt as [Rms rmstranamt],

rf.rmsbalance AS [Rms Balance]

FROM RMASTER rm

INNER JOIN RFINANL rf ON rf.RMSFILENUM = rm.RMSFILENUM

where rm.rmsacctnum = '4264287999172303'

4264287999172303 2878.95 0.00
4264287999172303 25910.55 28789.50

So Sum_rstranamt should be 2878.95 + 25910.55

|||

Here's a second look at what I've sruggled with:

I think we're almost there:
Let's work with account # '4264287999172303'
Query #1 - shows that there are 2 records in the RFINANL table for that account
SELECT rm.rmsacctnum AS [Rms Acct Num],
rf.rmstranamt as [Rms rmstranamt],
rf.rmsbalance AS [Rms Balance]
FROM RMASTER rm
INNER JOIN RFINANL rf ON rf.RMSFILENUM = rm.RMSFILENUM
where rm.rmsacctnum = '4264287999172303'
RESULTS:
4264287999172303 2878.95 0.00
4264287999172303 25910.55 28789.50

Query #2 - shows that there are 2 records in the RFINANL table for that account where RMSTRANCDE = '10'
--
select rm.rmsacctnum AS [Rms Acct Num], rf.rmstranamt
FROM RMASTER rm
INNER JOIN RFINANL rf ON rf.RMSFILENUM = rm.RMSFILENUM
where rf.RMSTRANCDE = '10'
and rm.rmsacctnum = '4264287999172303'
RESULTS:
4264287999172303 2878.95
4264287999172303 25910.55

Now this is what boggles my mind. If we were to take Query #1 and add a sum in it like below, you would get a result of 2878.95 + 25910.55 but I don't.
SELECT rm.rmsacctnum AS [Rms Acct Num],
sum(rf.rmstranamt) as [Rms rmstranamt], < added sum hoping to get 2878.95 + 25910.55
rf.rmsbalance AS [Rms Balance]
FROM RMASTER rm
INNER JOIN RFINANL rf ON rf.RMSFILENUM = rm.RMSFILENUM
where rm.rmsacctnum = '4264287999172303'
GROUP BY rm.rmsacctnum, rf.rmsbalance < but now I'm required to group by any values that do not contain an aggregate function
RESULTS:
4264287999172303 2878.95 0.00
4264287999172303 25910.55 28789.50
Take this a step further, now I add a sum to rf.rmsbalance since at this point I don't know what else do do. I then figure I should get 2878.95 + 25910.55 and 0.00 + 28.789.50 but again, I don't, I end up with both of them havin the same value. I have no idea why:
SELECT rm.rmsacctnum AS [Rms Acct Num],
sum(rf.rmstranamt) as [Rms rmstranamt],
Sum(rf.rmsbalance) AS [Rms Balance] <- Added sum to rf.rmsbalance
FROM RMASTER rm
INNER JOIN RFINANL rf ON rf.RMSFILENUM = rm.RMSFILENUM
where rm.rmsacctnum = '4264287999172303'
GROUP BY rm.rmsacctnum <- took out rf.rmsbalance because it's a sum now in the select
RESULTS:
4264287999172303 28789.50 28789.50

So at this point I'm still not getting this which I am ultimately wanting for each and every account if you we were to figure out the correct syntax:
(2878.95 + 25910.55) - (28789.50) as [Current Balance],
....