My table contain below records.. Table Name : Cus1
Column: c1 c2 c3 c4 c5 c6 c7 c8 c9 c10 c11 c12 c13 c14 c15
Records: 1 2 2 3 4 4 5 6 6 6 7 7 8 8 9 -1st record
1 2 3 3 3 4 4 5 5 5 5 6 7 7 8 -2nd record
How to get result like below using above table:
Column: c1 c2 c3 c4 c5 c6 c7 c8 c9 c10 c11 c12 c13 c14 c15
Records: 1 2 3 4 5 6 7 8 9 NULL NULL NULL NULL NULL NULL -1st record
1 2 3 4 5 6 7 8 NULL NULL NULL NULL NULL NULL NULL -2nd record
its possible in DB2 using if condition ?
No idea what is meant by "using if condition", but perhaps the following may be of interest?
Setup:
create table cus1
( c1 dec(3), c2 dec(3), c3 dec(3), c4 dec(3), c5 dec(3)
, c6 dec(3), c7 dec(3), c8 dec(3), c9 dec(3), c10 dec(3)
, c11 dec(3), c12 dec(3), c13 dec(3), c14 dec(3), c15 dec(3)
)
;
insert into cus1 values
( 1, 2, 2, 3, 4, 4, 5, 6, 6, 6, 7, 7, 8, 8, 9)
, ( 1, 2, 3, 3, 3, 4, 4, 5, 5, 5, 5, 6, 7, 7, 8)
;
Use table data from setup to generate queries; each created here as a VIEW to be queried for the final result:
create view cus1_unpivot_view as
with
cus1_rn as
( select row_number() over() as rn
, a.*
from cus1 a
)
SELECT rn, cn, cn_val
FROM cus1_rn as C
, lateral
( values ( 1, C.c1 ), ( 6, C.c6 ), ( 11, C.c11 )
, ( 2, C.c2 ), ( 7, C.c7 ), ( 12, C.c12 )
, ( 3, C.c3 ), ( 8, C.c8 ), ( 13, C.c13 )
, ( 4, C.c4 ), ( 9, C.c9 ), ( 14, C.c14 )
, ( 5, C.c5 ), ( 10, C.c10 ), ( 15, C.c15 )
) AS X( cn, cn_val )
; /* create an unpivot view of columns to rows */
create view cus1_unmatched as
select a.rn, a.cn, b.cn_val
from cus1_unpivot_view as a
left join
( select distinct rn, cn_val
from cus1_unpivot_view as d
) as b
on a.rn=b.rn
and a.cn=b.cn_val
; /* Generate unmatched values to get NULL results */
create view cus1_pivot as
select rn
, max( case when cn=1 then cn_val end ) as c1
, max( case when cn=2 then cn_val end ) as c2
, max( case when cn=3 then cn_val end ) as c3
, max( case when cn=4 then cn_val end ) as c4
, max( case when cn=5 then cn_val end ) as c5
, max( case when cn=6 then cn_val end ) as c6
, max( case when cn=7 then cn_val end ) as c7
, max( case when cn=8 then cn_val end ) as c8
, max( case when cn=9 then cn_val end ) as c9
, max( case when cn=10 then cn_val end ) as c10
, max( case when cn=11 then cn_val end ) as c11
, max( case when cn=12 then cn_val end ) as c12
, max( case when cn=13 then cn_val end ) as c13
, max( case when cn=14 then cn_val end ) as c14
, max( case when cn=15 then cn_val end ) as c15
from cus1_unmatched
group by rn
; /* pivot row data back to columns with the NULLs */
Finally, query that last VIEW to omit the row-numbering and present the data; compare the results to the desired output:
select
c1 , c2 , c3 , c4 , c5
, c6 , c7 , c8 , c9 , c10
, c11 , c12 , c13 , c14 , c15
from cus1_pivot
order by rn
; -- the following is a likeness of a report from the above query:
....+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8....+...
C1 C2 C3 C4 C5 C6 C7 C8 C9 C10 C11 C12 C13 C14 C15
1 2 3 4 5 6 7 8 9 - - - - - -
1 2 3 4 5 6 7 8 - - - - - - -
******** End of data ********
Related
I want to get next column values from which I selected.
SELECT * FROM test
WHERE round > (
SELECT round from test
WHERE b1 = '9' AND b2 = '16' and b3 = '21'
)
ORDER BY round
LIMIT 1
I tried this sql code but it returns
more than one row returned by a subquery used as an expression error...
SELECT round from test
WHERE b1 = '9' AND b2 = '16' and b3 = '21'
this sql returns multiple rows and I want to get all next rows after this selected rows
if my data is looks like
round b1 b2 b3
1 9 16 21
2 2 13 24
3 9 16 21
4 5 14 22
I want to get
(2,2,13,24)(4,5,14,22)
Can I get help?? Thanks
demo:db<>fiddle
You can use lead() window function which puts the next value to the current record:
SELECT
next_round, next_b1, next_b2, next_b3
FROM (
SELECT
*,
lead(round) OVER w as next_round,
lead(b1) OVER w as next_b1,
lead(b2) OVER w as next_b2,
lead(b3) OVER w as next_b3
FROM t
WINDOW w AS (ORDER BY round)
) s
WHERE b1 = 9 AND b2 = 16 AND b3 = 21
Simplest way is to use (NOT) EXISTS():
SELECT *
FROM test t
WHERE NOT EXISTS (
SELECT round from test nx
WHERE nx.b1 = '9' AND nx.b2 = '16' and nx.b3 = '21'
AND nx.round >= t.round
);
This will retrieve all rows if no matching record(s) exist.
You can do it with a self join:
SELECT DISTINCT t1.*
FROM test t1 INNER JOIN test t2
ON t1.round > t2.round AND (t1.b1, t1.b2, t1.b3) <> (t2.b1, t2.b2, t2.b3)
WHERE (t2.b1, t2.b2, t2.b3) = (9, 16, 21)
ORDER BY t1.round
See the demo.
Results:
round
b1
b2
b3
2
2
13
24
4
5
14
22
I have structure like that(as example):
ID ClassId Name Parent
--------------------------------------
1 12 Boss
2 13 Manager1 1
3 13 Manager2 1
4 13 Manager3 1
5 14 SubManager1 3
6 15 UnderSubManager1 5
7 16 Worker1 2
8 16 Worker2 6
9 14 SubManager2 4
10 16 Worker3 9
Than, we have this:
Boss->Manager1->Worker1
Boss->Manager2->SubManager1->UnderSubManager1->Worker2
Boss->Manager3->SubManager2->Worker3
I need query, that give me a this reult:
Boss->Manager1->worker1
Boss->Manager2->worker2
Boss->Manager3->worker3
I try do this witch CTE using ClassId but with poor result :(
Assuming you want to show the 2 top levels (Boss, and ManagerX), and then the lowest level (WorkerX) -
create table #tmp (ID int, ClassID int, Name varchar(32), Parent int)
go
insert into #tmp (ID, ClassID, Name, Parent)
values
(1, 12, 'Boss', null)
, (2, 13, 'Manager1', 1)
, (3, 13, 'Manager2', 1)
, (4, 13, 'Manager3', 1)
, (5, 14, 'SubManager1', 2)
, (6, 15, 'UnderSubManager1', 5)
, (7, 16, 'Worker1', 2)
, (8, 16, 'Worker2', 6)
, (9, 14, 'SubManager2', 4)
, (10, 16, 'Worker3', 9)
go
with cte as (
select t.ID, t.ClassID, t.Name, t.Parent
, Path = cast(case when t.ClassID in (12, 13) then t.Name else '' end as varchar(max))
, NestLevel = 0
, IsWorker = case t.ClassID when 16 then 1 else 0 end
from #tmp t
where t.Parent is null
union all
select t.ID, t.ClassID, t.Name, t.Parent
, Path = cte.Path + cast(case when t.ClassID in (12, 13, 16) then '->' + t.Name else '' end as varchar(max))
, NestLevel = cte.NestLevel + 1
, IsWorker = case t.ClassID when 16 then 1 else 0 end
from #tmp t
inner join cte on t.Parent = cte.ID
)
select cte.Path
from cte
where cte.IsWorker = 1
order by cte.Path
drop table #tmp
go
The result:
Boss->Manager1->Worker1
Boss->Manager1->Worker2
Boss->Manager3->Worker3
Below is my table schema (I am using PostgreSQL 9.6.1)
my_table
------------
table_id SERIAL PRIMRY KEY,
A1 INTEGER,
A2 INTEGER,
A3 INTEGER,
A4 INTEGER,
A5 INTEGER,
A6 INTEGER,
A7 INTEGER,
A8 INTEGER,
A9 INTEGER,
A10 INTEGER,
A11 INTEGER,
A12 INTEGER,
A13 INTEGER,
A14 INTEGER,
A15 INTEGER,
A16 DOUBLE PRECISION,
A17 DOUBLE PRECISION
and i am trying to run below query in order to create a new table.
CREATE TABLE my_table_2 AS
SELECT
B1 AS C1
B3 AS C2,
B5 AS C3,
B7 AS C4 ,
B9 AS C5,
B11 AS C6 ,
B1 / CAST( NULLIF(B3, 0) AS FLOAT) AS C7,
B1 / CAST( NULLIF(B5, 0) AS FLOAT) AS C8,
B1 / CAST( NULLIF(B7, 0) AS FLOAT) AS C9,
B1 / CAST( NULLIF(B9, 0) AS FLOAT) AS C10,
B1 / CAST( NULLIF(B11, 0) AS FLOAT) AS C11,
B3 / CAST( NULLIF(B5, 0) AS FLOAT) AS C12,
B5 / CAST( NULLIF(B7, 0 )* NULLIF(B9,0) AS FLOAT) AS C13,
B2 AS C14 ,
B4 AS C15,
B6 AS C16,
B8 AS C17,
B10 AS C18,
B12 AS C19,
B2 / CAST(NULLIF(B4, 0) AS FLOAT) AS C20,
B2 / CAST(NULLIF(B6, 0) AS FLOAT) AS C21,
B2 / CAST(NULLIF(B8, 0) AS FLOAT) AS C22,
B2 / CAST(NULLIF(B10, 0) AS FLOAT) AS C23,
B2 / CAST(NULLIF(B12 , 0) AS FLOAT) AS C24
B4 / CAST(NULLIF(B6 , 0) AS FLOAT) AS C25
B6 / CAST(NULLIF(B8,0) * NULLIF( B10,0) AS FLOAT) AS C26
0.0 AS C27,
0.0 AS C28,
0.0 AS C29,
0.0 AS C30,
0.0 AS C31,
0.0 AS C32,
0.0 AS C33,
0.0 AS C34,
0.0 AS C35,
0.0 AS C36,
0.0 AS C37,
0.0 AS C38,
0.0 AS C39,
B13 AS C40
B14 AS C41,
B15 AS C42
FROM (
SELECT
table_id,
A1 - A2 + A3 AS B1,
A1 AS B2,
A4 - A5 AS B3,
A4 AS B4,
A6 - A7 AS B5,
A6 AS B6,
A8 - A9 AS B7,
A8 AS B8,
A10 - A11 AS B9,
A10 AS B10,
A12 - A13 AS B11,
A12 AS B12,
A14 AS B13,
A15 AS B14,
coalesce(coalesce(A16 * 100, 0) / NULLIF(A17, 0), 0) AS B15
FROM my_table
) v1;
I am running the above query as a background process using psql command
using below command
sudo -u postgres psql My_DB -X -a -f /tmp/test.sql > result.out 2>result.err &
Since my_table is a very huge table. But after sometime when I check result.err file. It has only one line saying
ERROR: integer out of range
For some sample ids in the WHere clause table gets generated without any issues but when i run it for entire table above error comes.
Can anyone explain why is this happening and how I can fix this?
Any of the + or - operations could cause this error, but the most likely cause is A16 * 100.
You can fix the problem by either avoiding operations that cause the overflow, or you could choose a data type like numeric or bigint that has a wider range of values.
check the multiplications
NULLIF(B7, 0 )* NULLIF(B9,0)
and
NULLIF(B8,0) * NULLIF( B10,0)
try to replace the order of calculation, the first division, then multiplication
( B5 / NULLIF(B7, 0) ) * NULLIF(B9,0) AS B15
( B6 / NULLIF(B8, 0) ) * NULLIF(B10,0) AS C26
Is there a native postgresql function that gives "IDs", based on the column.
column 1 column 2 id1 id2
aa AA 1 1
aa BB 1 2
bb BB 2 2
cc BB 3 2
cc CC 3 3
dd DD 4 4
I only want the "ID" to increment, when the value in the column changes. Otherwise, the "ID" should be the same.
SELECT o.column1, o.column2
, dense_rank() OVER (ORDER BY column1) AS id1
, dense_rank() OVER (ORDER BY column2) AS id2
FROM ordi o
;
I need to calculate the counts per city,state and country. For example if i have the following data in my table T1:
name city state country
------------------------------
name1 c1 s1 C1
name2 c1 s1 C1
name3 c2 s1 C1
name4 c3 s1 C1
name5 c4 s2 C1
name6 c5 s2 C1
name7 c5 s3 C1
name8 c12 s12 C2
the query should results in:
city state country citycount, statecount, countrycount
-------------------------------------------------------------------
c1 s1 C1 2 4 7
c2 s1 C1 1 4 7
c3 s1 C1 1 4 7
c4 s2 C1 1 2 7
c5 s2 C1 1 2 7
c5 s3 C1 1 1 7
c12 s12 C2 1 1 1
if i do group by and count then i need to write 3 different queries. But i want to do it in one query. please help.
You can use window functions, for example one solution could be this one:
SELECT DISTINCT city
,STATE
,country
,count(*) OVER (PARTITION BY city,STATE,country) AS citycount
,count(*) OVER (PARTITION BY STATE,country) AS statecount
,count(*) OVER (PARTITION BY country) AS countrycount
FROM T1
ORDER BY city
,STATE
,country
Please see a fiddle here.