define multiple variables in select query in oracle - oracle

Getting error.. unable to define 2 parameters in one query
define price = 99.969000
define isin = '9128283C2';
WITH A AS
(select BUY_SELL,CASE WHEN BUY_SELL = 0 THEN (&price - 0.0025) ELSE (&price + 0.0025) END BS,PRODUCT_ISIN,QUANTITY
from table1 t where t.t_dt = '01-nov-17'
AND BUY_SELL = 0
AND quantity = 25 and product_isin = &isin)
SELECT * FROM A where row_num = 1;

Related

ora-00933 : oracle update query using join sql command not properly ended

I have a query , when I run the below query in Oracle
update cust_logs el
set batch_id = '3344'
from
client c
join port p on p.pid = c.pid
and
p.flag = 0
where
c.cid = el.cid
and
c.flag = 0
and
p.export = 1
and
el.trans_id is nul
I am getting error as
ORA-00933: sql command not properly ended.
I have tried
merge into cust_logs el
using (
select *
from client c
join port p
on (
p.pid = c.pid
and p.flag = 0
)
) "s"
on ((
c.cid = el.cid
and c.flag = 0
and p.export = 1
and decode(el.trans_id, nul, 1, 0) = 1
))
when matched then update set
batch_id = '3344'
Can you please let me know how to resolve
This is my sql fiddle
http://sqlfiddle.com/#!4/c9166/1
You can't join in an update.
You haven't said what the issue is with your merge attempt. But your on clause referring to the base table instead of the "s" alias. And the select * will cause an ambiguous identifier error as pid appears in both tables. And it's null not nul.
So this might be closer, at least:
merge into cust_logs el
using (
select c.cid, c.flag, p.export
from client c
join port p
on p.pid = c.pid
where p.flag = 0
) s
on (
s.cid = el.cid
and s.flag = 0
and s.export = 1
and decode(el.trans_id, null, 1, 0) = 1
)
when matched then update set batch_id = '3344'
But we don't have your tables or data to verify it.
If batch_id is a numeric column then the last line should be:
when matched then update set batch_id = 3344

How to combine two query's results into one cell?

I have two different queries in oracle, I was able to show these queries side by side with cross join, but I want to see them in the same cell.
First query :
SELECT
pa.attrb_an_value
FROM
piece_attrb pa
WHERE
pa.piece_num_id = 1056436 AND
pa.attrb_code = 'PSP')
result : MCXTS
Second Query :
SELECT max(CAOSL.ATTRB_AN_VALUE)
FROM config_attrb_of_so_line caosl,
so_piece sp
WHERE sp.piece_num_id = 1056436
AND sp.so_id = caosl.so_id
AND sp.so_line_id = caosl.so_line_id
and SP.IS_ACTIVE_FLAG = 'Y'
AND CAOSL.ATTRB_CODE = 'GRS'
Result : DC0
I want to see like that in cell:
MCXTS - DC0
Concatenate them, then.
WITH
tab_1 (retval)
AS
-- first query
(SELECT pa.attrb_an_value
FROM piece_attrb pa
WHERE pa.piece_num_id = 1056436
AND pa.attrb_code = 'PSP'),
tab_2 (retval)
AS
-- second query
(SELECT MAX (caosl.attrb_an_value)
FROM config_attrb_of_so_line caosl, so_piece sp
WHERE sp.piece_num_id = 1056436
AND sp.so_id = caosl.so_id
AND sp.so_line_id = caosl.so_line_id
AND sp.is_active_flag = 'Y'
AND caosl.attrb_code = 'GRS')
-- final result
SELECT a.retval || ' - ' || b.retval as final_result
FROM tab_1 a CROSS JOIN tab_2 b

Return duplicate min values

See the table below for reference. I need to return duplicate rows containing the min value.
In this example I want to show only the 2 rows that have SLAT_LEN = 30 or rather the min SLAT_LEN. I tried rank but when I do it does a consecutive rank. I want the duplicate sizes to have the same rank and to rank consecutively by size.
The sizes change so I can't just use a condition like SLAT_LEN = 30.
Or is there a different approach that I should take?
select *
from(
select lg.wd_demand_id,wm.slat_len, wm.prof_size, wm.wd_material_id, wm.color, rank() over ( partition by wm.slat_len order by lg.wd_demand_id) as rank
from wd_demand_log lg, wd_bins wb, wd_material wm, wd_bins_material wbm
where lg.wd_bins_id = wb.wd_bins_id
and lg.wd_material_id = wm.wd_material_id
and lg.wd_bins_id = wbm.wd_bins_id
and lg.wd_material_id = wbm.wd_material_id
AND lg.plant_id = 44 AND lg.dept_id = 220 AND wb.plant_id = 44 AND wb.dept_id = 220
AND NOT EXISTS(SELECT dmpln.wd_demand_id FROM wd_demnd_pln_inv dmpln WHERE dmpln.wd_demand_id = lg.wd_demand_id)
AND wm.prof_size = '2' AND wm.color = 450
AND lg.wd_po_error is null)
You'd use PARTITION BY to give the range in which to rank. The ORDER BY specifies the ranking. So in your case you'll need something like
rank() over (order by wm.slat_len)
In case you have Oracle 12c you can limit your results with FETCH FIRST:
ORDER BY wm.slat_len
FETCH FIRST 1 ROW WITH TIES;
Perhaps I am not understanding the problem correctly, but here is my potential solution:
WITH aset
AS (SELECT lg.wd_demand_id
, wm.slat_len
, wm.prof_size
, wm.wd_material_id
, wm.color
FROM wd_demand_log lg, wd_bins wb, wd_material wm
, wd_bins_material wbm
WHERE lg.wd_bins_id = wb.wd_bins_id
AND lg.wd_material_id = wm.wd_material_id
AND lg.wd_bins_id = wbm.wd_bins_id
AND lg.wd_material_id = wbm.wd_material_id
AND lg.plant_id = 44
AND lg.dept_id = 220
AND wb.plant_id = 44
AND wb.dept_id = 220
AND NOT EXISTS
(SELECT dmpln.wd_demand_id
FROM wd_demnd_pln_inv dmpln
WHERE dmpln.wd_demand_id = lg.wd_demand_id)
AND wm.prof_size = '2'
AND wm.color = 450
AND lg.wd_po_error IS NULL)
SELECT *
FROM aset
WHERE slat_len = (SELECT MIN (slat_len)
FROM aset)

