Select Case when Grouping(Column1) not working in Snowflake - sql

I'm trying to do the following query in Snowflake:
Select case when grouping(column_1) = 'Total'
else column_1 end as column_1
,sum(column_2)
From Table
group by rollup(column_1)
But I keep getting the error: "SQL compilation error: GROUPING function cannot appear outside of SELECT, HAVING, and ORDER BY clauses."
Any suggestions on how to fix this or work arounds?

Try writing the query like this:
select (case when grouping(t.column_1) = 1
then 'Total' else column_1
end) as column_1,
sum(column_2)
From Table t
group by rollup(t.column_1);
That is, qualify the reference in the GROUP BY so it is not resolved to the column alias. Also, note that this fixes the case expression.

Related

Should I add another Group By Statement when I receive the error "column cannot be resolved"?

My query was running fine, but after a adding a case statement I received an error on some of the column.
QUERY with the case statement that I am receiving an error on:
select
date_,
afs,
sum(price) as Customer_price,
sum(cost) as store_cost,
pos_vd,
product
CASE
WHEN f.afs='C' and f.product in ('Blue','Pink','Yellow')
THEN 'Special'
ELSE 'PRODUCT GROUP NOT MAPPED' END as ProductGroup
from pie.crust f
where
date_ >= 201801 and
pos_vd = 'Stop'
Group By
date_,
afs,
pos_vd,
product,
CASE
WHEN f.afs='C' and f.product in ('Blue','Pink','Yellow')
THEN 'Special'
ELSE 'PRODUCT GROUP NOT MAPPED' END
Below are the fields I am receiving an error on. When I remove the fields the query works with the case statement, but I need the field in my data pull. I tried adding the case statement under Group By and it didn't work. I keep receiving the error like "Column 'price' cannot be received":
sum(price),
sum(cost)
You can not give alias in the group by. Remove the aliases from group by and also I don't think aggregate functions are required in the group by.
You should remove the following aggregate functions.
sum(cnt) as CNT,
sum(amt) as AMT,
sum(price) as Customer_price,
sum(cost) as store_cost,
And you can use case..when in group by without alias.
CASE
WHEN f.afs='C' and f.product in ('Blue','Pink','Yellow')
THEN 'Special'
ELSE 'PRODUCT GROUP NOT MAPPED' END -- no alias
Hello I solved this problem.
The table was originally created in HUE, so when I was trying to add the case statement in Presto and pull the aggregate function it wouldn't work. I went back to the original table in Hive and added the case statement there. Now when I pull the data from Presto everything is working.

SSRS cannot parse SQL query

