REGEXP_SUBSTR equivalent in HIVE SQ - hiveql

I'm looking for the equivalent of REGEXP_SUBSTR in HIVE
CASE WHEN REGEXP_INSTR(campaign,'(Retention|RTN)') <> 0 THEN 'Retention'
WHEN REGEXP_INSTR(campaign,'(Conquest|CQT)') <> 0 THEN 'Conquest'
WHEN REGEXP_INSTR(campaign,'(Segment|SGT)') <> 0 THEN 'Segment'
ELSE 'Missing Tactic' END AS Strategy,

Related

Postgresql count from multiple columns in a row

I have this table on a postgres db (proofs). proof_1, proof_2 and proof_3 are boolean columns indicating whether the user (user_id) has submitted the proofs:
user_id
proof_1
proof_2
proof_3
1
true
true
false
2
true
false
false
3
true
true
true
I need to count how many proofs are submitted by each user. This is the query that I came up:
> select
user_id,
length(
concat(
case when proof_1 then '1' end,
case when proof_2 then '1' end,
case when proof_3 then '1' end)
)) as proof_counts
from
proofs
The query above would work. But I don't think that it is the best query to do. Please advice on what query should be done?
select user_id,
case when proof_1 then 1 else 0 end
+ case when proof_2 then 1 else 0 end
+ case when proof_3 then 1 else 0 end as proof_counts
from (
values (1,true,true,false), (2,false,true,false)
) as proof (user_id, proof_1, proof_2, proof_3)
If there is no null values then below query
select user_id,
proof_1::integer
+ proof_2::integer
+ proof_3::integer as proof_counts
from (
values (1,true,true,false), (2,false,true,false)
) as proof (user_id, proof_1, proof_2, proof_3)
And version which handle nulls
select user_id,
coalesce(proof_1::integer,0)
+ coalesce(proof_2::integer,0)
+ coalesce(proof_3::integer,0) as proof_counts
from (
values (1,null,true,false), (2,false,true,false)
) as proof (user_id, proof_1, proof_2, proof_3)
I think this is also a good alternative:
select user_id,
sum(case when proof_1 = true then 1 else 0 end) + sum(case when proof_2 = true then 1 else 0 end) + sum(case when proof_3 = true then 1 else 0 end) as proof_counts
from proofs
group by user_id
This is possible just by converting your boolean to integer and then summing the three columns
Query:
select
userid_id,
sum(case when proof_1=true then 1 end)+sum(case when proof_2=true then 1 end)+sum(case when proof_2=true then 1 end) as total_proofs_submitted
from proofs
group by user_id

Presenting hierarchical data with different conditions using joins in SQL Server 2012

