I have 2 columns
Config Plant
------ -----
ROC DEP1
RET DEP1
ROC UAP1
RET UAP1
SSL UAP1
ROC PLP2
RET PLP2
How to get records of config which is same for all plants.
In this example result should be
ROC
RET
SELECT Config
FROM theTable
GROUP BY Config
HAVING COUNT(*) = (SELECT COUNT(DISTINCT(Plant)) FROM theTable)
SQL Fiddle
Related
I have a table table1 with these columns:
region (varchar)
surface (float8)
decision (bool)
I want to select AVG(surface) for the cases where decision is True and where it is False and group the result by region.
I finally want 3 columns :
region
the average surface of surfaces where decision is true
the average surface of surfaces where decision is false
I tried :
SELECT
region,
(SELECT AVG(surface_m2) FROM table1 WHERE avis_final_bri),
(SELECT AVG(surface_m2) FROM table1 WHERE avis_final_bri)
FROM
table1
GROUP BY
region
but the query does not work.
I also tried to define another table the WITH statement but it did not work. I tried with the JOIN but it failed as well.
You can use the FILTER clause:
demo:db<>fiddle
SELECT
region,
AVG(surface) FILTER (WHERE decision = true),
AVG(surface) FILTER (WHERE decision = false)
FROM
table1
GROUP BY region
Alternatively to use more common SQL, you can use the CASE clause:
SELECT
region,
AVG(CASE WHEN decision = true THEN surface END),
AVG(CASE WHEN decision = false THEN surface END)
FROM
table1
GROUP BY region
I am new to postgres.
My postgres table name is Vehicle consisting of following columns
1.ID
2.name
3. wheel (2,3,4,6,8) // two wheeleer,4 whellers
4. region ('hyderabad','mumbai','delhi',...)
5. polluted ('yes','no')
My query is how to select count of 4 wheeler vehicles which are polluted group by regions
Expected Output
hyderabad -> 4
mumbai -> 3
delhi -> 8,...
Ideally you should have a regions table somewhere which contains all regions. Assuming this, you could write the following query:
SELECT
r.region,
COALESCE(v.cnt, 0) AS count
FROM regions r
LEFT JOIN
(
SELECT region, COUNT(*) cnt
FROM Vehicle
WHERE wheel = 4 AND polluted = 'yes'
GROUP BY region
) v
ON r.region = v.region;
If you only have a Vehicle table, which is bad database design, then we can try the following query:
SELECT
region,
SUM(CASE WHEN wheel = 4 AND polluted = 'yes' THEN 1 ELSE 0 END) AS count
FROM Vehicle
GROUP BY region;
This is inefficient, but at least it would let you report every region even if it has no matching records.
I have a UDTF that will always return 1 row of 6 columns
The UDTF has one parameter
I want the 5 columns included in the result set of a query. The table I'm querying has a column that I want to use as the parameter for each row
I have not been able to figure out the correct syntax.
Any suggestions?
The UDTF
create function xxxx.UF_yyyyyy(USERID CHAR(10))
returns table (
p2User char(10),
STATUS CHAR(3),
USED DEC(7, 0),
CREATED DEC(7, 0),
SIGNON DEC(7, 0),
EXCLUDE DEC(7, 0))
language RPGLE
NOT DETERMINISTIC
NO SQL
DISALLOW PARALLEL
NOT FENCED
EXTERNAL NAME 'xxxx/UF_yyyyyy'
PARAMETER STYLE DB2SQL
example
select * from table(xxxx/UF_yyyyyy(CHAR('CMFIRST '))) a
result
P2USER STATUS USED CREATED SIGNON EXCLUDE
---------- ------ ------- ------- ------- -------
CMFIRST ACT 1170926 1150826 1170926 0
Here is an example of a select I tried
SELECT T1.AQABVN, T1.AQA8TX,
(SELECT COUNT(*) FROM fffff T4 WHERE T4.BDABVN = T1.AQABVN) AS SACCMS,
t2.p2User, t2.used
FROM
zzzzz T1
full join table(xxxx.UF_yyyyyy(T1.AQABVN)) t2 on T1.AQABVN = t2.p2User
Result
[SQL0205] Column P2USER not in table T2 in *N.
Got it working
SELECT T1.AQABVN, T1.AQA8TX,
(SELECT COUNT(*) FROM fffff T4 WHERE T4.BDABVN = T1.AQABVN) AS SACCMS,
t2.status, t2.used, t2.created, t2.signon, t2.exclude
FROM
zzzzz T1
join
table(SMLFQA.UF_XAJKUPR(T1.AQABVN)) t2 on T1.AQABVN = t2.p2User
DNO DNAME
----- -----------
1 Research
2 Finance
EN ENAME CITY SALARY DNO JOIN_DATE
-- ---------- ---------- ---------- ---------- ---------
E1 Ashim Kolkata 10000 1 01-JUN-02
E2 Kamal Mumbai 18000 2 02-JAN-02
E3 Tamal Chennai 7000 1 07-FEB-04
E4 Asha Kolkata 8000 2 01-MAR-07
E5 Timir Delhi 7000 1 11-JUN-05
//find all departments that have more than 3 employees.
My try
select deptt.dname
from deptt,empl
where deptt.dno=empl.dno and (select count(empl.dno) from empl group by empl.dno)>3;
here is the solution
select deptt.dname
from deptt,empl
where deptt.dno=empl.dno
group by deptt.dname having count(1)>3;
select
*
from departments d
inner join (
select dno from employees group by dno having count(*) > 3
) e on d.dno = e.dno
There are many approaches to this problem but almost all will use GROUP BY and the HAVING clause. That clause allows you to filter results of aggregate functions. Here it is used to choose only those records where the count is greater than 3.
In the query structure used above the group by is handled on the employee table only, then the result (which is known as a derived table) is joined by an INNER JOIN to the departments table. This inner join only allows matching records so this has the effect of filtering the departments table to only those which have a count() of greater than 3.
An advantage of this query structure is fewer records are joined, and also that all columns of the departments table are available for reporting. Disadvantage of this structure is the the count() of employees per department isn't visible.
I have table called temp_table which consist of following rows:
cola colb result
----------------
p4 s1 0
p8 s1 0
p9 s1 0
p5 f1 0
p8 f1 0
Now I need to update result column with the count(*) of colb. For which i am trying the following query:
update tem_table
set result = x.result
from tem_table tt
inner join(select colb,count(*) as result from tem_table group by colb) x
on x.colb = tt.colb;
And selecting distinct colb and result from temp_table:
select distinct colb,result from tem_table;
Getting output:
colb result
-----------
s1 3
f1 3
But the expected output is:
colb result
-----------
s1 3
f1 2
I am not getting where I am getting wrong in my query? Please help me.Thanks
You should not repeat the table to be updated in the from clause. This will create a cartesian self join.
Quote from the manual:
Note that the target table must not appear in the from_list, unless you intend a self-join (in which case it must appear with an alias in the from_list)
(Emphasis mine)
Unfortunately UPDATE does not support explicit joins using the JOIN keyword. Something like this should work:
update tem_table
set result = x.result
from (
select colb,count(*) as result
from tem_table
group by colb
) x
where x.colb = tem_table.colb;