How to execute a PostgreSQL with "WITH" statement? [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 need to execute a query with WITH statement. If I run it, my WITH data shows error. This is my sample query please guide me to execute it.
with
x1_fields as
(SELECT
x1.id as x_id,
x1.created_time as x_created,
sum(x1.value) as x_sum
FROM
xxxx1 x1
INNER JOIN xxxx2 x2 ON x2.id = x1.id
INNER JOIN xxxx3 x3 ON x3.id = x2.id
WHERE
x1.customer = 'microsoft'
GROUP BY
x1.id,
x1.created_time
)
SELECT
y.id as final_id,
x_t.sum as final_sum
FROM
yyyy as y
left join x1_fields as x_t on x_t.x_id = y.id
where
y.customer = 'microsoft'
and y.id = '123456'
I have try to run this query in DBeaver but I can't run this. But it works in SSRS. I need to check such queries in some SQL software and debug it. Please give me some instructions to solve this.

Since we don't know your database and table structure, we cannot find an error which might occure because of wrong relation names or something.
However: Syntax error is the , character after the WITH clause (before the final SELECT). You have to remove it.

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.

How to increase the speed sql query and get fastly record [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 2 years ago.
Improve this question
I have a large set of records in my database. I tried to run SQL query using where condition. I got the data and I show count of records also. It taking around 21 secs. I need to reduce the time.
Sql Query
select * from sample_rank where company_id = 1
Records Count
166270266
I added Index in db. but It's not fastly getting data. How to fix it and How to change the query
Assuming, going by your comments, that you only need to select two of the columns in your table:
SELECT col1, col2 FROM sample_rank WHERE company_id = 1;
then the following covering index is probably the best you can do here:
CREATE INDEX idx ON sample_rank (company_id, col1, col2);
The above index completely covers the entire query, meaning that, if used, your SQL engine can use the index alone to satisfy the entire query plan. I put "if" in bold, because depending on the cardinality of the data, the above index might not help the query run faster. For example, if you only have two company_id values, 1 and 2, with 50% of the record having each value, then your SQL engine might just decide that a full table scan would be faster than even resorting to use the index. As suggested in the comments, running EXPLAIN on your actual query would reveal more information.

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;

Msg 209, Level 16, State 1, Line 4 Ambiguous column name 'p_id' [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have the following query which is generating the error message
Msg 209, Level 16, State 1, Line 4 Ambiguous column name 'p_id'
Below is the offending query.
SELECT *
from (select playerperform.p_id,player.first_name, SUM (playerperform.score)as totalgoal, RANK() OVER(ORDER BY SUM(playerperform.score)DESC) Rnk
from playerperform,player
Group by p_id
)as a
WHERE Rnk = 1
Clearly you have a p_id column in both playerperform and player.
At the very minimum, change:
GROUP BY p_id
To
GROUP BY playerperform.p_id
It also seems suspicious that you are performing an implicit CROSS JOIN:
FROM playerperform,player
I suspect that should be an INNER JOIN with some condition, such as
FROM dbo.playerperform INNER JOIN dbo.player
ON playerperform.p_id = player.p_id
As an aside, p_id is a terrible name for a column...