HQL count statement - hql

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

Related

query group by with having and get results in two different columns

I have a table like these:
Name Source ended_status date Environment
House DC 1 2019/10/03 Pro
Cat DC2 1 2019/10/05 Pro
Pen DC 1 2019/10/03 Pro
Pen DC 0 2019/11/07 Pre
I would like to get:
Source Environment Ended_Status_with_1 Ended_Status_with_2
DC Pro 2 0
DC Pre 1 0
DC2 Pro 1 0
So, they must be grouped by Source,Environment and I must calculate a summation of all that they have Ended_Status with 1 and all with ended status 2 and put in the same line.
How could I do that?
I can make query grouped by every ended_status but I can't put the two summations of ended status in same line.
Many thanks and sorry for my poor English!
also, try below query
select source,environment,sum(decode(ended_status,1,1,0)) ended_status_with_1,
sum(decode(ended_status,2,1,0)) ended_status_with_2 from mytable
group by source,environment
You can use conditional aggregation:
select
source,
environment,
sum(case when ended_status = 1 then 1 else 0 end) ended_status_with_1,
sum(case when ended_status = 2 then 1 else 0 end) ended_status_with_2
from mytable
group by
source,
environment

duplicates with connect by in oracle

select RP.COUNTRYID,RP.PRDCODE,
RP.REPID,
RP.CHANNELID,
RP.CUSTOMERID,
RP.DIVISION,
RP.WWCOGS_GAUSS,
RP.WWCOGS_SAP,
RP.WWCOGS_BusLine,
RP.CURRID ,
ADD_MONTHS(to_date('01-01-'||rp.year,'DD-MM-YYYY'),level-1) KFDATE
from
(SELECT CP.COUNTRYID,
CP.PRDCODE,
CP.REPID,
CP.CHANNELID,
CP.CUSTOMERID,
IP.DIVISION,
CP.WWCOGS_GAUSS,
CP.WWCOGS_SAP,
CP.WWCOGS_BusLine,
CP.year,
CP.CURRID from
(select distinct IBC.COUNTRYID ,
decode(ls.dmdunit,null,mwa.ProductName,ls.dmdunit) PRDCODE,
ReportingUnit REPID,
'99' CHANNELID,
IBC.COUNTRYID CUSTOMERID ,
(
CASE
WHEN MWA.COGSSourceCF LIKE '%Gauss%'
THEN MWA.COGSPriceCF * MWA.ExchRate
ELSE NULL
END) WWCOGS_GAUSS,
(
CASE
WHEN MWA.COGSSourceCF LIKE '%SAP%'
THEN MWA.COGSPriceCF * MWA.ExchRate
ELSE NULL
END) WWCOGS_SAP,
(
CASE
WHEN MWA.COGSSourceCF NOT LIKE '%Gauss%'
AND MWA.COGSSourceCF NOT LIKE '%SAP%'
THEN MWA.COGSPriceCF * MWA.ExchRate
ELSE NULL
END) WWCOGS_BusLine,
mwa.year,
IRC.CURRID
from BAM.M_WWCOGS_AREA MWA,
MICSTAG.M_IBP_REPUNIT_CURRENCY IRC,
micstag.M_IBP_BDREPORTINGCOUNTRY IBC
,MICSTAG.M_LOCALPRODUCT_STAG LS
WHERE MWA.ReportingUnit=IRC.REPID
--and mwa.productname='FR21030390085'
--and MWA.GaussCountry='BE BELGIUM'
AND IBC.COUNTRYPLANNINGGROUP =MWA.GaussCountry
and IBC.businessdivision=31
and MWA.COGSPriceCF <>0
and ls.REPORTINGUNITID(+)=mwa.ReportingUnit
and ls.IBPLOCALPRDID(+)= mwa.ProductName ) CP, micstag.M_IBP_PRODUCT IP
where CP.PRDCODE=IP.PRDID) RP
CONNECT BY level <= 12 ;
the above query is getting unwanted duplicate, if i use distinct the query is running forever.
req. duplicate the records based on year in result set of rp
consider value of year is 2019 than 12 records should came from 1-jan-2019 to 1-dec-2019.
more than one value of year are possible
The CONNECT BY LEVEL <= 12 trick works nicely with dual because that table returns one row. Things are messier when they base result set returns more than one row, because the CONNECT BY generates a product. This is why you're getting duplicates.
What you need to do is specify some additional criteria for the connection. Ideally you will have a primary key in the projection - I'm going to assume it's REPID, so if it's something different you'll need to tweak this. Anyway, you'll need something like this:
) RP
CONNECT BY level <= 12
and rp.repid = prior rp.repid
and prior sys_guid() is not null
The prior sys_guid() bit prevents ORA-01436: CONNECT BY loop in user data.

Hive query counts of fields where fields are populated

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;

How to get the sum?

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.

Dynamic SQL Query in Oracle 10g

Afternoon All,
I am new to Oracle and SQL but i have a query with the below code i have created. Im currently using Oracle 10g. Im just wondering if anyone can help me make this code dynamic instead of hard coded.
The code i have simply looks at a table that logs the users activity. I then essentially count the number of records that we have for each user / PC and display this in a pivot style table.
Its not a very difficult query as such but i could have about 30 or so PC's that i would need to enter and this hard coded method is definitly not the best way to complete this task.
I have been searching on the internet to look at a dynamic statement i could use based on the host name or user_ID but i have not managed to find anything that simply loops through my data and then generates this piviot style view.
I have been looking at 'Cursors' but im think im way off by a long shot.
Ant help is much appriechiated in advance.
Regards
Betty
SELECT USER_ID,
SUM(CASE WHEN host LIKE 'PC1' THEN 1 ELSE 0 END) AS PC1,
SUM(CASE WHEN host LIKE 'PC2' THEN 1 ELSE 0 END) AS PC2,
SUM(CASE WHEN host LIKE 'PC3' THEN 1 ELSE 0 END) AS PC3,
SUM(CASE WHEN host IS NOT NULL THEN 1 ELSE 0 END) AS grand_total
FROM table_Name
GROUP BY USER_ID
When you ask an oracle question it's important to note the release. in your case - if you have 11g you can look into the pivot function.
In 10G (and in 11g) you can try something like that
create or replace function get_pivot()
return sys_refcursor
as
stmt varchar2(32000);
c sys_Refcursor;
cursor c_values as
select distinct host from table_name;
begin
stmt := 'select user_id , ';
for x in c_values loop
stmt := stmt || ' sum(case when host = '''||x.host||''' then 1 else 0 end) as ' ||host|| ',';
end loop;
stmt := stmt || ' count(host) as grand_total from table_name group by user_id';
open c for stmt;
return(c);
end get_pivot;
whether you are using the pivot or dynamic sql you have to query the distinct values.
Havent tested it - i don't have my oracle at the moment.

Resources