Hive query to find conversion ratio - hive

I am trying this query in Hive and it's not working.
select
(
(
select
count(*)
from
click_streaming
where
page_id= 'e7bc5fb2-1231-11eb-adc1-0242ac120002'
and is_page_view = 'Yes'
) / (
select
count(*)
from
click_streaming
where
button_id= 'fcba68aa-1231-11eb-adc1-0242ac120002'
and is_button_click= 'Yes'
)
) as conversion_ratio;
Error I am getting: cannot recognize input near 'select' 'count' '(' in expression specification
I am basically trying to get conversion rate of customers who view the page and click the button to book a cab.

This is not how the syntax can be. Just join them both or use a case when to do your job.
select
sum(case when page_id= 'e7bc5fb2-1231-11eb-adc1-0242ac120002' and is_page_view = 'Yes' then 1 else 0 end) /
sum(case when button_id= 'fcba68aa-1231-11eb-adc1-0242ac120002' and is_button_click= 'Yes' then 1 else 0 end) conv_ratio
FROM
click_streaming
or you can reuse your SQLs but you got to join them
select c1/c2
from (
select
count(*) c1
from
click_streaming
where
page_id= 'e7bc5fb2-1231-11eb-adc1-0242ac120002'
and is_page_view = 'Yes') rs
join (select
count(*) c2
from
click_streaming
where
button_id= 'fcba68aa-1231-11eb-adc1-0242ac120002'
and is_button_click= 'Yes')rs2

Related

SQL check link between two records (CASE, WHEN, THEN)

