toad formatter starts next line under trailing comments - formatting

I find that toad formatter starts the next line of code under trailing comments.
Is there a way to change this.
This happens regardless of wrapping/stacking options.
e.g.
Original SQL:
SELECT C1, C2, C3, C4, C5 FROM DUAL
Now I add a trailing comment after C3:
SELECT C1, C2, C3 -- this is a test
, C4, C5 FROM DUAL
Result of formatting - C4 starts below the trailing comment:
SELECT C1, C2, C3 -- this is a test
, C4, C5 FROM DUAL
Preferred solution - Stack the columns:
SELECT C1
, C2
, C3 -- this is a test
, C4
, C5
FROM DUAL
Is there a setting I can use to change this?
Kind regards
fe
Decided to add another example where the comment is in the WHERE clause:
Original SQL:
SELECT C1
, C2
, C3
FROM DUAL
WHERE C1 = 1 AND C2 = 2 AND C3 = 3
Adding comment and formatting:
SELECT C1
, C2
, C3
FROM DUAL
WHERE C1 = 1 AND C2 = 2 -- this is a test
AND C3 = 3
Preferred solution :
SELECT C1
, C2
, C3
FROM DUAL
WHERE C1 = 1
AND C2 = 2 -- this is a test
AND C3 = 3
fe

Related

Variable ordering based on value Oracle SQL

I have this query that are a number of positions that goes from A1 to A100, then B1 to B100, and so on.
I want to order this query so that the positions appear alphabetically in ascending order, but when it reaches the next letter, it goes descending, and keep this until the end of the table.
Here is an example.
SELECT
POSITION
FROM POSITION_TABLE
-------------------
POSITION
--------
A1
A2
...
A99
A100
B100
B99
B98
...
B2
B1
C1
C2
..
How can I do this ordering inside a query?
I tried ordering by the position using CASE, but I wanted something more generic, in case I needed to increase in size.
You can use:
SELECT position
FROM (
SELECT position,
REGEXP_SUBSTR(position, '^\D+') AS leading_string,
CASE MOD(DENSE_RANK() OVER (ORDER BY REGEXP_SUBSTR(position, '^\D+')), 2)
WHEN 1
THEN ROW_NUMBER() OVER (
PARTITION BY REGEXP_SUBSTR(position, '^\D+')
ORDER BY TO_NUMBER(REGEXP_SUBSTR(position, '\d+$')) ASC
)
ELSE ROW_NUMBER() OVER (
PARTITION BY REGEXP_SUBSTR(position, '^\D+')
ORDER BY TO_NUMBER(REGEXP_SUBSTR(position, '\d+$')) DESC
)
END AS rn
FROM position_table
)
ORDER BY
leading_string,
rn;
Which, for the sample data:
CREATE TABLE position_table (POSITION) AS
SELECT 'A' || LEVEL FROM DUAL CONNECT BY LEVEL <= 10 UNION ALL
SELECT 'B' || LEVEL FROM DUAL CONNECT BY LEVEL <= 10 UNION ALL
SELECT 'C' || LEVEL FROM DUAL CONNECT BY LEVEL <= 10 UNION ALL
SELECT 'D' || LEVEL FROM DUAL CONNECT BY LEVEL <= 10;
Outputs:
POSITION
A1
A2
A3
...
A9
A10
B10
B9
...
B3
B2
B1
C1
C2
C3
...
C9
C10
D10
D9
...
D2
D1
fiddle

How to do aggregation with calculation in SQL

I have similar table with two columns, C2 will have multiple values . I need the output as
Condition for t2.c2 is If all the values in t1.C2 are <= 5, then 1 else 0, please advice what would be the best logic.
Another option
select distinct C1,
if(logical_and(C2 <= 5) over(partition by C1), 1, 0) as C2
from your_table
if applied to sample data in your question - output is
This will give desired result. First section has t1 table data, second section has t2 data based on logic requested.
with t1 as
(select 'A10' as C1, 2 as C2 union all
select 'A10' as C1, 3 as C2 union all
select 'A10' as C1, 4 as C2 union all
select 'A10' as C1, 5 as C2 union all
select 'A10' as C1, 3 as C2 union all
select 'A10' as C1, 4 as C2 union all
select 'A10' as C1, 2 as C2 union all
select 'A10' as C1, 4 as C2 union all
select 'A10' as C1, 5 as C2 union all
select 'A10' as C1, 3 as C2 )
select C1 , if(MAX(C2) <=5,1,0) as C2
from t1 group by C1;

SQL HIVE | Duplicate lines in Table

