Hive query with except conditions - sql

I am trying to build a hive query that does only the below features or a combination of these features. For example, the features include
name = "summary"
name = "details"
name1 = "vehicle stats"
Basically, the query should exclude all the other features in name and name1.
I am quite new to hive. In sql, i know this can be done using except keyword. Just wondering whether there is some functions that can achieve the same.
Thanks very much !!

If I understand correctly, I approach this using group by and having:
select ?
from t
group by ?
having sum(case when name = 'summary' then 1 else 0 end) > 0 and
sum(case when name = 'details' then 1 else 0 end) > 0 and
sum(case when name1 = 'vehicle_stats' then 1 else 0 end) > 0;
The ? is for the column that you want the summary of.

Related

How can I avoid zero values in an aggregate?

I have a query which is pulling back data correctly, but about a quarter of the lines of data are unnecessary because they have 0 in the primary data field. Here is the line I wrote to create that primary data field. How can I exclude any case where the output value for amort_FY2020 is 0?
sum(case when f.sales_revenue_period_calendar_sid in ('20200101',
'20200201',
'20200301',
'20200401',
'20200501',
'20200601',
'20200701',
'20200801',
'20200901',
'20201001',
'20201101',
'20201201')
then f.sales_revenue_before_tax_usd_amount
else 0 end) as amort_FY2020
Depending on your database, you can use:
having amort_FY2020 <> 0
Or may may need to repeat the expression:
sum(case when f.sales_revenue_period_calendar_sid in ('20200101','20200201','20200301','20200401','20200501','20200601','20200701','20200801','20200901','20201001','20201101','20201201')
then f.sales_revenue_before_tax_usd_amount else 0
end) <> 0

Trying to grab data from two columns and format them properly

So I have a database here with a table that lists off whether or not certain processes have failed. There are two columns, IsProcessed, and IsFailed. A failed process can still be considered processed if the error was handled, but I still need to recognize that it failed. They're both bit values, and so I have to try and grab and separate them despite that they may depend on one another. After they've been separated out, I need to count the relative successes and relative failures.
I utilize an AND statement in my WHERE clause to try and separate out the successes from the failures. I honestly have no idea where to go from here.
SELECT CAST(PQ.ProcessedDate AS Date) AS Date, COUNT(PQ.IsProcessed) AS Successes
FROM PQueue PQ
WHERE PQ.ProcessDate BETWEEN '2019-10-1' AND '2019-10-31' AND PQ.IsFailed = 0 AND PQ.IsProcessed = 1
GROUP BY CAST(PQ.ProcessDate AS Date)
ORDER BY CAST(PQ.ProcessDate AS Date) ASC
Because a failed process can still be processed in the system, we have to do a check first to try and grab the data that was processed but didn't flag a failure. Now I need to try and find a way to not exclude the failures, but include them and place them in a group. I can do the group part, but I'm relatively new to SQL so I don't know whether or not I can place something in an IF statement somewhere or try to use variables to get this done. Thank you in advance.
You seem to want conditional aggregation:
SELECT CAST(PQ.ProcessedDate AS Date) AS Date,
SUM(CASE WHEN PQ.IsFailed = 0 AND PQ.IsProcessed = 1 THEN 1 ELSE 0 END) as Successes,
SUM(CASE WHEN PQ.IsFailed = 1 AND PQ.IsProcessed = 1 THEN 1 ELSE 0 END) as Fails
FROM PQueue PQ
WHERE PQ.ProcessDate BETWEEN '2019-10-1' AND '2019-10-31'
GROUP BY CAST(PQ.ProcessDate AS Date)
ORDER BY CAST(PQ.ProcessDate AS Date) ASC
If SQL Server then maybe a CASE statement would help you out.
eg
SELECT ...........
CASE
WHEN IsFailed = 1 AND IsProcessed = 1 THEN "Processed But Failed"
WHEN IsFailed = 0 AND IsProcessed = 0 THEN "Not Processed"
WHEN IsFailed = 0 AND IsProcessed = 1 THEN "Processed Succesfully"
WHEN IsFailed = 1 AND IsProcessed = 0 THEN "Failed"
END as REsult

SQL Query to fetch exact values

I would like to know how to write query to fetch exact values from where clause. For example there is a table which has following data
USER ACCESS
A DEFAULT
A EVERYONE
B DEFAULT
B EVERYONE
B WEBACCESS
SQL query should be written in such a way that the output brings in only USER A . I have tried using group by and checking the count of records however it brought me both the users.
Thanks in advance!
I am guessing that you are storing the information on different rows, and you are trying to "merge" the results for each user. If so, you can do something like this:
select userid
from t
group by userid
having sum(case when access = 'EVERYONE' then 1 else 0 end) > 0 and
sum(case when access = 'DEFAULT' then 1 else 0 end) > 0 and
count(distinct access) = 2;

