我有以下表结构
TradeId TableName PricingSecurityID Quantity Price
2008 Fireball.dbo.Bond 506 50 100.0000
2009 Fireball.dbo.Bond 506 50 100.2500
2010 Fireball.dbo.Bond 588 50 100.7500
2338 Fireball.dbo.Bond 588 100 102.5000
我需要获得Quantity
匹配的总和,或者我们可以按特定PricingSecurityID
与PricingSecurityID=506
类似,我应该quantity=100
和PricingSecurityID=588
我应该quantity=150
如何编写此SQL查询?
我确实尝试过简单的分组声明 但是因为我也选择了tradeid我得到错误: 列'TradeId'在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中。
答案 0 :(得分:14)
修订问题 - 还需要TradeID。
SELECT f.TradeID, f.PricingSecurityID, s.TotalQuantity
FROM FollowingTableStructure AS f
JOIN (SELECT PricingSecurityID, SUM(Quantity) AS TotalQuantity
FROM FollowingTableStructure
GROUP BY PricingSecurityId
) AS s ON f.PricingSecurityID = s.PricingSecurityID
我并不完全相信这个问题是明智的,但那是你的问题。它可以很容易地扩展到处理其他表;只需添加适当的JOIN子句。
请记住在问题中包含一个表格名称 - 令人惊讶的是,在不给表格提供名称的情况下,提出SQL问题的频率是多少(因此,不仅是您忘记了任何方式)。
重新更新问题
因此,最初的匿名表显然是Fireball.dbo.Trade
或Fireball..Trade
。我可能会将11路UNION放入视图中,因为它可能会在多个地方使用。但是,忽略这一点,我们仍然可以将信息放入您的查询中:
SELECT t.TradeId,
ISNULL(Securities.SecurityType,'Other') SecurityType,
Securities.TableName,
CASE
WHEN SecurityTrade.SecurityId IS NOT NULL THEN SecurityTrade.SecurityId
ELSE Trade.SecurityId
END AS PricingSecurityID,
s.TotalQuantity AS Quantity,
t.Price,
CASE
WHEN (t.Buy = 1 AND t.Long = 1) THEN 1
WHEN (t.Buy = 0 AND t.Long = 0) THEN 1
ELSE 0
END AS Position
FROM Fireball_Reporting..Trade AS t
JOIN (SELECT PricingSecurityID, SUM(Quantity) AS TotalQuantity
FROM Fireball_Reporting..Trade
GROUP BY PricingSecurityId
) AS s ON t.PricingSecurityID = s.PricingSecurityID
LEFT JOIN
(SELECT TradeId, 'Bond' SecurityType, 'Fireball.dbo.Bond' TableName FROM Fireball..CorpBondTrade
UNION
SELECT TradeId, 'IRS' SecurityType, 'Fireball.dbo.Bond' TableName FROM Fireball..IRPTrade
UNION
SELECT TradeId, 'Treasury' SecurityType, 'Fireball.dbo.Bond' TableName FROM Fireball..TreasuryTrade
UNION
SELECT TradeId, 'Index' SecurityType, 'Fireball.dbo.CDSIndex' TableName FROM Fireball..CreditIndexTrade
UNION
SELECT TradeId, 'CDS' SecurityType, 'Fireball.dbo.CDS' TableName FROM Fireball..CDSTrade WHERE IsSovereign = 0
UNION
SELECT TradeId, 'Sovereign CDS' SecurityType, 'Fireball.dbo.CDS' TableName FROM Fireball..CDSTrade WHERE IsSovereign = 1
UNION
SELECT TradeId, 'Equity Option' SecurityType, 'Fireball.dbo.EquityOption' TableName FROM Fireball..EquityOptionTrade
UNION
SELECT TradeId, 'Equity' SecurityType, 'Fireball.dbo.Equity' TableName FROM Fireball..EquityTrade
UNION
SELECT TradeId, 'Loan' SecurityType, 'Fireball.dbo.Loan' TableName FROM Fireball..LoanTrade
UNION
SELECT TradeId, 'Swaption' SecurityType, 'Fireball.dbo.Bond' TableName FROM Fireball..SwaptionTrade
UNION
SELECT TradeId, 'Preferred Stock' SecurityType, 'Fireball.dbo.Bond' TableName FROM Fireball..PreferredEquityTrade
--UNION
--SELECT TradeId, 'Bond' SecurityType FROM Fireball..BondTrade
) AS Securities ON Securities.TradeId = t.TradeId
LEFT JOIN
(SELECT TradeID, SecurityId
FROM Fireball..CDSTrade
UNION
SELECT TradeID, SecurityId
FROM Fireball..CreditIndexTrade
) AS SecurityTrade ON SecurityTrade.TradeId = t.TradeId
这主要是复制和粘贴 - 通过一些重新格式化 - 查询,并在Trade表后面的FROM子句中隐藏了额外的子查询。如果是我的查询,我也会在最后两个子查询中使用单字母(或其他短助记符)别名;我只是没有花时间研究SecurityTrade和证券的适当缩写。
答案 1 :(得分:8)
select PricingSecurityID, sum(Quantity)
from table
group by PricingSecurityID
答案 2 :(得分:8)
select PricingSecurityID, sum(quantity)
from Fireball.dbo.Bond
group by PricingSecurityID
答案 3 :(得分:5)
SELECT PricingSecurityID, SUM(ISNULL(Quantity,0))
FROM Table
GROUP BY PricingSecurityId;