SQL combine basic order by and custom order by - sql

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
)

Related

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;

postgresql - count distinct combination of three columns- order doesn't matter

I'm trying to count distinct combinations of three columns, order of the columns doesn't matter
sample :
a a a
a a b
a b a
b b a
b a b
the result I'm getting :
a a a 1
a a b 1
a b a 1
b b a 1
b a b 1
desired result
aaa 1
aab 2
bba 2
You can use an ordered array
select v[1], v[2], v[3], count(*) n
from tbl t
cross join lateral (
select array_agg(col order by col) v
from (
values (c1),(c2),(c3)
) t(col)
) s
group by v[1], v[2], v[3];
db<>fiddle
Maybe you can use checksums for getting the required result eg if it is really just combinations 'a' and 'b' that you are dealing with, you could convert the letters to integers (by calling the ASCII() function) and add these up so that you get a checksum.
TABLE
create table t (c1, c2, c3 ) as
select 'a', 'a', 'a' union all
select 'a', 'a', 'b' union all
select 'a', 'b', 'a' union all
select 'b', 'b', 'a' union all
select 'b', 'a', 'b' ;
Checksums
select c1, c2, c3, ascii( c1 ) + ascii( c2 ) + ascii( c3 ) as checksum
from t ;
-- output
c1 c2 c3 checksum
a a a 291
a a b 292
a b a 292
b b a 293
b a b 293
If this works for you, then you can use window functions eg
select c1, c2, c3, rc_ as rowcount
from (
select c1, c2, c3
, count(*) over ( partition by ascii( c1 ) + ascii( c2 ) + ascii( c3 ) order by 1 ) rc_
, row_number() over ( partition by ascii( c1 ) + ascii( c2 ) + ascii( c3 ) order by 1 ) rn_
from t
) sq
where rc_ = rn_ ;
-- output
c1 c2 c3 rowcount
a a a 1
a b a 2
b a b 2
See dbfiddle.
If you are dealing with strings that cannot easily converted to integers, you could create a mapping between the strings and integers, and implement the map_ as a view (so that it is easy to use in subsequent queries) eg
MAP
-- {1} find all distinct elements
-- {2} map each element to an integer
create view map_
as
select val_, rank() over ( order by val_ ) weight_
from (
select distinct val_
from (
select distinct c1 val_ from t union all
select distinct c2 from t union all
select distinct c3 from t
) all_elements
) unique_elements ;
Once you have this map, you can use its values for creating checksums (maybe also in a view) ...
Checksums
create view t_checksums_
as
select c1, c2, c3, c1weight + c2weight + c3weight as checksum
from (
select
c1, ( select weight_ from map_ where c1 = map_.val_ ) c1weight
, c2, ( select weight_ from map_ where c2 = map_.val_ ) c2weight
, c3, ( select weight_ from map_ where c3 = map_.val_ ) c3weight
from t
) valandweight ;
... and then, you can use the same query as before, for obtaining the final result - see dbfiddle.

toad formatter starts next line under trailing comments

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

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.

Select first row in each GROUP BY group

I have a requirement in my project that I have this data with me:
C1 | C2 | C3 | C4
A | B | 2 | X
A | B | 3 | Y
C | D | 4 | Q
C | D | 1 | P
Where C1, C2, C3 and C4 are columns name in Database
And I have need to show data like this
C1 | C2 | C3 | C4
A | B | 5 | X
C | D | 5 | Q
The answer to this is fairly simple. Just follow my solution below:
--CREATE THE SAMPLE TABLE
CREATE TABLE TABLE1 (C1 char(1) NULL, C2 char(1) NULL, C3 int NULL, C4 char(1) NULL);
GO
--INSERT THE SAMPLE VALUES
INSERT INTO TABLE1 VALUES ('A', 'B', 2, 'X'), ('A', 'B', 3, 'Y'), ('C', 'D', 4, 'Q'), ('C','D', 1, 'P');
GO
--SELECT SUM(C3) AND GROUP BY ONLY C1 AND C2, THEN SELECT TOP 1 ONLY FROM C4
SELECT
C1,
C2,
SUM(C3) AS C3,
(SELECT TOP(1) C4 FROM TABLE1 AS B WHERE A.C1 = B.C1) AS C4
FROM
TABLE1 AS A
GROUP BY
C1,
C2;
GO
--CLEAN UP THE DATABASE, DROP THE SAMPLE TABLE
IF EXISTS(SELECT name FROM sys.tables WHERE object_id = OBJECT_ID(N'TABLE1')) DROP TABLE TABLE1;
GO
Let me know if this helps.
Assuming you mean the first record ordered by c4 (grouped by c1 and c2), then this will work establishing a row_number and using max with case:
with cte as (
select *,
row_number() over (partition by c1, c2 order by c4) rn
from yourtable
)
select c1, c2, sum(c3), max(case when rn = 1 then c4 end) c4
from cte
group by c1, c2
SQL Fiddle Demo
However, if you don't want to order by c4, then you need some other column to ensure the correct order of the results. Without an order by clause, there's no guarantee on how they are returned.
I hope you choose 'X' and 'Q' as those rows where inserted first, while grouping C1 and C2.
I would suggest you to add an identity column in your table and work based on it as given below.
Table:
DECLARE #DB TABLE (ID INT IDENTITY(1,1),C1 VARCHAR(10),C2 VARCHAR(10),C3 INT,C4 VARCHAR(10))
INSERT INTO #DB VALUES
('A','B',2,'X'),
('A','B',3,'Y'),
('C','D',4,'Q'),
('C','D',1,'P')
Code:
SELECT A.*,B.C4
FROM (
SELECT C1,C2,SUM(C3) C3 FROM #DB
GROUP BY C1,C2) A
JOIN
(
SELECT C1,C2,C4 FROM (
SELECT *,ROW_NUMBER() OVER (PARTITION BY C1,C2 ORDER BY ID) [ROW]
FROM #DB) LU WHERE LU.ROW = 1) B
ON A.C1 = B.C1 AND A.C2 = B.C2
Result: