Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8661121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:25:14+00:00 2026-06-12T16:25:14+00:00

Possible Duplicate: sql query to sum the data I have following table structure TradeId

  • 0

Possible Duplicate:
sql query to sum the data

I have following table structure

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    

I need to take a sum of Quantity of matching or we can say group by particular PricingSecurityID

like for PricingSecurityID=506 I should get quantity=100

and for PricingSecurityID=588 I should get quantity=150

How can I write this SQL query?

I did try with simple group by statement
but as i’m also selecting tradeid i’m getting error :
Column ‘TradeId’ is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T16:25:15+00:00Added an answer on June 12, 2026 at 4:25 pm

    Revised question — the TradeID is also needed.

    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
    

    I’m not wholly convinced the query is sensible, but that’s your problem. It can easily be extended to deal with other tables; just add appropriate JOIN clauses.


    Please remember to include a table name in the question — it is astonishing how often SQL questions are asked without giving the table a name (so it is not only you who forgets by any means).


    Re-updated question

    So the originally anonymous table is, apparently, Fireball.dbo.Trade or Fireball..Trade. I’d probably place the 11-way UNION into a view since it is likely to be used in multiple places. However, ignoring that, we can still slip the information into your query:

    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
    

    That’s mostly copy and paste — with some reformatting — of your query, with the extra sub-query tucked away in the FROM clause after the Trade table. Were it my query, I’d be using single-letter (or other short mnemonic) aliases for the last two sub-queries too; I just didn’t spend the time working out what were appropriate abbreviations for SecurityTrade and Securities.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: SQL Server dynamic PIVOT query? I have temporary table with following structure:
Possible Duplicate: Writing an SQL query to SELECT item from the following table I
Possible Duplicate: SQL query to find Missing sequence numbers I have a table which
Possible Duplicate: SQL Query JOIN with Table If this is the data in TestingTable1
Possible Duplicate: SQL Server Database query help Hi, I have a problem that I
Possible Duplicate: SQL Server: Only last entry in GROUP BY I have a table
Possible Duplicate: Parameterizing a SQL IN clause? Hi, I have a query looks like
Possible Duplicate: oracle PL/SQL: sort rows I run this query: Select a.product, sum( case
Possible Duplicate: Insert line into a query result (sum) I have a report that
Possible Duplicate: Duplicate result Interview - Detect/remove duplicate entries I have a SQL Query,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.