I'm having trouble getting my SQL results to do what I need them to. I've been reading for awhile and I'm still having a little snag. I have a database with times in it. What I'm trying to do is return the amount of calls that fall within each hour. I'm trying to avoid doing 24 different queries for each hour and was hoping the group function might be something I can work with. Is it possible to do one query on this table and be able to group them into one hour increments, or do I need to do individual queries for each hour. My end result will have the number of calls that happened in each of the 24 hours.
+----------+---------+
| calltime | time |
+----------+---------+
| 160523 | 4:05pm |
| 150259 | 3:02pm |
| 025942 | 2:59am |
| 024729 | 2:47am |
| 072419 | 7:24am |
| 142450 | 2:24pm |
| 201937 | 8:19pm |
| 190428 | 7:04pm |
+----------+---------+
Is this possible?
Answer:
SELECT COUNT(*) FROM table GROUP BY HOUR(calltime)
And then looping through results.
You can try this:
SELECT HOUR(STR_TO_DATE( time, '%l:%i:%S %p' )), COUNT(calltime) FROM table GROUP BY HOUR(STR_TO_DATE( time, '%l:%i:%S %p' ));
You can read more here.
Related
I need some help in achieving a functionality. The idea is to calculate the sum of duration for all distinct activities which the query is bringing. A couple of considerations
The query is not SQL, its FetchXML (Microsoft Dynamics 365).
Grouping is not done on Activity IDs instead Contact ID, and it has to be that way only.
Here is how the data looks like
| Contact ID | a.activityid | b.activityid | b.duration |
|------------|--------------|--------------|------------|
| A | a1 | act_1 | 15 |
| A | b1 | act_1 | 15 |
| A | c1 | act_1 | 15 |
Now, I'm trying to Sum(b.duration) and it gives me result as 45, but I want the sum as 15 because b.activityid is getting duplicated in the resultset.
In short Sum of duration for distinct b.activityid.
Can anyone help me out a little here?
I have a table which looks like this
|Application No | Status | Amount | Type |
==========================================
|90909090 | Null | 3,000 | Null |
|90909090 | Forfeit| Null | A |
What I want to achieve is to combine the values together and end with a result like
|Application No | Status | Amount | Type |
==========================================
|90909090 | Forfeit| 3,000 | A |
I am new to SQL Query and have no idea how to do this
Thanks in advance
No need to join, use max() aggregate function and group by:
select applicationno, max(status), max(amount), max(type)
from yourtable
group by applicationno
However, if you have several non-null values for an application number in a field, then you may have to define a more granular rule than a simple aggregation via max.
I'm sure the answer is out there somewhere, but can't find it...
One table stores logged hours for employees, classified by type (type = Project or Task).
For each day, I can have hours logged on multiple tasks.
By doing a simple sum and grouping I can get the total hours per type and per day, but I'd like to go one step further:
display only the "error cases" where sum(hours) per Project is different than sum(hours) for all Tasks for a given date.
mysql> select sum(logged), type, date from `hoursLog` group by idEmployee, date, type;
+-------------+----------+----------+
| sum(logged) | type | date |
+-------------+----------+----------+
| 0.8 | Project | 20160525 |
| 1.0 | Task | 20160525 |
| 0.3 | Project | 20160526 |
| 0.3 | Task | 20160526 |
| 0.3 | Project | 20160527 |
| 0.5 | Task | 20160527 |
+-------------+----------+----------+
From the above table, I want only the dates 20160525 and 20160527, for which the sum is different.
Appreciate any help!
SELECT
SUM(IF(`type`='Task',logged,0)) task_hours,
SUM(IF(`type`='Project',logged,0)) project_hours,
date
FROM `hoursLog`
GROUP by idEmployee, date
HAVING task_hours <> project_hours
But I am not sure if you really need GROUP BY idEmployee. I guess this works now just because you have few records in test DB with only 1 employee involved. Do you need time summarized per person? or just per date?
I have multiple rows with the same name in this table, and I want to show only one of row of each. For example, with the following data:
| name | number |
+------+--------+
| exe | 1 |
| exe | 10 |
| exe | 2 |
| bat | 1 |
| exe | 3 |
| bat | 4 |
I would like to see the following results:
| name | number |
+------+--------+
| exe | 16 |
| bat | 5 |
How can I achieve this result?
Duplicate response: My question only have 1 table, the JOIN ..ON command creates confusion in understanding, i think this simple question can help many guys!
Try something like this:
SELECT t.`name`, SUM(t.`number`) AS `number`
FROM mytable t
GROUP BY t.`name`
ORDER BY `number` DESC
let the database return the result you want, rather than mucking with returning a bloatload of rows, and collapsing them on the client side. There's plenty of work for the client to do without doing what the database can do way more efficiently.
You can use an aggregation function for this:
SELECT name, SUM(number) AS total
FROM myTable
GROUP BY name;
Here is a reference on aggregate functions, and here is an SQL Fiddle example using your sample data.
I have a set of MySQL data similar to the following:
| id | type | start | end |
===============================================================
| 1 | event | 2011-11-01T00:00:00 | 2012-01-02T00:00:00 |
| 2 | showing | 2012-11-04T00:00:00 | 2012-11-04T00:00:00 |
| 3 | conference | 2012-12-01T00:00:00 | 2012-12-04T00:00:00 |
| 4 | event2 | 2012-01-01T00:00:00 | 2012-01-01T00:00:00 |
I want to retrieve events within a certain date range, but I also want to return individual results for each row that has a time span of more than one day. What's the best way to achieve this?
EDIT: In other words, I want to return two results from the event row, four results from the conference row and a single result for all the others.
Any ideas would be greatly appreciated.
Try this statement:
SELECT * FROM table
WHERE START BETWEEN '2012-01-01' AND '2012-01-03'
OR END BETWEEN '2012-01-01' AND '2012-01-03'
OR TO_DAYS(end) - TO_DAYS(start) > 1
I have created it for testing on SQL Fiddle