Different value counts on same column using LIKE - mysql

I have a database like below
+------------+---------------------------------------+--------+
| sender | subject | day |
+------------+---------------------------------------+--------+
| Darshana | Re: [Dev] [Platform] Build error | Monday |
| Dushan A | (MOLDOVADEVDEV-49) GREG Startup Error | Monday |
+------------+---------------------------------------+--------+
I want to get the result using the above table. It should check if the subject contains the given word then add one to the that word column for a given day.
|Day | "Dev" | "startup"|
+---------+------------+----------+
| Monday | 1 | 2 |
| Friday | 0 | 3 |
I was thought of using DECODE function but I couldn't get the expected result.

You can do this with conditional aggregation:
select day, sum(subject like '%Dev%') as Dev,
sum(subject like '%startup%') as startup
from table t
group by day;

Related

SSRS report - pivoting data (last columns grouping)

I am trying to achieve the attached prototype for ssrs report. I was able to get all the data but can not figure out how to show the last three columns ("First Time Posted", "Last Time Cancelled" and "Total Duration")
When i try with expression it show data for every single corresponding line, just want to show one entry based on group.
Thank you,
Desired output
Report design
Data will be like
| Cp Label | Time Posted | Time Cancelled| Duration | Link ID|
---------------------------------------------------------------------
| Cp 1 | 12:56:00 | 12:57:05 | 00:01:05 | 1 |
---------------------------------------------------------------------
| Cp 2 | 12:57:05 | 1:00:00 | 00:02:55 | 1 |
---------------------------------------------------------------------
| Cp 3 | 12:57:00 | 1:15:00 | 00:18:00 | 2 |
---------------------------------------------------------------------

Sum a column or another based on a helper column - SQL

I'm using Google Data Studio, and have 12 columns, 1 per month, with numbers, and another column with dates. I'd like to SUM all the numbers that will fall inside a date range based on the date column.
So I've something like this:
+---------+---------+---------+---------+
| DATE | January | February | March |
+---------+---------+---------+---------+
|20180101 | 500 | | |
|20180203 | 150 | | |
|20180201 | | 100 | |
|20180301 | | | 200 |
+---------+---------+---------+---------+
I'd like to have 650 as the result from January extraction, but can't find a solution yet.
Are you looking for aggregation?
select sum(january)
from t
where date >= :datestart and date < :dateend

SQL COUNT query similar to UNION but with results in multiple columns

I have a single-table SQL database built from DHCPD logs, structured as below:
+------+-------+------+----------+---------+-------------------+-----------------+
| id | Month | Day | Time | Type | MAC | ClientIP |
+------+-------+------+----------+---------+-------------------+-----------------+
| 9305 | Nov | 24 | 03:20:00 | DHCPACK | 00:04:f2:4b:dd:51 | 10.123.246.116 |
| 9307 | Nov | 24 | 03:20:07 | DHCPACK | 00:04:f2:99:4c:ba | 10.123.154.176 |
| 9310 | Nov | 24 | 03:20:08 | DHCPACK | 00:19:bb:cf:cd:28 | 10.99.107.3 |
| 9311 | Nov | 24 | 03:20:08 | DHCPACK | 00:19:bb:cf:cd:28 | 10.99.107.3 |
Every DHCP event from the log will eventually make its way into this database, so events from any point in time will be potentially used in the construction of graphs. To make use of the data for graphing, I need to be able to create an output table with multiple columns, but with values derived from a count of those in a single column matching a specific pattern.
The closest thing I've managed to come up with is this query:
select 'Data' as ClientIP, count(*) from Log where ClientIP like '10.99%' and MAC like '00:04:f2%'
union
select 'Voice' as ClientIP, count(*) from Log where ClientIP like '10.123%' and MAC like '00:04:f2%';
Which yields the following result:
+-----------+-------+
| ClientIP | Count |
+-----------+-------+
| Data | 4618 |
| Voice | 13876 |
+-----------+-------+
Fine for a one-off query, but I want to take those two rows, turn them into two columns, and run the same query with one row per hour (for instance). I want something like this:
+------+-------+------+
| Hour | Voice | Data |
+------+-------+------+
| 03 | 22 | 4 |
| 04 | 123 | 23 |
| 05 | 45 | 5 |
Any advice is greatly welcomed.
Thanks
You can group by hour and use conditional computation to count Data and Voice traffic.
For example:
SELECT
HOUR(time) AS `Hour`,
SUM(CASE WHEN ClientIP like '10.99%' and MAC like '00:04:f2%' THEN 1 ELSE 0 END) AS `Data`,
SUM(CASE WHEN ClientIP like '10.123%' and MAC like '00:04:f2%' THEN 1 ELSE 0 END) AS `Voice`
FROM log
GROUP BY HOUR(time)
Create a separate table for (as you want) :
+------+-------+------+
| Hour | Voice | Data |
+------+-------+------+
and update it every hour using triggers.

want to get day name for the corresponding date

I have a table setup as shown below.
Table Name: activity.
| ACTIVITY_ID | DATE | ASSIGN_ENGR | TASK_TYPE | TASK_STATUS |
|-------------|------------|-------------|-----------|-------------|
| 1 | 2013-12-31 | Sachin | Monthly | Scheduled |
| 2 | 2013-12-23 | Mikel | Weekly | Done |
| 3 | 2013-10-18 | John | Monthly | Done |
I want to get day name against my date field using query.
MySql Query
SELECT DAYNAME('2007-02-03');
Output:
Saturday
Your Query would be like this
select Activity_ID, Date , DayName(Date) As Day, Assign_Engr, Task_Type,Task_Status From Your_Table_Name;
Dayname() function
Mysql provides with DAYNAME() function.
DATE_FORMAT(NOW(),'%a') # %a: Abbreviated weekday name
For more information: w3schools
A simple query could look like this:
SELECT DATE_FORMAT(date, ‘%a’) AS dateday FROM users WHERE id = 1;

Returning multiple results from one row

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