CREATE proc procname
as
set nocount on
/* simply such that we can report if any XXXX trades exist with a position <> 0 */
IF EXISTS(SELECT buys-sells AS Difference
FROM
(SELECT Symbol,SUM(quantity) AS Buys
FROM database.dbo.viewname
WHERE side = 'B'
AND endpoint = 'XXXX'
GROUP BY symbol) buys JOIN
(SELECT Symbol,SUM(quantity) AS Sells
FROM database.dbo.viewname
WHERE side = 'S'
and endpoint = 'XXXX'
group by symbol) sells
ON buys.Symbol = sells.Symbol
WHERE buys <> sells)
BEGIN
SELECT buys.EndPoint,buys.Symbol,Buys,Sells,buys-sells AS Difference
FROM
(SELECT EndPoint,Symbol, SUM(quantity) AS buys
FROM database.dbo.viewname
WHERE side = 'B'
AND endpoint = 'XXXX'
GROUP BY endpoint,symbol) buys JOIN
(SELECT EndPoint,Symbol, SUM(quantity) AS sells
FROM database.dbo.viewname
WHERE side = 'S'
AND endpoint = 'XXXX'
GROUP BY endpoint,symbol) sells
ON buys.Symbol = sells.Symbol
WHERE buys <> sells
ORDER BY buys.Symbol
END
set nocount off
GO
|