I have these three tables :
RESEARCHER(Re_Id, Re_Name, Re_Address, Re_Phone, Re_HomePhoneNumber,
Re_OfficeNumber, Re_FirstScore, Re_Second_Score)
PUBLICATION(Pub_ID, Pub_Title, Pub_Type, Pub_Publisher, Pub_Year,Pub_Country, Pub_StartingPage, Pub_Number_of_Page, Score1, Score2)
WRITTEN_BY(Re_Id, Pub_ID)
I want to change the authors of the publication “Introduction to Database System” to “Henry Gordon” and “Sarah Parker”.
The problem is in WRITTEN_BY table,I just store the researcher's ID and publication's ID.
My idea is to change the Re_Id in WRITTEN_BY by those names are "Henry Gorgon" , "Sarah Parker" , which are already existed in RESEARCHER table
UPDATE WRITTEN_BY
SET Re_Id = ....( SELECT Re_Id
FROM RESEACHER
WHERE Re_Name = ‘Henry Gordon’ OR Re_Name = ‘Sarah Paker’ )
WHERE Pub_ID IN ( SELECT Pub_ID
FROM PUBLICATION
WHERE Pub_Name = ‘Introduciton to Database system’ );
I have problem in the SET part,so how to write the SQL statement for that requirement?
Here is the sqlfiddle link for my schema : http://sqlfiddle.com/#!9/b9118/1
I'd use something like below query:
DELETE FROM WRITTEN_BY WHERE Pub_ID IN (
SELECT Pub_ID FROM PUBLICATION
WHERE Pub_Title = 'Introduciton to Database system' )
INSERT INTO WRITTEN_BY
SELECT Re_Id,Pub_Id
FROM RESEARCHER CROSS JOIN PUBLICATION
WHERE Re_Name = 'Henry Gordon' OR Re_Name = 'Sarah Paker'
AND Pub_Title like 'Introduciton to Database system'
SELECT * FROM WRITTEN_BY
The idea is to first drop the existing mapping- you should not update it- and the insert a new one.
The reason for delete/insert approach vs update in case of mapping table is justified in favor of delete/insert as most mapping tables contain many-many mapping and usually one-to-many mappings.
Initially we may have a book mapped to say n number of authors where n <>1 then we either add extra rows, or are left with extraneous rows.
See sample fiddle: http://sqlfiddle.com/#!6/a0e72/13
The real deal however is CROSS JOIN. This does not take any ON like other JOINs and is used to produce cartesian product type map.
We are restricting it to get only limited number of rows as per our need by adding suitable WHERE clauses
Related
In one of my use case, i have two tables namely flow and conf. The flow table contains list of all flight data. It has columns creationdate,datafilename,aircraftid. The conf table contains configuration information. It has columns configdate, aircraftid, configurationame. There are multiple versions of configurations created for one aircraft type. So, when we process a datafilename, we need to identify the aircraftid from the flow table, and pick up the configuration from conf table that was created just before the datafilename was created. So, i tried this,
FROM (
SELECT
F_FILE_CREATION_DATE,
F_FILE_ARCHIVED_RELATIVE_PATH,
F_FILE_ARCHIVED_NAME,
K_AIRCRAFT
from T_FLOW f )x left join
(
select c.config_date, c.aircraft_id, c.configurationfrom t_conf c
) y on y.aircraft_id = x.K_AIRCRAFT
select
x.F_FILE_CREATION_DATE,
x.F_FILE_ARCHIVED_RELATIVE_PATH,
x.F_FILE_ARCHIVED_NAME,
x.K_AIRCRAFT,
y.config_date,
y.aircraft_id,
y.configuration;
This picks up all the configurations created for the aircraft which is obvious as there is no condition to check conf.config_date < flow.f_file_creation_date. I tried to include this condition like this,
FROM (
SELECT
F_FILE_CREATION_DATE,
F_FILE_ARCHIVED_RELATIVE_PATH,
F_FILE_ARCHIVED_NAME,
K_AIRCRAFT
from T_FLOW f )x join
(
select c.config_date, c.aircraft_id, c.FILEFILTER from t_conf c
) y on y.aircraft_id = x.K_AIRCRAFT where y.config_date < x.f_file_creation_date
select
x.F_FILE_CREATION_DATE,
x.F_FILE_ARCHIVED_RELATIVE_PATH,
x.F_FILE_ARCHIVED_NAME,
x.K_AIRCRAFT,
y.config_date,
y.aircraft_id,
y.filefilter;
This time failed with the error
required (...)+ loop did not match anything at input 'where' in statement
Can someone give me a hint or two where i am going wrong and on how to fix this?
select f.f_file_creation_date
,f.f_file_archived_relative_path
,f.f_file_archived_name
,f.k_aircraft
,c.config_date
,c.aircraft_id
,c.filefilter
from t_flow as f
join (select config_date
,aircraft_id
,filefilter
,lead (config_date,1,date '3000-01-01') over
(
partition by aircraft_id
order by config_date
) as next_config_date
from t_conf
) c
on c.aircraft_id =
f.k_aircraft
where f.f_file_creation_date >= c.config_date
and f.f_file_creation_date < c.next_config_date
Please read carefully
Posting a question
When you post a data related question -
Supply a data sample: source data + required results.
It is going to be more clear than any explanation you give.
It will also supply a common background for further discussions and a way for you and others to verify the correctness of the given solutions.
Supply the size properties (records/volume) of the tables.
It is important for performance considerations ans might impact the given solution.
SQL
Hive currently does not support any JOIN condition type other than equijoin (e.g. t1.X = t2.X and t1.Y = t2.Y). This is why you get an error.
If you are doing an inner join (and not outer join) then you can move the non-equijoin conditions to the WHERE clause.
Stick to ISO SQL standard. There is a conventional order for SQL clauses: SELECT-FROM-WHERE...
You gain nothing from esoteric syntax except for esoteric error messages.
There is no reason what so ever to use sub-queries in order to narrow the columns list.
Just to make it perfectly clear - There isn't any performance gain doing that. More than that, if it would have work as you assume (and it does not) the performance would have been worse, not better.
I can't reproduce your error. I guess your query is valid.
What version do you use for Hive ? I tested this query with hive 2.1.1.
DROP TABLE IF EXISTS t_flow;
CREATE TABLE IF NOT EXISTS t_flow (
f_file_creation_date DATE
, f_file_archived_relative_path STRING
, f_file_archived_name STRING
, k_aircraft STRING
);
-- Conf table contains configuration information.
-- It has columns configdate, aircraftid, configurationame
DROP TABLE IF EXISTS t_conf;
CREATE TABLE IF NOT EXISTS t_conf (
config_date DATE
, aircraft_id STRING
, filefilter STRING
);
SELECT
x.f_file_creation_date,
x.f_file_archived_relative_path,
x.f_file_archived_name,
x.k_aircraft,
y.config_date,
y.aircraft_id,
y.filefilter
FROM
(SELECT
f_file_creation_date,
f_file_archived_relative_path,
f_file_archived_name,
k_aircraft
FROM t_flow f) x
JOIN
(SELECT
c.config_date,
c.aircraft_id,
c.filefilter
FROM t_conf c) y on y.aircraft_id = x.k_aircraft where y.config_date < x.f_file_creation_date;
I'm developing a functionality that fills a collection, using two select lists, with a shuttle . I'm not getting the correct way to display the name, and recover its correspondent id, on shuttle's select item at right side, when I change an specific list, after repeated interventions. I developed a procedure to update, insert or delete the selected items on shuttle, and I suppose its works well after several tests on sql commands.
My test case uses three tables : contracts, worksations and employees. I intend to insert the employees of any workstation of a given contract to another contract. That new contract, whose will receive the employees, must have its owns workstations, previously inserted. The general structure of the tables is:
contracts : pk_contract, number_contract, company_name etc...
workstations : pk_workstation, fk_contract, description etc...
employees: pk_employee, fk_workstation, employee_name, employee_sex etc...
I created an app to demonstrate it: https://apex.oracle.com/pls/apex/f?p=43921
USER: test,
PASSWORD : TEST
Page 2- Migrate Employees - has three major regions, and 2 another to display information inserted :
Contracts with a select list, that must choice, preferrable the contract 070/2016, witch have workstation inserted, and a display field with static value, representing a contract, with workstations and employees attached.
workstations: with two select list, the gordian knot, I suppose : old workstations, related original contract's wokstations - related to display field, at contract's region; and new workstation, related to new contract selected above.
Employees: with a shuttle that represents, at left side, a bunch of employees, of a given original workstation and button witch do nothing.
Two more regions representing a classical report, only to inform the inserted, updated or deleted employees, from shuttles right size, using a collection. I made two report's regions because don't know make joins with collections and tables. This collection have the employee id, old_workstation id, new_workstation id, and new contract id, for further migration, not yet implemented.
I'm submiting almost all the fields, for recover its values on apex session's state.
Apparently, the collection works well, with a procedure, but when I choose again the original worksation the shuttle don't display, on right size, the previous inserted employee on that workstation. I configured on my list of values to not include extra values, when i change it, it shows the employee's ids, not their names, regards to source type listed here below:
SELECT e.pk_employee
FROM tb_employee e
INNER JOIN tb_workstation pt
ON pt.pk_workstation = e.fk_workstation
WHERE pt.typo IS NOT NULL
AND e.fk_workstation = p2_original_workstation
AND e.pk_employee IN (
SELECT to_number(c001) AS id_employee
FROM apex_collections
WHERE collection_name = 'WORKSTATION_EMPLOYEES');
My shutlle's lov has this rationale:
SELECT e.name, e.pk_employee
FROM tb_employee e
INNER JOIN tb_workstation pt
ON pt.pk_workstation = e.fk_workstation
WHERE pt.typo IS NOT NULL
AND e.fk_workstation = p2_original_workstation
AND e.pk_employee NOT IN (
SELECT to_number(c001) AS id_employee
FROM apex_collections
WHERE collection_name = 'WORKSTATION_EMPLOYEES');
Could anyone help me on this issue?
Regards!
I consider this a regular and fine solution. The shuttle permits recover entire registers on left side, changing it to another original workstation. After have removed the last "and" subclause, added at SHUTTLE an source sql query - return colon separated value, like this:
select e.pk_employee from employee e
inner join workstations w on w.pk_workstation = e.fk_workstation
where w.typo is not null and e.fk_workstation = :P2_ORIGINAL_WORKSTATION and e.pk_employee in (
SELECT TO_NUMBER(c001) as id_employee FROM APEX_collections WHERE collection_name = 'WORKSTATION_EMPLOYEES' and c002 = :P2_NEW_WORKSTATION);
But for purpose of goood usabilty, and my better interpretation that what shuttle obect does, I reinsert the 'and' clause with an 'and' more, reflecting that on lov has all employees of a given original workstation, except by the employees also inserted and that ones became from another new workstations. This particularité avoids the user thinks that a employee previously attached, on new workstation needed to be inserted:
select e.name, e.employee from employees e
inner join workstations w on w.pk_workstation = e.workstation
where w.typo is not null and e.worktation = :P2_ORIGINAL_WORKSTATION and e.pk_employee not in (
SELECT TO_NUMBER(c001) as id_employee FROM APEX_collections WHERE collection_name = 'WORKSTATION_EMPLOYEES' and c002 != :P2_NEW_WORKSTATION)
I could be consider use without these cited subclauses, using a disable jquery, in case of same original_wrkstion but at a diferent new, at left size, however its will be an improvement, and I need to understand the jqueries features. I consider this an basic and good, yet functional solution.
TablePatient.Patient_ID(PK)
TableProviders.Encounter (joins to PK)
TableProviders.Provider_Type
TableProviders.Provider_ID
TableNames.Full_Name
TableNames.Provider_ID (joins to Table Names)
I want a query that will give, for all the Patient_IDs, the Full_Name of the provider for every Provider ID.
There are about 30 provider_types.
I have made this already using a left join a ton of left joins. It takes a long time to run and I am thinking there is a trick I am missing.
Any help?
Ok, my previous answer didn't match at all what you meant. You want to pivot the table to have on each line one Patient_ID with every Full_name for every provider_type. I assume that each patient has only one provider for one type and not more ; if more, you will have more than one row for each patient, and anyway I don't think it's really possible.
Here is my solution with pivot. The first part is to make it more understandable, so I create a table named TABLE_PATIENT in a subquery.
WITH TABLE_PATIENT AS
(
SELECT TablePatient.Patient_ID,
TableProviders.Provider_Type,
TableNames.Full_Name
FROM TablePatient LEFT JOIN
TableProviders on TablePatient.Patient_ID = TableProviders.Encounter
LEFT JOIN
TableNames on TableNames.Provider_ID = TableProviders.Provider_ID
group by TablePatient.Patient_ID,
TableProviders.Provider_Type,
TableNames.Full_Name
)
SELECT *
FROM TABLE_PATIENT
PIVOT
(
min(Full_name)
for Provider_type in ([type1], [type2],[type3])
) AS PVT
So TABLE_PATIENT just has many rows for each patient, with one provider each row, and the pivot puts everything on a single row. Tell me if something doesn't work.
You need to write every type you want in the [type1],[type2] etc. Just put them inside [], no other character needed as ' or anything else.
If you put only some types, then the query will not show providers of other types.
Tell me if something doesn't work.
If I understand what you mean, you just want to group the answer by Patient Id and then Provider ID. A full name is unique on a provider id right ?
This should be something like
SELECT TablePatient.Patient_ID,
TableProviders.Provider_ID,
TableNames.Full_Name
FROM TablePatient LEFT JOIN
TableProviders on TablePatient.Patient_ID = TableProviders.Encounter
LEFT JOIN
TableNames on TableNames.Provider_ID = TablerProviders.Provider_ID
group by TablePatient.Patient_ID,
TableProviders.Provider_ID,
TableNames.Full_Name
You can either group by TableNames.Full_Name or select First(TableNames.Full_Name) for example if indeed a full name is unique to a provider ID.
Note : I used the SQL server Syntax, there can be différences with Oracle ..
How can I solve this error:
ORA-017779 : cannot modify a column which maps to non key-preserved table.
My Code:
UPDATE (SELECT SOBS034.T1 as OLD, SOBS063.T1 as NEW
FROM SOBS034
INNER JOIN SOBS063 ON SOBS034.ID = SOBS063.ID
where SOBS034.ID='111000' AND SOBS063.T2='' ) t
SET t.OLD =t.NEW
To update a JOIN, Oracle needs to be absolutely sure that for each row of the table you are trying to update, there will be at most one row of the joined table.
Here you'll be able to update the join if and only if SOBS063.ID is unique (explicitely declared by a unique constraint/pk).
If somehow SOBS063.ID is unique for this record with your join condition but is not declared as such, you won't be able to use this method. You could however transform this DML into an equivalent MERGE, something like this:
MERGE INTO SOBS034 a
USING SOBS063 b
ON (a.id = b.id AND a.ID='111000' AND b.T2 IS NULL)
WHEN MATCHED THEN UPDATE SET a.t1 = b.t1;
By the way SOBS063.T2='' is never true in Oracle right now.
Try to create a unique index on SOBS034 (ID) and SOBS063 (ID).
my table looks like this:
If the field name contains cost or quantity for the same lineItemIds, I have to display the result as:
cost is changed from 8*1=8
(fromVal*fromVal) to 9*6=54
(toVal*toVal) for itemID 123.
any help will be appreciated.
SELECT tc.LINE_ITEM_ID ITEM_ID,
tc.FROMVAL COST_FROMVAL,
tq.FROMVAL QTY_FROMVAL,
(tc.FROMVAL*tq.FROMVAL) PROD_FROMVAL,
tc.TOVAL COST_TOVAL,
tq.TOVAL QTY_TOVAL,
(tc.TOVAL*tq.TOVAL) PROD_TOVAL,
FROM
(SELECT LINE_ITEM_ID,
FROMVAL,
TOVAL,
FROM table
WHERE FIELDNAME = 'cost') tc
JOIN (SELECT LINE_ITEM_ID,
FROMVAL,
TOVAL,
FROM table
WHERE FIELDNAME = 'quantity') tq
ON tc.LINE_ITEM_ID = tq.LINE_ITEM_ID
I would look into using product aggregate functions. You'll have to compile them yourself though, Oracle doesn't include them as system functions. http://radino.eu/2010/11/17/product-aggregate-function/
If it's just for this one case where cost or quantity are used, then you could also just use subqueries, or temporary transaction based tables.
I'd provide you with a query example, but unfortunately don't have an Oracle instance accessible presently.