I am attempting update a Yes/No field (Sealed) in a table called [Assets] from a subquery.
The subquery results contain the ID from [Assets] and the desired result of the field to be updated.
I have tried a few options, including WHERE or FROM clauses, but can't achieve the appropriate result.
Below is the basic Update statement and the subquery. Any advice is greatly appreciated.
UPDATE [Assets] SET Sealed = IsSealed
SELECT y.Asset AS Asset,
IIF(y.TotalSeals>0,Yes,No) AS IsSealed
FROM (
SELECT x.Asset,
SUM(IIF(x.BOMTypes="Seal",1,0)) AS BOMSeals,
SUM(IIF(x.MatMovTypes="Seal",1,0)) AS MMSeals,
SUM(IIF(x.POTypes="Seal",1,0)) AS POSeals,
(BOMSeals + MMSeals + POSeals) AS TotalSeals
FROM (
SELECT [Data - Assets].ID AS Asset,
[Data - Inventory].Type AS BOMTypes,
NULL AS MatMovTypes, NULL AS POTypes, NULL AS TotalSeals
FROM [Data - Inventory] INNER JOIN ([Data - Assets] INNER JOIN [Data - BOM]
ON [Data - Assets].ID = [Data - BOM].Asset)
ON [Data - Inventory].ID = [Data - BOM].Component
UNION ALL
SELECT [Data - Assets].ID AS Asset, NULL AS BOMTypes,
[Data - Inventory].Type AS MatMovTypes,
NULL AS POTypes, NULL AS TotalSeals
FROM ([Data - Assets] INNER JOIN [Data - WO]
ON [Data - Assets].ID = [Data - WO].Asset)
INNER JOIN ([Data - Inventory] INNER JOIN [Data - MatMov]
ON [Data - Inventory].ID = [Data - MatMov].Component)
ON [Data - WO].ID = [Data - MatMov].WorkOrder
UNION ALL
SELECT [Data - Assets].ID AS Asset,
NULL AS BOMTypes, NULL AS MatMovTypes,
[Data - Inventory].Type AS POTypes, NULL AS TotalSeals
FROM ([Data - Assets] INNER JOIN [Data - WO]
ON [Data - Assets].ID = [Data - WO].Asset)
INNER JOIN ([Data - Inventory] INNER JOIN [Data - PO]
ON [Data - Inventory].ID = [Data - PO].Component)
ON [Data - WO].ID = [Data - PO].WorkOrder
) AS x
GROUP BY x.Asset
) AS y;
Assuming that your table Assets contains a field Asset which corresponds to the y.Asset column in your subquery, then I would suggest the following:
update assets a inner join
(
select y.asset, iif(y.totalseals > 0, Yes, No) as issealed
from
(
***TLDR
) y
) t on a.asset = t.asset
set a.sealed = t.issealed
This assumes that your subquery does not contain any form of aggregation, else the query will not be updateable.
If your subquery includes aggregation, you could use a domain aggregate function, such as DCount or DSum (depending on the operations performed by your subquery).
Related
I am using Join operator to check two values in table but it does not work
select = [[NSString alloc] initWithFormat:#"select * FROM ContentMaster LEFT JOIN Category ON ContentMaster.CategoryID= Category.CategoryID where ContentMaster.ContentTitle='%#' || ContentMaster.ContentTagText='%#' ",appDelegate.tagInput,appDelegate.tagInput];
If i do not use operator and use one in where clause then it shows result.
like
select = [[NSString alloc] initWithFormat:#"select * FROM ContentMaster LEFT JOIN Category ON ContentMaster.CategoryID= Category.CategoryID where ContentMaster.ContentTitle='%#'",appDelegate.tagInput];
select = [[NSString alloc] initWithFormat:#"select * FROM ContentMaster LEFT JOIN Category ON ContentMaster.CategoryID= Category.CategoryID where ContentMaster.ContentTagText='%#'",appDelegate.tagInput];
Any idea how to fix this issue so that or operator works
In SQL (and SQLite in particular), operator || typically means string concatenation.
If you want logical or, use operator OR.
In sqlite just use or for logical or operation:
SELECT *
FROM ContentMaster
LEFT JOIN Category ON ContentMaster.CategoryID = Category.CategoryID
WHERE ContentMaster.ContentTitle = '%#' OR ContentMaster.ContentTagText= '%#'
I have IPhone application in which I am inserting data. It works fine when I fetch all the data without any condition then it works fine if given any condition like
following query then it does not show anything.
NSString *select =
[NSString stringWithFormat:
#"SELECT * from ContentMaster As ml
LEFT JOIN ContentTagging As cat
ON cat.ContentID = ml.ContentID
where cat.ContenTagText= \'%#\'" ,appDelegate.tagInput];
const char *sql = [select UTF8String];
then it does not show any thing using this if i use the select * form ContentMaster then it shows all records added.
Try this.
SELECT * from ContentMaster
LEFT JOIN ContentTagging
ON ContentMaster.ContentID = ContentTagging.ContentID
WHERE ContentTagging.ContenTagText= \'%#\'"
I am doing an iPad project, there are 3 tables in UI. if i select row in each table. Sqlite query will take the value from table and run the query. please find my Query below.
NSString * str3=tableFou.string4;
NSString * str4=tableFiv.string5;
NSString * str2=tablethr.string3;
sqlite3 *database;
favorite=[[NSMutableArray alloc]init];
if(sqlite3_open([databasePath UTF8String], &database)== SQLITE_OK){
NSString *sql=[NSString stringWithFormat:#"Create view prizeview as SELECT country.name,item_country.id,text.text,substr(text,1,2) FROM country,item_country,prize,item_prize,gender,item_gender,text WHERE country.name = '%#' AND text.item = item_country.item AND country.id = item_country.id AND prize.name = '%#' and text.item=item_prize.item and prize.id = item_prize.id and gender.name = '%#' and text.item=item_gender.item and gender.id = item_gender.id ",str3,str4,str2];
const char *sqlStatement = [sql UTF8String];;
problem is that, the query is running if i select all the tables only.. but i want results if i select even one table. how can i do that?
Try with if condition use with and condition and pass all your str in if condition only.
use your if condition like this.
if(([propertyTypeLabel.text length]==0) || ([cityLabel.text length]==0 ) ||( [locationLabel.text length]==0 )|| ([priceLabel.text length] ==0)||([postedLabel.text length]==0))
In this like any of text is zero condition true you also use if ant of the table cell is tap your query run.
You can't search for empty fields unless the field is actually empty. F.ex. if you search
select * from mytable where username = "";
You won't find it, but if you are ok with the field being empty or whatever result, you must use like instead of =
select * from mytable where username like "%";
or better yet, you can create your query like this, lets say you have three fields to search for, username, firstname and lastname. The code below needs a few more if's and buts, but you should be able to make this work and only add the parameters to where if they fulfill the requirements.
NSMutableString *query = [[NSMutableString alloc] init];
[query appendString:#"select * from table where "];
if (username.text.lenghth >1) [query appendString:[NSString stringWithFormat:#"username = '%#'"]];
if (firstname.text.length >1) [query appendString:[NSString stringWithFormat:#"&& firstname = '%#'"]];
if (lastname.text.length >1) [query appendString:[NSString stringWithFormat:#"&& lastname = '%#'"]];
Hope this helps
I have a method which is supposed to return a PatientDetails object when I insert an unique nric as the parameter. This doesn't work. However when I changed this method to accept int age instead of NSString * nric, it worked.
Is there something wrong with my syntax "WHERE ic = %s" cause it seems wierd. I have googled 2 days on this and cannot find a solution.
Please help as I am a newbie.
- (PatientDetails *)patientDetails:(NSString *)nric {
PatientDetails *retval = nil;
NSString *query = [NSString stringWithFormat:#"SELECT ic, district, age, race, referralSource, DMRelatedAdmin, postalCode FROM patientInfo WHERE ic = %s" , nric];
sqlite3_stmt *statement;
%s denotes the standard C string. In order to use NSString objects as the argument, you need to use the object notation %#.
WHERE ic = '%#'
Yes, the format specifier for Objective-C strings is %#. So it should be,
NSString *query = [NSString stringWithFormat:#"SELECT ic, district, age, race, referralSource, DMRelatedAdmin, postalCode FROM patientInfo WHERE ic = %#" , nric];
I'm using the following for a LIKE query. Is this technique for LIKE correct?
selectstmtSearch = nil;
if(selectstmtSearch == nil){
const char *sql = "SELECT col1, col2 FROM table1 t1 JOIN table2 t2 ON t1.cityid = t2.cityid where t1.cityname like ?001 order by t1.cityname";
if(sqlite3_prepare_v2(databaseSearch, sql, -1, &selectstmtSearch, NULL) == SQLITE_OK)
{
sqlite3_bind_text(selectstmtSearch, 1, [[NSString stringWithFormat:#"%%%#%%", searchText] UTF8String], -1, SQLITE_TRANSIENT);
}
}
The problem I'm having is after a few uses of this, I get an error 14 on sqlite3_open(), which is unable to open database. If I replace the LIKE with something such as:
SELECT col1, col2
FROM table1 t1
JOIN table2 t2 ON t1.cityid = t2.cityid
where t1.cityname = ?
order by t1.cityname
It works fine. I do open/close the DB before after the above code. Is there a way to troubleshoot exactly why the database can't be opened and what its relationship to my LIKE syntax is?
You must sqlite3_reset or sqlite3_finalize(selectstmtSearch) before you close the database connection.