Having two cloumn name having same
My Coding,
'usertile' => array(self::HAS_MANY, 'UserTile', 'tile_id',
'condition'=>'usertile.tile_id IS NULL'),
tile_id column name having same in that Usertitle table and Question table.
I want once tile_id in usertitle table mean that id value not showing in view list question table.
Now I got error like this,
CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'tile_id' in where clause is ambiguous. The SQL statement executed was: SELECT COUNT(DISTINCT `t`.`id`) FROM `tbl_question` `t` LEFT OUTER JOIN `tbl_user_tile` `usertile` ON (`usertile`.`tile_id`=`t`.`id`) WHERE (is_deleted = :deleteflag and is_sample = :sampleFlag and tile_id IS NOT NULL) AND (usertile.tile_id IS NULL)
The error is clear:
Column 'tile_id' in where clause is ambiguous
and relation is right.
Check where you execute this query and other conditions added to query.
Related
In this query I inserting records into a new empty table I created. These records are derived from another table where I am left joining that table to itself, in order to output records that are not included in the recent table that is appended on top of an older table. So basically it outputs records that were deleted.
CREATE DEFINER=`definer` PROCEDURE `stored_procedure_name`()
MODIFIES SQL DATA
SQL SECURITY INVOKER
BEGIN
START TRANSACTION;
INSERT INTO exceptions_table (
`insert_date`,
`updated`,
`account_number`,
`id_number`)
SELECT
`insert_date`,
`updated`,
`account_number`,
`id_number`
FROM original_table ot1
LEFT JOIN original_table ot2
ON ot1.`account_number` = vdcaas2.`account_number`
AND ot2.`insert_date` = '2022-12-20'
WHERE ot1.`insert_date` = '2022-12-10'
AND ot2.`account_number` IS NULL;
COMMIT;
END
I get an error stating: "SQL Error: Column "insert_date" in field list is ambiguous.
I'm not sure why because I have specified which table I am grabbing "insert_date" from when INSERTING and when SELECTING and JOINING..
Every row in your query has two columns called insert_date: one from the table you've aliased as "ot1", and one from the table (as it happens, the same table) you've aliased as "ot2".
The database system doesn't know which one you want, so you have to tell it by writing either "ot1.insert_date" or "ot2.insert_date", just as you do elsewhere in the query:
... ot2.`insert_date` = '2022-12-20'
...
... ot1.`insert_date` = '2022-12-10'
The same is true of the other columns you've listed to select.
You need to change this
SELECT
`insert_date`,
`updated`,
`account_number`,
`id_number`
to this
SELECT
ot1.`insert_date`,
ot1.`updated`,
ot1.`account_number`,
ot1.`id_number`
or this
SELECT
ot2.`insert_date`,
ot2.`updated`,
ot2.`account_number`,
ot2.`id_number`
or some combination
Issue
SQL Error: Column "insert_date" in field list is ambiguous error means that the query is trying to reference the "insert_date" column from both tables, ot1 and ot2.
Try the following:
SELECT
ot1.`insert_date`,
ot1.`updated`,
ot1.`account_number`,
ot1.`id_number`
Also, you have a typo in your query:
ON ot1.`account_number` = vdcaas2.`account_number` -> ON ot1.`account_number` = ot2.`account_number`
I have a query that updates all records inside my table and the values inside of each column should be in sync with my other table so i created this query:
UPDATE dashboard.event SET operation_start_time IN
(SELECT operation_start FROM dashboard.inventory), operation_end_time IN
(SELECT operation_end FROM dashboard.inventory)
WHERE terminal_id IN (SELECT terminal_id FROM dashboard.inventory)
but the problem postgres keep returning me "ERROR: syntax error at or near "IN"" in which i don't get why. If i put "=" instead of "IN" it returns me the error:
ERROR: more than one row returned by a subquery used as an expression
For the logic of these query. I have an inventory table and in it has a column name operation_start and operation_end. I want the data in those columns to be updated or inserted in the event table for each terminal_id
Any help will be appreciated please. Thank you!
If you want to update the columns in event from inventory for a given terminal_id, the the syntax would look like:
UPDATE dashboard.event e
SET operation_start_time = e.operation_start,
operation_end_time = e.operation_end
FROM dashboard.inventory i
WHERE e.terminal_id = i.terminal_id;
This selection works great with NOT IN, but with NOT EXISTS it returns an error:
SQL Error: ORA-00920: invalid relational operator
00920. 00000 - "invalid relational operator"
Does NOT EXISTS works other way?
select COMPANY.TITLE_COMPANY
from COMPANY
outer join LOCATION on (LOCATION.NAME_LOC = COMPANY.NAME_LOC)
where COMPANY.NUM_COMPANY not exists (select NUM_COMPANY from COMPANY_SUC)
;
Your syntax is wrong and it's easy to spot why. Mind that a where not exists clause is used to subtract one set of data from another set:
select
ename
from
emp
where NOT EXISTS
(select
null
from
dependents
where
emp.empno = dependents.empno
and ...
);
But you're trying to relate it to a specific field in the base query, which is wrong, it is not exactly as NOT IN, which compares data in a column to a subquery result.
Don't try to replace it with one another.
I am creating a view merging two tables with some similar fields and some dissimilar fields. I have this 95% working but there is one field from table A that matches up with a field from table B but only if you use that field from B as a join to pull a field from table C. The only part of the code below that isn't working is the JOIN. I could just put both of the fields in and do the logic to get provider_id from the ehruser_id in the model, but I feel like it should be doable in the SQL and I just don't have the knowledge to get that last bit working yet
DROP VIEW IF EXISTS vunifiedschedule CASCADE;
CREATE VIEW vunifiedschedule AS
SELECT
schedule_block.id as vid,
schedule_block.reason as vblock_reason,
NULL as vappointment_reason_id,
schedule_block.when_ts as vwhen,
schedule_block.deleted_ts as vdeleted_when,
schedule_block.placeofservice_id as vlocation_id,
schedule_block.duration as vduration,
true as vappointment_book,
schedule_block.note as vnote
FROM schedule_block
LEFT JOIN ( SELECT id FROM provider) as vprovider_id ON provider.ehruser_id = schedule_block.ehruser_id
UNION ALL
SELECT
appointment.id AS vid,
NULL as vblock_reason,
appointment.appointmentreason_id AS vappointment_reason_id,
appointment.appt_when AS vwhen,
appointment.deleted_when AS vdeleted_when,
appointment.location_id AS vlocation_id,
appointment.visit_length AS vduration,
appointment.appointment_book as vappointment_book,
appointment.note AS vnote,
appointment.provider_id as vprovider_id
FROM appointment
The error I get is
ERROR: missing FROM-clause entry for table "provider"
LINE 14: ...ELECT id FROM provider) as vprovider_id ON provider.e...
The error is pretty clear. You have no provider. I think you intend:
FROM schedule_block LEFT JOIN
( SELECT id FROM provider) vprovider_id
ON vprovider.ehruser_id = schedule_block.ehruser_id
--------^
However the subquery is unnecessary:
FROM schedule_block LEFT JOIN
provider
ON provider.ehruser_id = schedule_block.ehruser_id
1st select stmt of view has 9 columns but 2nd select stmt has 10 columns. This view will error with message "Query block has incorrect number of result columns
You selected 9 columns from the first table and 10 from the second, when using union you must have the same number of columns in both queries and the type columns selected must match.
I have a following SELECT query -
SELECT C.CASE_TITL_NM,RA.V_CUST_NUMBER
FROM KDD_CASES C
JOIN FCT_RA RA
ON RA.N_RA_ID = C.RA_ID
WHERE UPPER(C.CNTRY_KEY_ID) LIKE '%MANUAL%'
AND C.SCORE_CT IN (99,100)
AND C.STATUS_CD = 'CCD'
AND C.CASE_TITL_NM NOT LIKE 'MANUAL%'
I need to update value in col V_CUST_NUMBER with value in col CASE_TITL_NM so I plugged my SELECT inside following UPDATEstatement and ran it only to get ORA01779 -
UPDATE (
SELECT C.CASE_TITL_NM,RA.V_CUST_NUMBER
FROM KDD_CASES C
JOIN FCT_RA RA
ON RA.N_RA_ID = C.RA_ID
WHERE UPPER(C.CNTRY_KEY_ID) LIKE '%MANUAL%'
AND C.SCORE_CT IN (99,100)
AND C.STATUS_CD = 'CCD'
AND C.CASE_TITL_NM NOT LIKE 'MANUAL%'
) X
SET X.V_CUST_NUMBER = X.CASE_TITL_NM;
SQL Error: ORA-01779: cannot modify a column which maps to a non key-preserved table
01779. 00000 - "cannot modify a column which maps to a non key-preserved table"
*Cause: An attempt was made to insert or update columns of a join view which
map to a non-key-preserved table.
*Action: Modify the underlying base tables directly.
Can anybody explain what does this error mean and what would be the right UPDATE query?
What it means is the query as specified results in an output set that has duplicated rows for RA. Seeing as one RA row maps to two different C rows you cannnot update RA, because there is the potential to try and update the sole RA row to have two different values
You can try using a MERGE statement, using SQL-that-writes-SQL to create a bunch of UPDATE statements, or modifying the join condition so duplicate rows from RA are not present in the output and the primary key from RA is covered
I was able to run my query it by using EXIST clause
UPDATE FCT_RA F
SET F.V_CUST_NUMBER = ( SELECT CASE_TITL_NM
FROM KDD_CASES C
WHERE F.N_RA_ID = C.RA_ID
AND UPPER(CNTRY_KEY_ID) LIKE '%MANUAL%'
AND SCORE_CT IN (99,100)
AND STATUS_CD = 'CCD'
AND CASE_TITL_NM NOT LIKE 'MANUAL%')
WHERE EXISTS ( SELECT 1
FROM KDD_CASES C
WHERE F.N_RA_ID = C.RA_ID
AND UPPER(CNTRY_KEY_ID) LIKE '%MANUAL%'
AND SCORE_CT IN (99,100)
AND STATUS_CD = 'CCD'
AND CASE_TITL_NM NOT LIKE 'MANUAL%');
Please look here, especially about key preserved
The updatable view query must unambiguously return each row of the
modified table only one time. The query must be “key preserved”, which
means Oracle must be able to use a primary key or unique constraint to
ensure that each row is only modified once.