I am trying to define an SQL query in my SSRS report, but I am getting some syntax errors:
Error in SELECT clause: expression near 'WHEN'.
Missing FROM clause.
Error in SELECT clause: expression near ','.
Unable to parse query text.
The query is not that complicated, but rather a bit convoluted, but I'll try to convey at least the structure of it here:
select
(CASE WHEN columnA1 is null THEN columnA2 ELSE columnA1 END) as columnA,
(CASE WHEN columnB1 is null THEN columnB2 ELSE columnB1 END) as "custom_name_for_columnB"
from
(
(select a.columnA1, ...
from myTable a, ...
// join conditions
)
union
select * from
(select a.columnA1, ...
from myTable a, ...
// join conditions
order by someColumn) source
)
);
I don't think it really matters what the query does since I ran it in my DMBS successfully, so I'm pretty sure it's correct SQL syntax (I'm working on Oracle DB). I think what I'm not seeing is some syntax specific to SSRS. I'm completely new to it, so I don't know whether it supports the entire SQL syntax like CASE WHEN, unions etc.
As it complains about CASE (as if it doesn't recognize the syntax), try some more options:
COALESCE:
select coalesce(columnA1, columnA2) columnA,
coalesce(columnB1, columnB2) columnB
from ...
NVL:
select nvl(columnA1, columnA2) columnA,
nvl(columnB1, columnB2) columnB
from ...
DECODE:
select decode(columnA1, null, columnA2, columnA1) columnA,
decode(columnB1, null, columnB2, columnB1) columnB
from ...
Correct my query if I made too many changes, but I think there are a couple of things wrong:
SELECT (CASE WHEN t.columnA1 IS NULL THEN t.columnA2 ELSE t.columnA1 END) as columnA,
(CASE WHEN t.columnB1 IS NULL THEN t.columnB2 ELSE t.columnB1 END) as [custom_name_for_columnB]
FROM(
SELECT a.columnA1, ...
FROM myTable a, ...
JOIN conditions
UNION
SELECT * FROM
(
SELECT a.columnA1, ...
FROM m myTable a, ...
JOIN conditions
--ORDER BY someColumn --Can't have ORDER BY in subquery without TOP(n) / FOR XML.
) source
)t; --Needs an alias

Oracle SQL Developer Case Expression nested query causing a "ORA-00937: not a single-group group function" error

I'm trying to build a fairly straight-forward query that can be run by a monitoring platform. For purposes of this question, I need my query to return a 1 if alert criteria have been met and 0 if all is well. Here's a snippet of the query I have which WORKS properly:
select case when count(*)<1700
THEN '1'
ELSE '0'
END CASE
FROM SomeTable
WHERE date_value= '11-DEC-2015';
However, rather than hardcoding the 1700 value in my comparison, I'd like to do a nested query to pull the value form another table in the same database. When I re-write the query like so...:
SELECT case when count(*)<(select count(*) from Another_table)
THEN '1'
ELSE '0'
END CASE
FROM SomeTable
WHERE date_value= '11-DEC-2015';
...It returns the following at the start of my nested query:
ORA-00937: not a single-group group function
00937. 00000 - "not a single-group group function"
I know there is probably a better way of doing this type of comparison or perhaps a something simple I'm overlooking but I'm still pretty new to Oracle SQL and would greatly appreciate any suggestions on how to do this same type of nested query case expression. If I'm replacing an integer value with a nested query that also returns a single integer result, why do I get the above error?
Thanks in advance for any suggestions!
You can initially get the counts from both the tables and then use those values for comparison.
select case when s_count < a_count then '1' else '0' end some_col
from (select
(select count(*) from another_table) a_count,
(select count(*) from sometable where date_value= '11-DEC-2015') s_count
from dual
) x
Another option would be to use the count window function.
select distinct
case when count(*) over() < (select count(*) from another_table) then '1' else '0' end
from sometable
where date_value= '11-DEC-2015'

CASE SQL Oracle Query - What is wrong?

I have following query to select the view to be used in the query, however I get the error:
FROM keyword not found where expected
select *, (CASE WHEN 'employee' = 'employee' THEN employee ELSE vw END) FROM type1
select type1.*,
(CASE WHEN 'employee' = 'employee'
THEN employee
ELSE vw
END)
FROM type1
I always prefix with the tablename/table alias to the * and it works!!
We just need to specify, fetch all the column from this table, when we specify combination of wildcard and other select expressions!
You cannot use * and individual columns together in select statement.
SELECT (CASE WHEN 'employee' = 'employee' THEN 'employee' ELSE 'vw' END)
FROM dual
You cant do a *, in Oracle without an alias/TableName(.) in front of it. List out the columns you want to see in the result set or add a table alias/tablename.
You cannot set the name of the view the select statement shall retrieve data from in the column list part of the statement.

How Do I Use Case Statement Column In Group By

As stated by the question, I'm trying to formulate a query that has a case statement in the column results, and then I want to include that column in the query's group by statement. To give a concrete example, here is all little of what my query looks like:
SELECT SOME_TABLE_ALIAS.COLUMN1, OTHER_TABLE_ALIAS.COLUMN2,
CASE
WHEN SOME_TABLE_ALIAS.COLUMN3 IS NOT NULL THEN 'A'
ELSE 'B'
END AS CASE_COLUMN
FROM SOME_TABLE SOME_TABLE_ALIAS
... (other table joins and where clauses)
GROUP BY SOME_TABLE_ALIAS.COLUMN1, OTHER_TABLE_ALIAS.COLUMN2, CASE_COLUMN
Before coming here, I checked out a few websites, including this one, to try solve my problem. I've tried adding another alias after the CASE keyword like is shown in the linked web page but have had no luck. The error message I continue to receive is the following:
[Error] Script lines: 127-151 ----------------------
CASE_COLUMN IS NOT VALID IN THE CONTEXT WHERE IT IS USED. SQLCODE=-206, SQLSTATE=42703, DRIVER=3.53.71
Has anyone else run into the issues I'm facing and been able to use a GROUP BY on the results of a CASE statement? Any help would be appreciated. Oh, and the DB2 version is a z/OS instance, version 10 (DSN10015)
The alias isn't available to use in the GROUP BY because when GROUP BY happens the alias isn't defined yet:
Here's the order:
1.FROM
2.WHERE
3.GROUP BY
4.HAVING
5.SELECT
6.ORDER BY
You can work around that with:
SELECT column1,column2,case_column
FROM (
SELECT SOME_TABLE_ALIAS.COLUMN1, OTHER_TABLE_ALIAS.COLUMN2,
CASE
WHEN SOME_TABLE_ALIAS.COLUMN3 IS NOT NULL THEN 'A'
ELSE 'B'
END AS CASE_COLUMN
FROM SOME_TABLE SOME_TABLE_ALIAS
... (other table joins and where clauses)
) a
GROUP BY COLUMN1, COLUMN2, CASE_COLUMN
Or just use the case you use in SELECT in GROUP BY
You can either use the case as is in the group by, like this:
SELECT SOME_TABLE_ALIAS.COLUMN1, OTHER_TABLE_ALIAS.COLUMN2,
CASE
WHEN SOME_TABLE_ALIAS.COLUMN3 IS NOT NULL THEN 'A'
ELSE 'B'
END AS CASE_COLUMN
FROM SOME_TABLE SOME_TABLE_ALIAS
... (other table joins and where clauses)
GROUP BY SOME_TABLE_ALIAS.COLUMN1, OTHER_TABLE_ALIAS.COLUMN2,
CASE
WHEN SOME_TABLE_ALIAS.COLUMN3 IS NOT NULL THEN 'A'
ELSE 'B'
END
or use a sub-query like this:
select COLUMN1, COLUMN2, CASE_COLUMN
from (
SELECT SOME_TABLE_ALIAS.COLUMN1, OTHER_TABLE_ALIAS.COLUMN2,
CASE
WHEN SOME_TABLE_ALIAS.COLUMN3 IS NOT NULL THEN 'A'
ELSE 'B'
END AS CASE_COLUMN
FROM SOME_TABLE SOME_TABLE_ALIAS
... (other table joins and where clauses)
) a
GROUP BY COLUMN1, COLUMN2, CASE_COLUMN