HOW TO PRINT A TO Z ALPHABETS IN QUERY WITHOUT USING TABLE
For Oracle, here's one option:
SQL> select chr(level + 64) letter
2 from dual
3 connect by level <= ascii('Z') - ascii('A') + 1;
LETTER
----------
A
B
C
D
E
F
<snip>
X
Y
Z
26 rows selected.
SQL>
For Postgres:
select chr(code)
from generate_series(ascii('A'), ascii('Z')) as t(code)
order by code
select chr(generate_series(65,97));
output
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z
Related
What should be pig equivalent script of the below SQL query:
SELECT fld1, fld2, fld3, SUM(fld4)
FROM Table1
GROUP BY fld1, fld2, fld3;
For Table1:
A B C 2 X Y Z
A B C 3 X Y Z
A B D 2 X Y Z
A C D 2 X Y Z
A C D 2 X Y Z
A C D 2 X Y Z
OUTPUT:
A B C 5
A B D 2
A C D 6
Ref : https://pig.apache.org/docs/r0.11.1/basic.html#GROUP, you can
find a multi-group example
For your usecase below code should be suffice
A = load 'input.csv' using PigStorage(',') AS (fld1:chararray,fld2:chararray,fld3:chararray,fld4:long,fld5:chararray,fld6:chararray,fld7:chararray);
B = FOREACH(GROUP A BY (fld1,fld2,fld3)) GENERATE FLATTEN(group) AS (fld1,fld2,fld3), SUM(A.fld4) AS fld4_aggr;
DUMP B;
Given I have columns A B C D ….. Z
I want to Group-By on A, B , C Having Count(*) > 1 and then for each of those rows, I want to SELECT the rest of the Columns as well that were not included in the aggregate.
The result would be like
Occurrences A B C D E F G ------- Z
3 e e k q r y e ------- j
3 e e k f t d t ------- y
3 e e k w e q c ------- d
2 f r s w e q c ------- d
2 f r s w e q c ------- d
How can I do that?
You don't want GROUP BY, you want ORDER BY. To get the first column, you can use window functions, which are ANSI standard and supported by most databases:
select t.*
from (select count(*) over (partition by a, b, c) as occurrences,
t.*
from t
order by a, b, c
) t
where occurrences > 1;
Consider the following table
CREATE TABLE #temp
(
color1 VARCHAR(10),color2 VARCHAR(10),color3 VARCHAR(10)
)
INSERT INTO #temp
VALUES ('R','R','R'),
('R','R','B'),
('R','B','B'),
('R','R','G'),
('R','G','G'),
('B','B','B'),
('B','B','G'),
('B','G','G'),
('B','B','R'),
('B','G','R'),
('G','G','G'),
('G','G','B'),
('G','B','B'),
('G','G','R'),
('G','R','R')
I need the order the output such that the rows that contain values 'R' (in any of the three columns) should be the top of the result. Any suggestions ?
Note : The following query dosn't help.
SELECT *
FROM #temp
ORDER BY color1 DESC,color2 DESC,color3 DESC
The expected output is
color1 color2 color3
R R R
R R B
R B B
R R G
R G G
B B R
B G R
G G R
G R R
G G G
G G B
G B B
B B B
B B G
B G G
Thanks in advance.
SELECT *
FROM TEMP
ORDER BY CASE WHEN Color1='R' OR Color2='R' OR Color3='R' THEN 0 ELSE 1 END
my table looks like:
column 1 | column 2
--------------------
A L
B M
C N
D O
E P
F Q
G R
H S
I T
J U
K V
Now I want to "concatenate" fractions of n-rows (n is variable) together.
For example:
-) if n = 4, the output should be:
column 1 | column 2 | column 1 | column 2 | column 1 | column 2
A L E P I T
B M F Q J U
C N G R K V
D O H S
-) if n = 10, the output should be:
column 1 | column 2 | column 1 | column 2
A L K V
B M
C N
D O
E P
F Q
G R
H S
I T
J U
I thought about using a temp table and a loop. After every iteration, I select fractions of n-rows and concatenate them together (with union). However, I think this solution could be a little slow for large datasets.
Maybe someone of you knows a better approach?
I would really appreciate if someone could help me with this.
Kind Regards
Bernhard
this is not complete
pivot is left,i was thinking of doing something like this,
Declare #n int=4
Declare #j int
Declare #k int
Select #j=count(*) from #t
if(#j%#n=0)
set #k= (#j/#n)
else
set #k= (#j/#n)+1
--select #k
;with CTE as
(
select *,ntile(#k)over(order by column1)rn from #t
)
select * from cte
Data : I have written two queries one is WITH and Other is SELECT and then self joining the table below, but both queries return different results, why it happens ?
table name is test_cur
ID_SOURCE_CUR ID_TARGET_CUR
------------- --------------
A B
B C
C D
D E
A Z
G A
K A
Q A
J J
K K
K L
L K
B A
Z A
So why the two queries below return different results ?
SELECT *
FROM test_cur tu, test_cur fu
WHERE tu.id_target_cur = 'A'
AND fu.id_source_cur = 'A'
AND tu.id_source_cur <> fu.id_target_cur;
returns 8 rows.
ID_SOURCE_CUR ID_TARGET_CUR ID_SOURCE_CUR_1 ID_TARGET_CUR_1
-------------- -------------- -------------- --------------
G A A B
K A A B
Q A A B
Z A A B
G A A Z
K A A Z
Q A A Z
B A A Z
And -
WITH qry1 AS
(SELECT *
FROM test_cur)
SELECT *
FROM qry1 tu, qry1 fu
WHERE tu.id_target_cur = 'A'
AND fu.id_target_cur = 'A'
AND tu.id_source_cur <> fu.id_target_cur;
returns 25 rows.
ID_SOURCE_CUR ID_TARGET_CUR ID_SOURCE_CUR_1 ID_TARGET_CUR_1
-------------- -------------- -------------- --------------
G A G A
G A K A
G A Q A
G A B A
G A Z A
K A G A
K A K A
K A Q A
K A B A
K A Z A
Q A G A
Q A K A
Q A Q A
Q A B A
Q A Z A
B A G A
B A K A
B A Q A
B A B A
B A Z A
Z A G A
Z A K A
Z A Q A
Z A B A
Z A Z A
Why ?
Your second query is different, you have a different WHERE clause. The first WHERE is :
WHERE tu.id_target_cur = 'A'
AND fu.id_source_cur = 'A'
AND tu.id_source_cur <> fu.id_target_cur;
The second is:
WHERE tu.id_target_cur = 'A'
AND fu.id_target_cur = 'A' -- this line is different, it should be fu.id_source_cur = 'A'
AND tu.id_source_cur <> fu.id_target_cur;
Change those and the results are the exact same on both queries.
The where clauses are different fu.id_source_cur = 'A' vs. fu.id_target_cur = 'A'