MS Access delete Query - ms-access

I have query in MS Access that produces a correct records result, but Access refuses to run the Query as a delete query?
Can any one help me rewrite this query to run in access.
Delete Table_A.*
FROM (SELECT Table_A.Main_RecID, Table_A.Fld_Unique_ID, Table_A.Actiontaken FROM Table_A
WHERE Table_A.Actiontaken="MainRecord deleted") AS Tmp_B
LEFT JOIN Table_A ON Tmp_B.Main_RecID=Table_A.Main_Recid
WHERE (((Table_A.Actiontaken)<>"MainRecord deleted"));
If the "Delete" is replaced by a select or I ask for a datasheet view the Query produces what I would expect. Which is a list of the records in the table that have the same Main_RecID as records with Actiontaken field = "MainRecord deleted" but do not have their Actiontaken field equal to "MainRecord deleted".
Access responds with the message "Could not delete from specified tables."

I started with this data in Table_A ...
Fld_Unique_ID Main_RecID Actiontaken
1 1 MainRecord deleted
2 1
3 2 MainRecord deleted
4 2 something else
5 3 something else
Note Actiontaken is Null in second row (where Fld_Unique_ID = 2).
Executing the DELETE statement below leaves this data in the table.
Fld_Unique_ID Main_RecID Actiontaken
1 1 MainRecord deleted
3 2 MainRecord deleted
5 3 something else
DELETE
FROM Table_A
WHERE
(
Actiontaken<>'MainRecord deleted'
OR Actiontaken Is Null
)
AND
(
DCount("*", "Table_A",
"Main_RecID = " & Main_RecID
& " AND Actiontaken='MainRecord deleted'") > 0
);
If that's not what you're after, please show us sample data before and after DELETE.

Related

Mysql query not return correctly

I am having problem with these result. I have been trying but no luck.
jobs table
id, title ... all details
1, title1,...
2, title2,...
3, title3,...
4, title4,...
job_user table
id,id_job,id_user
1,1,1
2,2,3
3,3,3
4,4,4
following_job table
id,id_job,id_user
1, 1, 3
So basically, user 3 has 2 jobs (2,3), and he follows job 1 of user 1. so, if i login as user 3, i would like to get all details jobs of user <> 3 (just the requirement that i need to do). i would get the result
id,id_job,id_user
1,1,1
4,4,4
My goal results would be :
id,title..., following_id
1,title1,...,1
4,title4,...,0
the following_id will be added as result above, since user 3 followed id_job 1 so its following_id = 1 else = 0. And id_job 1,4 will joined with jobs table to get details about it : title ...
I am doing the follow/unfollow job functionality
Thanks all
So... it seems like you want something like:
SELECT
jobs.id,
jobs.title,
jobs....,
CASE WHEN following_job.id_user = 3 THEN 1 ELSE 0 END as following_job
FROM
jobs
INNER JOIN job_user ON job.id = job_user.id_job
LEFT OUTER JOIN JOIN following_job on job.id = following_job.id_job
WHERE
job_user.id_user <> 3;
Joining all three tables according to your schema. Filtering via the WHERE clause to insure that no jobs that are no jobs that user 3 has. And then a CASE (or IF()) statement to flag the job as followed by user 3.

Setting priority in SQL

We have a table test in which we have status_cd as one of the column. Status Code can have three value - Prelim, Approved and confirmed.
I have to write a query in such a way that it should fetch the record for confirmed status cd. If confirmed status cd is not present, then fetch for Approved, if approved is not present, fetch for prelim, if prelim is also not present, fetch for null
id rule_id status_cd
1 1 prelim
2 1 null
3 1 approved
in above example, the query should return approved for rule_id=1
Try this way:
SELECT T1.*
FROM test T1 JOIN
(SELECT *,CASE status_cd WHEN 'confirmed' THEN 1
WHEN 'approved' THEN 2
WHEN 'prelim' THEN 3
ELSE 4 END AS Rank
FROM test) T2 ON T1.id=T2.id AND T1.Rule_id =T2.Rule_id
ORDER BY T2.Rank
LIMIT 1
Result:
ID RULE_ID STATUS_CD
3 1 approved
Sample result in SQL Fiddle.
Use the values '3 Confirmed','2 Approved', '1 Prelim' and '0 Null' or just use a default value of zero in a numerical field and set the value to 1, 2, 3 as the rule progresses (much faster execution, but avoid the null either way by using a default value of '0 Null' in the field if you keep a string implementation).
Then fetch the records with this query:
SELECT status_cd FROM YourTableName WHERE rule_id=1 ORDER BY status_cd DESC;
The first row of the query will always contain the answer you want. Additionally, the other rows will be there to confirm that all the other steps were present, etc. if you want to look at those rows too. If you know you only need the one row, then use
SELECT TOP 1 status_cd FROM ... (the rest is the same)

