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

  • Home
  • SEARCH
  • 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 8556843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:26:42+00:00 2026-06-11T15:26:42+00:00

I was writing a query the other day in SQL Server, and had to

  • 0

I was writing a query the other day in SQL Server, and had to get a sum of a column added to the query.

Now, the query I had to add the sum column already had about 20 selected columns.

In order for the sum (aggregate function) to work, I had to add GROUP BY for each pre selected column.

This ended up as an extremely tedious and boring task (see the query below). I had to add about 50 new lines to the query just to get SUM for one column to work.

Is there a way to automate writing such queries using SQL Server Management Studio?

Is this the same with other databases (such as PL/SQL, Oracle, MySQL?)

This is what the query looks like.

SELECT 
       mvLogisticContracts.[mvLogisticContract_id]
      ,mvLogisticContracts.[CONTRACTNUMBER] 
      ,mvLogisticContracts.[CUSTNMBR]
      ,mvLogisticContracts.[DateCreated]
      ,mvLogisticContracts.[TruckAllocatedTotal]      ,mvLogisticContracts.[mvLOgisticAddress_id_StartOriginal]
          ,mvLogisticContracts.[mvLogisticAddress_id_StartOriginal]       ,mvLogisticContractLineItems.[LineItemQuantity]     ,mvLogisticContractLineItems.[LineItemNo]
          ,mvLogisticContractLineItems.[OffloadRequired]
          ,mvLogisticContractLineItems.[CarrierRequested]     ,mvLogisticContractLineItems.[RequestedSerialNo]
          ,mvLogisticContractLineItems.[mvLogisticAddress_id_Start]
          ,mvLogisticContractLineItems.[DateReadyBy]      ,mvLogisticContractLineItems.[DateCustomerRequested],
          mvLogisticContractLineItems.[mvLogisticContractLineItem_id]
      ,mvLogisticSalespersons.[FirstName],mvLogisticSalespersons.[LastName],
      mvLogisticServiceTypes.ServiceType,
      mvLogisticStatuses.Description,
      mvLogisticSKUs.[SKU],       mvLogisticSKUs.[Length],
      a1.CITY as \"CityStart\", a1.AddressName as \"AddressNameStart\", a1.Address1 as \"Address1Start\", a1.STATE as \"StateStart\", a1.ZIP as \"ZipStart\",
      a2.CITY as \"CityEnd\", a2.AddressName as \"AddressNameEnd\", a2.Address1 as \"Address1End\", a2.STATE as \"StateEnd\", a2.ZIP as \"ZipEnd\",
      mvLogisticContracts.[mvLogisticAddress_id_Start] AS aa1,
      mvLogisticContracts.[mvLogisticAddress_id_End] AS aa2,
      SUM(mvLogisticReleases.ReleaseQuantity) as ReleaseQtySum
            FROM mvLogisticContractLineItems
            LEFT JOIN mvLogisticContracts ON mvLogisticContractLineItems.mvLogisticContract_id = mvLogisticContracts.mvLogisticContract_id
            LEFT JOIN mvLogisticSalespersons ON mvLogisticContracts.[mvLogisticSalesperson_id] = mvLogisticSalespersons.[mvLogisticSalesperson_id]
            LEFT JOIN mvLogisticServiceTypes ON mvLogisticContractLineItems.mvLogisticServiceType_id = mvLogisticServiceTypes.mvLogisticServiceType_id
            LEFT JOIN mvLogisticStatuses ON mvLogisticContractLineItems.mvLogisticStatus_id = mvLogisticStatuses.mvLogisticStatus_id
            LEFT JOIN mvLogisticSKUs ON mvLogisticContractLineItems.[SKU_id] = mvLogisticSKUs.[SKU_id]
            LEFT JOIN mvLogisticAddresses a1 ON a1.[mvLogisticAddress_id] = mvLogisticContractLineItems.[mvLogisticAddress_id_Start]
            LEFT JOIN mvLogisticAddresses a2 ON a2.[mvLogisticAddress_id] = mvLogisticContractLineItems.[mvLogisticAddress_id_End]
            /*LEFT JOIN mvLogisticAddresses a1 ON a1.[mvLogisticAddress_id] = mvLogisticContracts.[mvLogisticAddress_id_Start]
            LEFT JOIN mvLogisticAddresses a2 ON a2.[mvLogisticAddress_id] = mvLogisticContracts.[mvLogisticAddress_id_End]*/
            LEFT JOIN mvLogisticReleases ON mvLogisticContractLineItems.mvLogisticContractLineItem_id = mvLogisticReleases.mvLogisticContractLineItem_id
            GROUP BY
            mvLogisticContracts.[mvLogisticContract_id]
      ,mvLogisticContracts.[CONTRACTNUMBER] 
      ,mvLogisticContracts.[CUSTNMBR]
      ,mvLogisticContracts.[DateCreated]
      ,mvLogisticContracts.[TruckAllocatedTotal]      ,mvLogisticContracts.[mvLOgisticAddress_id_StartOriginal]
          ,mvLogisticContracts.[mvLogisticAddress_id_StartOriginal]       ,mvLogisticContractLineItems.[LineItemQuantity]     ,mvLogisticContractLineItems.[LineItemNo]
          ,mvLogisticContractLineItems.[OffloadRequired]
          ,mvLogisticContractLineItems.[CarrierRequested]     ,mvLogisticContractLineItems.[RequestedSerialNo]
          ,mvLogisticContractLineItems.[mvLogisticAddress_id_Start]
          ,mvLogisticContractLineItems.[DateReadyBy]      ,mvLogisticContractLineItems.[DateCustomerRequested],
          mvLogisticContractLineItems.[mvLogisticContractLineItem_id]
      ,mvLogisticSalespersons.[FirstName],mvLogisticSalespersons.[LastName],
      mvLogisticServiceTypes.ServiceType,
      mvLogisticStatuses.Description, 
      mvLogisticSKUs.[SKU],       
      mvLogisticSKUs.[Length], 
      a1.CITY, a1.AddressName, a1.Address1, a1.STATE, a1.ZIP, 
      a2.CITY, a2.AddressName, a2.Address1, a2.STATE, a2.ZIP, 
      mvLogisticContracts.[mvLogisticAddress_id_Start], 
      mvLogisticContracts.[mvLogisticAddress_id_End]
  • 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-11T15:26:43+00:00Added an answer on June 11, 2026 at 3:26 pm

    Subqueries and common table expressions (CTEs) are two ways to isolate the aggregation(s) down to a localized spot that is easy to join up to the main SELECT statement.

    Correlated/synchronized subquery example:

    SELECT c.contract_id, c.contractnum, c.customernum,
        ( SELECT SUM( li.price * li.orderquantity ) 
         FROM lineitems li 
         WHERE li.contract_id = c.contract_id
         ) AS grand_total
    FROM contracts c 
    WHERE ...
    

    Referencing the correlated subquery in your SELECT list is a convenient way to bring back a single value.

    Joining to a CTE:

    WITH ordersummary AS 
    ( SELECT li.contract_id, 
          COUNT( DISTINCT li.partnum ) AS distinctproducts, 
          SUM( li.orderquantity ) AS itemcount,
          SUM( li.price * li.orderquantity) AS totalprice,
          MIN( p.ship_ready_date ) AS earliest_partial_ship_date,
          MAX( p.ship_ready_date ) AS earliest_complete_ship_date
          FROM lineitems li 
          INNER JOIN parts p 
              ON p.partnum = li.partnum
          GROUP BY li.contract_id
    )
    SELECT c.contract_id, c.contractnum, c.customernum,
    s.distinctproducts, s.itemcount, s.totalprice,
    s.earliest_partial_ship_date, s.earliest_complete_ship_date
    FROM contracts c 
    INNER JOIN ordersummary s
    ON s.contract_id = c.contract_id
    WHERE ...
    

    Joining to an expression:

    SELECT c.contract_id, c.contractnum, c.customernum,
    s.distinctproducts, s.itemcount, s.totalprice,
    s.earliest_partial_ship_date, s.earliest_complete_ship_date
    FROM contracts c 
    INNER JOIN
    ( SELECT li.contract_id, 
          COUNT( DISTINCT li.partnum ) AS distinctproducts, 
          SUM( li.orderquantity ) AS itemcount,
          SUM( li.price * li.orderquantity) AS totalprice,
          MIN( p.ship_ready_date ) AS earliest_partial_ship_date,
          MAX( p.ship_ready_date ) AS earliest_complete_ship_date
          FROM lineitems li 
          INNER JOIN parts p 
              ON p.partnum = li.partnum
          GROUP BY li.contract_id
    ) AS s
    ON s.contract_id = c.contract_id
    WHERE ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know several ways of writing paged query in SQL Server 2005. But I
I'm writing a SQL query in SQL Server in which I need to replace
I'm writing a query and as I go along I'm analysing the SQL Profiler
I'm writing a query to get ALL of the products in the products table,
I totally can't get my head around writing a correct query for my problem
I have some problems with writing a SQL query for MySQL. I have a
I'm writing a linq-to-sql query that joins 4 tables. I have something like this:
I'm writing a query to get results matching one of multiple phrases, like {
I am facing a problem for writing an sql query which should be easy
Hi I am trying to improvise my query to get better performance.Is any other

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.