Place product_id into a price range - sql

I would like to add for a given product_id a price range, in 500 increments. For example a product with the price of 450 should have the price range of 500 and a product with a price 2450 should have the price range of 2000.
Main table
product_id price
32828 2593
23224 456
34344 1000
58283 2420
43585 550
Output table
product_id price price_range
32828 2593 3000
23224 456 500
34344 1000 1000
58283 2420 2000
43585 550 600

you could use case when for manage the range as you prefer
select case when price between 0 and 500 then 500
when price between 501 and 600 then 600
when price between 601 and 1500 then 1000
when price between 1501 and 2500 then 2000
END range

Related

plsql sum another table values to joined tables

I have 3 tables.First table is student.Second is student_detail and last one special_codes.
student table
studentname | invoiceno |tax |invoiceamount
Paul 10 500 1950
Georghe 20 1000 6850
Mary 30 1500 1900
Messy 40 2000 7050
studentdetail
invoiceno | code | product | amount
10 101 pencil 100
10 102 rubber 350
10 103 bag 1500
20 108 wheel 5000
20 109 tv 1500
20 110 ps 300
20 111 mouse 50
30 103 bag 1500
30 105 keyboard 400
40 111 mouse 50
40 112 car 7000
I can join these two table like this and get result table
select s.studentname,s.tax,s.invoiceamount,st.product,sum(st.amount) from student s, studentdetail st
where s.invoiceno = st.invoiceno
group by
s.studentname,
s.tax,
s.invoiceamount,
st.product
result table
studentname tax invoiceamount product amount
Paul 500 1950 bag 1500
Paul 500 1950 pencil 100
Paul 500 1950 rubber 350
Messy 2000 7050 car 7000
Messy 2000 7050 mouse 50
Mary 1500 1900 bag 1500
Mary 1500 1900 keyboard 400
Georghe 1000 6850 mouse 50
Georghe 1000 6850 ps 300
Georghe 1000 6850 tv 1500
Georghe 1000 6850 wheel 5000
Last table is special codes.It contains only one column which is called code
specialcodes table
code
101
102
113
104
105
110
111
What i want to do is to look up studentdetail table and to find codes that are same in specialcodes.Then to sum amount values and write sum to result table as another column.Result table
should be like that
result table(final)
studentname tax invoiceamount product amount taxexclude
Paul 500 1950 bag 1500 450
Paul 500 1950 pencil 100 450
Paul 500 1950 rubber 350 450
Messy 2000 7050 car 7000 50
Messy 2000 7050 mouse 50 50
Mary 1500 1900 bag 1500 400
Mary 1500 1900 keyboard 400 400
Georghe 1000 6850 mouse 50 350
Georghe 1000 6850 ps 300 350
Georghe 1000 6850 tv 1500 350
Georghe 1000 6850 wheel 5000 350
You can use analytic functions rather than GROUP BY and aggregating:
select s.studentname,
s.tax,
invoiceamount,
SUM(d.amount) OVER (PARTITION BY s.invoiceno) AS inv_amt_calc,
d.product,
d.amount,
SUM(CASE WHEN c.code IS NOT NULL THEN d.amount END)
OVER (PARTITION BY s.invoiceno) AS taxexclude
from student s
INNER JOIN studentdetail d
ON s.invoiceno = d.invoiceno
LEFT OUTER JOIN specialcodes c
ON (c.code = d.code)
Note: You can (and should) calculate the invoice amount from the studentdetails table rather than duplicating the data in the student table and violating Third-Normal Form.
Which, for your sample data, outputs:
STUDENTNAME
TAX
INVOICEAMOUNT
INV_AMT_CALC
PRODUCT
AMOUNT
TAXEXCLUDE
Paul
500
1950
1950
rubber
350
450
Paul
500
1950
1950
pencil
100
450
Paul
500
1950
1950
bag
1500
450
Georghe
1000
6850
6850
tv
1500
350
Georghe
1000
6850
6850
wheel
5000
350
Georghe
1000
6850
6850
ps
300
350
Georghe
1000
6850
6850
mouse
50
350
Mary
1500
1900
1900
bag
1500
400
Mary
1500
1900
1900
keyboard
400
400
Messy
2000
7050
7050
mouse
50
50
Messy
2000
7050
7050
car
7000
50
If you really want a version using GROUP BY then:
SELECT s.studentname,
s.tax,
s.invoiceamount,
SUM(d.amount) OVER (PARTITION BY s.invoiceno) AS inv_amt_calc,
d.product,
d.amount,
t.taxexclude
FROM student s
INNER JOIN studentdetail d
ON s.invoiceno = d.invoiceno
LEFT OUTER JOIN (
SELECT invoiceno,
SUM(amount) AS taxexclude
FROM studentdetail
WHERE code IN (SELECT code FROM specialcodes)
GROUP BY
invoiceno
) t
ON s.invoiceno = t.invoiceno;
db<>fiddle here

