I have an encountered problem in my query in Oracle SQL. I don't know how to get the sum of this query:
select call_type, channel
,count (case when status="no answer" then 1 end else 0) as cnt_no_answer
,count (case when status="answered" then 1 end else 0) as cnt_answer
from app_account.cc_call;
Please Help me. Thanks!
To get the answered and not answered records sum instead of count. To get the number of all records that are either answered or not answered use count(status). To get the count of all records, i.e. also records with status null use count(*). Strings need single quotes, not double quotes. The case statement needs END.
EDITED (there were too many END used):
select call_type, channel
, sum(case when status='no answer' then 1 else 0 end) as cnt_no_answer
, sum(case when status='answered' then 1 else 0 end) as cnt_answer
, count(status) as cnt_all_stated
, count(*) as cnt_all_records
from app_account.cc_call
group by call_type, channel;
try this again, i edit it:
SELECT call_type, channel,
sum (CASE WHEN status='no answer' THEN 1 ELSE 0 END) AS cnt_no_answer,
sum (CASE WHEN status='answered' THEN 1 ELSE 0 END) AS cnt_answer
FROM app_account.cc_call
GROUP BY call_type, channel;
Check this out:
SELECT CALL_TYPE,
CHANNEL,
COUNT (CASE WHEN UPPER(STATUS) = UPPER('no answer') THEN 1 ELSE NULL END)
AS CNT_NO_ANSWER,
COUNT (CASE WHEN UPPER(STATUS) = UPPER('answered') THEN 1 ELSE NULL END)
AS CNT_ANSWER
FROM APP_ACCOUNT.CC_CALL
GROUP BY CALL_TYPE, CHANNEL;
select call_type, channel
, sum(case when status='no answer' then 1 else 0 end) as cnt_no_answer
, sum(case when status='answered' then 1 else 0 end) as cnt_answer
, count(status) as cnt_all_stated
, count(*) as cnt_all_records
from app_account.cc_call
group by call_type, channel;
why usind end twice
SELECT Call_type, Channel
,COUNT(CASE WHEN status="no answer" then 1 else 0 end ) as Cnt_no_answer
,COUNT(CASE WHEN status="answered" then 1 else 0 end ) as Cnt_answer
from App_account.cc_call
GROUP BY call_type,channel;
I think there is some error in table deceleration. Pleas give table structure. And you want SUM and YOU are COUNTING there I dnt think it is error but lack of overview. Please give some more details.
Related
I have a case statement where it returns 2 regardless of the value from v_trcdate. The v_trcdate is supposed to be from a table value. Then, I've tried manually inputting the value and still the same result.
Declare v_trcdate DATE := '10-JAN-23';
(CASE WHEN v_accesstype = 'Y' THEN
(CASE WHEN TRUNC(sysdate) < v_trcdate THEN 1 WHEN TRUNC(sysdate) > v_trcdate THEN 2 END)
ELSE 3 END) AS PRICE
Oracle SQL Developer noob here. I am trying to create a quarterly sales analysis based on warehouse names and output the Quarterly sales as Q1, Q2 etc. Using Drill down query concepts.
I am unsure if what I am doing is in any way related to a Drill Down concept but this is one of my many attempts. I am hoping for a way to remove the null value outputs to be left with proper data.
It is in the hopes that with the removal of the null data, all the outputs related to specific warehouse names will also all output to 1 line. Leaving me with Warehouse_Name(1), Q1 Data, Q2 Data, etc
I am currently using two tables for this query which are
Warehouse: Warehouse_id, warehouse_name and quantity_sold
Time_Period: Date_id, Full_date, Days, Month_short and year.
My Code is as follows:
SELECT TO_CHAR(Full_date, 'Q') AS MY_QTR,
Sum(Quantity_sold) AS HOW_MANY_SOLD_PER_QTR
FROM warehouse, Time_Period
GROUP BY TO_CHAR(Full_date, 'Q')
ORDER BY 1;
Select warehouse_Name,
case
when TO_CHAR(Full_date, 'Q') = 1
then Sum(Quantity_sold)
End as Q1_2019,
case
when TO_CHAR(Full_date, 'Q') = 2
then Sum(Quantity_sold)
End as Q2_2019,
case
when TO_CHAR(Full_date, 'Q') = 3
then Sum(Quantity_sold)
End as Q3_2019,
case
when TO_CHAR(Full_date, 'Q') = 4
then Sum(Quantity_sold)
End as Q4_2019
FROM warehouse w1, Time_Period t1
where Q1_2019 IS NOT NULL
GROUP BY warehouse_Name,TO_CHAR(Full_date, 'Q')
ORDER BY 1;
Which provides me with an output of
Waarehouse_Name Q1 Q2 Q3 Q4
--------------- ---- ---- ---- ----
Henderson 990 Null Null Null
Henderson Null 1001 Null Null
Henderson Null Null 1012 Null
Henderson Null Null Null 1012
Edit :
As mentioned by #mathguy
“He was using conditional SUM (conditional by quarter) and also was grouping by quarter in the GROUP BY clause. If you remove the quarter from GROUP BY (which you eventually did), there would be no more null in the output already. The main point of the answer has noting to do with adding else 0 to the case expressions. “
So using else here is unnecessary
Removing quarter from GROUP BY does the thing.
Select warehouse_Name, SUM( case when TO_CHAR(Full_date, 'Q') = 1 then Quantity_sold else 0 End) as Q1_2019,
SUM( case when TO_CHAR(Full_date, 'Q') = 2 then Quantity_sold else 0 End) as Q2_2019,
SUM( case when TO_CHAR(Full_date, 'Q') = 3 then Quantity_sold else 0 End) as Q3_2019,
SUM( case when TO_CHAR(Full_date, 'Q') = 4 then Quantity_sold else 0 End) as Q4_2019
FROM warehouse w1
where Full_date IS NOT NULL
GROUP BY warehouse_Name
ORDER BY 1;
Hi I have below Source table "status table"
date status name
2017-06-22 true 1.tar
2017-06-22 true 2.tar
2017-06-22 false 3.tar
2017-06-22 true 4.tar
2017-06-22 false 5.tar
2017-06-21 false 6.tar
2017-06-21 false 6.tar
2017-06-21 false 6.tar
2017-06-21 true 6.tar
I have below destination table columns with expected data
True False Total Date
3 2 5 2017-06-22
1 3 4 2017-06-21
I wrote below query to load the data from source table to destination table, but it says
Expression not in GROUP BY key
SET hive.exec.dynamic.partition.mode=nonstrict;
SET hive.auto.convert.join=true;
INSERT OVERWRITE TABLE destination PARTITION(date_time)
SELECT
count(status=true) AS success,
count(status=false) AS fail,
success + fail
FROM
status;
Please help me with the missing link. Thanks in advance.
Hive does not support aliases references in the SELECT clause (success + fail)
COUNT counts everything that is not NULL. FALSE is not NULL.
Date is a reserved word. use `Date` or even better, find another name.
You haven't grouped by Date.
When using dynamic partitioning, the partition column(s) should be selected, last.
select count (case when status = true then 1 end) as success
,count (case when status = false then 1 end) as fail
,count (status) as total
,`date`
from status
group by `date`
thanks for answering, however I found out my mistake, I was using count function instead of sum. Here is the code below.
SELECT
sum(case when status ='true' then 1 else 0 end),
sum(case when status ='false' then 1 else 0 end),
sum(case when status ='true' then 1 else 0 end) + sum(case when status='false' then 1 else 0 end)
from status where date='2017-06-21';
I have a huge Hive table consisting of ten product fields, date fields for the purchases, and an identifier. The product fields are named like prod1, prod2, ... , prod10 and refer to the last ten products purchased. For most IDs, we don't have purchase history all the way back to ten products.
I'd like to construct a distribution of population rates for each of the prod<X> fields, to show the breakdown of purchase history across the entire dataset.
Currently, I'm running a bash script that runs ten consecutive queries against the table like:
hive -e "select count(1) from db.tbl where prod<X> != '';"
... and saving the output to a file. This seems clunky and inefficient. Is there a better way to specify Hive counts on a range of fields with a range of field conditions? I've tried to come up with a strategy using groupby or even mapping a range of fields, but can't quite wrap my head around specifying the != '' condition for each field.
Thanks in advance for any direction.
select id,
sum(case when prod1='' then 0 else 1 end),
sum(case when prod2='' then 0 else 1 end),
sum(case when prod3='' then 0 else 1 end),
sum(case when prod4='' then 0 else 1 end),
sum(case when prod5='' then 0 else 1 end),
sum(case when prod6='' then 0 else 1 end),
sum(case when prod7='' then 0 else 1 end),
sum(case when prod8='' then 0 else 1 end),
sum(case when prod9='' then 0 else 1 end),
sum(case when prod10='' then 0 else 1 end)
from table group by id;
I having a code like this
(case app.test when 2 then '' end) as tested
I am trying to get count of this app.state so written like this after referring to this document http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single/#queryhql-aggregation
count (case app.test when 2 then 'tested' end) as tested
After this I getting error like
unexpected token: case
Any idea
But the same statement is working mysql
COUNT(CASE WHEN app.test= 2 THEN "tested" END) 'tested',
Select count(app.state) as Confirmed from App app where app.state=2
I found the solution by
sum(case when app.test=2 then 1 else 0 end) as tested