SQL - need help creating query [closed] - sql

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

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

SQL Query to output single column with below values in each row - 1 2 2 3 3 3 4 4 4 4 [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 10 months ago.
Improve this question
Need to write SQL query for getting below output with the given input table:
Input Table:
Col1
1
2
3
Output:
Col1
1
2
2
3
3
3
WITH CTE(NN)AS
(
SELECT 1 UNION ALL
SELECT 2 UNION ALL
SELECT 3 UNION ALL
SELECT 4 UNION ALL
SELECT 5
)
SELECT C.NN
FROM CTE AS C
CROSS JOIN CTE C2
WHERE C2.NN<=C.NN
CTE is your input table

how to get diff between subsequent rows without lag [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 have
id value
1 12
1 15
1 17
1 22
1 22
1 23
And I need like this
id value
1 --
1 3
1 2
1 5
1 0
1 1
Could you tell me, how to achive this?
You can try the below -
select id,
value-max(value) over(order by id rows between 1 preceding and 1 preceding) as value
from tablename
You seem to want lag(), which I'm guessing is per id and based on the ordering of value:
select t.*,
(value - lag(value) over (partition by id order by value)) as diff
from t
order by value;
That said, your sample data has exact duplicates. That is unusual in a SQL table.

How to Update tables based on self join [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 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

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