Finding double mrp's in SQL - sql

I have table with productcode and mrp like
Pcode MRP
1 30
2 30
2 35
3 100
4 150
4 150
5 45
6 120
6 122
6 125
I want to find which productcodes have more than two mrp.
Thanks in advance.

If you get the count and use the having clause, you should get what you are looking for.
select pcode, count(pcode)
From tab
group by pcode
having count(pcode) > 1

Related

Update and delete at the same time Postgres

I have a table of this kind
Table of items with price under 100
Prod_ID
PRICE
1
45
2
67
3
89
4
91
5
23
6
98
Now I Want to write PostgresSQL command to update the price of each item increasing it by $10 and want to delete all the items with price more than $100
After the query, the table should be:
Prod_ID
PRICE
1
55
2
77
3
99
5
33
How can I do this?
You can use update first and then delete.
UPDATE your_table_name set PRICE=PRICE + 10;
DELETE FROM your_table_name WHERE PRICE > 100;

Finding Max Price and displaying multiple columns SQL

I have a table that looks like this:
customer_id item price cost
1 Shoe 120 36
1 Bag 180 50
1 Shirt 30 9
2 Shoe 150 40
3 Shirt 30 9
4 Shoe 120 36
5 Shorts 65 14
I am trying to find the most expensive item each customer bought along with the cost of item and the item name.
I'm able to do the first part:
SELECT customer_id, max(price)
FROM sales
GROUP BY customer_id;
Which gives me:
customer_id price
1 180
2 150
3 30
4 120
5 65
How do I get this output to also show me the item and it's cost in the output? So output should look like this...
customer_id price item cost
1 180 Bag 50
2 150 Shoe 40
3 30 Shirt 9
4 120 Shoe 36
5 65 Shorts 14
I'm assuming its a Select statement within a Select? I would appreciate the help as I'm fairly new to SQL.
One method that usually has good performance is a correlated subquery:
select s.*
from sales s
where s.price = (select max(s2.price)
from sales s2
where s2.customer_id = s.customer_id
);

SQL return row when sum value is null

I got two tables. One with a bill of material and one with purchasing orders. Now I want to display the full bill of material of a product with the total on order amounts from the table purchasing.
**Billofmaterials**
BOM_ID BOM_Prod_id BOM_item_id
1 5 11
2 5 13
3 6 11
4 6 15
5 6 20
Example prod_id (product id) 6 has 3 items (id 11, 15 and 20).
**Purchasing**
PU_ID PU_item_id PU_amount PU_status
1 11 100 On order
2 11 650 On order
3 11 40 Received
4 20 600 On order
5 8 10 On order
6 15 150 Received
Now i got the following SQL
SELECT
BOM_item_id,
SUM(DISTINCT purchasing.PU_amount) as total_on_order
FROM Billofmaterials
LEFT JOIN purchasing
ON Billofmaterials.BOM_item_id= purchasing.PU_item_id
AND purchasing.PU_status != 'Received'
AND BOM_prod_id = 6
GROUP BY BOM_item_id
This query returns the following:
**Query result**
BOM_item_id total_on_order
11 750
20 600
Because there is only one received purchase order for BOM_item_id 15 it doesn't return a value. Now i want to retun BOM_item_id 15 also but with a total_on_order as 0 like:
**Expected result**
BOM_item_id total_on_order
11 750
15 0
20 600
What SQL feature/function do I need to use to get the expected result?
You can try the below -
SELECT BOM_item_id,coalesce(total_on_order,0) as total_on_order
FROM Billofmaterials left join
(
select PU_item_id,SUM(purchasing.PU_amount) as total_on_order
from purchasing
where purchasing.PU_status != 'Received'
group by PU_item_id
) purchasing
ON Billofmaterials.BOM_item_id= purchasing.PU_item_id
where BOM_prod_id = 6

Combine multiple rows using SUM that share a same column value but has different other column values

I thought this would be a very simple query but for some reason, I can't seem to get the results I'm looking for. I have a table that has this structure. I just want a single entry for each account while summing the charges. I don't really care which date I keep, just one of them.
Account Charges Charges2 Date
1 100 50 1/1/2015
1 50 0 1/2/2015
2 50 0 2/4/2015
2 70 30 2/19/2015
3 100 0 1/12/2014
4 0 20 4/3/2015
4 40 20 4/9/2015
The result I want is:
Account Charges Charges2 Date
1 150 50 1/1/2015
2 120 30 2/4/2015
3 100 0 1/12/2014
4 40 40 4/3/2015
The result I currently get is:
Account Charges Charges2 Date
1 100 50 1/1/2015
2 70 30 2/19/2015
3 100 0 1/12/2014
4 40 40 4/9/2015
I thought this would be very simple and I tried below. But this doesn't sum them up, it just seems to return the rows where Charges2 is NOT 0.
SELECT Account, SUM(Charges) As TotCharges, SUM(Charges2) AS TotCharges2
FROM TABLE
GROUP BY Account
ORDER BY Account
You can apply the min() aggregate function to the date to limit the number of rows returned to one per account:
SELECT
Account,
SUM(Charges) AS TotCharges,
SUM(Charges2) AS TotCharges2,
MIN(Date) AS Date
FROM TABLE
GROUP BY Account
ORDER BY Account
Sample SQL Fiddle

selecting min from joined tables

I can't figure this question out by myself so I am wondering if you guys could help me. I tried to find my answer on google, stackoverflow ans several other locations but without any result..
I made a query:
SELECT leverancier.leveranciers_id,
medicijn.artikelnr,
medicijn.naam,
medicijn.in_voorraad,
medicijn.min_voorraad,
min(order_medicijn.inkoopprijs) AS price
FROM leverancier
INNER JOIN voorraadorder
ON leverancier.leveranciers_id = voorraadorder.leverancier_id
INNER JOIN order_medicijn
ON order_medicijn.voorraadorder_id = voorraadorder.voorraadorder_id
INNER JOIN medicijn
ON medicijn.artikelnr = order_medicijn.artikel_id
GROUP BY leverancier.leveranciers_id,
medicijn.artikelnr,
medicijn.naam,
medicijn.in_voorraad,
medicijn.min_voorraad
ORDER BY artikelnr
Which gives the following result:
Leveranciers_ID / artikelnr / naam in_voorraad / min_voorraad / price
2 1 Aspirine 100 50 0.74
1 1 Aspirine 100 50 0.75
2 2 Abacivr 200 180 4.50
4 2 Abacivr 200 180 4.00
4 3 Acetazolamide 100 90 1.20
5 4 Ciclovir 145 120 0.50
3 5 levoceterizine 125 120 2.00
Here comes the question:
What query would I need to get the same result but only with the records where the price is the lowest for each artikelnr. So the result would be like this:
Leveranciers_ID / artikelnr / naam in_voorraad / min_voorraad / price
2 1 Aspirine 100 50 0.74
4 2 Abacivr 200 180 4.00
4 3 Acetazolamide 100 90 1.20
5 4 Ciclovir 145 120 0.50
3 5 levoceterizine 125 120 2.00
If there is any additional information required to answer this question, please ask me.
Thanks in advance
One of the possible solutions: Adding one more inner SQL that computes the min price of each artikelnr and using the HAVING clause, which uses the inner SQL, to filter the result.
Cheers