Calculate Discount amount Based on Quantity - sql

I have a table
SELECT PRD_CODE,PRD_MAKE,PRD_NAME FROM PRD_MST
SELECT PDI_DISC_QTY,PDI_DISC_PRICE FROM PRD_DISC_INF
select customerid,Productid,quantity from ShoppingCartItem
Records
PRD_MST
PRD_CODE PRD_MAKE PRD_NAME
----------- ---------------- ----------
4 mobile moto e
5 cycle hero
PRD_DISC_INF
PDI_DISC_QTY PDI_DISC_PRICE
----------------- ------------------
2.00 2.10
1.00 2.31
ShoppingCartItem
customerid Productid quantity
----------- ----------- -----------
1 5 5
The Problem is that
if a customer buy 5 mobile and price of single unit is 200 then how do i calculate discount amount base on PDI_DISC_QTY in table 'PRD_DISC_INF'
Need help i am unable to get the logic

Try this DEMO
select PDI_DISC_QTY *
(cast(round(((select quantity from ShoppingCartItem)/PDI_DISC_QTY),0) as int))
from PRD_DISC_INF ;

Related

Aggregation from multiple tables not counting

I got this selection:
select EN.NAME_COMP, EN.CODE_CODE_COMP, sum(IT.AMOUNT) as TOTAL_AMOUNT
from COMPANY EN, SALARY IT
where IT.NO_ACC = EN.NO_ACC
group by EN.NAME_COMP, EN.CODE_CODE_COMP, IT.AMOUNT
;
sum(IT.AMOUNT) must totalize all amount from NAME_COMP, but instead of this, it display every NAME_COMP separately and with separately amount, it not make his SUM
NAME_COMP CODE_CODE_COMP TOTAL_AMOUNT
------------------------------ -------------------- --------------
COMP 1 23 1500.5
COMP 1 23 500.5
COMP 4 12 2100.75

How to get the SUM of a column that is created from a join in SQL

I have 3 tables, PRODUCT_INVENTORY, CUSTOMER_INFORMATION, and SALES_ORDER.
Im using the following SQL to generate a Monthly revenue report
SELECT
PRODUCT_INVENTORY.UNIT_PRICE, SALES_ORDER.UNITS_SOLD , SALES_ORDER.SALE_DATE,
PRODUCT_INVENTORY.UNIT_PRICE * SALES_ORDER.UNITS_SOLD AS TOTAL_SALES
FROM PRODUCT_INVENTORY
INNER JOIN SALES_ORDER
ON PRODUCT_INVENTORY.PRODUCT_ID = SALES_ORDER.PRODUCT_ID
WHERE SALES_ORDER.SALE_DATE >= '01-JAN-09'
AND SALES_ORDER.SALE_DATE <= '31-JAN-09';
This is the data Im getting back.
UNIT_PRICE UNITS_SOLD SALE_DATE TOTAL_SALES
---------- ---------- --------- -----------
900 2 11-JAN-09 1800
1700 2 12-JAN-09 3400
My question is how do I add the values in the new TOTAL_SALES column and have it display something like this?
UNIT_PRICE UNITS_SOLD SALE_DATE TOTAL_SALES
---------- ---------- --------- -----------
900 2 11-JAN-09 1800
1700 2 12-JAN-09 3400
TOTAL_REVENUE
5200
You can use CTE or subquery
With CTE
As
(
Your query
)
Select sum(total_sales) as total_revenue ,
From CTE

get multiple column from both table by joining a table on the basis of other max table column

1) vendor table
--------------------------------------------
VENDid VENDname
--- -----
1 ABC
2 XYZ
3 WXY
2)purchase table
---------------------------------------------
VENDid Purchasedate
------ ------------
1 12-01-2012
1 10-11-2013
2 22-02-2014
2 11-04-2014
3 10-05-2014
3 11-06-2014
1 14-06-2014
output(list all rows of vendor table and only max(purchasedate) from purchase table)
---------------------------------------------
VENDid VENDname PurchaseDate
------- -------- -------------
1 ABC 14-06-2014
2 XYZ 11-04-2014
3 WXY 11-06-2014
i got some queries like to solve previous problem-
SELECT v.VendID, VendName, Max(PurchaseDate)
FROM vendor v
INNER JOIN purchase p
ON v.VendID = p.VendID
Group By v.VendID, VendName
select VENDid, VENDname,
(select top 1 purchaseDate from purchase p
where p.VENDid=v.VENDid order by purchaseDate desc) as 'Purchase date'
from Vendor v
Que. If i will add some more column in purchase table like -
2)purchase table
------------------------------------------
VENDid Purchasedate amount_paid
------ ------------ ------------
1 12-01-2012 10000
1 10-11-2013 20000
2 22-02-2014 15000
2 11-04-2014 30000
3 10-05-2014 80000
3 11-06-2014 17000
1 14-06-2014 28000
and i want amount_paid along with previous output like-
---------------------------------------------
VENDid VENDname PurchaseDate amount_paid
------- -------- ------------- -------------
1 ABC 14-06-2014 28000
2 XYZ 11-04-2014 30000
3 WXY 11-06-2014 17000
then what will be query..
You appear to be using SQL Server. If so, you can use cross apply:
select v.VENDid, v.VENDname, p.PurchaseDate, p.Amount_Paid
from Vendor v cross apply
(select top 1 p.*
from purchase p
where p.VENDid = v.VENDid
order by p.purchaseDate desc
) p ;

