SQL Count Function - Need some help - sql

Looking for some help in making an SQL query where:
If the value per hour is more than 40 then it will count it and this count will display how many times in that hour the value was above 40.
I am very new to SQL so please forgive my n00bness :)
Thanks!

SELECT Count(*) As myCount FROM myTable WHERE colPerHour > 40
This will return one row with a column called myCount. The value in that cell will be your desired number.

select colperhour, count (*)
from mytable
where colperhour > 40
group by colperhour
This should give you each value of colperhour greater than 40 (which are in the table), along with the number of times each value exists

Related

Fill in the most recent value highest value in the row field over the date column using tableau or sql

I have a data having 0,1,2 values in the row field and dates in increasing order in the column field and I would like the last '2' value keep constant moving forward in the dates. Please do let know a way to workaround. Example may 2027 has 2 and then 0 but I would like to have 2 in june 2027 and the rest of the dates.I would like to keep previousor beginning date values same but has the maximum value 2 in this case carry forward to the later dates.
Thank you.
Question
MS SQL Syntax:
SELECT TOP 1 value FROM YourTableName WHERE value <> 0 ORDER BY date_action DESC
MySQL Syntax:
SELECT value FROM YourTableName WHERE value <> 0 ORDER BY date_action DESC LIMIT 1

SQL CountIf a condition is met from another column based on average

I want to get a count of cells in Column A that meet a criteria (average of 10 minutes or more) in Column B using SQL. Apologies as I'm definitely not an SQL expert. Column B has wait times by second and I need to change it to minute, so basically my equation would be Column_B/60, but then also I want to only count the cells in Column A that have an average of 10 minutes or more in Column B. My current formula is below, I know it's off but to give an idea:
COUNT(CASE Column_A
WHERE Column_B/60 >= 10)
END
You can try
SELECT COUNT(*) FROM table_name WHERE Column_B/60 >= 10;
It can be achieved using the two step process below:
1) HH:MM:SS
To display Column_B in an HH:MM:SS format, set the Field Type to:
Number > Duration (Sec.)
If required, change the aggregation as required, such as to AVG.
2) CASE Statement
One way that it can be achieved is by displaying the Column_B value in seconds (60 seconds * 10 minutes):
COUNT(CASE
WHEN Column_B >= 600 THEN Column_A
ELSE NULL END)
Google Data Studio Report and a GIF to elaborate:

Trying to return rows based on condition of summed columns

Apologies if this has been asked. For what I thought was a relatively simple question, I couldn't seem to find the answer in my searches.
linesInserted and linesDeleted are two columns in the table. I'm trying to return rows where linesInserted + linesDeleted >= 200. The query I have is
SELECT *, (linesInserted + linesDeleted) as total
FROM table
WHERE total >= 200
GROUP BY id
This doesn't work as I'm getting an error saying:
Unknown column 'TOTAL' in where clause
I'm using RMySql for those curious.
If you sum over linesInserted + linesDeleted then there would be no problem:
SELECT id, sum(linesInserted + linesDeleted) as total
FROM table
GROUP BY id
having total >= 200
try like below
SELECT id, sum(linesInserted + linesDeleted) as total
FROM table
GROUP BY id
having sum(linesInserted + linesDeleted)>=200
Here sum is an aggregate function, so we couldn't use where to check the condition as the total is greater than or equal to 200. We should use the having to check the condition since we use sum to calculate the values.

SQL Query to Count IDs

I'm trying to write a SQL Query in MS Access to count the number of times each ID appears in a data set. The data set is formatted as follows:
ID Time
1 12345
1 12346
1 12350
2 99999
2 99999
If the Time for one ID is within 3 seconds of another Time for that same ID, I only want it to be counted once. So the results should look like this:
ID Count
1 2
2 1
The time column is not formatted as a datetime, so I can't use the datediff function. Any help would be appreciated.
This:
SELECT ID, COUNT(newtime)
FROM (SELECT DISTINCT ID, Time\3 AS newtime FROM times)
GROUP BY ID
groups the Time field values in triples using the integer division for Time\3 in Access.
The comment provided by #Andy G worked for my purposes:
"You first need a function to round up (or down) to the nearest multiple of 3. See here (allenbrowne) for example."
I rounded the time values to the nearest multiple of 3, and counted based on that criteria.

MS Access Query to records with same data in different fields in the same row

The title is a bit confusing but I'll explain my problem here:
So i have a database table with millions of lines of spending data broken up into different time fields (period1 - period14). Now what i need to do is write a query that will return the records where the spending in one period is equal to the spending in a different period within the same record. So basically that means if i have a reecord where the spending in period1 is $100 and then the spending in period5 is also $100, it will add that record to a new table. I tried something like the code below but since I'm very new to access it is rather complex/inefficient and also doesn't do what i need it to.
INSERT INTO Contracts
SELECT *
FROM SPENDDETAIL
WHERE (SPENDDETAIL.Period1 = SPENDDETAIL.Period2 OR SPENDDETAIL.Period3 [...] OR SPENDDETAIL.Period14)
AND (SPENDDETAIL.Period1 <> 0 OR SPENDDETAIL.Period2 <> 0 [...] OR SPENDDETAIL.Period14 <> 0);
Any help much appreciated, thanks!
Oh also i know this code snippet would only return the records where the period1 spend equals the spend from any of the other periods it was just a beginning attempt at making the query do what i need it to.
Something along these lines might get you started:
SELECT Id, Value, COUNT(*) FROM
(SELECT Id AS Id, 1 AS Period, Period1 AS Value FROM SPENDDETAIL
UNION ALL
SELECT Id AS Id, 2 AS Period, Period2 AS Value FROM SPENDDETAIL
UNION ALL
SELECT Id AS Id 3 AS Period, Period3 AS Value FROM SPENDDETAIL
etc...) x
GROUP BY Id, Value
HAVING COUNT(*) > 1
Where Id is some unique identifier for each row of the data (assuming there is such a thing).
This will give you a list of Ids and matching values.