can anyone help me with this query?
select trans_dt,trans_acc_no,trans_desc,trans_amt,transaction_type as trans_type , replace(trans_type,'credit','CR'), replace(trans_type,'debit','DB') from bank_transaction
where (trans_amt>10000 and cust_type != bank_rd_account)
order by(trans_type asc and trans_date desc) ;
remove and between order by clause - it is giving u the syntax error
select
trans_dt,
trans_acc_no,
trans_desc,
trans_amt,
transaction_type as trans_type,
replace(trans_type, 'credit', 'CR'),
replace(trans_type, 'debit', 'DB')
from
bank_transaction
where
(
trans_amt > 10000
and cust_type != bank_rd_account
)
order by
trans_type asc,
trans_date desc
Please refer the url for more reference about order by
https://www.techonthenet.com/oracle/order_by.php
In sql order cannot be given inside brackets.it should be given as below
Example:
SELECT * FROM table_name ORDER BY column1 ASC|DESC, column2 ASC|DESC
Query you required is given below:
select
trans_dt,
trans_acc_no,
trans_desc,
trans_amt,
transaction_type as trans_type,
replace(trans_type, 'credit', 'CR'),
replace(trans_type, 'debit', 'DB')
from
bank_transaction
where
trans_amt > 10000
and cust_type != bank_rd_account
order by
trans_type asc,
trans_date desc
Related
I have below query whereby i want to select all records at a specific hour but the other hours i want to filter to whitelisted records
SELECT
*
FROM
MY_TABLE
WHERE
COLUMN_A IN
(CASE
WHEN TO_CHAR(COL_TIMESTAMP, 'YYYYMMDDHH24') != '2021111217' THEN (
SELECT DISTINCT COLUMN_A
FROM
ANOTHER_TABLE )
ELSE COLUMN_A
END);
However with the query i get error
SQL Error [1427] [21000]: ORA-01427: single-row subquery returns more
than one row
How do i write this query without using union
Use AND and OR:
SELECT *
FROM MY_TABLE
WHERE (TO_CHAR(COL_TIMESTAMP, 'YYYYMMDDHH24') != '2021111217'
AND COLUMN_A IN (SELECT DISTINCT COLUMN_A FROM ANOTHER_TABLE))
OR TO_CHAR(COL_TIMESTAMP, 'YYYYMMDDHH24') = '2021111217'
OR col_timestamp IS NULL;
The IS NULL filter is necessary as != does not return false for NULL != '2021111217' so to match your original logic you need to include the extra filter.
Rewrite it to
select *
from my_table a
where a.column_a in
(select case when to_char(a.col_timestamp, 'yyyymmddhh24') <> '2021111217' then b.column_a
else a.column_a
end
from another_table b
)
As MT0 commented, that won't really work if another_table is empty, but this should:
select *
from my_table a
where a.column_a in
(select b.column_a
from another_table b
where to_char(a.col_timestamp, 'yyyymmddhh24') <> '2021111217'
union
select a.column_a
from dual
where to_char(a.col_timestamp, 'yyyymmddhh24') = '2021111217'
or a.col_timestamp is null
);
How could I get the date of the Maximum Value, by means of a subquery
I can't put the Date in the Main query because I would have to add it to the group by it would bring me a lot of data
Here is the Code:
SELECT MAX (A1.VALOR) AS VALOR,
(SELECT sq1.FECHA
FROM VARIABLE_VALORES_SMEC sq1
WHERE sq1.ID_AGENTE = A1.ID_AGENTE)
MES, -- {<-- Here is the Problem}
(SELECT CODIGO_AGENTE
FROM AGENTES
WHERE ID_AGENTE = A1.ID_AGENTE)
Agentess,
(SELECT NOMBRE_AGENTE
FROM AGENTES
WHERE ID_AGENTE = A1.ID_AGENTE)
Nombre_Agente
FROM VARIABLE_VALORES_SMEC A1
WHERE A1.VALOR < '1'
AND A1.VALOR != '0'
AND A1.ID_AGENTE IN (SELECT C1.ID_AGENTE
FROM VARIABLE_VALORES_SMEC C1
WHERE A1.FECHA = C1.FECHA)
AND A1.ID_AGENTE IN (SELECT B1.ID_AGENTE
FROM AGENTES B1
WHERE ID_CATEGORIA_AGENTE = 'AC006')
AND (A1.FECHA BETWEEN (ADD_MONTHS (TO_DATE ( :FECHAIN, 'MM/DD/YYYY'),
-1))
AND (LAST_DAY (
ADD_MONTHS (
TO_DATE ( :FECHAIN, 'MM/DD/YYYY'),
-1))))
AND A1.ID_VARIABLE LIKE '%_calc_total_pot#%'
GROUP BY ID_AGENTE
Am I correct that you need (fecha) for maximum A1.VALOR?
If - yes, you can use the following query, or if - no, just replace A1.VALOR with the required column in keep() clause:
SELECT MAX (A1.VALOR) AS VALOR,
max(A1.FECHA)keep(dense_rank first order by A1.VALOR desc) MES, -- A1.VALOR is used here as sort key, replace it with what you want
(SELECT CODIGO_AGENTE
FROM AGENTES
WHERE ID_AGENTE = A1.ID_AGENTE)
Agentess,
(SELECT NOMBRE_AGENTE
FROM AGENTES
WHERE ID_AGENTE = A1.ID_AGENTE)
Nombre_Agente
FROM VARIABLE_VALORES_SMEC A1
WHERE A1.VALOR < '1'
AND A1.VALOR != '0'
AND A1.ID_AGENTE IN (SELECT C1.ID_AGENTE
FROM VARIABLE_VALORES_SMEC C1
WHERE A1.FECHA = C1.FECHA)
AND A1.ID_AGENTE IN (SELECT B1.ID_AGENTE
FROM AGENTES B1
WHERE ID_CATEGORIA_AGENTE = 'AC006')
AND (A1.FECHA BETWEEN (ADD_MONTHS (TO_DATE ( :FECHAIN, 'MM/DD/YYYY'),
-1))
AND (LAST_DAY (
ADD_MONTHS (
TO_DATE ( :FECHAIN, 'MM/DD/YYYY'),
-1))))
AND A1.ID_VARIABLE LIKE '%_calc_total_pot#%'
GROUP BY ID_AGENTE
You can use row_number analytical function to fetch one record for which value is highest and use the fecha of that record. Use following sub query:
(Select fecha from
(SELECT sq1.FECHA, row_number() over (order by sq1.value desc nulls last) as rn
FROM VARIABLE_VALORES_SMEC sq1
WHERE sq1.ID_AGENTE = A1.ID_AGENTE)
Where rn = 1) MES
I am trying to get all the customers whether they have install service or not from TRANS_TABLE.
NOA- query to get only the MAX product and join again with TRANS_TABLE by email id to get the all the MAX customers details (wwhther they have install service by adding Y OR N, but this query return duplicate with REP Product as well
Below is my Oracel Query which give duplicated
with CTE as (SELECT NOA.*,
CASE
WHEN TRANS_TABLE.product_name LIKE '%Installation%' THEN 'Y'
ELSE 'N'
END AS Installaion ,
ROW_NUMBER() OVER (PARTITION BY TRANS_TABLE.email_address ORDER BY TRANS_TABLE.email_address) AS rn
FROM (SELECT DISTINCT email_address
FROM TRANS_TABLE
WHERE email_address IS NOT NULL
and pdct_name like '%MAX%'
) NOA
LEFT JOIN TRANS_TABLE
ON NOA.email_address = TRANS_TABLE.email_address
select * from cte where rn='1'
The following code will help:
CTE AS (
SELECT
NOA.*,
CASE
WHEN TRANS_TABLE.PDCT_NAME LIKE '%INSTALLATION%' THEN 'Y' -- case sensitive name is used
ELSE 'N'
END AS INSTALLAION,
ROW_NUMBER() OVER(
PARTITION BY TRANS_TABLE.EMAIL_ADDRESS
ORDER BY
TRANS_TABLE.EMAIL_ADDRESS
) AS RN
FROM
(
SELECT DISTINCT
PDCT_NAME,
EMAIL_ADDRESS
FROM
TRANS_TABLE
WHERE
EMAIL_ADDRESS IS NOT NULL
AND PDCT_NAME LIKE '%MAX%'
) NOA
LEFT JOIN TRANS_TABLE ON NOA.EMAIL_ADDRESS = TRANS_TABLE.EMAIL_ADDRESS
WHERE TRANS_TABLE.PDCT_NAME NOT LIKE '%REP%') -- added this WHERE condition
SELECT
PDCT_NAME, EMAIL_ADDRESS, INSTALLAION
FROM
CTE
WHERE
RN = '1'
db<>fiddle demo
Cheers!!
I'm new to Oracle and having knowledge of MS SQL. I'm trying to get a phone number depending upon the user_id from Table2 and here is the business logic:
Case1: if a single match is found in Table1 then get it's respective toll free number from Table2
Case2: if no match is found in Table1 then get the default toll free number from Table2
Case3: if an multiple match is found in Table1 then for all those assigned_care_levels get the Care value from Table2 ordered by asc or desc and select the top row phone number.
I wrote the following query which works fine when I run it individually. However, when I cobine it using the if else statements I'm getting the following error ERROR: ORA-00907: missing right parenthesis. Here is my code:
if ((select count(distinct care_level) from Table1 where user_id = '100') > 0)
select phone from Table2 where care_level in (select distinct care_level from Table1 where user_id = '100')
and rownum = 1
order by care_level asc
else if((select count(distinct care_level) from Table1 where user_id = '100') = 0)
select phone from Table2 where care_level = 'default'
SET SERVEROUTPUT ON;
DECLARE
v_CARE_COUNT NUMBER := 0;
v_PHONE VARCHAr2(40) := NULL;
BEGIN
select count(distinct care_level)
into v_CARE_COUNT
from Table1
where user_id = '100';
IF(v_CARE_COUNT > 0) THEN
select phone into v_PHONE
from Table2
where care_level in
(select distinct care_level from Table1 where user_id = '100')
and rownum = 1;
ELSE
select phone into v_PHONE
from Table2
where care_level = 'default';
END IF;
DBMS_OUTPUT.PUT_LINE('PHONE is <'||v_PHONE||'>');
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Exception : '||SQLERRM);
END;
/
EDIT: ( AS SIngle SQL)
SELECT PHONE
FROM TABLE2
WHERE CARE_LEVEL in
(
SELECT CARE_LEVEL FROM TABLE1 WHERE USER_ID='100'
UNION
SELECT CARE_LEVEL FROM TABLE1 WHERE CARE_LEVEL='default'
AND NOT EXISTS
(SELECT 'X' FROM TABLE1 WHERE USER_ID='100')
)
AND ROWNUM = 1;
The 'else if' syntax is wrong. In Oracle you need to use 'ELSIF' and complete the whole conditional statement with an 'END IF'. The SQL statements within should also be followed with a ;
Try:
IF ((select count(distinct care_level) from Table1 where user_id = '100') > 0) THEN
select phone from Table2 where care_level in (select distinct care_level from Table1 where user_id = '100')
and rownum = 1
order by care_level asc;
ELSIF ((select count(distinct care_level) from Table1 where user_id = '100') = 0) THEN
select phone from Table2 where care_level = 'default'; END IF
I have these codes:
select case
when exists (select 1
from table1
where id=608071)
then column1
else 0
end as abc
from table1 where id=608071;
and
select decode(count(column1), 0, column1) abc
from (select column1
from table1
where id=608071) group by column1;
and none of them returns me no column1, no 0, no error. It gives me null, i.e. nothing. No rows return. I need to get 0 when particular id does not exist. What is wrong in here?
try this!!
select case when exists (select 1 from table1 where id= '608071') then (select column1 from table1) else '0' end as abc from dual
Assuming this is your requirement: "I need to get 0 when particular id does not exist"
select
nvl(T.COLUMN1, 0) COLUMN1_OR_ZERO
from
DUAL D left join(
select
*
from
TABLE1
where
id = '608071') T on (1=1)
With this query, you only scan the table once.