i have sql script which uses two query to get the result that i expect , how can i achieve this with the single query ,example below - oracle

query1 where condition = "condition1" ; queryresult1 = number1
query2 where condition = "condition2" ; queryresult2 = number2
I want number1-number2 , how can i make this possible with just a single query

Assuming i understand your question correctly, easiest way is joining two table and perform your whatever calculation.
select valueone, valuetwo, valueone - valuetwo as finalresult
from (select 18 as valueone from dual where 1 = 1) -- query one)
inner join (select 6 as valuetwo from dual where 1 = 1) -- query two)
on 1 = 1 -- join condition
where 1 = 1; -- some condition

Related

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

Linq how to join tables with a where clause on each table and a count

The following LINQ query isn't allowed when I use multiple where elements - it stops liking the 'into':
var query =
from ph in _db.PlayHits
join ua in _db.UserAgents on ph.UserAgentId equals ua.UserAgentId
where (ph.VideoId == 1 && ua.AgentString.Contains("test"))
into hits
select new
{
ResultCount = hits.Count()
};
Any idea why or how I amend this?
The equivilent sql I want is:
select count(*) as ResultCount
from Playhits ph
join UserAgents ua on ph.UserAgentId = ua.UserAgentId
where ph.VideoId = 1
and ua.AgentString like '%test%'
To my understanding, counting of the results can be done using the extension methond Count as follows.
var query =
from ph in _db.PlayHits
join ua in _db.UserAgents on ph.UserAgentId equals ua.UserAgentId
where (ph.VideoId == 1 && ua.AgentString.Contains("test"));
int Result = query.Count();

Oracle 11g Count query slowness

I have written one query which provides me a count of item type using group by.
The select query if I fire without group up and count provides results in less than a second on sql developer.
However count query runs for ever that I have to kill the process.
Below is the query ..
WITH permList
AS (SELECT user_perm_level,
item_category,
active
FROM usr_perm_levels
WHERE usr_pk = '7'
UNION ALL
SELECT home_key,
'ALL',
0
FROM user_home_list
WHERE usr_pk = 7
UNION ALL
SELECT home_key,
'UYT',
0
FROM user_home_list
WHERE usr_pk = 7
UNION ALL
SELECT home_key,
'ABR',
0
FROM user_home_list
WHERE usr_pk = 7),
list1
AS (SELECT a.parent_home_key,
b.item_category
FROM user_hier a
INNER JOIN indhome b
ON a.child_home_key = b.user_perm_level),
list2
AS ((SELECT oh.child_home_key,
u.item_category
FROM indhome U
INNER JOIN user_hier oh
ON u.user_perm_level = oh.parent_home_key
AND u.active = 1)
UNION ALL
SELECT user_perm_level,
item_category
FROM indhome)
SELECT type,
Count(*)
FROM inventory
WHERE is_available = 0
AND active = 1
AND EXISTS (SELECT 0
FROM inventory_auth c
WHERE EXISTS (SELECT 0
FROM orglist1
WHERE c.user_perm_level = orglist1.parent_home_key
AND c.item_category =
orglist1.item_category
AND c.active = 1)
OR EXISTS (SELECT 0
FROM orglist2
WHERE c.user_perm_level = orglist2.child_home_key
AND c.item_category =
orglist2.item_category)
AND inventory.item_key = c.item_key
AND inventory.item_category = c.item_category)
GROUP BY type;
In explain plan I see that Cost of group by clause is 145297221173.
Now here inventory table has 10 million records and the exists block which works around inventory table also runs into 15 million records.
Where as views permList, list1 and list2 have records only in 100s.
I have tried various combination but none of them seems to bring better performance. Such as I have tried inner join between inventory and inner query with exists clause but that doesn't help much.

Conversion of Oracle query to teradata query

How to convert this below query to equivalent teradate query. I tried but results varies a lot.
select il.domainN as listname, il.SourceID, ns.sourcename, cbo.customerid, cu.username, hv.domainN as HTname
, nvl((select 1
from mydb.customerPP cpp
where cbo.customerid = cpp.customerid
and NOT EXISTS (select 1 from mydb.customerPI cpt where cpp.customerid = cpt.customerid)
and trunc(cpp.startdate) <= sysdate
group by cpp.customerid),0) as BBID
from mydb.customerBO cbo
join mydb.customers cu on cbo.customerid = cu.customerid
join mydb.inv il on cbo.domainN = il.domainN
join mydb.Sources ns on il.SourceID = ns.SourceID
left join mydb2.HT hv on (il.domainN = hv.domainN
and hv.sDate+1 >= il.dDate
and il.dDate+1 >= hv.sDate)
where cbo.customerBOID = 1
and cu.statusid = 1
and il.sourceTID = 2
and il.joinbydate >= cbo.cDate
and trunc(il.dDate) = trunc(sysdate)
Thanks.
Use coalesce instead nvl and compare explain statements to see whether you have left outer joins converted to inner joins for nvl on Teradata.

oracle procedure cursor query when case statement

CURSOR BULKUPDATE IS
SELECT SUM(B.ACCOUNT_BALANCE) AS ACCOUNT_BALANCE,C.CIF AS CIF_ID FROM _ACCOUNTS_STAGING2 B JOIN _RELATION_STAGING2 C
ON B.ACCOUNT_IDENTIFICATION_NUMBER = C.ACCOUNT_IDENTIFICATION_NUMBER AND B.SOURCEID=C.SOURCEID JOIN _CUSTOMER_STAGING2 A ON A.CIF=C.CIF AND A.SOURCEID=C.SOURCEID WHERE C.ROLE_ON_ACCOUNT IN
(Select Rollonaccount From _Roleaccount_Master Where Aggregatebalance='Y')
And upper(B.Scheme_Type) In (Select Scheme_Type From _Schema_Type_Master Where
Depository_Account = 'Y') Group By C.Cif;
Rec_Bulkupdate Bulkupdate%Rowtype;
I am using this query to sum account balances based on different cif and source. The question is I want to calculate four different types of sum on the basis of _Schema_Type_Master. For example I want to check now current_account='Y' instead of Depository_Account='Y'
_ACCOUNTS_STAGING2 B JOIN _RELATION_STAGING2 C
ON B.ACCOUNT_IDENTIFICATION_NUMBER = C.ACCOUNT_IDENTIFICATION_NUMBER AND B.SOURCEID=C.SOURCEID JOIN _CUSTOMER_STAGING2 A ON A.CIF=C.CIF AND A.SOURCEID=C.SOURCEID WHERE C.ROLE_ON_ACCOUNT IN
(Select Rollonaccount From _Roleaccount_Master Where Aggregatebalance='Y')
And upper(B.Scheme_Type) In (Select Scheme_Type From _Schema_Type_Master Where
current_account='Y') Group By C.Cif;
Rec_Bulkupdate Bulkupdate%Rowtype;
Is there any way or do I need to write four different cursors for that??
You can remove dipository_account='Y' and current_account='Y' and use case in select as -
SELECT SUM(CASE WHEN Depository_Account = 'Y' THEN B.ACCOUNT_BALANCE ELSE 0 END) AS DIPOSITORY_ACCOUNT_BALANCE,
SUM(CASE WHEN current_account = 'Y' THEN B.ACCOUNT_BALANCE ELSE 0 END) AS CURRENT_ACCOUNT_BALANCE
and then rest of your code. You will get two different columns for sum of Depository account and Current account.
And if filter for dipository_account='Y' and current_account='Y' is required, then use them in where condition with or operator :
AND (dipository_account='Y' or current_account='Y')

Resources