I have a table like this where the keys are [c_1, c_2, c_3], I want to non duplicates in my table.
Input :
C1 C2 C3 C4 C5
A1 D1 V1 X1 F3
A2 D1 V1 X2 F2
A1 D1 V1 X1 F3
A2 D1 V1 X2 F2
A4 D1 V2 X1 F3
A2 D1 V1 X1 F3
Output :
C1 C2 C3 C4 C5
A1 D1 V1 X1 F3
A2 D1 V1 X2 F2
A4 D1 V2 X1 F3
Regards,
try below:
insert overwrite table yourtable select distinct * from yourtable;
you can select the non duplicated data by
SELECT DISTINCT * FROM Table
then you can truncate the table and insert the above result to the table.
You can use ROW_NUMBER() window function:
select t.c1, t.c2, t.c3, t.c4, t.c5
from (
select *, row_number() over (partition by c1, c2, c3 order by c4, c5) rn
from tablename
) t
where t.rn = 1
You can remove order by c4, c5 if you are not interested in the 1st row of that order.
Does aggregation do what you want?
select c1, c2, c3, max(c4), max(c5)
from t
group by c1, c2, c3;
This does not guarantee that c4 and c5 come from the same row, but it does guarantee that the triple c1/c2/c3 appears only once.

SQL combine basic order by and custom order by

I am using Oracle database and I am trying to combine a basic Order By and a custom one in one of my query.
Here's my table :
table1
-----------------
C1 | C2 | C3 | C4
I am trying to order it like that :
SELECT C1,C2,C3,C4 FROM table1
ORDER BY C1, C2, C3, (
CASE C4
WHEN C4 = 'value1' THEN 1
WHEN C4 = 'value2' THEN 2
WHEN C4 = 'value3' THEN 3
END
)
But I'm getting "Missing keyword" and I can't find which one, any ideas?
You can try
SELECT C1,C2,C3,C4 FROM table1
ORDER BY C1, C2, C3, (
CASE
WHEN C4 = 'value1' THEN 1
WHEN C4 = 'value2' THEN 2
WHEN C4 = 'value3' THEN 3
END
)
OR
SELECT C1,C2,C3,C4 FROM table1
ORDER BY C1, C2, C3, (
CASE C4
WHEN 'value1' THEN 1
WHEN 'value2' THEN 2
WHEN 'value3' THEN 3
END
)

How to write conditional select insert statement to fetch record from a table, and based on some particular column's values

for eg:
Table1:
c1,c2,c3 (1,10,'123')
Table2
c1,c2,c3,c4
Now I want to select record from table1 and if c3 column value starting two digit is 12 then I have to populate AA, and if 34 then BB in Table2 c4 column:
Table1 -- (1,10,'123')
Table2 -- (1,10,'123','AA')
Table1 -- (1,10,'3444')
Table2 -- (1,10,'3444','BB')
OP has provided additional information in Comments under the original post. In particular, c3 is VARCHAR2 and always begins with either 12 or 34.
The following should work. If c3 does not begin with 12 or 34, then the value inserted in column c4 will be NULL. That is the default behavior of CASE expressions, so it doesn't need to be coded explicitly; if instead something like 'ZZ' is desired in that case, then we can add an else clause to CASE. (The OP said c3 always begins with either 12 or 34; if so, this is a moot point.)
insert into table2 ( c1, c2, c3, c4 )
select c1, c2, c3,
case when c3 like '12%' then 'AA'
when c3 like '34%' then 'BB'
end
from table1
;
Demo:
SQL> create table table1 ( c1 number, c2 number, c3 varchar2(25) );
Table created.
SQL> insert into table1 ( c1, c2, c3 ) values ( 1, 10, '123' );
1 row created.
SQL> insert into table1 ( c1, c2, c3 ) values ( 1, 10, '3444' );
1 row created.
SQL> commit;
Commit complete.
SQL> select * from table1;
C1 C2 C3
---------- ---------- -------------------------
1 10 123
1 10 3444
2 rows selected.
Then:
SQL> create table table2 ( c1 number, c2 number, c3 varchar2(25), c4 varchar2(10) );
Table created.
SQL> select * from table2;
no rows selected
SQL> insert into table2 ( c1, c2, c3, c4 )
2 select c1, c2, c3,
3 case when c3 like '12%' then 'AA'
4 when c3 like '34%' then 'BB'
5 end
6 from table1
7 ;
2 rows created.
SQL> select * from table2;
C1 C2 C3 C4
---------- ---------- ------------------------- ----------
1 10 123 AA
1 10 3444 BB
2 rows selected.