Purpose of using syntax code "CASE GROUPING" - sql

I don't understand the purpose of using syntax code "CASE GROUPING"?
Unfortunately, I don't have the database to review the sourcecode below.
SELECT
CASE GROUPING(st.stor_name) WHEN 0 THEN st.stor_name ELSE 'ALL' END AS Store,
CASE GROUPING(s.type) WHEN 0 THEN s.type ELSE 'ALL TYPES' END AS Type,
SUM(s.qty) AS TotalSold
FROM
(SELECT DISTINCT st.stor_id, t.type, 0 AS qty
FROM stores st CROSS JOIN titles t
UNION ALL
SELECT
s.stor_id,
t.type, s.qty
FROM sales s JOIN titles t ON s.title_id=t.title_id) s
JOIN stores st ON (s.stor_id=st.stor_id)
GROUP BY st.stor_name, s.type WITH CUBE

CASE is a conditional expression, like an if statement.
GROUPING is a function that:
Indicates whether a specified column expression in a GROUP BY list is aggregated or not. GROUPING returns 1 for aggregated or 0 for not aggregated in the result set. GROUPING can be used only in the SELECT list, HAVING, and ORDER BY clauses when GROUP BY is specified.

Related

How to use case statement and min() with group by?

The following query when I that execute
SELECT CASE
WHEN spd.IS_MAIN_DEFECT='Y'
THEN spd.piece_Defect_num_id
ELSE min(spd.PIECE_DEFECT_NUM_ID)
END AS defect
FROM piece P,
STY_PIECE_DEFECT spd,
STY_DEFECT_CATALOGUE sdc,
piece_history ph,
piece_history_out pho,
PLANT_CONFIG pc
(...inner join and where clause)
GROUP BY p.PIECE_ID,
CASE
WHEN spd.IS_MAIN_DEFECT='Y'
THEN spd.piece_Defect_num_id
ELSE min(spd.PIECE_DEFECT_NUM_ID)
end
It seems error
ORA-00934: group function is not allowed here
I guess , there is error min() in group by.
How can I solve this problem?
You have to use analytical MIN() function like the below without group by
SELECT distinct CASE WHEN spd.IS_MAIN_DEFECT='Y'
THEN spd.piece_Defect_num_id
ELSE min(spd.PIECE_DEFECT_NUM_ID) over () END AS defect
FROM piece P , STY_PIECE_DEFECT spd ,STY_DEFECT_CATALOGUE sdc ,piece_history ph
, piece_history_out pho, PLANT_CONFIG pc
(...inner join and where clause)
I understood you want to preserve the PIECE_DEFECT_NUM_ID in rows with IS_MAIN_DEFECT = 'Y' and get the MIN value for the non-main defect lines.
The simplest solution for the case there is only one row per PIECE_ID with IS_MAIN_DEFECT = 'Y' - you group on PIECE_ID, IS_MAIN_DEFECTand calculates min(PIECE_DEFECT_NUM_ID)
which is fine a MIN for one row group is equal to the original value
select PIECE_ID,IS_MAIN_DEFECT,
min(PIECE_DEFECT_NUM_ID) PIECE_DEFECT_NUM_ID
from tab
group by PIECE_ID, IS_MAIN_DEFECT
order by 1,2 desc;
In case you can have more rows (same PIECE_ID) with the main flag, simple select them ungrouped and add the grouped non-main part using UNION ALL
select PIECE_ID,IS_MAIN_DEFECT,PIECE_DEFECT_NUM_ID
from tab
where IS_MAIN_DEFECT = 'Y'
UNION ALL
select PIECE_ID,IS_MAIN_DEFECT,
min(PIECE_DEFECT_NUM_ID) PIECE_DEFECT_NUM_ID
from tab
where nvl(IS_MAIN_DEFECT,'N') != 'Y'
group by PIECE_ID, IS_MAIN_DEFECT

How to combine CASE statement with Inner Join for Alphanumeric OrderBY