SQL to get the one to many mapping (like reverse CASE WHEN THEN END or decode in Oracle)

For example,
Through "CASE WHEN THEN END" I can get a many-to-one mapping as below.
REGION = CASE WHEN IOG IN (1,2,14,37,72,101) THEN '1'
WHEN IOG IN (11,22,48,77) THEN '7'
WHEN IOG IN (7,13,18,24,39) THEN '3'
ELSE NULL END
Currently, I want get the reverse mapping (one-to-many) which is used in WHERE clause, the logic is like as below (but it's not correct, what I mean is the logic)
CASE WHEN REGION = 1 THEN IOG in (1,2,14,37,72,101)
WHEN REGION = 7 THEN IOG in (11,22,48,77)
WHEN REGION = 3 THEN IOG in (7,13,18,24,39)
ELSE NULL END
There is grammatical mistake in above sql, but how can I make ? Is it possible to get a one-to-many mapping?
One more thing, which I'm using is Oracle DB, seems that it's not available by using "decode" because it's also one-to-one mapping.
Could anyone help with it? Thanks in advance.
Here is another approach:
WHERE (CASE WHEN REGION = 1 and IOG in (1,2,14,37,72,101) then 1
WHEN REGION = 7 and IOG in (11,22,48,77) then 1
WHEN REGION = 3 and IOG in (7,13,18,24,39) then 1
ELSE 0
END) = 1
Or, you can get rid of the case entirely:
WHERE (REGION = 1 and IOG in (1,2,14,37,72,101) ) or
(REGION = 7 and IOG in (11,22,48,77) ) or
(REGION = 3 and IOG in (7,13,18,24,39) )
Add Condition in where and using CASE .. WHEN .. THEN..
TRY TO FOLLOWING WAY..
select col1,col2,...colN..from Table
Where clm1 = CASE WHEN #PARAMETER = 0 THEN COL1 ELSE #PARAMETER
ONCE YOU SET SAME COLUMN NAME INTO WHERE CONDITION THEN YOU RETRIEVE ALL DATA FROM TABLE ELSE IF YOU WANT TO PASS AS PARAMETER THEN RETRIEVE BASE ON CONDITION VALUES FROM TABLE.

Multiple columns within a single CASE statement

I'm sure this has be covered many times so please pardon my repeat. I have a query that works but currently has 6 CASE statements within one select. Someone mentioned that it would be best optimized by putting all my WHEN conditions within a single CASE. However, I'm unable to achieve this
select right(RTRIM(region),5) as cell_id,
sum(CASE WHEN LEFT(cparty,3) in ('999','998','997') THEN chargeduration/60 else 0 END) AS OnNet_Minutes,
sum(CASE WHEN LEFT(cparty,3) in ('996','995') THEN chargeduration/60 else 0 END) AS OffNet_C_Minutes,
sum(CASE WHEN LEFT(cparty,3) in ('994','993','992') THEN chargeduration/60 else 0 END) AS OffNet_A_Minutes,
sum(CASE WHEN LEFT(cparty,3) in ('991','990') THEN chargeduration/60 else 0 END) AS OffNet_S_Minutes,
sum(CASE WHEN LEFT(cparty,2) = '00' THEN chargeduration/60 else 0 END) AS OffNet_T_Minutes,
sum(CASE WHEN len(cparty) < 6 and LEFT(cparty,1) <> 0 THEN chargeduration/60 else 0 END) AS SC_Minutes
from August.dbo.cdr20130818
where CHARGEDURATION > 0 and ISNULL(region,'''')<>'''' and LEN(region) > 5
group by right(RTRIM(region),5)
order by right(RTRIM(region),5) asc
In your case, you can't put them all into one CASE, since the results all go into different columns of the select.
BTW, you should remove your ISNULL(region, '''') <> '''' condition, as it's redundant when paired with the LEN(region) > 5 condition. (When region is null, then LEN(region) is also null, and NULL > 5 is false.)
I think you have it right, six different SUM()'s that each have meaning on their own.
If all of your criteria were in the same CASE statement you'd lose detail, you'd be returning the SUM() of your currently separate statements combined into one.
Combining redundant criteria in the WHERE clause can clean up a CASE statement, but you don't have anything completely redundant here.