I have two different table.
(1)dog_breeds
(2)cat_breeds
now in the third table, I have column name pet_cat. Now I want to join with dog_breeds if pet_cat == 1 else join with cat_breeds. How can I do this just by using CodeIgniter active record.
Related
I am trying to update a field on a table the query needs to get the value from a 2nd table and use that to get the data from a 3rd table. I keep getting the "ORA-01427: single-row subquery returns more than one row" Any help is appreciated.
update ord_detail set cuser1 = (select c.email from contact c,ord_detail m join orders o on o.ID = m.orders_ID where c.email is not null)
where EXISTS (select email from contact,orders where orders.contact_id2 = contact.id)
Since you did not provide further information like the results of your sub selects, it's not 100% clear what exactly is your problem. You should check those results and if this does not answer your question, please provide the details.
My guess is that this sub query will give multiple rows because you are missing a join from the table "contact" to one of the other tables:
select c.email from contact c,ord_detail m
join orders o on o.ID = m.orders_ID where c.email is not null
Therefore, this sub query will always lead to many rows as result unless the table "contact" contains one row only whose column email is not null.
Add column in Table items based on left join on item_category table. I want to add column itemcategory_category in the items table based on the left join
=ADDCOLUMNS (
items, LOOKUPVALUE (
'item_category'[ITEMCATEGORY_CATEGORY],
'items'[KEY_PRODUCTCATEGORY], item_category[KEY_PRODUCTCATEGORY]
)
)
Items Table
KEY_PRODUCTCATEGORY
1
item_category table
KEY_PRODUCTCATEGORY ITEMCATEGORY_CATEGORY
1 A
If you have one to one relationship
Table = ADDCOLUMNS(items,"new",RELATED(item_category[ITEMCATEGORY_CATEGORY]))
If you have no relationship
Table 2 = ADDCOLUMNS(items,"new",CALCULATE(MAXX(filter(item_category,item_category[KEY_PRODUCTCATEGORY]=MAX(items[KEY_PRODUCTCATEGORY])),item_category[ITEMCATEGORY_CATEGORY])))
If you are only keen on LOOKUPVALUE with or without relationship. LOOKUPVALUE is not dependent upon any relationship but it has performance issues.
Table 3 = ADDCOLUMNS(items,"new",LOOKUPVALUE(item_category[ITEMCATEGORY_CATEGORY],item_category[KEY_PRODUCTCATEGORY],items[KEY_PRODUCTCATEGORY]))
I tested out with following and it works fine.
how to compare current table column to related table column
example: A.quantity < B.criticalQuantity
similarly like this AModel::where('quantity', "<", "b.criticalQuantity")->get()
the relations is
B HasMany A
A BelongsTo B
you can use whereColumn
it is specialist in comparing columns not a column with value.
anyway you can't directly compare two columns from two table, you have to join them first by anyway of join types
something like:
$values = ModelA::join('model_b_table_name', 'model_b_table_name.id', 'model_a_table_name.model_b_id')
->whereColumn('model_b_table_name.column.quantity', 'model_b_table_name.quantity')
->get();
you must be specific in joining the table, you should join by the columns that consist the relation between the two tables.
->whereRaw('table_1.name = table_2.name')
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).
I'm having difficulty modifying a script for this situation and wondering if someone maybe able to help:
I have an address table and a phone table both sharing the same column called id_number. So id_number = 2 on both tables refers to the same entity. Address and phone information used to be stored in one table (the address table) but it is now split into address and phone tables since we moved to Oracle 11g.
There is a 3rd table called both_ids. This table also has an id_number column in addition to an other_ids column storing SSN and some other ids.
Before the table was split into address and phone tables, I had this script:
(Written in Sybase)
INSERT INTO sometable_3 (
SELECT a.id_number, a.other_id,
NVL(a1.addr_type_code,0) home_addr_type_code,
NVL(a1.addr_status_code,0) home_addr_status_code,
NVL(a1.addr_pref_ind,0) home_addr_pref_ind,
NVL(a1.street1,0) home_street1,
NVL(a1.street2,0) home_street2,
NVL(a1.street3,0) home_street3,
NVL(a1.city,0) home_city,
NVL(a1.state_code,0) home_state_code,
NVL(a1.zipcode,0) home_zipcode,
NVL(a1.zip_suffix,0) home_zip_suffix,
NVL(a1.telephone_status_code,0) home_phone_status,
NVL(a1.area_code,0) home_area_code,
NVL(a1.telephone_number,0) home_phone_number,
NVL(a1.extension,0) home_phone_extension,
NVL(a1.date_modified,'') home_date_modified
FROM both_ids a, address a1
WHERE a.id_number = a1.id_number(+)
AND a1.addr_type_code = 'H');
Now that we moved to Oracle 11g, the address and phone information are split.
How can I modify the above script to generate the same result in Oracle 11g?
Do I have to first do INNER JOIN between address and phone tables and then do a LEFT OUTER JOIN to both_ids?
I tried the following and it did not work:
Insert Into..
select ...
FROM a1. address
INNER JOIN t.Phone ON a1.id_number = t.id_number
LEFT OUTER JOIN both_ids a ON a.id_number = a1.id_number
WHERE a1.adrr_type_code = 'H'
There is a syntax error in your query. You wrote in comment below that your query is:
..
FROM address a1 JOIN telephone t ON a1.id_number = t.id_number
AND RIGHT OUTER JOIN tt_gl_both_ids a ON a.id_number = a1.id_number
WHERE
..
Throw out AND and it will work. Also, replace right outer join with left outer join, since that's what you're trying to achieve (again, based on comments you wrote).