Update query on mutiple table in Sqlite or SQL

Hi I am new to SQL database.
I have two tables one is a "Master" and other is "Sub" like this
Master
uid(primary key) f_name l_name
1 fAaa lAaa
2 fBbb lBbb
second table
Sub
tid(primary key) uid(foreign key) time is_free
1 1 1:00AM 0
2 1 2:00AM 1
3 1 3:00AM 0
4 2 1:30PM 0
5 2 2:30PM 1
from both table we can say that user fAaa lAaa is free at 2:00AM and NOT free at 1:00AM and 3:00AM.
now I want to update like this, for user 1(fAaa lAaa), I want to delete time 2:00AM and want to insert new two time like 5:00AM and 6:00AM for user 1 than what should be my join query for update.
please help me!
Thanks
Like this?
DELETE FROM secondtable WHERE uid = 1 AND (time = "1:00AM" OR time = "2:00AM");
INSERT INTO secondtable (uid, time) VALUES (1, "5:00AM"), (1, "6:00AM");
Or
UPDATE secondtable SET time = "5:00AM" WHERE uid = 1 AND time = "1:00AM";
UPDATE secondtable SET time = "6:00AM" WHERE uid = 1 AND time = "2:00AM";
This is some pretty basic stuff, I recommend you do a search for "sql delete from", "sql insert into", "sql update" and look for beginner tutorials.

Change results in mysql query

I would like to manipulate the result I get from a query.
I have a set of 2.5m rows and there are 10 different ID's for a status. These statusses are not mapped in another table but I would like to manipulate the result I get in SQLyog.
What I would like to do is:
Count(Id) | Status
------------------
500.000 | 1
750.000 | 2
convert into a result
Count(Id) | Status
-------------------
500.000 | Initial order
750.000 | Cancelled
Can this be done in the query? Note that I'm not using PHP or a browser to display the results.
select
count(*) as TotalRecs,
case status
when 1 then "Initial Order"
when 2 then "Cancelled "
when 3 then "whatever "
else "all others "
end case as WordStatus
from
YourTable
group by
2
You can either inline it in a case statement
select COUNT(id),
case status
when 1 then 'initial order'
when 2 then 'cancelled'
# without an else, the rest go to NULL
end status
from tbl
group by status # yes, just on status
Or I would strongly encourage you to create a reference table for this
Tbl Status contains 2 columns ID and Description
select COUNT(tbl.id), status.description
from tbl
LEFT join status on status.id = tbl.status
group by status.description

SELECT & UPDATE according to status in one sql

I am wondering how to do this:
At the moment I have this, as you can see I am selecting all the status 0 & 2, then later in the code updating status 0 to 97 and status 2 to 99
SELECT id_queue, status FROM table WHERE status IN (0,2) ORDER BY status, id_queue ASC FOR UPDATE;
UPDATE table SET status = 97 WHERE id_queue= " + id_sms + ";
UPDATE table SET status = 99 WHERE id_queue= " + id_sms + ";
I want to SELECT & UPDATE, and also get the row ids of id_queue for later use
Can anyone help?
Much appreciated.
UPDATE table
SET status = CASE status WHEN 0 THEN 97
WHEN 2 THEN 98
END
WHERE status IN (0, 2)
If you want to save the id_queue values, then you'll either need to create a temp table, or store them in your program outside the database. I'd do the second for most reasonably sized set - a lot less housekeeping if you can keep them in scope in your program.