Which Postgres query is equivalent to the following Oracle query?
select CID,
max(cus.customer_id) keep (dense_rank first order by decode(acct.CUS_Type,'X',4,'M',5,'F',6,'G',7,'I',5,6), start_date) best_CUS
from cusomer cus
group by CID ;
Related
I have 3 tables in PostgreSQL.
the first table I have watertracker
1)tablename: watertracker
2)tablename:weighttracker
tablename: handwashtracker
I want to show below data in one query:
where cid is 86 and date is 2020-08-30 of all table data.
To select data from all tables use join clause in the select statement as follows:
select * from watertracker wt
join weighttracker wet on wet.id=wt.cid
join handwashtracker ht on ht.id=wt.cid
where wt.cid=86 and wt.wt_date::date='2020-08-30';
Suppose I have a query which says
Select * from (
select coalesce(mytable.created_date,mytable1.created_date) as created_date,...
from mytable
left join mytable1 ON (mytable.id=mytable1.id)
--Other Joins and tables here
) as foo
where created_date > CURRENT_DATE
Will Postgres select only the rows where created_date is > CURRENT_DATE for inner query joins where I am joining many tables?
Or will it take all rows from mytable and make joins with other tables on inner query, then check for created_date > CURRENT_DATE.
Is my previous query the same as
select coalesce(mytable.created_date,mytable1.created_date),... from mytable
left join mytable1 ON (mytable.id=mytable1.id)
--Other Joins and tables here
WHERE
coalesce(mytable.created_date,mytable1.created_date) > CURRENT_DATE
As you can see when you use EXPLAIN, the optimizer can “flatten” such subqueries, so that the execution plans for these two queries will be the same.
In other words, the optimizer is able to push the WHERE condition into the subquery and the join, so that it can be executed first.
Moreover, if created_date happens to be a column of mytable1, PostgreSQL will deduce that created_date can never be NULL and perform an inner join rather than an outer join.
SELECT pl_id,
distinct ON (store.store_ID),
in_user_id
FROM plan1.plan_copy_levl copy1
INNER JOIN plan1._PLAN_STORE store
ON copy1.PLAN_ID = store .PLAN_ID;
while running this query in postgres server i am getting the below error..How to use the distinct clause..in above code plan 1 is the schema name.
ERROR: syntax error at or near "distinct" LINE 2: distinct ON
(store.store_ID),
You are missing an order by where the first set of rows should be the ones specified in the distinct on clause. Also, the distinct on clause should be at start of the selection list.
Try this:
SELECT distinct ON (store_ID) store.store_ID, pl_id,
in_user_id
FROM plan1.plan_copy_levl copy1
INNER JOIN plan1._PLAN_STORE store
ON copy1.PLAN_ID = store .PLAN_ID
order by store_ID, pl_id;
I want to write a SELECT statement to create a list of jobs containing the job_title and the mean value of the two fields MIN_SALARY and MAX_SALARY sorted from the highest mean value to the lowest.
I wrote this:
select job_title, MAX_SALARY, MIN_SALARY, count(*)
from HR.EMPLOYEES, HR.JOBS
order by count(*) desc
group by j.job_title;
but it gives error:
ORA-00933: SQL command not properly ended 00933. 00000 - "SQL command not properly ended"
I think that you should have this query
select job_title, MAX_SALARY, MIN_SALARY, count(*)
from HR.EMPLOYEES, HR.JOBS
group by job_title
order by count(*) desc
I have reordered the clauses and removed the spurious j
See - select
I have a table in a Postgres 9.3 database with a json column like this:
CREATE mytable (
mycolumn json
)
I would like to execute queries from a Java application that look like this:
SELECT
mycolumn->>'somefield',
count(*)
FROM
mytable
GROUP BY
mycolumn->>'somefield'
When I try to use a PreparedStatement like this:
SELECT
mycolumn->>?,
count(*)
FROM
mytable
GROUP BY
mycolumn->>?
I get the following error:
PSQLException: ERROR: column "mytable.mycolumn" must appear in the GROUP BY clause or be used in an aggregate function
It makes sense that this happens because Postgres cannot guarantee that the two positional parameters are the same.
Using psql, I can prepare a statement like this:
PREPARE mystatement AS
SELECT
mycolumn->>$1,
count(*)
FROM
mytable
GROUP BY
mycolumn->>$1
Is it possible to do this with JDBC?
No, this isn't possible. JDBC only has positional parameters, and therefor the PostgreSQL driver will render it as:
PREPARE mystatement AS
SELECT
mycolumn->>$1,
count(*)
FROM
mytable
GROUP BY
mycolumn->>$2
And as the value of $1 is not necessarily the same as $2, the parser of PostgreSQL will reject it as you are potentially not grouping on the same column.
The solution might be to do:
SELECT a.aColumnLabel, count(*)
FROM (
SELECT mycolumn->>? as aColumnLabel
FROM mytable
) a
GROUP BY a.aColumnLabel