I have two tables. In one table my raw data and in other table the relationships as below
Declare #Emp table(EmpId int,EmpName Varchar(100),CITY VARCHAR(100),Designation Varchar(100),ReportingManager Int)
INSERT INTO #Emp
VALUES(1,'Ram','Hyderabad','TL',6)
,(2,'Laxman','Hyderabad','TL',9)
,(3,'Suresh','Bangalore','Officer',6)
,(4,'Rajesh','Bangalore','Officer',9)
,(5,'Lokesh','Delhi','TL',6)
,(6,'Venkatesh','Mumbai','Manager',6)
,(7,'Subbu','Patna','Officer',9)
,(8,'Ravi','Hyderabad','Officer',9)
,(9,'Sai','Hyderabad','Manager',9)
,(10,'Satish','Hyderabad','Officer',6)
DECLARE #EmpRelation TABLE(EmpRelationShipID INT IDENTITY NOT NULL,ReportingTo INT,EmpID INT)
INSERT INTO #EmpRelation
VALUES(1,6)
,(2,9)
,(3,1)
,(4,5)
,(5,6)
,(7,2)
,(8,5)
,(10,1)
Here the ReportingManager Column in #Emp table indicates that If Emp
reports TL, then the TL's ManagerName.
Here ReportingTo Column in #EmpRelation indicates to whom he is
reporting.(TL or Manager).
Officers reports to TLs and TLs reports to Managers.
To get the result the have the below query which is working good
SELECT E.EmpId
,E.EmpName
,CASE
WHEN E.EmpId = E.ReportingManager
THEN 1
ELSE 0
END AS IsManager
,CASE
WHEN EXISTS (
SELECT NULL
FROM #EmpRelation ER
WHERE ER.ReportingTo = E.EmpId
)
THEN 1
ELSE 0
END AS HasSubordinates
,CASE WHEN E.EmpId != ReportingManager THEN 1 ELSE 0 END AS IsSubordinate
FROM #Emp E;
I want the query to be written using the joins instead of using the tables in Case statement.
I tried the below query. Please suggest any correction in terms of performance keeping view the future data size.
SELECT E.EmpId
,E.EmpName
,CASE
WHEN E.EmpId = E.ReportingManager
THEN 1
ELSE 0
END AS IsManager
,CASE WHEN HasSubordinate>0 THEN 1 ELSE 0 END HasSubordinates
,CASE WHEN E.EmpId != ReportingManager THEN 1 ELSE 0 END AS IsSubordinate
FROM #Emp E
LEFT JOIN (SELECT COUNT(*)HasSubordinate,ReportingTo Reporting FROM #EmpRelation ER GROUP BY ReportingTo)X ON X.Reporting=E.EmpID
Appreciate your help.
Thanks

DB2 SQL, cant define column as

I have some SQLthat is part of that is a section of a with statement
And I keep getting an error that "NEWID" is not valid in the context where it is used sqlstate 42703.
Update: The error has been comming from the group by clause using a having function I didnt put in the original code as I thought it wasnt the issue.So I updated the code to show the full version.
Does anyone know what the problem is with the statement?
HATSTABLE1 (HATId, NewID) as (
select HA.HATId as "ID",
round(
cast(
sum(
case when HA.ID = 4 or
HA.ID < 0
then 1 else 0 end
) AS FLOAT
) / count(*) * 100,
2
) AS NewID
from Hats T
join Heads HD on
T.ID=HD.HatID
group by T.ID
having NewID >1
try it
with tmp as (
select T.HATId as "ID",
sum(case when T.ID = 4 or HA.ID < 0 then 1 else 0 end) as sum1,
count(*) as nb
from Hats T
group by T.HATId
)
select HATId, round(cast(sum1 as decimal)/ nb * 100, 2) NewID
from tmp

SQL Select where exclusive Or /Case

I need a query based on an exclusive Or statement for this I try using the case but I won't get a result in case of Null...
...
and b.[U_Periode] = CASE
when (b.U_Periode= #period) then #period
when (b.U_Periode is NULL ) then null
end
...
The Case that won't be catched is... if B.U_Status is null and b.U_Periode is null.
If the var Periode match the Value and the U_Status = 0 or 1
the only way getting this working for me was this:
...
and
ISNULL( b.[U_Status],'0') = CASE
when (b.U_Status= '1') then '1'
when (isnull( b.U_Status,'0')= '0') then '0'
end
and
ISNULL (b.[U_Periode],'01.01.1901') = CASE
when (b.U_Periode= #period) then #period
when (ISNULL (b.U_Periode,'01.01.1901') = '01.01.1901' ) then '01.01.1901'
end
are there any other better solutions for this?
Best regards
Oliver
Okay... here is my Problem
Table1
InsID ContractID
1 1
2 1
Table2
ID insid Period Status Count
1 1 null null 100
2 1 30.09.2015 1 500
3 2 null null 100
4 2 30.09.2015 1 500
Case '31.08.2015'
in total Value should be 200
in case of '30.09.2015'
the Value should be 1.000
XOR /OR will do the same in this case.
Value case '31.08.2015' = 200
value Case ' 30.09.2015 = 2200
So this is somesing like a subquery
left join (
[dbo].[Table2]b
inner join [dbo].[Table 3]K on k.DocEntry = b.DocEntry and CAST( k.U_CSetID as int) >0
)
on b.[U_InsID] in(select... but here I should have an if statement...
If there are results matching the Date join this
If not than join the result NULL is matching to periode...
Okay.. here is the complete query
the Table2 with the Date of 31.08.2015 should have one Record that include
B_STATUS = Null and B.Preiode = null and there is no available record with the U_Periode '31.08.2015' and a Staus ...
with a date of 30.09.2015
there is a Record matching U_Period = '30.09.2015' in this case the Record with U_Period=null should not effect the result...
Declare #period as varchar(20)= '31-08-2015 00:00:00'
declare #Customer as Varchar(15)='12345'
declare #Contract as varchar(30) = '123'
declare #test as varchar(1) = null
select SUM(cast(K.U_Count as decimal))as counter, K.U_CounterTyp
from [dbo].[Table1] a
left join (
[dbo].[Table2]b
inner join [dbo].[Table 3]K on k.DocEntry = b.DocEntry and CAST( k.U_CSetID as int) >0
)
on b.[U_InsID]=a.[insID] and b.[U_ObjectType]in ('5','1') and
ISNULL( b.[U_Status],'0') = CASE
when (b.U_Status= '1') then '1'
when (isnull( b.U_Status,'0')= '0') then '0'
end
and
ISNULL (b.[U_Periode],'01.01.1901') = CASE
when (b.U_Periode= #period) then #period
when (ISNULL (b.U_Periode,'01.01.1901') = '01.01.1901' ) then '01.01.1901'
end
where a.[customer] =#Customer and a.[Status]='A' and a.[U_ContrCount]='1'
and a.[manufSN] in(
select c.[ManufSN] from [dbo].[Table4] c
inner join [dbo].[OCTR]d on d.[ContractID] = c.[ContractID]
where c.[ManufSN]=a.[manufSN]
and d.[CstmrCode] = a.[customer] and d.[ContractID]=#Contract
)
group by K.U_CounterTyp
you must use the "^" operand, this is XOR operand in TSQL
expression ^ expression
the expression must return or 0 or 1...
stupid example:
WHERE (name like "stackoverflow") ^ (age > 10)
font: https://msdn.microsoft.com/en-us/library/ms190277(v=sql.105).aspx
Update for your check
WHERE (CONVERT(VARCHAR(10), a.[U_Periode], 104) = '30.08.2015') != (a.U_Periode IS NULL)
Olay here is my funktion that chek either a Date Result is there or not.
The only thing is, that the performance is not the best if I run hundreths of sentences ... foreach record (up to app. 1000) I need to query each subtable... and there are many many records in...
Anyway this is the function
CREATE FUNCTION fn_GetInsCounters(#InsId as Varchar(30), #Date as datetime)
returns datetime
as
begin
declare #count as int = 0
declare #Retruns as Datetime
set #count =
(
select count( b.Docentry)
from [dbo].[Table2]b
where b.U_Insid =#InsID
and b.U_Status <> '2'
and b.[U_ObjectType]in ('5','1')
and b.U_Periode=#Date
)
if(#count>0)
begin
set #Retruns = #date
end
else
begin
set #Retruns = '01.01.1901'
end
return #Retruns
end
if anyone gets a better idea???
Best regards
Oliver

Using the result of a subquery in a CASE expression with T-SQL

I'm writing a query with some CASE expressions and it outputs helper-data columns which help me determine whether or not a specific action is required. I would like to know if I can somehow use the result of a subquery as the output without having to perform the same query twice (between WHEN (subquery) THEN and as the result after THEN)
The dummy code below describes what I'm after. Can this be done? I'm querying a MS2005 SQL database.
SELECT 'Hello StackOverflow'
,'Thanks for reading this question'
,CASE
WHEN
(
SELECT count(*)
FROM sometable
WHERE condition = 1
AND somethingelse = 'value'
) > 0 THEN
-- run the query again to get the number of rows
(
SELECT count(*)
FROM sometable
WHERE condition = 1
AND somethingelse = 'value'
)
ELSE 0
END
SELECT 'Hello StackOverflow'
,'Thanks for reading this question'
,CASE
WHEN
(
SELECT count(*)
FROM sometable
WHERE condition = 1
AND somethingelse = 'value'
) AS subqry_count > 0 THEN
-- use the subqry_count, which fails... "Incorrect syntax near the keyword 'AS'"
subqry_count
ELSE 0
END
Just use the subquery as the source you are selecting from:
SELECT 'Hello StackOverflow'
,'Thanks for reading this question'
,CASE subqry_count.Cnt
WHEN 0 THEN 0
ELSE subqry_count.Cnt
END
FROM ( SELECT count(*) AS Cnt
FROM sometable
WHERE condition = 1
AND somethingelse = 'value'
) subqry_count
As an aside, if you are just going to return 0 if the output from COUNT is 0, then you don't even need to use a CASE statement.