I have a table with 2 columns ID, Name, Now I am trying to make a two different data with the same table that will come around 2 new tables but I want both the table rows merged into parent table.
I will explain with sample data:
Parent table:
ID Name
1 abc
2 def
I am writing a select query as
Select ID, Name||'_First' as a from table
This will give me
ID Name
1 abc_First
2 def_First
Now my another select query as
I am writing a select query as
Select ID, Name||'_Second' as b from table
This will give me
ID Name
1 abc_second
2 def_Second
Now I am trying to join both the queries and produce the parent table as
Tried like this:
Select ID,a,b from
(Select ID, Name||'_First' as a from table
Inner join
Select ID, Name||'_Second' as b from table)
on joins here
But this is producing me 3 columnslike
ID a b
1 abc_First abc_second
2 def_First def_Second
But I need as
ID Name
1 abc_First
2 def_First
1 abc_second
2 def_Second
I am stuck at this point.
Use union all
Select ID, Name||'_First' as name from table
union all
Select ID, Name||'_Second' as name from table
Related
Is it possible to join 2 tables inside the query in oracle while importing ? I have a dump for 2 tables tab1 and tab2. and I am trying to query on 1 of the tables:
Directory=backup
DUMPFILE=twotables.dmp
logfile=tablesimp.log
tables = tab1,tab2
Query=tab1:"WHERE code= 2
AND NOT EXISTS (SELECT 1 FROM tab2 b WHERE type=b.type and numb=b.numb)"
it just give me 0 rows exported.
Would ku$. do any good? Documentation says that
The table alias used by Data Pump for the table being loaded is KU$.
Query=tab1:"WHERE ku$.code= 2
AND NOT EXISTS (SELECT 1 FROM tab2 b WHERE ku$.type=b.type and ku$.numb=b.numb)"
Let me explain the question.
I have two tables, which have 3 columns with same data tpyes. The 3 columns create a key/ID if you like, but the name of the columns are different in the tables.
Now I am creating queries with these 3 columns for both tables. I've managed to independently get these results
For example:
SELECT ID, FirstColumn, sum(SecondColumn)
FROM (SELECT ABC||DEF||GHI AS ID, FirstTable.*
FROM FirstTable
WHERE ThirdColumn = *1st condition*)
GROUP BY ID, FirstColumn
;
SELECT ID, SomeColumn, sum(AnotherColumn)
FROM (SELECT JKM||OPQ||RST AS ID, SecondTable.*
FROM SecondTable
WHERE AlsoSomeColumn = *2nd condition*)
GROUP BY ID, SomeColumn
;
So I make a very similar queries for two different tables. I know the results have a certain number of same rows with the ID attribute, the one I've just created in the queries. I need to check which rows in the result are not in the other query's result and vice versa.
Do I have to make temporary tables or views from the queries? Maybe join the two tables in a specific way and only run one query on them?
As a beginner I don't have any experience how to use results as an input for the next query. I'm interested what is the cleanest, most elegant way to do this.
No, you most probably don't need any "temporary" tables. WITH factoring clause would help.
Here's an example:
with
first_query as
(select id, first_column, ...
from (select ABC||DEF||GHI as id, ...)
),
second_query as
(select id, some_column, ...
from (select JKM||OPQ||RST as id, ...)
)
select id from first_query
minus
select id from second_query;
For another result you'd just switch the tables, e.g.
with ... <the same as above>
select id from second_query
minus
select id from first_query
I have an oracle query that uses a created table as part of the code. Every time I need to run a report I delete current data and import the new data I receive. This is one column of id's. I need to create a report on SSRS in which the user can input this data into said table as a parameter. I have designed a simple report that they can enter some of the id's into a parameter, but there may be times when they need to enter in a few thousand id's, and the report already runs long. Here is what the SSRS code currently says:
select distinct n.id, n.notes
from notes n
join (
select max(seq_num) as seqnum, id from notes group by id) maxresults
on n.id = maxresults.ID
where n.seq_num = maxresults.seqnum
and n.id in (#MyParam)
Is there a way to have MyParam insert data into a table I would join called My_ID, joining as Join My_Id id on n.id = id.id
I do not have permissions to create functions or procedures in the database.
Thank you
You may try the trick with MATERIALIZE hint which normally forces Oracle to create a temporary table :
WITH cte1 AS
( SELECT /*+ MATERIALIZE */ 1 as id FROM DUAL
UNION ALL
SELECT 2 DUAL
)
SELECT a.*
FROM table1 a
INNER JOIN cte1 b ON b.id = a.id
I have table with and ProjectId and more data for instance Name and Quantity.
I have another table with ProjectId, ProjectType and NumberOfthings
How can I created a calculated column that has the summatory of the NumberOfThings values by Project Type and ProjectId ?
For instance getting data by projecttype F in this example I should get in the calculated columns the follow values.
You can use a query to summarize the number of things by ProjectID and ProjectType
SELECT Table1.ProjectID AS ID, Table2.ProjectType AS Type, Sum(Table2.NumberOfThings) AS Things
FROM Table1 INNER JOIN Table2 ON Table1.ProjectID = Table2.ProjectID
GROUP BY Table1.ProjectID, Table2.ProjectType;
Results:
ID Type Things
1 F 3
2 G 34
3 F 100
I want to copy records from one table to another.
The only records from table 1 that will be copied to table 2 are the ones that still dont exist in table 2.
If duplicate records exists in Table 1 then only be copied to table 2 the record with the larger size name.
I could already implement a query that almost does what I want.
The problem I have is when there are names with the same maximum size of characters.
In these cases, my query returns more than one record and I just want to insert one new record in table 2.
Does anyone know how I can fix this?
Here is my code:
For x in (Select distinct xdd.id_t, xdd.name_t
From table1 xdd
Where xdd.id_t not in (Select distinct det.id_t2
From table2 det)
And LENGTH(xdd.name_t) in (Select Max(LENGTH(xdd2.name_t))
From table1 xdd2
Where xdd2.id_t = xdd.id_t)
) Loop
Insert into id_t2 (id_t2, name_t2)
Values (x.id_t, x.name_t);
End loop;
Can you give me an example to solve this?
Sure. If I understood requirements correctly, then the merge statement will look similar to this one:
We use row_number() analytic function to choose a duplicate record with longer name_t
merge into table_two t2
using(
select id_t
, name_t
from (select id_t
, name_t
, row_number() over(partition by id_t
order by length(name_t) desc) as rn
from table_one) q
where q.rn = 1
) t1
on (t2.id_t = t1.id_t)
when not matched then
insert(id_t, name_t)
values(t1.id_t, t1.name_t)
SQLFiddle demo
This is a merge statement that should "upsert" data from table 1 into table 2. Matching keys should update only when the name field in table1 is greater than that of table 2. And inserts should occur when keys from table one are not matched to table 2.
MERGE INTO table2 D
USING (SELECT table1.id_t, table1.name_t FROM table1) S
ON (D.id_t2 = S.id_t)
WHEN MATCHED THEN UPDATE SET D.name_t2 = S.name_t
WHERE (LENGTH(S.name_t) > LENGTH(D.name_t2))
WHEN NOT MATCHED THEN INSERT (D.id_t, D.name_t)
VALUES (S.id_t2, S.name_t2);