Combine the distinct column of same table in db2 [closed] - db2

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I want to combine distinct column of an one table, I want a column wise distinct.
SELECT ALT_SRC_HOST FROM ATABLE WHERE ALT_SRC_HOST IS NOT null
UNION
SELECT SOURCE_IP AS SOURCE_IP FROM ATABLE WHERE SOURCE_IP IS NOT null
output is coming, in one column. I want the output is in two column
expected output
ALT_SRC_HOST SOURCE_IP
- 10.262.737.21
1.34.34.112

Not sure why you want it in a single SQL but this is an option
select distinct ALT_SRC_HOST as col1 , NULL as col2
from ATABLE
union all
select distinct NULL as col1 , SOURCE_IP as col2
from ATABLE

Related

Is it more efficient to bind parameters or use excluded in an upsert query? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 days ago.
Improve this question
I have a table I need to write an upsert statement for. Since I'm using Go the query kinda looks like this:
INSERT
INTO
my_table(
col1,
col2
)
VALUES (
$1,
$2
)ON
CONFLICT(col1) DO
UPDATE
SET
col2 = $2
I'm just wondering if there's any benefit to instead use the following query:
INSERT
INTO
my_table(
col1,
col2
)
VALUES (
$1,
$2
)ON
CONFLICT(col1) DO
UPDATE
SET
col2 = EXCLUDED.col2
Thanks in advance!

Postgresql how to use union for two selects in the same table? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have one table:
id
created_at
author
1
02.02.2020
manager
2
02.02.2020
client
3
02.02.2020
manager
I want to get result like below, and date sorted:
date
manager
client
02.02.2020
2
1
03.02.2020
5
3
I tried:
SELECT created_at::date, count(id) AS manager FROM orders where author = 'manager' GROUP BY created_at::date
union
SELECT created_at::date, count(id) AS client FROM orders where author = 'client' GROUP BY created_at::date
And I got:
date
manager
03.02.2020
2
02.02.2020
5
You don't need a union, this can be done in a single query using a filtered aggregate:
select created_at::date,
count(*) filter (where author = 'manager') as managers,
count(*) filter (where author = 'client') as clients
from orders
group by created_at::date
order by created_at::date

what is getting wrong with query ? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
INSERT INTO EMP_COMPANY(ename,cname,salary,jdate) VALUES
('ANIL','ACC',1500.00,'01-MAY-89'),
........
('AMOL','ACC',1000.00,'17-MAR-95');
Error: ORA-00933: SQL command not properly ended .
what is missing in syntax ?
You cannot multiple records like the way you are doing. You can try like this:
INSERT INTO EMP_COMPANY(ename,cname,salary,jdate)
select 'ANIL','ACC',1500.00,'01-MAY-89' from dual
union all
select 'SHANKAR','TATA',2000.00,'10-MAY-90' from dual
union all
select 'JAYA','CMC',1800.00,'7-JULY-91' from dual
union all
select 'SUNIL','CMC',1700.00,'1-JAN-88' from dual
union all
select 'VIJAY','TATA',5000.00,'3-JAN-88' from dual
union all
select 'PRAKASH','TATA',3000.00,'27-MAY-89' from dual
There is one more option to use INSERT ALL like this:
INSERT ALL
INTO EMP_COMPANY (ename,cname,salary,jdate) VALUES ('ANIL','ACC',1500.00,'01-MAY-89')
INTO EMP_COMPANY (ename,cname,salary,jdate) VALUES ('SHANKAR','TATA',2000.00,'10-MAY-90')
INTO EMP_COMPANY (ename,cname,salary,jdate) VALUES ('JAYA','CMC',1800.00,'7-JULY-91')
...........
SELECT 1 FROM DUAL;

Implementing the relational algebra division operator in postgresql [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Can some one guide in implementing the relational algebra division operator using sorting with syntactic support in postgresql?
Edit: This is to deal with the source code of postgresql. I need to add the division functionality in Postgresql.
Not sure what you're meaning with sorting syntactic support, but the relational division operator seems like a mere convoluted join:
select a_.id
from (select a.id, array_agg(a.b_id) as b_ids from a group by a.id) as a_
join (select array_agg(b.id) as b_ids from b) as b_ on b_.b_ids <# a_.b_ids
http://en.wikipedia.org/wiki/Relational_algebra#Division_.28.C3.B7.29
denis=# create table a (id int, b_id int);
CREATE TABLE
denis=# create table b (id int);
CREATE TABLE
denis=# insert into a values (1,1), (1,2), (1,3), (2,1), (2,3), (3,1), (3,2);
INSERT 0 7
denis=# insert into b values (1), (2);
denis=# select a_.id from (select a.id, array_agg(a.b_id) as b_ids from a group by a.id) as a_ join (select array_agg(b.id) as b_ids from b) as b_ on b_.b_ids <# a_.b_ids;
id
----
1
3
(2 rows)

print the 1 to 10 numbers using Table [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I want to print the 1 to 10 numbers based on table.
Table1
A
null
outout is
1
2
3
4
.
.
10
I have a table that table contains only one record...using this table i want to print 1 to 10 numbers please tell me
I written like this...is there any alternate way to write this
select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 from table1;
or
select generate_series(1,10) from table1;
Because your question is almost devoid of useful information, I don't feel bad simply presenting a link to the docs. Take a look at generate_series()
http://www.postgresql.org/docs/current/static/functions-srf.html