select source_type, data_load_type from wc_table_inf
where not (source_type IN ('EHUB','FOCUS','SAPReport')
and data_load_type in ('FULL','INCR'));
This is a EDW query. I need equivalent query in hive. How can i solve this NOT??
You have to expand the Boolean logic NOT (A and B) = NOT A OR NOT B
SELECT source_type
,data_load_type
FROM wc_table_inf
WHERE source_type NOT IN (
'EHUB'
,'FOCUS'
,'SAPReport'
)
OR data_load_type IN (
'FULL'
,'INCR'
);
Try this.
select source_type, data_load_type from wc_table_inf where source_type not in ('EHUB','FOCUS','SAPReport') and data_load_type not in ('FULL','INCR');
Related
I'm having a hard time with sql and probably this will look stupid but it shows what I am trying to achieve.
SELECT
CASE WHEN ( ( SELECT 1 FROM table_1 WHERE = condition ) IS NULL ) THEN
SELECT 'No result'::varchar
ELSE
SELECT
val_1,
val_2,
val_3
FROM
table_1
END;
A null answer is not good in my situation, I can't just use the sub-query as the main. And even if I could that would still leave the question open if the two tables were NOT the same like:
SELECT
CASE WHEN ( ( SELECT 1 FROM table_1 WHERE = condition ) IS NULL ) THEN
SELECT 'No result'::varchar
ELSE
SELECT
val_1,
val_2,
val_3
FROM
table_2 --TABLE REPLACED!
END;
As CASE-WHEN only works for one column it would be horrifying to have 20 of them with the same condition. Any help is appreciated! Thanks!
So you want to SELECT the table_log and if the result is not NULL show it to the client and if it is NULL show a message?
I created a fake table for testing. What you are looking for is the last SELECT-statement:
DROP TABLE IF EXISTS table_log;
CREATE TEMP TABLE table_log (
id INTEGER
,log_info VARCHAR)
;
INSERT INTO table_log VALUES
(1, 'test_entry')
;
ANALYZE table_log;
SELECT
COALESCE(b.log_info, 'No changes done!') AS log_info
FROM
(SELECT 'Fake-Data') a
LEFT OUTER JOIN (SELECT * FROM table_log WHERE id = 1) b ON (1=1);
If the given id = 1, you get the result, if it is something else (because it is not in the test-table) the premade message is given.
Here is a link to the db<>fiddle.
I am trying to pivot a sql result. I need to do this all in the one query. The below is telling me invalid identifier for header_id. I am using an Oracle database.
Code
Select * From (
select ppd.group_id,g.group_name, ct.type_desc,ht.hos_cat_descr
from item_history ih, item ci, contract ppd,
header ch, group g, cd_std_type ct, cd_hos h,
cd_std_hospital_cat ht
where ih.item_id = ci.item_id
and ih.header_id = ch.header_id
and ci.hos_id = h.hos_id
and ih.item_id = ci.item_id
and ch.user_no = ppd.user_no
and ppd.group_id = g.group_id
and ch.header_type = ct.header_type_id
and ci.hos_id = h.hos_id
and h.cat_id = ht.cat_id
)
Pivot
(
count(distinct header_id) as Volume
For hos_cat_descr IN ('A')
)
Your inner query doesn't have header_id in its projection, so the pivot clause doesn't have that column available to use. You need to add it, either as:
Select * From (
select ppd.group_id,g.group_name, ct.type_desc,ht.hos_cat_descr,ih.header_id
---------------------------------------------------------------^^^^^^^^^^^^^
from ...
)
Pivot
(
count(distinct header_id) as Volume
For hos_cat_descr IN ('A')
)
or:
Select * From (
select ppd.group_id,g.group_name, ct.type_desc,ht.hos_cat_descr,ch.header_id
---------------------------------------------------------------^^^^^^^^^^^^^
from ...
)
Pivot
(
count(distinct header_id) as Volume
For hos_cat_descr IN ('A')
)
It doesn't really matter which, since those two values must be equal as they are part of a join condition.
You could achieve the same thing with simpler aggregation instead of a pivot, but presumably you are doing more work in the pivot really.
I have two similar queryes.
Like folowing:
with CategoryHistory AS (
SELECT
category.GUID as id,
----HERE MANY FIELDS----
category.__$operation AS operation,
map.tran_begin_time as beginT,
map.tran_end_time as endT
FROM cdc.fn_cdc_get_all_changes_dbo_EXT_BalanceHC_ZapasCategory(sys.fn_cdc_get_min_lsn('dbo_EXT_BalanceHC_ZapasCategory'), sys.fn_cdc_get_max_lsn(), 'all') AS category
INNER JOIN [cdc].[lsn_time_mapping] map
ON category.[__$start_lsn] = map.start_lsn
)
SELECT operation, fields, valueses, beginT, endT FROM CategoryHistory
unpivot ( [valueses] for fields in
(
----HERE MANY FIELDS----
))p
Can i UNION of WITH results? Problem that i want UNPIVOT some fields from result of query.
Perhaps you simply want something like this:
SELECT ch.operation, ch.beginT, ch.endT, v.field, v.val
FROM CategoryHistory ch cross apply
(values ('field1', field1), ('field2', field2), . . .
) f(field, val);
I find this easier than using unpivot.
I'm creating a view in MariaDB and i'm having trouble making it work for a couple of fields. Currently this is working:
( SELECT DISTINCT IFNULL(grades.`grade`,'No Grade')
FROM `table` grades
WHERE userinfo.`id` = grades.`id`
AND grades.`Item Name` = 'SOMEINFO'
) 'SOMENAME',
But i need to add a select where the 'No grade' is, in the following form
( SELECT DISTINCT IFNULL( grades.`grade`,
SELECT IF( EXISTS
( SELECT *
FROM `another_table`
WHERE userid = 365
AND courseid = 2
), 'Enrolled', 'Not enrolled'
)
)
FROM `table` grades
WHERE userinfo.`id` = grades.`id`
AND grades.`Item Name` = 'SOMEINFO'
) 'SOMENAME',
i know that
SELECT IF( EXISTS( SELECT *
FROM `another_table`
WHERE userid = 365
AND courseid = 2
),
'Enrolled', 'Not enrolled'
)
is working too, but now the whole thing it's giving me an error, so any suggestions would be greatly appreciated
Thanks
This looks like a subquery:
(SELECT DISTINCT IFNULL(grades.`grade`,
SELECT IF( EXISTS (SELECT *
FROM `another_table`
WHERE userid = 365 AND courseid = 2
), 'Enrolled', 'Not enrolled'
)
)
FROM `table` grades
WHERE userinfo.`id` = grades.`id` AND
grades.`Item Name` = 'SOMEINFO'
) as SOMENAME,
You are using a subquery that returns two columns in a position where a scalar subquery is expected. A scalar subquery returns one column in at most one row.
Unfortunately, there is no easy way to do what you want in MySQL, because of the restrictions on views. I would advise you to rewrite the logic so the exists is handled using a left join in the from clause.
I have problem, because I get error unsupported column aliasing and I dont't know where is the cause. Does anyone know the answer and can help? Thanks in advance :) Code:
with
ContractByRole (intermediary_nr, beneficiary_role, contract_nr) as (
select intermediary_nr, beneficiary_role, max(contract_nr) contract_nr
from boscs.atcs_commission_beneficiary
where beneficiary_role in( 'LEAD', 'SUP_FOR_LEAD', 'COAGENT' )
and intermediary_nr is not null
group by intermediary_nr, beneficiary_role
)
AllRoles(contract_nr) as (
select contract_nr
from ContractByRole
group by contract_nr
having count(*) = 3
)
select cbr.*
from ContractByRole cbr
join AllRoles ar
on ar.contract_nr = cbr.contract_nr;
Error show in this place:
ContractByRole(intermediary_nr, beneficiary_role, contract_nr) as (
You have missed a comma to seperate the two subqueries in your WITH clause -
Edit Try this : (Not sure if you really need the recursive subquery)
WITH contractbyrole
AS
(
SELECT intermediary_nr,
beneficiary_role,
Max(contract_nr) contract_nr
FROM boscs.atcs_commission_beneficiary
WHERE beneficiary_role IN( 'LEAD',
'SUP_FOR_LEAD',
'COAGENT' )
AND intermediary_nr IS NOT NULL
GROUP BY intermediary_nr,
beneficiary_role
)
, --> here is the missing comma
allroles
AS
(
SELECT contract_nr
FROM contractbyrole
GROUP BY contract_nr
HAVING count(*) = 3
)
SELECT cbr.*
FROM contractbyrole cbr
JOIN allroles ar
ON ar.contract_nr = cbr.contract_nr;