In this query, I am trying to select all distinct (alphanumeric) machine names and order them correctly (1,2,5,10,15 instead of 1,10,15,2,5). The CASE statement is proven to work when the LocalName is not joined by INNER JOIN, so I suspect this is where the problem lies.
SELECT DISTINCT MCGroup, VisionMachinePerformance.MCSAP, ZAssetRegister.LocalName
FROM [VisionMachinePerformance] INNER JOIN ZAssetRegister ON VisionMachinePerformance.MCSAP=ZAssetRegister.SAP_Number
ORDER BY
CASE WHEN PATINDEX('%[0-9]%',LocalName) > 1 THEN
LEFT(LocalName,PATINDEX('%[0-9]%',LocalName)-1)
ELSE LocalName END ,
CASE WHEN PATINDEX('%[0-9]%',LocalName) > 1 THEN
CAST(SUBSTRING(LocalName,PATINDEX('%[0-9]%',LocalName),LEN(LocalName)) as float)
ELSE NULL END
The error that is reported is "SQL Error (145): ORDER BY items must appear in the select list if SELECT DISTINCT is specified".
I have tried changing all references in the CASE statement to ZAssetRegister.LocalName and VisionMachinePerformance.LocalName without success.
Removing all of the CASE statement and ordering by LocalName does work, but with the wrong order as mentioned above (1,10,15,2,5).
Could anybody suggest how to make this work?
TIA!
You can separate both parts using a subquery:
SELECT * FROM (
SELECT DISTINCT MCGroup, VisionMachinePerformance.MCSAP, ZAssetRegister.LocalName
FROM [VisionMachinePerformance]
INNER JOIN ZAssetRegister ON VisionMachinePerformance.MCSAP=ZAssetRegister.SAP_Number
) DISTINCT_DATA
ORDER BY
CASE WHEN PATINDEX('%[0-9]%',LocalName) > 1
THEN LEFT(LocalName,PATINDEX('%[0-9]%',LocalName)-1)
ELSE LocalName END,
CASE WHEN PATINDEX('%[0-9]%',LocalName) > 1
THEN CAST(SUBSTRING(LocalName,PATINDEX('%[0-9]%',LocalName),LEN(LocalName)) as float)
ELSE NULL END

Using Count with Case Statement (T-SQL)

The below query produces the following error message: "Cannot perform an aggregate function on an expression containing an aggregate or a subquery."
I'm trying to get a count of elements (loans) from a table based on certain criteria that I've put into a case statement. I'm using a case statement instead of simply inserting the criteria into the WHERE clause because I'm pulling multiple metrics with this single query and these criteria only apply to this specific metric and not the others. How can I fix it?
SELECT COUNT(
CASE
WHEN (SELECT CONVERT(DATE, MAX(Dates)) FROM (VALUES (S.SchedClosingDate), (S.SchedClosingDate)) AS SchedDates (Dates)) BETWEEN '05/01/18' AND '05/31/18' THEN FD.FileName
END
) AS [Scheduled to Close]
FROM FileData AS FD
JOIN Status AS S ON FD.FileDataID = S.FileDataID
Note: I've removed the other metrics from the query for readability.
Move the calculation to the FROM clause using APPLY:
SELECT SUM(CASE WHEN v.dte >= '2018-05-01' AND v.dte < '2018-06-01' AND
FD.FileName IS NOT NULL
THEN 1 ELSE 0 END
END) AS [Scheduled to Close]
FROM FileData FD JOIN
Status S
ON FD.FileDataID = S.FileDataID CROSS APPLY
(SELECT MAX(dte)
FROM (VALUES (S.SchedClosingDate), (S.SchedClosingDate)) SchedDates(dte)
) as v(dte);

Using SQL SUM with Case statement containing inner SELECT