Creating a view with sums from multiple tables (Oracle SQL)

I'm trying to create a view that gets the sums of a couple of different rows in various tables. (I'm not sure how to explain this properly)
Here is how my tables are set out:
Visitors:
VISITORID FNAME LNAME PHONE HOTELID
---------- --------------- --------------- --------------- ----------
23 Bella Morgan 0394110625 3
Bookings:
BOOKINGID HOTELID ROOMNO BOOKINGDATE BOOKINGDAYS BEDANDBREA VISITORID
---------- ---------- ---------- ------------------- ----------- ---------- ----------
28 3 509 28-04-2013 00:00:00 3 Yes 23
Rooms:
ROOMNO HOTELID ROOMTYPE PRICE
---------- ---------- ------------------------- ----------
509 3 Double 700
Services:
SERVICEID SERVICENAME COST HOTELID
---------- -------------------------------------------------- ---------- ----------
1-CLTH Cloth Cleaning 14.95 1
2-RMSV Room Service 9.95 2
Booking_services:
SERVICEID BOOKINGID
---------- ----------
2-RMSV 32
1-CLTH 32
I want to create a view called bills that gives me the total of room cost and cost of all services.
To get the room price, the sum is rooms.price*bookings.bookingdays.
For the services, it's the sum of all the rows in the services table that match the SERVICEID in booking_services for the matching bookingID.
Currently there are more rows in all of the tables than I've shown (so it doesn't take up too much space on here) and I have a query but it's only showing 2 of the visitors that i'd like the total for. I know it's because of line 5, but I'm not sure how I can get it to calculate that as well as those who do not have a row in booking_services.
Here is that query:
CREATE VIEW bills AS
SELECT v.fname, SUM((r.price*b.bookingdays)+s.cost) AS total
FROM visitors v, rooms r, bookings b, services s, booking_services bs
WHERE v.visitorid = b.visitorid
AND
s.serviceid in(select bs.serviceid from booking_services where bs.bookingid = b.bookingid)
AND
b.roomno = r.roomno
GROUP BY v.fname;
Any help to get what I'm after (if this makes any sense) would be appreciated.
Here is the SQLFiddel Demo
You can try below query for your view:
Select v.fname, sum((r.price*b.bookingdays)+nvl(bso.cost,0))
From visitors v
Join bookings b
on v.visitorid = b.visitorid
Join rooms r
on b.roomno = r.roomno
left outer join (select bs.BOOKINGID,sum(cost) as cost
from booking_services bs
Join services s
on s.SERVICEID = bs.SERVICEID
group by bs.BOOKINGID) bso
on bso.BOOKINGID = b.BOOKINGID
GROUP BY v.fname;

Select column values not present in group by clause

I have a table consisting of customer purchase records having following columns
CUSTOMER TRANSDATE SALENUM STORE TRANSTYP PRICE
-------- --------- --------- ----- --------- ------
1 12-FEB-2013 777 O CASH 7.99
1 12-FEB-2013 777 O CASH 6.99
1 12-FEB-2013 778 O CARD 9.17
2 23-APR-2013 987 D CASH 7.65
1 15-MAY-2013 1098 T CARD 2.35
I want to aggregate over salenum i,e for each salenum, i want the total price, as well as the store, transtyp, transdate,customer values as these are same for a particular salenum
However if i use
select customer,transdate,salenum,store,transtyp,sum(price) from table1 group by salenum
Its obviously saying not a valid group by value. How to get the desired result?
SAMPLE RESULT:
CUSTOMER TRANSDATE SALENUM STORE TRANSTYP PRICE
-------- --------- --------- ----- --------- ------
1 12-FEB-2013 777 O CASH 15.98
1 12-FEB-2013 778 O CARD 9.17
All non-aggregated columns should be in the group by:
select customer,transdate,salenum,store,transtyp,sum(price)
from table1
group by customer,transdate,salenum,store,transtyp
SELECT CUSTOMER,TRANSDATE,SALENUM,STORE,TRANSTYP,SUM(PRICE)
FROM TABLE1
GROUP BY CUSTOMER,TRANSDATE,SALENUM,STORE,TRANSTYP
ORDER BY CUSTOMER
I using the order by clause because it will be in order and also in sequence.