Concat the values using the case condition - sql

select
case when concat('ACC','-',NVL(trim(bvmo.booking),''),'-',NVL(trim(bvmo.org),''),'-',NVL(trim(bvma.sparm),''),'-',NVL(trim(bvmo.id),'') like 'ACC--%' then
NULL ELSE concatconcat('ACC','-',NVL(trim(bvmo.booking),''),'-',NVL(trim(bvmo.org),''),'-',NVL(trim(bvma.sparm),''),'-',NVL(trim(bvmo.id),'') END AS Parent_id
from bvmo
output:
Parent-id
:----------:
ACC-1123-1344--
ACC-4567-6528--
ACC-7890-9827--
ACC-1143-8079--
ACC-1883-8944--
am expecting as the below output where i can remove the "-" in above case condition in which the values are not appearing in respective columns
Expected output:
Parent-id
:----------:
ACC-1123-1344
ACC-4567-6528
ACC-7890-9827
ACC-1143-8079
ACC-1883-8944

Use concat_ws. It should take care of nulls in the way you wanted.
select
concat_ws('-', 'ACC', trim(bvmo.booking), trim(bvmo.org), trim(bvma.sparm), trim(bvmo.id)) AS Parent_id
from bvmo

Related

mismatched input 'from'. Expecting: ',', <expression>

I have a query that I am running on AWS athena that should return all the filenames that are not contained in the second table. I am basically trying to find all the filename that are not in ejpos landing table.
The one table looks like this (item sales):
origin_file
run_id
/datarite/ejpos/8023/20220706/filename1
8035
/datarite/ejpos/8023/20220706/filename2
8035
/datarite/ejpos/8023/20220706/filename3
8035
The other table looks like this (ejpos_files_landing):
filename
filename1
filename2
filename3
filename4
They don't have the same number of rows, hence I am trying to find the file names that are in ejpos_pos_landing but not in item sales table.
I get this error when I run:
mismatched input 'from'. Expecting: ',', <expression>
The query is here:
SELECT trim("/datarite/ejpos/8023/20220706/" from "validated"."datarite_ejpos_itemsale" where
run_id = '8035') as origin_file,
FROM "validated"."datarite_ejpos_itemsale"
LEFT JOIN "landing"."ejpos_landing_files" ON "landing"."ejpos_landing_files".filename =
"validated"."datarite_ejpos_itemsale".origin_file
WHERE "landing"."ejpos_landing_files".filename IS NULL;
The expected result would be:
|filename4|
Because it is not in the other table
Can anyone assist?
There is a lot of wrong stuff in your query based on the example data and declared goals.
trim("/datarite/ejpos/8023/20220706/" from "validated"."datarite_ejpos_itemsale" where run_id = '8035') as origin_file is not a valid sql.
ON "landing"."ejpos_landing_files".filename = "validated"."datarite_ejpos_itemsale".origin_file will not work cause origin_file is prefixed. You can use strpos if there should be only one instance of filename in the origin_file.
your join and filtering condition are build to find items present in datarite_ejpos_itemsale and missing in ejpos_landing_files while you state the vise versa is needed.
the mentioned in the comments extra comma
Try next:
-- sample data
WITH item_sales(origin_file, run_id) AS (
VALUES ('/datarite/ejpos/8023/20220706/filename1', 8035),
('/datarite/ejpos/8023/20220706/filename2', 8035),
('/datarite/ejpos/8023/20220706/filename3', 8035),
('/datarite/ejpos/8023/20220706/filename4', 8036)
),
ejpos_files_landing(filename) as(
VALUES ('filename1'),
('filename2'),
('filename3'),
('filename4')
)
-- query
select filename
from ejpos_files_landing l
left outer join item_sales s -- reverse the join
on strpos(s.origin_file, l.filename) >= 1 -- assuming that filename should be present only one time in the string
and s.run_id = 8035 -- if you need to filter out run id
where s.origin_file is null
Output:
filename
filename4
Alternative approach you can try:
-- query
select filename
from ejpos_files_landing l
where filename not in (
select element_at(split(origin_file, '/'), -1) -- split by '/' and get last
from item_sales
where run_id = 8035
)

query to find all db parameter values including null

does this query get all values including nulls,where the parameter not available in Table.
select name,display_value
from v$spparameter where nvl(name,'null') in(
'memory_target','sga_target','pga_aggregate_target','pga_aggregate_limit','db_cache_size',
'shared_pool_size','large_pool_size','result_cache_max_size','processes',
'session_cached_cursors','open_cursors','db_securefile','cpu_count',
'parallel_max_servers','job_queue_processes','log_buffer','_b_tree_bitmap_plans',
'use_large_pages','audit_trail','nls_sort','plsql_code_type','resource_manager_plan',
'shared_servers','max_shared_servers','dispatchers','event','undo_retention',
'_highthreshold_undoretention','parallel_adaptive_multi_user','parallel_force_local',
'db_files','db_performance_profile','_srvntfn_max_concurrent_jobs',
'_optimizer_use_feedback','optimizer_dsdir_usage_control',
'_sql_plan_directive_mgmt_control','_fix_control','global_txn_processes',
'_disable_autotune_gtx', '_ges_server_processes','sga_max_size',
'_securefiles_concurrency_estimate','streams_pool_size')
order by name;
You can try to add OR name is null in where caluse.
Because SQL NULL isn't a value. you need to use IS NULL to get it.
select name,display_value
from v$spparameter
where
name in('memory_target','sga_target','pga_aggregate_target','pga_aggregate_limit','db_cache_size','shared_pool_size','large_pool_size','result_cache_max_size','processes','session_cached_cursors','open_cursors','db_securefile','cpu_count','parallel_max_servers','job_queue_processes','log_buffer','_b_tree_bitmap_plans','use_large_pages','audit_trail','nls_sort','plsql_code_type','resource_manager_plan','shared_servers','max_shared_servers','dispatchers','event','undo_retention','_highthreshold_undoretention','parallel_adaptive_multi_user','parallel_force_local','db_files','db_performance_profile','_srvntfn_max_concurrent_jobs','_optimizer_use_feedback','_optimizer_dsdir_usage_control','_sql_plan_directive_mgmt_control','_fix_control','global_txn_processes','_disable_autotune_gtx', '_ges_server_processes','sga_max_size','_securefiles_concurrency_estimate','streams_pool_size')
OR
name is null
order by name;
Almost. You're converting the null value into the string 'null', but you forgot to include it in the "in" list. Just add 'null' to list.
select name,display_value
from v$spparameter where nvl(name,'null') in('null',
'memory_target','sga_target','pga_aggregate_target','pga_aggregate_limit','db_cache_size',
'shared_pool_size','large_pool_size','result_cache_max_size','processes',
'session_cached_cursors','open_cursors','db_securefile','cpu_count',
'parallel_max_servers','job_queue_processes','log_buffer','_b_tree_bitmap_plans',
'use_large_pages','audit_trail','nls_sort','plsql_code_type','resource_manager_plan',
'shared_servers','max_shared_servers','dispatchers','event','undo_retention',
'_highthreshold_undoretention','parallel_adaptive_multi_user','parallel_force_local',
'db_files','db_performance_profile','_srvntfn_max_concurrent_jobs',
'_optimizer_use_feedback','optimizer_dsdir_usage_control',
'_sql_plan_directive_mgmt_control','_fix_control','global_txn_processes',
'_disable_autotune_gtx', '_ges_server_processes','sga_max_size',
'_securefiles_concurrency_estimate','streams_pool_size')
order by name;

`0` values are not shown when I put a string value filter SQL

SELECT DISTINCT ISNULL(a.[BPOAGE], 0) AS BPOAGE, a.[BPOAttic]
FROM [Legacy].[dbo].[MyTables] as a
Result :
SELECT DISTINCT ISNULL(a.[BPOAGE], 0) AS BPOAGE, a.[BPOAttic]
FROM [Legacy].[dbo].[MyTables] as a
where (a.[BPOAGE] not in ('New'))
Result :
Q : Can you tell me why 0 values are not shown when I put this condition a.[BPOAGE] not in ('New')?
Sql works on Three valued logic.It considers NULL as unknown,since it is unknown it will not got selected in the condition you write.If you want to include null rewrite the code as
SELECT DISTINCT ISNULL(a.[BPOAGE], 0) AS BPOAGE,a.[BPOAttic] FROM [Legacy].
[dbo].[MyTables] as a where (a.[BPOAGE] not in ('New') or a.[BPOAGE] is null)

Build a field in the Select Q

I am trying to add a field in my Select statment that will create a field with my desired output or make the field results empty. My Boss doesn't want to scan the entire mopdescription field if they have to.
Please see code ** to see if you can make sense of this question/request.
SELECT MOPACTIVITY.MOPID,
**UPPER(MOPACTIVITY.MOPDESCRIPTION) LIKE '%FTTT%'
OR UPPER(MOPACTIVITY.MOPDESCRIPTION) LIKE '%VZW%' AS "NOTE_DISP"**,
MOPACTIVITY.MOPDESCRIPTION
FROM MOPUSER.MOPACTIVITY
SELECT mopid
, CASE
WHEN INSTR(UPPER(mopdescription), 'FTTT') > 0
THEN SUBSTR(mopdescription, INSTR(UPPER(mopdescription), 'FTTT'))
WHEN INSTR(UPPER(mopdescription), 'VZW') > 0
THEN SUBSTR(mopdescription, INSTR(UPPER(mopdescription), 'VZW'))
ELSE NULL
END AS note_disp
FROM mopuser.mopactivity';

Issue with union query - select list not compatible

Been working on this query for some time now... Keep getting the error "Corrosponding select-list expressions are not compatible. I am selecting the same # of columns in each select statement.
create volatile table dt as (
SELECT
gcv.I_SYS_IDV,
gcv.i_pln,
gcv.c_typ_cov,
gcv.d_eff,
gcv.d_eff_pln,
gcv.c_sta,
gcv.d_sta,
gcv.c_mde_bft_fst,
gcv.a_bft_fst,
gcv.c_mde_bft_sec,
gcv.a_bft_sec,
gcv.c_mde_bft_trd,
gcv.a_bft_trd,
gcv.p_cre_hom,
gcv.c_cl_rsk,
gpv.c_val,
gpv.i_val,
gcv.c_pol,
gpv.i_prv
FROM Pearl_P.tltc906_gcv gcv,
pearl_p.tltc912_gpv gpv
WHERE gcv.i_pln > 0
AND gcv.i_pln = gpv.i_pln
and gpv.i_prv = '36'
and gcv.c_pol between 'lac100001' and 'lac100004'
UNION
SELECT
gcv.I_SYS_IDV,
gcv.i_pln,
gcv.c_typ_cov,
gcv.d_eff,
gcv.d_eff_pln,
gcv.c_sta,
gcv.d_sta,
gcv.c_mde_bft_fst,
gcv.a_bft_fst,
gcv.c_mde_bft_sec,
gcv.a_bft_sec,
gcv.c_mde_bft_trd,
gcv.a_bft_trd,
gcv.p_cre_hom,
gcv.c_cl_rsk,
gcv.c_pol,
gpv.i_val,
gpv.i_pln,
gpv.i_prv
FROM Pearl_P.tltc906_gcv gcv,
pearl_p.tltc912_gpv gpv
where NOT EXISTS(
SELECT 1
FROM pearl_p.tltc906_gcv gcv,
pearl_p.tltc912_gpv gpv
WHERE gcv.i_pln > 0
AND gcv.i_pln = gpv.i_pln
and gpv.i_prv = '36'
)
) with data
PRIMARY INDEX (i_sys_idv)
on commit preserve rows;
You should check the data types of each column. The data types must be compatible between each SELECT statement.
The last 4 values of your second select statement don't match the ones in your first statement. Try naming(using aliases) those columns the same thing(pairing them). To union you need to have the same columns between the sets.
Check out: http://msdn.microsoft.com/en-us/library/ms180026.aspx
The following are basic rules for combining the result sets of two
queries by using UNION:
The number and the order of the columns must be the same in all
queries.
The data types must be compatible.
Are the data types of each column the same in both portions of the query?
If the first character of the column name indicates the data type (i = integer, c = character, etc.) I'm guessing that the problem is with the second to last column in the select list. The first query is selecting gcv.c_pol, the second query is selecting gpv.i_pln.
Start commenting-out lines until it works. I.e., comment out all of the fields in each select list, and then one-by-one, un-comment the first one out, then the second, etc. You'll find it eventually.