Calculation in query based on the condition - oracle

I have a package in oracle contains several procedures, in one of those procedures I need to calculate some fee based on specific condition like this:
if X_flag = 1
then
fee = (.5 * orders.total_count) + (.3 * orders.total_amount)
else
fee = (.7 * orders.total_count) + (.4 * orders.total_amount)
so, what is the better way to do that?
The procedure that I need to add this calculation on it :
procedure informatonRPT (
p_customerID in number,
p_orderID in number)
as
begin
select
cusromers.costomerId,
cusromers.costomerName,
cusromers.coustomerPhone,
orders.price
from cusromers
inner join orders
on cusromers.orderId = orders.orderID
when
p_customerID is null or p_customerID = cusromers.costomerId
and p_orderID is null or p_orderID = orders.orderID ;
end informatonRPT;
customers table columns:
customerID
custumerName
customerPhone
ordedID
order table columns:
orderID
price
total_Amount
total_count
Note that:
Fee column doesn't exist in the data base, I have to calculate it in query

Again Im not sure because you didnt provide enought information, but you need use a CASE expresion
SELECT C.costomerId,
C.costomerName,
C.coustomerPhone,
O.price,
CASE X_flag
WHEN 1 THEN (.5 * O.total_count) + (.3 * O.total_amount)
ELSE 7 * O.total_count) + (.4 * O.total_amount)
END as fee
FROM cusromers C
join orders O
on C.orderId = O.orderID
oracle CASE documentation

Related

PL/SQL error "not enough values" during "select into"

I'm working on creating a pl/sql function that finds the highest average of students from a list of classes. I have the average computation part working correctly; however, I need to return the results as a table of records and I'm running into the error while attempting to store the results into the record.
My record declaration is as follows
create or replace TYPE studentRec as object (
term varchar2(10),
lineNum number(4),
coTitle varchar2(50),
stuId varchar2(5),
average number);
The error comes when I'm trying to fill the record using a select into statement.
create or replace function highest_avg(stu_id scores.sid%type,
line_no scores.lineno%type)
return stuRecTab
as
stuRec stuRecTab;
average number;
studentRec_t studentRec;
begin
stuRec := stuRecTab();
select avg(points)
into average
from scores, courses
where scores.sid = stu_id
and scores.lineno = line_no
and scores.term = courses.term
and scores.lineno = courses.lineno;
SELECT DISTINCT c.term, c.lineno, cc.ctitle, s.sid, average
INTO studentRec_t
from courses c, class_catalog cc, scores s
where s.sid = stu_id
and s.lineno = line_no
and s.term = c.term
and s.lineno = c.lineno
and c.cno = cc.cno;
stuRec := studentRec_t;
return(stuRec);
end;
I've run it as just a query and I'm getting back what I expect so I'm not sure why this error is popping up. Any help would be greatly appreciated as this is my first time working with pl/sql.
First, you can't SELECT INTO the fields of an object instance variable - you have to create an object instance in your select, then SELECT that INTO your object instance variable. You can't simply assign an instance variable to a collection - you need to put it at an appropriate index. So what you end up with is something like:
create or replace function highest_avg(stu_id scores.sid%type,
line_no scores.lineno%type)
return stuRecTab
as
stuRec stuRecTab;
average number;
studentRec_t studentRec;
begin
stuRec := stuRecTab();
select avg(points)
into average
from scores s
inner join courses c
on c.term = s.term and
c.lineno = s.lineno
where s.sid = stu_id and
s.lineno = line_no;
SELECT studentRec(term, lineno, ctitle, sid, average)
INTO studentRec_t
FROM (SELECT DISTINCT c.term, c.lineno, cc.ctitle, s.sid, average
from scores s
INNER JOIN courses c
ON s.term = c.term and
s.lineno = c.lineno
INNER JOIN class_catalog cc
ON cc.cno = c.cno
where s.sid = stu_id and
s.lineno = line_no);
stuRec(1) := studentRec_t;
return(stuRec);
end;
As no test data was provided I haven't tested this - but at least it compiles at dbfiddle.