sum tabel based on certain content

select
om.orderno,
od.product,
od.preorderqty,
(od.preorderqty * od.nto) as preordernet
from OrderMaster om
join OrderDetail od on od.orderid = om.id
and ((od.prodcode like '100-%') or (od.prodcode like '200-%'))
order by om.orderno,sod.prodcode
Current result:
ORDERNO PRODUCT PREORDERQTY PREORDERNET
1000 100-A 2 200
1000 100-B 2 300
1000 100-C 1 450
2000 100-A 3 300
2000 100-B 1 150
2000 200-A 2 900
3000 200-A 1 450
I would need help getting a script that:
Sums the orders based on specific content
Orders can contain products beginning with 300- etc.
Expected result:
ORDERNO PRODUCT PREORDERQTY PREORDERNET ORDERNET
1000 100-A 2 200 950
1000 100-B 2 300 950
1000 100-C 1 450 950
2000 100-A 3 300 1350
2000 100-B 1 150 1350
2000 200-A 2 900 1350
3000 200-A 1 450 450
As I understood from the expected output ,use sum as window function to get it,
select om.orderno
,od.product
,od.preorderqty
,(od.preorderqty * od.nto) as preordernet
,sum(od.preorderqty * od.nto) over (partition by om.orderno) as ordernet
from OrderMaster om
join OrderDetail od
on od.orderid = om.id
and ((od.prodcode like '100-%')
or (od.prodcode like '200-%'))
order by om.orderno,sod.prodcode

How to calculate a total for a given condition on each row?

I have dollar and euro currencies.
I want to calculate in the last column only euro prices.
SELECT
CUSTOMER,
PRODUCT,
PRICE,
CURRENCY,
FROM MORE.PRODUCTS
WHERE CUSTOMER = '1000'
CUSTOMER PRODUCT PRICE CURRENCY
1000 BIKE 100 €
1000 CAR 200 €
1000 BIKE 50 $
1000 CANON 120 €
1000 TRAIN 300 $
Example, I want SUM of € values only:
CUSTOMER PRODUCT PRICE CURRENCY TOTAL PRICE
1000 BIKE 100 € 420
1000 CAR 200 € 420
1000 BIKE 50 $ 420
1000 CANON 120 € 420
1000 TRAIN 300 $ 420
What is best way to do this?
I tried to use a subquery in the SELECT clause, but I was not able to get it to work.
Use an analytic function to calculate the total:
SELECT CUSTOMER,
PRODUCT,
PRICE,
CURRENCY,
SUM( CASE CURRENCY WHEN '€' THEN PRICE END ) OVER () AS TOTAL_PRICE
FROM MORE.PRODUCTS
WHERE CUSTOMER = '1000'
Output:
CUSTOMER PRODUCT PRICE CURRENCY TOTAL_PRICE
-------- ------- ----- -------- -----------
1000 BIKE 100 € 420
1000 CAR 200 € 420
1000 BIKE 50 $ 420
1000 CANON 120 € 420
1000 TRAIN 300 $ 420
SELECT CUSTOMER, SUM(TOTAL_PRICE)
FROM MORE.PRODUCTS
WHERE CUSTOMER = '1000' and CURRENCY = '€'
You have to filter for euro prices.

