I want to increment Total by one if we insert the same Name and Age. Otherwise it should be inserted as a new row with count 1.
id Name Age Total
1 Priyanka 23 1
First check if record is there
SELECT * FROM yourTableName WHERE Name = 'your name' AND Age = your
age;
check if you found record from above query then
UPDATE yourTableName SET Total = youraboverecord.total+1 WHERE ID = youraboverecord.id;
Else Simply insert your record.
Related
I have a table that is a list of updates over time timeOfOfferChange. I can grab the most recent value with the query below but is there a way to delete all the records that are not the max?
select asin, uniqueid, max(timeOfOfferChange) from tbl_sqs group by asin
You could use exists logic:
DELETE
FROM tbl_sqs t1
WHERE uniqueid NOT IN (SELECT uniqueid
FROM (
SELECT t2.uniqueid
FROM tbl_sqs t2
WHERE t2.asin = t1.asin AND
t2.timeOfOfferChange > t1.timeOfOfferChange
) t);
C. Create an INSTEAD OF row trigger on EMP_VU that increases the current count for a department by 1 if a new employee is added and subtracts 1 from the count for a department if an employee is deleted
create or replace trigger count_trigger
instead of insert or delete on emp_vu
begin
if deleting then
update dept_count
set count_emps = count_emps - 1
where dept_id = :OLD.department_id ;
elsif inserting then
update dept_count
set count_emps = count_emps + 1
where dept_id = :NEW.department_id ;
end if ;
end ;
/
D. Look at the counts for all departments in DEPT_COUNT. Test to see if your trigger fires correctly by inserting a row into EMP_VU. Look at the count for the department of the new employee. Delete a row from EMP_VU. Look at the count for the department where the employee was just deleted.
SELECT * FROM dept_count;
INSERT INTO emp_vu VALUES (606, 'Sillins', 90);
DELETE FROM emp_vu WHERE employee_id = 606;
Excuse me is there anyone help to repair this query?
Table rental has values (ID, odo_out, date),
table vehicle has values (ID, odo, car),
both with more columns but not relevant to this.
I have attempted to create a trigger:
CREATE TRIGGER odo_update AFTER INSERT ON rental
BEGIN
UPDATE rental SET odo_out = (SELECT Vehicle.odo FROM Vehicle WHERE rental.ID = Vehicle.ID)
WHERE EXISTS (SELECT * FROM Vehicle WHERE Vehicle.ID = rental.ID);
END;
which should detect a NULL for rental.odo_out and replace it with the value in Vehicle.odo for corresponding ID. This does work, but it updates every row in table, whereas I want it to ONLY update the row with NULL, ie the new row being inserted. An ID can be repeated multiple times in the rental table. How can I do this?
You must set the condition so that only the new row is updated.
This is where you need the keyword NEW to refer to the columns of the new row:
CREATE TRIGGER odo_update AFTER INSERT ON rental
WHEN NEW.odo_out IS NULL
BEGIN
UPDATE rental
SET odo_out = (SELECT odo FROM Vehicle WHERE ID = NEW.ID)
WHERE ID = NEW.ID;
END;
I am trying to setup a trigger that will auto calculate an ID field based on the sum of a specific type of entry. I have it working where the ID number in the ID indexes based on the number of all entries
> BEGIN
> UPDATE master_workorders
> SET wo_no = master_workorders.wo_sub || substr('0000'||master_workorders.pkuid, -4,4)||'-'|| substr(master_workorders.rdate,3,2)
> WHERE rowid = NEW.rowid;
> END
This returns ID's like WO0001-17 BB0002-17 and M0003-17 each ID number (middle 4 digits) is an entry. I want my ID numbers to represent the number of each type (WO, BB, M these values are stored in the wo_sub column) as WO0001-17 BB0001-17 M0001-17 and if a new BB work order is added it would be BB0002-17 and so on for each type.
To replace the autoincremented ID with the current count, replace master_workorders.pkuid with a subquery:
... || (SELECT COUNT(*) FROM master_workorders WHERE wo_sub = NEW.wo_sub) || ...
i am trying to count contacts in sqlite table GROUP by msisdn .
without GROUP BY msisdn return 210
with GROUP BY msisdn return 1
var queryTotal = db.execute('SELECT cid , COUNT(*) AS totalFriends FROM contact WHERE deleted = 0 AND synced = 1 GROUP BY msisdn');
var total = queryTotal.fieldByName('totalFriends');
alert(total);
what is the problem here !
Are you sure your query is correct ?
SELECT cid , COUNT(*) AS totalFriends FROM contact WHERE deleted = 0 AND synced = 1 GROUP BY msisdn
try with:
GROUP BY cid
You should include msisdn in your selected column:
SELECT msisdn, COUNT(*) AS totalFriends FROM contact WHERE deleted = 0 AND synced = 1 GROUP BY msisdn