I have two tables, an Orders table which contains a list of a users orders and a OrderShippingCosts table which contains a price for shipping each item based on the OrderTypeID in the Orders table.
I am running a query like below to calculate the total shipping costs:
SELECT
SUM(CASE
WHEN OR.OrderTypeID = 1
THEN (SELECT CostOfShippingSmallParcel
FROM OrderShippingCosts)
ELSE (SELECT CostOfShippingBigParcel
FROM OrderShippingCosts)
END) AS TotalShippingCost
FROM
Orders AS OR
But I'm getting the following error:
Cannot perform an aggregate function on an expression containing an aggregate or a subquery
Does anyone know what is wrong with my query?
Function SUM takes an expression on input, which evaluates into single data value, not a dataset. Expression definition from MSDN:
Is a combination of symbols and operators that the SQL Server Database Engine evaluates to obtain a single data value.
You trying to pass to SUM function a dataset (which is result of subquery), not a single data value. This is simplification of what you trying to query:
SELECT SUM(SELECT Number FROM SomeTable)
It is not valid. The valid query would be:
SELECT SUM(Value) FROM SomeTable
In your particular case looks like you missing JOIN. Your original logic will result in summary of entire OrderShippingCosts table for each row of Orders table. I think, it should be something like this:
SELECT
SUM
(
CASE
WHEN ord.OrderTypeID = 1 THEN ship.CostOfShippingSmallParcel
ELSE ship.CostOfShippingBigParcel
END
) TotalShippingCost
FROM Orders AS ord
JOIN OrderShippingCosts ship ON /* your search condition, e.g.: ord.OrderID = ship.OrderID */
By the way, it is not a good idea to use reserved symbols as aliases, names and so on. In your query you use OR as alias for Orders table. Symbol OR is reserved for logical or operation. If you really need to use reserved symbol, wrap it into [ and ] square braces. Look here and here for more details.
The error message is clear, you can avoid it with a join:
SELECT
SUM(CASE WHEN [OR].OrderTypeID = 1
THEN CostOfShippingSmallParcel
ELSE CostOfShippingBigParcel END) AS TotalShippingCost
FROM Orders [OR]
CROSS JOIN OrderShippingCosts
You can try like this...
SELECT
CASE WHEN OR.OrderTypeID = 1
THEN (SELECT SUM(CostOfShippingSmallParcel) FROM OrderShippingCosts)
ELSE (SELECT SUM(CostOfShippingBigParcel) FROM OrderShippingCosts) END AS TotalShippingCost
FROM Orders AS OR
Let me know
select sum (or.TotalShippingCost)
FROM
SELECT
(CASE WHEN OR.OrderTypeID = 1
THEN (SELECT CostOfShippingSmallParcel FROM OrderShippingCosts)
ELSE (SELECT CostOfShippingBigParcel FROM OrderShippingCosts) END) AS TotalShippingCost
FROM Orders AS OR
Try this
SELECT
ISNULL
(
SUM
(
CASE
WHEN O.OrderTypeID = 1 THEN C.CostOfShippingSmallParcel
ELSE C.CostOfShippingBigParcel END
), 0
) AS TotalShippingCost
FROM
Orders AS O LEFT JOIN
OrderShippingCosts C ON O.Id = C.OrderId -- Your releation id

T-SQL Group By; Contains or IfAny

SQL2005 and/or SQL2008
Is there any kind of built-in aggregate, within T-SQL, for Contains or IfAny or whatever? Something where any in the group equals a value?
Similar to Max(xyz)=value except not limited to max.
Select custID, case when Min(ProductGroup)= "A" then 'Have Ordered Group A' else 'Haven't Ordered Group A' end hasOrdered
from orders
inner join products on ordPoductId = productID
group by custID
This works for a single value comparison, if it is min/max, but instead I want something like:
Select custID, case when contains(ProductGroup, "G") then 'Have Ordered Group G' else 'Haven't Ordered Group G' end hasOrdered
from orders
inner join products on ordPoductId = productID
group by custID
I could use Min(ProductGroup)="A" if the value I'm concerned about is a min/max or change the from-clause to (case when 'G' then 0 else 1 end) to create a fake maximum. Currently I am only concerned with a single value, but I would like something more intuitive and flexible if possible.
Any ideas?
Your examples at the end are close to what I'd normally do. Something like:
CASE MAX(CASE WHEN ProductGroup = 'G' THEN 1 ELSE 0 END)
WHEN 1 THEN 'Have Ordered'
ELSE 'Haven''t ordered'
END
Where the inner CASE expression will obviously be evaluated against each row, whereas the outer CASE expression determines whether the inner expression ever succeeded.