Adding Calculated Columns to a SQL Group By query

Matthew Moran

I'm trying to learn how to run a couple calculated fields on columns calculated in a group by query.

I'm running a SQL query via (ODBC) in Microsoft Access.

SELECT 
    MasterItemID, ItemID,
    SUM(SWITCH(JournalEX = 18, StockQtyRec, JournalEX <> 18, 0)) AS orderedByZG,
    SUM(SWITCH(JournalEX = 19, StockQty, JournalEX <> 19, 0)) AS orderedFromZG,
    ABS(SUM(SWITCH(JournalEX = 9 AND IncludeInInvLedger = 1, StockQty, JournalEX <> 9 AND IncludeInInvLedger <> 1, 0))) AS returnedItems,
    SUM(SWITCH(JournalEX = 14 AND IncludeInInvLedger = 1, StockQty, JournalEX <> 14 AND IncludeInInvLedger <> 1, 0)) AS inventoryAdjustments
FROM 
    qryHdrtoLineItem
GROUP BY 
    MasterItemID, ItemID;

I need to include two additional columns, each a calculation of the grouped by fields but which are not aggregated totals and do not run in the group by query.

quantityOnHand is orderedByZG - orderedFromZG - returnedItems + inventoryAdjustments

returnPercentage is returnedItems / orderedFromZG * 100

I have it working by saving the above query and running a query referencing the column Names and then adding the calculated columns.

Can this be done via a CTE? Is there a better way to accomplish this?

Note: In Access odbc the switch function is the same as Case.

JNevill

With a subquery:

SELECT 
   MasterItemId,
   ItemID,
   orderedByZG - orderedFromZG - returnedItems + inventoryAdjustments as QtyOnHand,
   returnedItems / orderedFromZG * 100 as returnPercentage
FROM 
   (

    SELECT 
        MasterItemID, ItemID,
        SUM(SWITCH(JournalEX = 18, StockQtyRec, JournalEX <> 18, 0)) AS orderedByZG,
        SUM(SWITCH(JournalEX = 19, StockQty, JournalEX <> 19, 0)) AS orderedFromZG,
        ABS(SUM(SWITCH(JournalEX = 9 AND IncludeInInvLedger = 1, StockQty, JournalEX <> 9 AND IncludeInInvLedger <> 1, 0))) AS returnedItems,
        SUM(SWITCH(JournalEX = 14 AND IncludeInInvLedger = 1, StockQty, JournalEX <> 14 AND IncludeInInvLedger <> 1, 0)) AS inventoryAdjustments
    FROM 
        qryHdrtoLineItem
    GROUP BY 
        MasterItemID, ItemID

) sub

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to make sql query for adding calculated values group by id?

SQL Server issue with select and group by with calculated columns

Group columns of a SQL query into an array

Problem with SQL group by and summing totals with a calculated column and sub query

ADDING COLUMNS WITHOUT ADDING IN GROUP BY, NESTED QUERY INSTEAD

SQL adding two columns and group by count

SQL query to get a calculated column based on few conditions on existing columns

SQL query by adding two columns in where clause?

How to GROUP BY a calculated column in a query?

Postgresql - Optimize query with calculated columns

Adding filters to calculated fields SQL

Get related columns in group by SQL query

How to group by and split columns dynamically with sql query?

SQL Server : query columns to JSON object with group by

SQL query/UDF across columns in GROUP by

Sql Calculated Group By syntax possibility

Sum of two calculated columns in SQL

SQL null spaces in calculated columns

SQL - Using a Group By with Adding More Columns Showing Additional Rows

SQL Query to Sum Calculated Field

Sql query to create a calculated field

How to group a calculated column by multiple columns?

Oracle SQL query output in a required format by adding columns

Adding multiple partitioned columns to BigQuery table from SQL query

Adding percentages calculated by group_by to all rows in that group?

How is rand() calculated when used in a query with GROUP BY

Does mysql query cache the dynamically calculated columns

Converting Access query with calculated columns to TSQL

SQL Server: adding a calculated column for end date