Join two Oracle queries - oracle

I've to query from two tables and want one result.. how can i join these two queries?
First query is querying from two tables and the second one is only from one.
select pt.id,pt.promorow,pt.promocolumn,pt.type,pt.image,pt.style,pt.quota_allowed,ptc.text,pq.quota_left
from promotables pt,promogroups pg ,promotablecontents ptc ,promoquotas pq where pt.id_promogroup = 1 and ptc.country ='049' and ptc.id_promotable = pt.id and pt.id_promogroup = pg.id and pq.id_promotable = pt.id order by pt.promorow,pt.promocolumn
select pt.id,pt.promorow,pt.promocolumn,pt.type,pt.image,pt.style,pt.quota_allowed from promotables pt where pt.type='heading'

Use UNION or UNION ALL. As long as you have the same number of columns and they are compatible types that should do what you want.
SELECT pt.id, pt.promorow, pt.promocolumn, pt.type, pt.image, pt.style, pt.quota_allowed, ptc.text, pq.quota_left
FROM promotables pt, promogroups pg, promotablecontents ptc, promoquotas pq
WHERE pt.id_promogroup = 1
AND ptc.country ='049'
AND ptc.id_promotable = pt.id
AND pt.id_promogroup = pg.id
AND pq.id_promotable = pt.id
UNION
SELECT pt.id, pt.promorow, pt.promocolumn, pt.type, pt.image, pt.style, pt.quota_allowed, NULL, NULL
FROM promotables pt
WHERE pt.type='heading'
ORDER BY 2, 3
If you want to display duplicates (e.g identical rows coming from both queries), use UNION ALL

Related

MAX DATE with Multiple tables/Inner Joins in Toad/Oracle