How to convert exists condition inside where clause in Linq

I want to add following where clause to Linq query. How subquery like below using linq
WHERE (Restaurants.[IsActive] = 1)
AND exists
(
select 1 from APIKeys
where ApiKey = 'on35e5xbt3m4cbcef4e4448t6wssg11o'
and (KeyType = 1
and fk_RestaurantsID = [t2].[RestaurantsID]
or KeyType = 2
and fk_RestaurantGroupID = RG.RestaurantGroupsID
and [t1].[fk_RestaurantsID] in
(SELECT RestaurantsID
FROM Restaurants
WHERE RestaurantGroupsID = RG.RestaurantGroupsID))
)
AND (0 = (COALESCE([t0].[fk_MembersID],0)))
AND (1 = [t0].[fk_BookingStatusID])
AND ([t0].[Email] = 'nike.s#gmail.com')
AND (([t0].[Phone] = '9999999990') OR ([t0].[MobilePhone] = '9999999990'))
Use Any() to produce subquery which translated to EXISTS. E.g. with AdventureWorks database sample:
from p in Products
where p.FinishedGoodsFlag &&
SalesOrderDetails.Any(od => od.ProductID == p.ProductID)
select new { p.ProductID, p.Name }
Will produce following query to database:
SELECT [t0].[ProductID], [t0].[Name]
FROM [Production].[Product] AS [t0]
WHERE ([t0].[FinishedGoodsFlag] = 1) AND (EXISTS(
SELECT NULL AS [EMPTY]
FROM [Sales].[SalesOrderDetail] AS [t1]
WHERE [t1].[ProductID] = [t0].[ProductID]
))

LINQ Left Join with use of Where Or syntax

How can I migrate this stored procedure into LINQ
DECLARE #customerIDs TABLE (customerID INT)
DECLARE #c INT
if (#customerName != '')
BEGIN
INSERT INTO #customerIDs
select id from customer
where name like '%' + #customerName + '%'
SELECT #c = COUNT(*) FROM #customerIDs
END
else
SET #c = 0
-- Insert statements for procedure here
SELECT DISTINCT l.*, dbo.GetListOfBranches(l.id) as 'list_of_branches', dbo.GetTopLevelCustomer(cid.customerID) FROM [login] l
LEFT JOIN login_to_customer ltc ON ltc.login_id = l.id
LEFT JOIN #customerIDs cid ON cid.customerID = ltc.customer_id
WHERE
(#c = 0 OR ltc.customer_id = cid.customerID) AND
(l.surname LIKE '%' + ISNULL(#surname, l.surname) + '%') AND
(l.forename LIKE '%' + ISNULL(#forename, l.forename) + '%') AND
(user_type = ISNULL(#userType, user_type)) AND
(l.active = ISNULL(#active, l.active))
ORDER BY surname ASC
I have got this so far:
List<Int32> customerIds = new List<int>();
if (!String.IsNullOrEmpty(customerName))
{
CustomerDataContext custDataContext = new CustomerDataContext();
customerIds = (from a in custDataContext.Customers
where a.name.Contains(customerName)
select a.id).ToList<Int32>();
}
LoginDataContext dataContext = new LoginDataContext();
var query = (from t in dataContext.logins
join t2 in dataContext.login_to_customers on t.id equals t2.login_id into a
from subquery in a.DefaultIfEmpty()
select new { t, listOfBranches = dataContext.GetListOfBranches(t.id), topLevelCustomer = dataContext.GetTopLevelCustomer(t.id) });
I need would then do a
if (customerIds.Count() > 0)
{
query = query.Where(a => customerIds.Contains(a.t.customer_id))
}
but as I need to perform the distinct I cant have the customer_id column in the query. Is there a way to include the
LEFT JOIN #customerIDs cid ON cid.customerID = ltc.customer_id
WHERE
(#c = 0 OR ltc.customer_id = cid.customerID)
Within the LINQ?
Thanks,
Richard

Resources