I posted this question quite some time ago, but unfortunately, the problem persists:
http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/Q_23472149.htmlVery long story short, this report is done on a certain type of trade, called an 'strade'. The 1st query is a detailed report of all strades. The 2nd query is a summation of the detailed report. For example, if 1st report includes 50 trades for userA, 10 of which were 'strades', then the summation will be the volume of the 50 totaltrades, along with the volume and count of the 10 strades.
It has come to my attention that some of them are correct, others are reported inaccurately. For example, this one is coming back as 16 strades, when it should be 7.
First query returns 7 records:
SELECT DISTINCT b.EndPoint,b.UserID AS BUserID,b.AcctID AS BAcctID,s.UserID AS SUserID,s.AcctID AS SAcctID,b.Symbol,b.Quantit
y, b.Price,
CONVERT(VARCHAR(10),b.time
field,101)
+' '+SUBSTRING(CONVERT(VARCHA
R(20),b.ti
mefield,10
0),13,7) AS TradeTime
FROM database.dbo.strades b WITH (NOLOCK)
INNER JOIN database.dbo.strades s WITH (NOLOCK)
ON b.endpoint = s.endpoint
AND b.Quantity = s.Quantity
AND b.Price = s.Price
AND b.tradetime = s.tradetime
AND b.side = 'B'
AND s.side = 'S'
AND b.endpoint = 'cccc' and b.Userid = 'abc' and b.symbol = 'ymu8'
ORDER BY EndPoint,BUserID,b.Symbol
endpoint buserid bacctid suserid sacctid s.symbol s.qty s.price, s.time
cccc abc 0814 CH 08139 YMU8 1 12169.00000000 06/17/2008 2:24PM
cccc abc 08142 JR 08139 YMU8 1 12167.00000000 06/17/2008 2:39PM
cccc abc 08142 JR 08139 YMU8 1 12169.00000000 06/17/2008 2:21PM
cccc abc 08142 JR 08139 YMU8 1 12195.00000000 06/17/2008 11:33AM
cccc abc 08142 JR 08139 YMU8 1 12213.00000000 06/17/2008 12:24PM
cccc abc 08142 JR 08139 YMU8 1 12221.00000000 06/17/2008 10:29AM
cccc abc 08142 JR 08139 YMU8 1 12255.00000000 06/17/2008 9:34AM
Second one does a summation on the results of the first, yet it gives a total count of 16, instead of the 7 identified in query #1 :
SELECT b.endpoint,b.userID,b.Symb
ol,SUM(b.q
uantity)AS
STradeVol,COUNT(*) AS TotalSTrades
FROM database.dbo.strades b WITH (NOLOCK)
INNER JOIN database.dbo.strades s WITH (NOLOCK)
ON b.endpoint = s.endpoint
AND b.Quantity = s.Quantity
AND b.Price = s.Price
AND b.tradetime = s.tradetime
AND b.side = 'B'
AND s.side = 'S'
AND b.endpoint = 'cccc' aND b.userID = 'abc' and b.symbol = 'YMU8'
GROUP BY b.endpoint,b.userID,b.Symb
ol
endpoint userid symbol stradeVolume totalSTrades
cccc abc YMU8 16 16
Why do these two queries produce different resultsets? The first details 7 records, the count in the summation should be 7, not 16.
Start Free Trial