I Have in table some records:
ID Services
2 A
2 C
2 C1
2 D2
I`m trying make query that will be select a link between services.
For example: If for ID 2 exists Services C then check if exist Service C1, result Yes or No.
SELECT a. ID, a.service,
CASE
WHEN (a.service ='C') = (a.service = 'C1') THEN 'Yes'
ELSE 'No'
END
FROM t1 a
Try this query:
SELECT *
FROM yourTable t1
WHERE NOT EXISTS (SELECT 1 FROM yourTable t2
WHERE (t2.Services LIKE t1.Services + '%' OR
t1.Services LIKE t2.Services + '%') AND
t1.ID = t2.ID AND t1.Services <> t2.Services);
This returns A and D2 only.
Demo
Hmm... what about this? But I now have problem with checking relationship for each ID independently...
SELECT a. ID, a.service,
CASE
WHEN a.service IN ('C','C1') THEN 'Yes'
ELSE 'No'
END
FROM t1 a
If I understand correctly, you can use aggregation:
SELECT ID,
(CASE WHEN SUM(CASE WHEN service = 'C' THEN 1 ELSE 0 END) > 0 AND
SUM(CASE WHEN service = 'C1' THEN 1 ELSE 0 END) > 0
THEN 'Yes' ELSE 'No'
END) as c_c1_flag
FROM t1
GROUP BY ID;
The SUM(CASE . . . ) counts the number of rows that match the conditions. The > 0 simply says that at least one row exists.

SQL Select a specific value in the group

I have this following table
Dept---------- Sub_Dept---- Dept Type
Sales.............Advertising........A
Sales.............Marketing......... B
Sales.............Analytics.......... C
Operations.....IT..................... C
Operations.....Settlement........C
And the result should be if a department got a department type as A then change all record of that department to A, else keep it same
Dept---------- Sub_Dept---- Dept Type
Sales.............Advertising........A
Sales.............Marketing......... A
Sales.............Analytics.......... A
Operations.....IT..................... C
Operations.....Settlement........C
Anybody can give a suggestion on this? I thought of using the GROUP BY but have to output the Sub Department as well
Thanks a lot
I would do:
update t
set depttype = 'a'
where exists (select 1 from t t2 where t2.dept = t.dept and t2.dept = 'a') and
t.dept <> 'a';
If you just want a select, then do:
select t.*,
(case when sum(case when depttype = 'a' then 1 else 0 end) over (partition by dept) > 1
then 'a'
else depttype
end) as new_depttype
from t;
Use below query
select a11.dept, a12.Sub_Dept, (case when a12.min_dep_type='A' then 'A' else a11.dep_type) as dep_type
from tab a11
JOIN (select dept, min(dep_type) min_dep_type from tab group by dept) a12
on a11.dept = a12.dept
Try this:
update table
set depttype= case when dept in (select dept from table where depttype='a') then 'a' else depttype end
This should work:
select a.dept, a.sub_dept,
case when b.dept is not null then 'A' else dept_type end as dept_type
from aTable a
left join(
select distinct Dept from aTable where dept_type = 'A'
)
b on b.dept = a.dept
You could use analytic functions to check whether exists the specific value in the group.
Try below query:
SELECT t.Dept,
t.Sub_Dept,
NVL(MIN(CASE WHEN t.Dept_Type = 'A'
THEN Dept_Type END) OVER (PARTITION BY t.Dept), t.Dept_Type) AS Dept_Type
FROM table_1 t
Using the analytic function MIN(), you can search for the value of 'A' (if it does exist inside the group). MIN works for non-null values only, so if you don't have any 'A' in the group, the result will be NULL.
At this point, you can use NVL to choose whether to print the value found in the group or the actual dept_type of the row.

I want to group and remove values from a query with union

Select
pnf_notas_processadas.pnf_notas_dest_cnpj_cpf,
Count(Distinct pnf_notas_processadas.pnf_notas_nnf) As destinadas,
Sum(0) As emitidas
From
pnf_notas_processadas
Group By
pnf_notas_processadas.pnf_notas_dest_cnpj_cpf
Having
pnf_notas_processadas.pnf_notas_dest_cnpj_cpf In ('03846642000102',
'03846642000285', '03846642000447', '03846642000528')
Union
Select
pnf_notas_processadas.pnf_notas_emit_cnpj_cpf,
Sum(0) As destinadas,
Count(Distinct pnf_notas_processadas.pnf_notas_nnf)
From
pnf_notas_processadas
Group By
pnf_notas_processadas.pnf_notas_emit_cnpj_cpf
Having
pnf_notas_processadas.pnf_notas_emit_cnpj_cpf In ('03846642000102',
'03846642000285', '03846642000447', '03846642000528')
result:
My expectation for the query
One method is to do the aggregation in one step. However, you need somehow to aggregate along two fields.
Here is a method. Essentially, the rows are "doubled" and then the aggregation pulls out the right values:
select (case when which = 1 then np.pnf_notas_dest_cnpj_cpf else pnf_notas_emit_cnpj_cpf end) as cnpj_cpf,
count(distinct case when which = 1 then np.pnf_notas_nnf end) as destinadas,
count(distinct case when which = 2 then np.pnf_notas_nnf end) as emitidas
from pnf_notas_processadas np cross join
(values (1), (2)) as v(which)
where np.pnf_notas_dest_cnpj_cpf In ('03846642000102', '03846642000285', '03846642000447', '03846642000528')
Group By cnpj_cpf;
I adjusted the query
select (case when which = 1 then pnf_notas_processadas.pnf_notas_dest_cnpj_cpf else pnf_notas_processadas.pnf_notas_emit_cnpj_cpf end) as cnpj_cpf,
count(distinct case when which = 1 then pnf_notas_processadas.pnf_notas_nnf end) as destinadas,
count(distinct case when which = 2 then pnf_notas_processadas.pnf_notas_nnf end) as emitidas
from pnf_notas_processadas cross join
(values (1), (2)) as v(wich)
where pnf_notas_processadas.pnf_notas_dest_cnpj_cpf In ('03846642000102', '03846642000285', '03846642000447', '03846642000528')
Group By cnpj_cpf;
but returned this error
select sentence invalidates unexpected element "2" on line 5 , position 16

How to use case statement inside where clause of sql 2000

I have a query that contains a WHERE clause with a CASE statement in it (See code below), somehow it doesn't seem to work.
select * FROM
details
where orgcode in
(case when orgtype='P' then
(SELECT distinct [PCode]
FROM [GPOS_Extract].[dbo].[GP8288List])
else
0 end )
How about
select * FROM details
where (orgtype <> 'P' AND orgcode = 0)
or orgcode in
(
SELECT distinct [PCode]
FROM [GPOS_Extract].[dbo].[GP8288List]
)
Or try this:
SELECT * FROM details
WHERE details.orgcode IN
( SELECT DISTINCT
(CASE WHEN details.orgtype='P'
THEN [GPOS_Extract].[dbo].[GP8288List].PCode
ELSE 0 END)
FROM [GPOS_Extract].[dbo].[GP8288List] )
I think the following is the logic that is equivalent to your attempt:
select *
FROM details
where (orgtype = 'P' and
orgcode in (SELECT distinct [PCode]
FROM [GPOS_Extract].[dbo].[GP8288List]
)
) or
((orgtype <> 'P' or orgtype is NULL) and orgcode = 0);
what about this,
select a.*,case when orgtype='P' then PCode else '0' end FROM
details a
left join [GPOS_Extract].[dbo].[GP8288List] b on a.orgcode=b.PCode
case returns a single value. You are trying to use it as though it returns a result set. What you want is:
select * FROM details d
where (orgtype = 'p'
And exists (Select *
From GPOS_Extract.dbo.GP8288List
Where PCode = d.orgcode))
or (orgtype <> 'p' And orgcode= 0)

How to do a SUM() inside a case statement in SQL server

I want to add some calculation inside my case statement to dynamically create the contents of a new column but I get the error:
Column 'Test1.qrank' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
This is the code I'm working on
case
when test1.TotalType = 'Average' then Test2.avgscore
when test1.TotalType = 'PercentOfTot' then (cnt/SUM(test1.qrank))
else cnt
end as displayscore
I did try to group but it didn't work.
Any hints?
The error you posted can happen when you're using a clause in the GROUP BY statement without including it in the select.
Example
This one works!
SELECT t.device,
SUM(case when transits.direction = 1 then 1 else 0 end) ,
SUM(case when transits.direction = 0 then 1 else 0 end) from t1 t
where t.device in ('A','B') group by t.device
This one not (omitted t.device from the select)
SELECT
SUM(case when transits.direction = 1 then 1 else 0 end) ,
SUM(case when transits.direction = 0 then 1 else 0 end) from t1 t
where t.device in ('A','B') group by t.device
This will produce your error complaining that I'm grouping for something that is not included in the select
Please, provide all the query to get more support.
You could use a Common Table Expression to create the SUM first, join it to the table, and then use the WHEN to to get the value from the CTE or the original table as necessary.
WITH PercentageOfTotal (Id, Percentage)
AS
(
SELECT Id, (cnt / SUM(AreaId)) FROM dbo.MyTable GROUP BY Id
)
SELECT
CASE
WHEN o.TotalType = 'Average' THEN r.avgscore
WHEN o.TotalType = 'PercentOfTot' THEN pt.Percentage
ELSE o.cnt
END AS [displayscore]
FROM PercentageOfTotal pt
JOIN dbo.MyTable t ON pt.Id = t.Id
If you're using SQL Server 2005 or above, you can use the windowing function SUM() OVER ().
case
when test1.TotalType = 'Average' then Test2.avgscore
when test1.TotalType = 'PercentOfTot' then (cnt/SUM(test1.qrank) over ())
else cnt
end as displayscore
But it'll be better if you show your full query to get context of what you actually need.