Order By in Postgres

I want to get the result set in the descending range order. Below is my Query
select quantity_range as quantity_range, count(*) as number_of_items,
sum(amount) as total_amount,
from (
select *,case
when quantity between 0 and 500 then '<=500'
when quantity between 501 and 525 then '501-525'
when quantity between 526 and 550 then '526-550'
when quantity between 551 and 575 then '551-575'
when quantity between 576 and 600 then '576-600'
when quantity between 601 and 625 then '601-625'
when quantity between 626 and 650 then '626-650'
when quantity between 651 and 675 then '651-675'
when quantity between 676 and 700 then '676-700'
when quantity between 701 and 725 then '701-725'
when quantity between 726 and 750 then '726-750'
else '>750' end as quantity_range
from Sales )
group by quantity_range order by quantity_range;
I want my Result set like:
<=500 100 100000.00
600-625 10 5000.00
>700 25 25000.00
How to get this ordering? If i give Order By clause then >700 coming at 2nd position.
Use RIGHT to get last number from string:
select quantity_range as quantity_range, count(*) as number_of_items,
sum(amount) as total_amount
from (
select *,case
when quantity between 0 and 500 then '<=500'
when quantity between 501 and 525 then '501-525'
when quantity between 526 and 550 then '526-550'
when quantity between 551 and 575 then '551-575'
when quantity between 576 and 600 then '576-600'
when quantity between 601 and 625 then '601-625'
when quantity between 626 and 650 then '626-650'
when quantity between 651 and 675 then '651-675'
when quantity between 676 and 700 then '676-700'
when quantity between 701 and 725 then '701-725'
when quantity between 726 and 750 then '726-750'
else '>750' end as quantity_range
from Sales ) as sub
group by quantity_range
order by RIGHT(quantity_range,3);
SqlFiddleDemo

Using case expression in a view statement

I have been searching for a long time how to do this, however due to the words involved when searching it is incredibly hard to find something close to what I am trying to find out!
How can I use the sql CASE expression within a create view?
Could someone please show me the correct syntax?
Below is how mine looks at the moment, but it is not working correctly.
create view vw_price as
select vehicle.price
(case when price between 0 and 999 then ‘0-999’
when price between 1000 and 1999 then ‘1000-1999’
when price between 2000 and 2999 then ‘2000-2999’
when price between 3000 and 3999 then ‘3000-3999’
when price between 4000 and 4999 then ‘4000-4999’
when price between 5000 and 5999 then ‘5000-5999’
when price between 6000 and 6999 then ‘6000-6999’
when price between 7000 and 7999 then ‘7000-7999’
when price between 8000 and 8999 then ‘8000-8999’
when price between 9000 and 9999 then ‘9000-9999’
end) as price_group from vehicle;
The below syntax should work to create the required view. I think you are just missing a comma before the case statement.
USE [<<<database_name>>>]
GO
CREATE VIEW vw_price
AS
select price,
(case
when price between 0 and 999 then '0-999'
when price between 1000 and 1999 then '1000-1999'
when price between 2000 and 2999 then '2000-2999'
when price between 3000 and 3999 then '3000-3999'
when price between 4000 and 4999 then '4000-4999'
when price between 5000 and 5999 then '5000-5999'
when price between 6000 and 6999 then '6000-6999'
when price between 7000 and 7999 then '7000-7999'
when price between 8000 and 8999 then '8000-8999'
when price between 9000 and 9999 then '9000-9999'
end) "price_group" from vehicle;
GO