仅检索SQL表中每个实体的一条记录

时间:2013-10-28 15:03:04

标签: sql sql-server outer-join

我有一个复杂的连接查询,导致大约12000个项目,如下所示

SELECT        a.ID AS 'DealID', a.TradeDate, c.COMPANYNAME AS 'Seller Company', a.SellCommission, h.BROKER_FULLNAME AS 'Seller Trader', 
                         j.DisplayName AS 'Seller Broker', d.COMPANYNAME AS 'Buyer Company', a.BuyCommission, g.BROKER_FULLNAME AS 'Buyer Trader', 
                         i.DisplayName AS 'Buyer Broker', e.PRODUCT_NAME, f.TYPE_DESC AS 'Quantity Type', f.NBR_OF_GALLONS AS 'Quantity Multiplier', a.ContractVolume, 
                         a.TotalVolume, a.DeliveryPoint, a.Price, a.ContractStart, a.ContractEnd
FROM            Confirmations AS a WITH (nolock) LEFT OUTER JOIN
                         COMPANY AS c WITH (nolock) ON c.COMPANY_ID = a.SellCompany LEFT OUTER JOIN
                         COMPANY AS d WITH (nolock) ON d.COMPANY_ID = a.BuyCompany LEFT OUTER JOIN
                         BIOPRODUCTTYPES AS e WITH (nolock) ON e.ID = a.ProductID LEFT OUTER JOIN
                         BIO_QUANTITY_TYPE AS f WITH (nolock) ON f.ID = a.QuantityTypeID LEFT OUTER JOIN
                         COMPANYBROKER AS g WITH (nolock) ON g.COMPANYBROKER_ID = a.BuyTrader LEFT OUTER JOIN
                         COMPANYBROKER AS h WITH (nolock) ON h.COMPANYBROKER_ID = a.SellTrader LEFT OUTER JOIN
                         Users AS i WITH (nolock) ON i.ID = a.BuyBroker LEFT OUTER JOIN
                         Users AS j WITH (nolock) ON j.ID = a.SellBroker
WHERE        (a.TradeDate >= '2011-04-01 00:00:00') AND (a.TradeDate <= '2013-10-25 00:00:00')
ORDER BY a.TradeDate

但是,我只想要一条记录,而不是所有12000条记录,这是每个“卖方交易者”的最新记录(目前上方查询返回卖方交易者完成的所有交易),这可能会返回大约300条记录。

2 个答案:

答案 0 :(得分:1)

要获取最新记录,一种方法是对数据进行子查询:

SELECT   a.ID AS 'DealID',
         a.TradeDate,
         c.COMPANYNAME AS 'Seller Company',
         a.SellCommission,
         h.BROKER_FULLNAME AS 'Seller Trader',
         j.DisplayName AS 'Seller Broker',
         d.COMPANYNAME AS 'Buyer Company',
         a.BuyCommission,
         g.BROKER_FULLNAME AS 'Buyer Trader',
         i.DisplayName AS 'Buyer Broker',
         e.PRODUCT_NAME,
         f.TYPE_DESC AS 'Quantity Type',
         f.NBR_OF_GALLONS AS 'Quantity Multiplier',
         a.ContractVolume,
         a.TotalVolume,
         a.DeliveryPoint,
         a.Price,
         a.ContractStart,
         a.ContractEnd
FROM Confirmations AS a WITH (NOLOCK)
         LEFT OUTER JOIN COMPANY AS c WITH (NOLOCK) ON c.COMPANY_ID = a.SellCompany
         LEFT OUTER JOIN COMPANY AS d WITH (NOLOCK) ON d.COMPANY_ID = a.BuyCompany
         LEFT OUTER JOIN BIOPRODUCTTYPES AS e WITH (NOLOCK) ON e.ID = a.ProductID
         LEFT OUTER JOIN BIO_QUANTITY_TYPE AS f WITH (NOLOCK) ON f.ID = a.QuantityTypeID
         LEFT OUTER JOIN COMPANYBROKER AS g WITH (NOLOCK) ON g.COMPANYBROKER_ID = a.BuyTrader
         LEFT OUTER JOIN COMPANYBROKER AS h WITH (NOLOCK) ON h.COMPANYBROKER_ID = a.SellTrader
         LEFT OUTER JOIN Users AS i WITH (NOLOCK) ON i.ID = a.BuyBroker
         LEFT OUTER JOIN Users AS j WITH (NOLOCK) ON j.ID = a.SellBroker
WHERE    a.ID in 
         (
            Select max(ID) 
            from Confirmations (NOLOCK)
            Where TradeDate >= '2011-04-01 00:00:00' AND TradeDate <= '2013-10-25 00:00:00'
            Group by SellTrader
        )
ORDER BY a.TradeDate;

答案 1 :(得分:1)

你可以写成:

select * from (
SELECT      a.ID AS 'DealID',
            a.TradeDate,
            c.COMPANYNAME AS 'Seller Company',
            a.SellCommission,
            h.BROKER_FULLNAME AS 'Seller Trader', 
            j.DisplayName AS 'Seller Broker',
            d.COMPANYNAME AS 'Buyer Company',
            a.BuyCommission,
            g.BROKER_FULLNAME AS 'Buyer Trader', 
            i.DisplayName AS 'Buyer Broker',
            e.PRODUCT_NAME,
            f.TYPE_DESC AS 'Quantity Type',
            f.NBR_OF_GALLONS AS 'Quantity Multiplier',
            a.ContractVolume, 
            a.TotalVolume,
            a.DeliveryPoint,
            a.Price,
            a.ContractStart,
            a.ContractEnd
            ,ROW_NUMBER() over ( partition by  h.BROKER_FULLNAME order by a.TradeDate desc) as row_num
FROM            Confirmations AS a WITH (nolock) LEFT OUTER JOIN
                         COMPANY AS c WITH (nolock) ON c.COMPANY_ID = a.SellCompany LEFT OUTER JOIN
                         COMPANY AS d WITH (nolock) ON d.COMPANY_ID = a.BuyCompany LEFT OUTER JOIN
                         BIOPRODUCTTYPES AS e WITH (nolock) ON e.ID = a.ProductID LEFT OUTER JOIN
                         BIO_QUANTITY_TYPE AS f WITH (nolock) ON f.ID = a.QuantityTypeID LEFT OUTER JOIN
                         COMPANYBROKER AS g WITH (nolock) ON g.COMPANYBROKER_ID = a.BuyTrader LEFT OUTER JOIN
                         COMPANYBROKER AS h WITH (nolock) ON h.COMPANYBROKER_ID = a.SellTrader LEFT OUTER JOIN
                         Users2 AS i WITH (nolock) ON i.ID = a.BuyBroker LEFT OUTER JOIN
                         Users2 AS j WITH (nolock) ON j.ID = a.SellBroker
WHERE        (a.TradeDate >= '2011-04-01 00:00:00') AND (a.TradeDate <= '2013-10-25 00:00:00')
)  T
where row_num = 1
ORDER BY T.TradeDate