Home > Database > Mysql Tutorial > How to Perform Conditional Summation in MSSQL Group By Queries?

How to Perform Conditional Summation in MSSQL Group By Queries?

Barbara Streisand
Release: 2025-01-08 07:08:40
Original
722 people have browsed it

How to Perform Conditional Summation in MSSQL Group By Queries?

Conditional Summation with MSSQL's GROUP BY Clause

Microsoft SQL Server (MSSQL) offers flexible data aggregation using the GROUP BY clause. To perform conditional summation within a GROUP BY query, leverage the CASE statement to selectively include values in the sum based on specific criteria.

Consider an OrderDetails table with the following structure:

<code>----------------------------------------------------------------
|  OrderId  |  CopyCost  |  FullPrice  |  Price  |  PriceType  |
----------------------------------------------------------------
|  16       |  50        |  100        |  50     |  CopyCost   |
----------------------------------------------------------------
|  16       |  50        |  100        |  100    |  FullPrice  |
----------------------------------------------------------------
|  16       |  50        |  100        |  50     |  CopyCost   |
----------------------------------------------------------------
|  16       |  50        |  100        |  50     |  CopyCost   |
----------------------------------------------------------------</code>
Copy after login

To calculate the ItemCount, TotalCopyCost, and TotalFullPrice for each OrderId, use this query:

<code class="language-sql">SELECT OrderId, 
       COUNT(*) AS ItemCount,
       SUM(CASE WHEN PriceType = 'CopyCost' THEN Price ELSE 0 END) AS TotalCopyCost,
       SUM(CASE WHEN PriceType = 'FullPrice' THEN Price ELSE 0 END) AS TotalFullPrice
FROM OrderDetails
GROUP BY OrderId;</code>
Copy after login

The CASE statement within the SUM() function conditionally sums Price values. If PriceType is 'CopyCost', the Price is included; otherwise, 0 is added. This allows for separate totals for 'CopyCost' and 'FullPrice' per OrderId.

The output would be:

<code>----------------------------------------------------------------
|  OrderId  |  ItemCount  |  TotalCopyCost  |  TotalFullPrice  |
----------------------------------------------------------------
|  16       |  4          |  150            |  100             |
----------------------------------------------------------------</code>
Copy after login

This demonstrates how CASE statements enable powerful conditional aggregation within GROUP BY queries, providing a flexible method for data summarization and analysis in MSSQL.

The above is the detailed content of How to Perform Conditional Summation in MSSQL Group By Queries?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template