Deleting based on compare - ms-access

I'm sorry if this has already been asked, but I have not found it already answered entirely yet - so I'm hoping someone can help provide some clarification.
I have a delete query in access that I only want to delete certain rows based on if they sum to 0
Ex:
Tom -1
Jill 5
John -3
Tom 2
Jill -3
John 3
I only want to delete John's records because they sum to 0.
I have the delete where exists written -- and it is good about not deleting if none sum to 0, but if even one person sums to 0 it's trying to delete all the records. How can I get it to delete only the ones that sum to 0? Any help would be greatly appreciated.

I would go with something like:
Delete from [TableName] Where [Column1] In (
Select [Column1] From [TableName] Group By [Column1] Having Sum([Column2]) = 0 )
Of course, replace with your own [TableName],[Column1],[Column2]

Related

Update a value and a table with a foreign key

ticketId(**)
timeExpected
timeElapsed
187
5
5
225
4
8
856
8
15
782
10
8
**primary key
*foreign key
id(**)
(*)ticketId
beyondTime
1
187
0
2
225
1
3
856
1
4
782
0
I have to know which ticket his out of time and I have this in mind and in my database but I can't figure it out with SQL. I want to know when a ticket is out of time like the ticket number 225 is, I would like to update the other table with a binary 1 for "out of time" and 0 "good".
I don't know if I can update the "beyondTime" table when I do "timeExpected - timeElapsed" in the first table when a ticket exceed the time expected.
First, let's simplify the problem you're looking a solution for:
Comparing two data field values for the rows in a table
I believe that's basically what you're trying to do. And of course, you can make a decision based on the comparison and act accordingly like updating some values in another table if you want to.
But if all you're looking for is kind of a report, then all you need would be just a select statement equipped with the logic doing the comparison for you.
You can use SQL Case Statement for this. Here is an easy guide if you want to take a look.
The select statement you need would be like this:
select ticketId, (case when timeElapsed > timeExpected then 1 else 0 end) as beyondTime from tickets
The result would be like this:
Two things to remember:
Don't get disappointed by the negative points and keep asking questions :)
Identify the issue you need help with and be specific with your question. I'd suggest you to update this question.
Good luck!

Remove duplicated based on two columns [duplicate]

This question already has answers here:
How to select and/or delete all but one row of each set of duplicates in a table?
(2 answers)
How can I remove duplicate rows?
(43 answers)
Closed 1 year ago.
I've a flights table that consists of few columns but somebody seem to have ran a migration twice that resulted in creation of same data twice.
Anyway, the flight should only have only data from the following condition: The flight_number and the date.
Basically the table is looking like this at the moment:
flight_number
date
123
2021-09-16
123
2021-09-16
123
2021-09-17
124
2021-09-18
124
2021-09-18
Result I want:
flight_number
date
123
2021-09-16
123
2021-09-17
124
2021-09-18
Basically, keep only one and remove duplicated (if the flight_number is same of the same date).
I'm looking for a DELETE SQL query but couldn't find the one like I am looking for.
What is the query that can help me achieve it?
Thanks!
EDIT: Yes, all the data has a column id that is unique even if the data is same.
You need to identify which rows to keep and which to remove; this can be done as such:
delete ff from
flight ff
inner join (
select flight_number, row_number() over (partition by flight_number order by date) as RN
from flight f
) dups
on ff.flight_number = dups.flight_number
where dups.rn > 1
Basically, this uses Row_Number to create a row identifier based on certain criteria, in this case, for each (partition) Flight_number, create a row number then delete any records where the row_number is > 1.
You will need to change this to use the actual ID column on the join, like this https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=58a4ac7235ea22b557116ad68c8449c3

How to delete NON-duplicate rows from a table?

I have a table single_post which looks something like below
id | for_post
0 50
1 50
2 100
3 75
How to delete every entry on which for_post value exist only once ?. Which means after executing the query, the last two entries will be removed because for_post exist only once.
I didn't try anything yet because I have no idea what to try. Thanks in advance
How About Something like this:
DELETE FROM single_post
WHERE for_post IN
(SELECT for_post FROM single_post GROUP BY for_post HAVING count(for_post)=1)

count column items in mysql

I have a column called "WasCancelled" in a MySQL DB, it's of a Boolean type.
I save the number 0,1 and 2 to it.
I need a query that will count how many one's, two's and zeros are there.
for example:
my column:
WasCanceled
-----------
0
0
1
1
1
2
0
0
0
2
0
0
1
I need the query to output:
number | times
-------|------
"0" | 7
"1" | 4
"2" | 2
please help.
thank you.
Dave.
You can do this by grouping:
SELECT WasCancelled, COUNT(*)
FROM <Table>
GROUP BY WasCancelled
If you need more information, look here for details on Group By and Count.
Update
To include the question in the comments: to restrict on special values, you can add a WHERE clause:
SELECT WasCancelled, COUNT(*)
FROM <Table>
WHERE WasCancelled = "1"
GROUP BY WasCancelled
In further questions, please edit your overall question to include sub-questions or open new topics. Please read How To Ask Good Questions.
Update 2
SQL also allows the HAVING clause, which is like WHERE but allows the comparison of aggregated values. See here for details (e.g. you want to know which value appears more than 5 times, etc.).
Use GROUP BY with COUNT fuction:
Try this:
SELECT WasCanceled, COUNT(1) as times
FROM tableA
GROUP BY WasCanceled;
I think this might work!
select
count(WasCanceled) as CNT, WasCancelled
from
YourTableNameHere
group by
WasCanceled
This works for me
SELECT WasCanceled AS number, COUNT(WasCanceled) AS times FROM tblTest GROUP BY WasCanceled

Retrieve unique data from MYSQL database

I have a table in my database which contains 5 rows. I am trying to write an sql statement that will retrieve all rows which only have 1 agency assigned to them.
case_id agency_ID
1 4
2 4
3 3
4 2
4 4
To clarify I would like to select the required rows (and any further rows) but only if the case_id is unique. Any rows with duplicates would be ommited.
I have tried to use DISTINCT(case_id), COUNT(*) to count all rows but it doesn't work and it's slowly sapping away my soul. It is probably an easy fix, but for the life of me I just can't see it.
Hope this is enough information to go on. Any help would be greatly appreciated.
SELECT * FROM your_table GROUP BY case_id HAVING COUNT(agency_ID) = 1
You can try
SELECT case_id,agency_ID,COUNT(case_id) as c
FROM yourTable
GROUP BY case_id
HAVING (c=1)