Oracle - Check if there is a data in range of date

I have three tables (receipts, receiptaddinfo, shops). I have select, that gives me a data with a all receipts from all shops since the declared date:
select *
from receipts r
join receipt receiptaddinfo ri on r.receiptid=ri.receiptid and r.shop=ri.shop
join shops s on ri.shop=s.shop and shoptype=0
where ri.creationtime >= '2016-05-19 00:00:00'
order by ri.creationtime desc
The table shops, contain all shops, however, I want to check if there is a shop, which had no 'sale/receipts' since the declared date. Could somebody help?
You can try the following SQL statement.
SELECT * from shops s
WHERE s.shoptype = 0
AND NOT EXISTS
(SELECT 1
FROM receipts r,
receiptaddinfo ri
WHERE r.receiptid = ri.receiptid
AND r.shop = ri.shop
AND ri.shop = s.shop
AND ri.creationtime >= '2016-05-19 00:00:00')

Query to get list of Inventory Items for which there is no material transaction in Oracle

Have a small doubt I want to compile a SQL query in Inventory where I have to get those Items for which transactions have not been recorded during a period of at least a specified number of days.
The days could be 30 days or 2 months depends. So I want to get those items for which no transaction was recorded for lets say 30 days. Could anyone give me an idea of how to go about this thing?? I am using r12. I came up with the following query but it is giving many records. The commented portions of this query remains commented only
select distinct msi.segment1, msi.description, msi.primary_uom_code,
msi.inventory_item_id
from mtl_system_items_b msi /*,
mtl_material_transactions mmt*/
where /*msi.inventory_item_id = mmt.inventory_item_id
AND msi.organization_id = mmt.organization_id
AND NVL((SELECT SUM(transaction_quantity)
FROM mtl_onhand_quantities
WHERE inventory_item_id = msi.inventory_item_id),
0) = 0
AND TRUNC(mmt.transaction_date) <= SYSDATE - &D
AND*/
not exists
(select *
from mtl_material_transactions mmt
where msi.inventory_item_id = mmt.inventory_item_id
and msi.organization_id = mmt.organization_id
and trunc(mmt.transaction_date) < sysdate - &D)
Here is a variation of your query that will give all items with on hand inventory that have not been transacted in a determined number of days.
select distinct msi.segment1
,msi.description
,msi.primary_uom_code
--,msi.inventory_item_id
,q.organization_id
,q.quantity
from mtl_system_items_b msi
join (SELECT inventory_item_id, organization_id, SUM(transaction_quantity) quantity
FROM mtl_onhand_quantities
GROUP BY inventory_item_id, organization_id) q on msi.inventory_item_id = q.inventory_item_id and msi.organization_id=q.organization_id
where not exists (select *
from mtl_material_transactions mmt
where msi.inventory_item_id = mmt.inventory_item_id
and msi.organization_id = mmt.organization_id
and trunc(mmt.transaction_date) > sysdate - 180)
order by q.organization_id,msi.segment1;

Display only the largest count in a group by statment

I'm trying to display only the largest group in this group by statement;
SELECT COUNT(type) AS booking, type FROM booking b, room r WHERE r.rno = b.rno AND r.hno = b.hno GROUP BY type;
I modified it so we get this query response now you can see group double is larger then family.
BOOKING TYPE
5 double
2 family
I know there is a HAVING keyword you can add in order display only a count compared to a number so I could do COUNT(type) HAVING > 2 or similar but that's not very dynamic and that would only work in this instance because I know the two amounts.
ORDER BY COUNT(type) DESC LIMIT 1
There isn't a having statement that does this. But you can use rownum with a subquery:
select t.*
from (SELECT COUNT(type) AS booking, type
FROM booking b join
room r
on r.rno = b.rno AND r.hno = b.hno
GROUP BY type
order by count(type) desc
) t
where rownum = 1;
Just order your query..
order by booking desc
regards
TRY this
SELECT COUNT(type) AS booking, type FROM booking b, room r WHERE r.rno = b.rno AND r.hno = b.hno ORDER BY type DESC LIMIT 1

