How to Update tables based on self join [closed] - sql

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 5 years ago.
Improve this question
Update table based on self join on customer_po = ID
Table XYZ
ID Invoice Date Delivery_date Customer_po
123 01-01-2018 null null
125 10-01-2018 null 123
I want Output record like below in Oracle SQL
ID Invoice Date Delivery_date Customer_po
123 01-01-2018 01-01-2018 null
125 10-01-2018 01-01-2018 123

You need an update statement like this.
UPDATE xyz
SET Delivery_date =
(SELECT MAX (invoice_date)
FROM xyz
WHERE Customer_po = 123);
Note that I have used MAX to avoid the errors due to multiple values for Customer_po = 123

Related

how to SQL query to get below result: [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 4 days ago.
Improve this question
Example tables:
Table A Table B
A 1
B 2
C 3
Expected output:
A 1
A 2
A 3
B 1
B 2
B 3
C 1
C 2
C 3
You need a cross join between the two tables:
SELECT a.column_a, b.column_b
FROM a
CROSS JOIN b
ORDER BY a.column_a, b.column_b

Select in a select from another table [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 months ago.
Improve this question
I need to make a query to another table and add the result to the current one as a new record if there is no record with an id in it, but if the current table has a value with such an id then add the value.
table 1:
id
name
date
1
alex
2022-01-01
2
tom
2022-01-01
table 2:
id
name
pass
1
alex
111
3
mitch
222
expected result:
id
name
date
value
1
alex
2022-01-01
111
2
tom
2022-01-01
3
mitch
222
Its a merge results for both the tables so you query will be
select * from table1 full outer join table2 on table1.(some column name) = table2.(some column name)
here some column name represents you id in table1 and table2 respectively.
so your query will be
select * from table1 full outer join table2 on table1.id = table2.id

SQL - need help creating query [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 5 years ago.
Improve this question
Table: customer
Customer number PRODUCT_ID
---------------- -----
12345 1
23456 2
12345 3
23456 3
12345 4
I need to create a query that will list all customers having 2 or more PRODUCT_ID > 2
select c.customer_number, count(c.product_id) as product_count
from customer c
group by c.customer_number
having count(c.product_id) > 2
You should read through this explanation of how HAVING clause should be used in conjunction with aggregate functions: https://www.w3schools.com/sql/sql_having.asp

query solution sql [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 9 years ago.
Improve this question
i struggling with query:
select bl.REF_CORP_ID as [Dept id] , bl.REF_CORE_BUDGET_ID, rc.Col_1 , bl.SubmittedDate as [Created Date time] , bl.APPROVED_BY , bl.APPROVED_DATETIME , bl.APPROVAL_FROM
from TABLE1 bl right join TABLE2 rc on bl.REF_CORP_ID =rc.ref_corp_id AND bl.REF_CORE_BUDGET_ID =1
group by bl.REF_CORP_ID , REF_CORE_BUDGET_ID , Col_1 , SubmittedDate , APPROVED_BY , APPROVED_DATETIME , APPROVAL_FROM
result in following manner:
1 1 IT 2013-07-10 19:29:40.700
1 1 IT 2013-07-10 19:29:40.700
2 1 Sales NULL
2 1 Sales NULL
4 1 HR 2013-07-10 19:32:21.720
4 1 HR 2013-07-10 19:32:21.720
6 1 management 2013-07-10 20:24:29.890
but this is a i want:
1 1 IT 2013-07-10 19:29:40.700
2 1 Sales NULL
4 1 HR 2013-07-10 19:32:21.720
6 1 management 2013-07-10 20:24:29.890
i have two table in which i applied inner join but in one table have same record and second table have only department info with dept name
The crude solution to this issue is to add the keyword DISTINCT:
SELECT DISTINCT a,b,c,d
And this will remove rows which are exact duplicates. However, duplicate rows is almost always the sign of a logical error elsewhere in the query - it's better to fix that than to just bodge things with DISTINCT.

Change column in sql [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 9 years ago.
Improve this question
i have table Match:
ID (int)
ID_Match (int)
ID_Player (int)
Attend (bit)
Goals (int)
When i write
Select * from Match
i have this:
ID ID_Match ID_Player Attend Goals
1 69 1 10 1 2
2 70 1 11 0 0
And i want have this:
ID_Player ID_Match Attend Goals
10 1 1 2
Have you some idea?
try this :
select top 1 ID_Player ,ID_Match ,Attend ,Goals from table_name order by ID asc