I have only been using Toad/Oracle for a few weeks so am still learning coding etc I have knowledge of SQL Code in Access and trying to now learn Oracle.
I need to return the max date from UCMRBILDAT from tbl BIC/AZUCDMO0100 but only from contracts which are contained in linked tbl LH_DAT
I have also tried a having MAX UCMRBILDAT but this didnt work.
UCMRBILDAT (/BIC/AZUCDMO0100)
UC_MRESULT (/BIC/AZUCDMO0100)
UC_MRSTAT (/BIC/AZUCDMO0100
UC_MRCAT (/BIC/AZUCDMO0100)
CONTRACT_NUMBER (LH_DAT)
UC_MR_NUMB (/BIC/AZUCDMO0100) + (/BIC/AZUCDMO0200)
SELECT UCMRBILDAT,
UC_MRESULT,
UC_MRSTAT,
UC_MRCAT
FROM LH_DAT
( SELECT CONTRACT_NUMBER, MAX (UCMRBILDAT) MXBD
FROM SAPSR3."/BIC/AZUCDMO0100"
GROUP BY CONTRACT_NUMBER) GMR
LEFT OUTER JOIN SAPSR3."/BIC/AZUCDMO0200"
ON (CONTRACT_NUMBER = UCCONTRACT)
INNER JOIN SAPSR3."/BIC/AZUCDMO0100"
ON ("/BIC/AZUCDMO0200".UC_MR_NUMB = "/BIC/AZUCDMO0100".UC_MR_NUMB)
WHERE CONTRACT_NUMBER = '2000014420'
AND UCMRBILDAT = MXBD
AND MR.CONTRACT_NUMBER = GMR.CONTRACT_NUMBER
Max bill date from BIC/AZUCDMO0100 but only for contracts contained in table LH_DAT
EDIT NEED MAX DATE FOR UCMRBILDAT ON BELOW SCRIPT
SELECT CONTRACT_NUMBER,
UCMRBILDAT,
UC_MRESULT,
UC_MRCAT
FROM LH_DAT
LEFT OUTER JOIN SAPSR3."/BIC/AZUCDMO0200"
ON (CONTRACT_NUMBER = UCCONTRACT)
INNER JOIN SAPSR3."/BIC/AZUCDMO0100"
ON ("/BIC/AZUCDMO0200".UC_MR_NUMB = "/BIC/AZUCDMO0100".UC_MR_NUMB)
WHERE CONTRACT_NUMBER = '2000014420'
AND "/BIC/AZUCDMO0200".SOURSYSTEM = 'SP'
AND "/BIC/AZUCDMO0200".UCDELE_IND <> 'X'
To get the max value of "BIC/AZUCDMO0100".UCMRBILDAT where there's a linked value from LH_DAT you'd want to use:
SELECT MAX(ba.UCMRBILDAT)
FROM SAPSR3."BIC/AZUCDMO0100" ba
INNER JOIN LH_DAT ld
ON ld.some_field = ba.some_field
There must be fields which link "BIC/AZUCDMO0100" and LH_DAT together, but in your query they're not specified. Find those fields, plug them in to the query above, and you should get the result you're looking for.

Oracle Merge Into (Multiple Joined Tables) Update Set (Multiple Where Statements) Returns Invalid Column Specification

I am new to Oracle. I am trying to update the values of a table with the values from a SELECT DISTINCT statement using the MERGE INTO method. I want to update the values for a table based on what is in the USING table conditionally.
A quick diagram of what I am essentially going for is
MERGE
INTO update_table ut
USING
(SELECT DISTINCT
t1.column_1
,t2.column_2
FROM table_1 t1
INNER JOIN table_2 t2
ON t1.foreign_key = t2.primary_key) st
ON (ut.pk = st.column_1)
WHEN MATCH UPATE
SET(ut.update_column = st.column_2
WHERE st.column_1 = 1
AND st.column_2 = 1
,ut.update_column = st.column_2
WHERE st.column_1 = 2
AND st.column_2 = 2);
However, when I do so I get the INVALID COLUMN SPECIFICATION error on the line where I use SET. How can I work around this to successfully update the table, preferably in ANSI standard?
You can include the conditions that you have added in where clause in the selected column list in using clause itself. Like This. (Not tested. Your conditions in where clause were not appropriate)
MERGE
INTO update_table ut
USING (SELECT DISTINCT
t1.column_1 ,
CASE
WHEN t1.column_1 = 1
AND t2.column_2 = 1
THEN t2.column_1
WHEN t1.column_1 = 2
AND t2.column_2 = 2
THEN t2.column_2
END column_2
FROM
table_1 t1
INNER JOIN table_2 t2 ON t1.foreign_key = t2.primary_key
) st
ON (ut.pk = st.column_1)
WHEN MATCHED THEN
UPDATE SET ut.update_column = st.column_2 ;

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.

Need to Create calculated columns from input date -18 months in Spotfire for the actual columns

I need to create a report where for each columns there will be a previous column which will give the value from date 18 months back from the date that is given as input. Basically I am getting data from few columns into Spotfire for a particular date and want few of the columns to show the output that was 18 months back.
Code Summary-
This is the code i have to implement into Spotfire. This report takes input of a particular single day's date and gets column values for it. Sub-query gets for few of the values and they are sent to the main query. Typically this report has
few common columns and few other columns which have the value from current date and previous dates for the same columns. I can implement all the columns from the main query, but need suggestions to get values for the previous columns calculated in Spotfire or anyway to implement as an Oracle view since we will be getting only one input for the main query and sub-query will be deducting static no of days/month[in this case its 18 months]
Code Sample:-
select st.x1,
cs.x2 ,
sp.x3, sp.x4,
el.x5 current_zxc, --New data 2
el.xxxx current_zvvxx, --New data 3
por.x6 current_zczxc, --New data 4
el.x7 current_sdcvzv, --New data 5
prev_yr_data.prev_1 previous_czzxczxc,
prev_yr_data.prev_2 previous_xcvv,
prev_yr_data.prev_3 previous_zcvzxz,
prev_yr_data.prev_4 PREVIOUS_czxcvzxv,
prev_yr_data.prev_5 previous_vvvxcvxc,
prev_yr_data.prev_6 previous_zxvxvv,
from table1 cs
inner join table2 usr on cs.xxx = usr.zzzzz
inner join table3 emp on emp.xxx = usr.zzzzz
inner join table4 gbst on cs.xxxs = gbst.zzzzz
inner join table5 sp on cs.xxx = sp.zzzzz
inner join table6 st on sp.xxx = st.zzzzz
inner join table7 ol on ol.xxx = cs.zzzzz
inner join table8 el on el.xxx = ol.zzzzz
inner join table9 spt on trim(upper(el.xxxx)) = trim(upper(spt.xxx))
inner join table10 por on
por.xxx = el.xxxx and
por.xxxx = el.xxxx and
por.xxxx = cs.zzzzz
inner join
(select st.x1,
cs.zzzzz case_zzzzz,
cs.x2 prev_4,
sp.zzzzz ,
sp.x3, sp.x4,
spt.zzzzz ,
spt.xxx prev_1, --Old data 1
el.x5 prev_2, --Old data 2
el.x6 prev_3, --Old data 3
por.xxxx prev_5, --Old data 4
el.x7 prev_6 --Old data 5
from table1 cs
inner join table5 sp on cs.xxxx = sp.zzzzz
inner join table6 st on sp.xxxx = st.zzzzz
inner join table7 ol on ol.xxxx = cs.zzzzz
inner join table8 el on el.xxxxx = ol.zzzzz
inner join table9 spt on trim(upper(el.x_part_name)) = trim(upper(spt.x_part_number))
inner join table10 por on
por.xxx = el.xxxx and
por.xxxx = el.xxxx and
por.xxxx = cs.zzzzz
where ol.date_time between add_months(to_date('date_input','mm/dd/yyyy'), -18) and to_date('date_input','mm/dd/yyyy')
) prev_yr_data on
sp.zzzzz = prev_yr_data.zzzzz and
spt.zzzzz = prev_yr_data.zzzzzz
where ol.date_time >= to_date('date_input','mm/dd/yyyy') and ol.date_time < ( to_date('date_input','mm/dd/yyyy') + 1 )
I would suggest adding a transformation when you bring in the data set to calculate your date 18 months in the future (or past whichever you prefer). Then you can do a self join within Spotfire where [Date] = [18MonthsForward] and bring in the same value column(s) that you desire.
The steps to achieve this look like this when viewed under source information:
2. Select Insert > Transformations...
Add the transformations:
a. Calculate new column
Name: 18MonthsForward
Expression: DateAdd("month",18,[Date])
3. Select Insert > Columns...
Select 'From Current Analysis'
Source location: Data Table
Automatic update.
Match columns:
Date => 18MonthsForward
Ignore columns:
Date
Select join method: LeftOuterJoin
Treat empty values as equal: False
As a step by step instruction you would:
(1) Select Insert >> Transformation
(2) Select Calculation new column from the drop down
(3) Type the expression: DateAdd("month",18,[Date]) and name this whatever you prefer
(4) Select Insert >> Columns
(5) Select from current data and select your current data table
(6) Join on Date and your newly calculated 18MonthsForward date column
(7) Select your value column as the new column
Attached in an image of the data resultant data table. 18 months prior value

How do I write a LINQ query to combine multiple rows into one row?

I have one table, 'a', with id and timestamp. Another table, 'b', has N multiple rows referring to id, and each row has 'type', and "some other data".
I want a LINQ query to produce a single row with id, timestamp, and "some other data" x N. Like this:
1 | 4671 | 46.5 | 56.5
where 46.5 is from one row of 'b', and 56.5 is from another row; both with the same id.
I have a working query in SQLite, but I am new to LINQ. I dont know where to start - I don't think this is a JOIN at all.
SELECT
a.id as id,
a.seconds,
COALESCE(
(SELECT b.some_data FROM
b WHERE
b.id=a.id AND b.type=1), '') AS 'data_one',
COALESCE(
(SELECT b.some_data FROM
b WHERE
b.id=a.id AND b.type=2), '') AS 'data_two'
FROM a first
WHERE first.id=1
GROUP BY first.ID
you didn't mention if you are using Linq to sql or linq to entities. However following query should get u there
(from x in a
join y in b on x.id equals y.id
select new{x.id, x.seconds, y.some_data, y.type}).GroupBy(x=>new{x.id,x.seconds}).
Select(x=>new{
id = x.key.id,
seconds = x.Key.seconds,
data_one = x.Where(z=>z.type == 1).Select(g=>g.some_data).FirstOrDefault(),
data_two = x.Where(z=>z.type == 2).Select(g=>g.some_data).FirstOrDefault()
});
Obviously, you have to prefix your table names with datacontext or Objectcontext depending upon the underlying provider.
What you want to do is similar to pivoting, see Is it possible to Pivot data using LINQ?. The difference here is that you don't really need to aggregate (like a standard pivot), so you'll need to use Max or some similar method that can simulate selecting a single varchar field.

Resources