Hi I want to sum shares by grouped by ATS_MPID, then also by Trades_Last_Updated, and I want to then divide the two (ATSSum / Trades_Last_UpdatesSum). This is what I have so far..thank you
WITH symboltotals AS (
SELECT Symbol, SUM(Shares) AS SymTot, Trades_Last_Updated
FROM ATS AS S
GROUP BY Trades_Last_Updated, Symbol),
atstotals AS (
SELECT Symbol, ATS_MPID, SUM(Shares) as "ATSTotal"
FROM ATS AS A
GROUP BY ATS_MPID, Symbol)
SELECT *
FROM symboltotals, atstotals
WHERE symboltotals.Symbol = atstotals.Symbol
Sample Data:
Symbol ATS_MPID Shares Trades_Last_Updated
A BIDS 317800 9/17/2018
A CROS 125508 9/17/2018
A BIDS 124300 9/10/2018
A CROS 117419 9/10/2018
I'm trying to Get the percent per ATS_MPID for all shares traded. So sum Symbol A for BIDS (9/17 & 9/10), do the same for CROS, then divide those totals by the total shares to see their individual contribution %... i.e.: (Shares of BIDS 9/17 + Shares of BIDS 9/10) / Total Shares
答案 0 :(得分:1)
Based on the data and your statement of problem, following query will help
create table yourtable(Symbol varchar(100), ATS_MPID varchar(100), Shares bigint, Trades_Last_Updated datetime);
insert into yourtable values
('A', 'BIDS', 317800, '9/17/2018')
,('A', 'CROS', 125508, '9/17/2018')
,('A', 'BIDS', 124300, '9/10/2018')
,('A', 'CROS', 117419, '9/10/2018');
select *,
percentage=(sum(shares) over (partition by Symbol, ATS_MPID)*100.00)/sum(shares) over (partition by Symbol)
--,percentagePerLastUpdated=(sum(shares) over (partition by Symbol, ATS_MPID, Trades_Last_Updated)*100.00)/sum(shares) over (partition by Symbol)
from yourtable
also see live demo