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;
Related
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
);
I'm struggling to reorder the categories in an income group variable.
It is currently in alphabetical order but I wish to change it to be permanently in order of amount (so moving £100 000 and greater to the bottom)
Income Group | Freq. Percent Cum.
Under £20 000 | 11 9.73 9.73
£100 000 and greater | 7 6.19 15.93
£20 000 to 29 999 | 18 15.93 31.86
£30 000 to 49 999 | 38 33.63 65.49
£50 000 to 74 999 | 27 23.89 89.38
£75 000 to 99 999 | 12 10.62 100.00
Create a new variable and assign each of the categories a number. For example,
gen order = 1 if income=="Under £20 000"
replace order = 2 if income=="£20 000 to 29 999"
...
replace order = 6 if income=="£100 000 and greater"
sort order
drop order
If the income variable is numeric, rather than string, sub in the numeric values above.
This question already has answers here:
How to delete duplicate entries?
(16 answers)
Closed 6 years ago.
I have a table as :
id product id merchant id price upc
1 124 2 2000000 1234XDE
2 124 2 200000 1234XDE
3 124 2 200000 1234XDE
4 124 2 200000 1234XDE
5 124 2 200000 ASDER36
6 134 1 300 ASERT56
7 134 2 300 ASERT56
I want to delete all the multiple entries from the table.
Delete from
table where id not in (Select min(id) from table group by(merchant id))
but no success. I want resulting table as:
id product id merchant id price upc
1 124 2 2000000 1234XDE
5 124 2 2000000 ASDER36
6 134 1 300 ASERT56
7 134 2 300 ASERT56
Can someone help me in writing a query for this.
This should do it:
delete from flash
where id not in (select min(id)
from flash
group by product_id, merchant_id, upc);
SQLFiddle example: http://sqlfiddle.com/#!15/9edef/1
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
I have a table product, pick_qty, shortfall, location, loc_qty
Product Picked Qty Shortfall Location Location Qty
1742 4 58 1 15
1742 4 58 2 20
1742 4 58 3 15
1742 4 58 4 20
1742 4 58 5 20
1742 4 58 6 20
1742 4 58 7 15
1742 4 58 8 15
1742 4 58 9 15
1742 4 58 10 20
I want a report to loop around and show the number of locations and the quantity I need to drop to fulfil the shortfall for replenishment. So the report would look like this.
Product Picked Qty Shortfall Location Location Qty
1742 4 58 1 15
1742 4 58 2 20
1742 4 58 3 15
1742 4 58 4 20
Note that it is best not to think about SQL "looping through a table" and instead to think about it as operating on some subset of the rows in a table.
What it sounds like you need to do is create a running total that tells how many of the item you would have if you were to take all of them from a location and all of the locations that came before the current location and then check to see if that would give you enough of the item to fulfill the shortfall.
Based on your example data, the following query would work, though if Locations aren't actually numerics then you would need to add a row number column and tweak the query a bit to use the row number instead of the Location Number; It would still be very similar to the query below.
SELECT
Totals.Product, Totals.PickedQty, Totals.ShortFall, Totals.Location, Totals.LocationQty
FROM (
SELECT
TheTable.Product, TheTable.PickedQty, TheTable.ShortFall,
TheTable.Location, TheTable.LocationQty, SUM(ForRunningTotal.LocationQty) AS RunningTotal
FROM TheTable
JOIN TheTable ForRunningTotal ON TheTable.Product = ForRunningTotal.Product
AND TheTable.Location >= ForRunningTotal.Location
GROUP BY TheTable.Product, TheTable.PickedQty, TheTable.ShortFall, TheTable.Location, TheTable.LocationQty
) Totals
-- Note you could also change the join above so the running total is actually the count of only the rows above,
-- not including the current row; Then the WHERE clause below could be "Totals.RunningTotal < Totals.ShortFall".
-- I liked RunningTotal as the sum of this row and all prior, it seems more appropriate to me.
WHERE Totals.RunningTotal - Totals.LocationQty <= Totals.ShortFall
AND Totals.LocationQty > 0
Also - as long as you are reading my answer, an unrelated side-note: Based on the data you showed above, your database schema isn't normalized as far as it could be. It seems like the Picked Quantity and the ShortFall actually depend only on the Product, so that would be a table of its own, and then the Location Quantity depends on the Product and Location, so that would be a table of its own. I'm pointing it out because if your data contained different Picked Quantities/ShortFall for a single product, then the above query would break; This situation would be impossible with the normalized tables I mentioned.