Counting the total number of rows depending on a column value

I have a query that should count the total number of rows returned depending on a column value. For example:
As you can see, the M field should display the total number of rows returned which should be 5 because the FT_LOT are all the same value. Here is the query that I have so far:
SELECT DISTINCT
VBATCH_ID, MAXIM_PN, BAGNUMBER, FT_LOT
, m
, level as n
FROM
(
SELECT
VBATCH_ID, MAXIM_PN, BAGNUMBER, FT_LOT, QTY, DC, PRINTDATE, WS_GREEN, WS_PNR, WS_PCN, MSL, BAKETIME, EXPTIME
, una
, dulo
, (dulo - una) + 1 AS m
FROM
(
SELECT c.containername VBATCH_ID
,pb.productname MAXIM_PN
,bn.wipdatavalue BAGNUMBER
,ln.wipdatavalue FT_LOT
,aw.wipdatavalue QTY
,DECODE(ln.wipdatavalue,la.attr_081,la.attr_083
,la.attr_085,la.attr_087
,la.attr_089,la.attr_091
,la.attr_093,la.attr_095
,la.attr_097,la.attr_099
,la.attr_101,la.attr_103
,la.attr_105,la.attr_107
,la.attr_109,la.attr_111
,la.attr_113,la.attr_116
,la.attr_117,la.attr_119
) DC
,TO_CHAR(SYSDATE,'MM/DD/YYYY HH:MI:SS PM') PRINTDATE
,DECODE(UPPER(la.attr_158),'GREEN','HF',NULL) WS_GREEN
,DECODE(la.attr_140,NULL,NULL,'PNR') WS_PNR
,DECODE(la.Attr_080,NULL,NULL,'PCN') WS_PCN
,p.attr_011 MSL
,P.attr_013 BAKETIME
,p.attr_014 EXPTIME
, CASE
WHEN INSTR(bn.wipdatavalue, '-') = 0 THEN
bn.wipdatavalue
ELSE
SUBSTR(bn.wipdatavalue, 1, INSTR(bn.wipdatavalue, '-')-1)
END AS una
, CASE
WHEN INSTR(bn.wipdatavalue, '-') = 0 THEN
bn.wipdatavalue
ELSE
SUBSTR(bn.wipdatavalue, INSTR(bn.wipdatavalue, '-') + 1)
END AS dulo
FROM Container C
JOIN a_lotattributes la ON c.lotattributesid = la.lotattributesid
JOIN product p ON c.productid=p.productid
JOIN productbase pb ON p.productbaseid=pb.productbaseid
JOIN a_adhocwipdatarecord a ON a.objectrefid=c.containerid
JOIN a_adhocwipdatarecorddetails bn ON a.adhocwipdatarecordid=bn.adhocwipdatarecordid AND bn.wipdatanamename ='TR_BAG_NUMBER'
LEFT JOIN a_adhocwipdatarecorddetails ln ON a.adhocwipdatarecordid=ln.adhocwipdatarecordid AND ln.wipdatanamename ='TR_FT_LOT NUMBER'
LEFT JOIN a_adhocwipdatarecorddetails aw ON A.adhocwipdatarecordid=aw.adhocwipdatarecordid AND aw.wipdatanamename ='TR_FT LOT QTY'
WHERE ln.wipdatavalue = :ftlot AND bn.wipdatavalue LIKE :wip
)
) WHERE level LIKE :n
CONNECT BY LEVEL <= m
ORDER BY BAGNUMBER
Thanks for helping out guys.
Actually GROUP BY is not the solution. Having looked again at your desired output I have realised that what you want is an analytic count.
Your posted query is a bit of a mess and, sorry ,but I'm not prepared to invest time in it. This is the sort of structure you need:
select vbatch_id, maxim_pn, bagnumber, ft_lot
, count(*) over (partition by ft_lot) m
from whatever ...
Find out more.
Not sure why you need the DISTINCT. DISTINCT almost always indicates a failure to get the WHERE clause right.

Resources