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

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

Related

Doubts on Group By with ALias [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
SELECT
first_name || ' ' || last_name full_name,
SUM (amount) amount
FROM
payment
INNER JOIN customer USING (customer_id)
GROUP BY
full_name
ORDER BY amount;
Can i know how does this query works for group by full_name, by rigth the query should give error because of the order of the sql execution(https://stackoverflow.com/a/3841804/15279872). The output of the above query can be seen in this link (https://www.postgresqltutorial.com/postgresql-group-by/) under the Section 3 : Using PostgreSQL GROUP BY clause with the JOIN clause
Here is what the documentation says about GROUP BY in PostgreSQL:
In the SQL-92 standard, an ORDER BY clause can only use output column
names or numbers, while a GROUP BY clause can only use expressions
based on input column names. PostgreSQL extends each of these clauses
to allow the other choice as well (but it uses the standard's
interpretation if there is ambiguity). PostgreSQL also allows both
clauses to specify arbitrary expressions. Note that names appearing in
an expression will always be taken as input-column names, not as
output-column names.
SQL:1999 and later use a slightly different definition which is not
entirely upward compatible with SQL-92. In most cases, however,
PostgreSQL will interpret an ORDER BY or GROUP BY expression the same
way SQL:1999 does.
The “output column name” mentioned above can be an alias, so PostgreSQL allows aliases in the GROUP BY clause.

How to perform FULL JOIN, LEFT JOIN and RIGHT JOIN 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 2 years ago.
Improve this question
I am new to PostgreSQL. We have given an activity to perform FULL JOIN, LEFT JOIN, AND RIGHT JOIN. I have an answer but I am not sure if it is right. I need some help to correct me.
These are the questions and my answers.
Perform a FULL JOIN on staff and staff_ph TABLE WHERE region_id is 8 AND sort from the highest salary to the lowest salary.
SELECT *
FROM staff FULL JOIN staff_ph
ON staff.id = staff_ph.id
WHERE staff.region_id = 8
ORDER BY staff.salary DESC;
Perform a LEFT JOIN on staff and staff_ph TABLE WHERE region_id is 8 AND sort by last_name.
SELECT *
FROM staff LEFT JOIN staff_ph
ON staff.id = staff_ph.id
WHERE staff.region_id = 8
ORDER BY staff.last_name DESC;
Perform a RIGHT JOIN on staff and staff_ph TABLE WHERE region_id is 8 AND sort by gender.
SELECT *
FROM staff RIGHT JOIN staff_ph
ON staff.id = staff_ph.id
WHERE staff.region_id = 8
ORDER BY staff.gender DESC;
Your FULL JOIN and RIGHT JOIN don't make much sense, since you then filter out the rows where staff.region_id was fabricated as NULL. On the other hand, it seems to be the question, not your answer, that is to blame for this. Maybe the intention was for that condition to go in the ON, not the WHERE.

Combine the distinct column of same table in db2 [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 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

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;

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