mysql> SELECT * FROM con_transactions;
+------+------+---------------------+--------+
| t_id | p_id | date | amount |
+------+------+---------------------+--------+
| 10 | 1 | 2016-02-17 19:24:05 | 1800 |
| 12 | 2 | 2016-02-18 11:40:13 | 200 |
| 17 | 3 | 2016-02-18 11:42:04 | 100 |
| 19 | 4 | 2016-02-18 11:45:43 | 1 |
| 20 | 5 | 2016-02-18 11:45:54 | 999 |
| 21 | 1 | 2016-02-18 11:46:02 | 1500 |
| 41 | 2 | 2016-02-18 17:23:14 | 500 |
| 42 | 3 | 2016-02-18 17:23:14 | 500 |
| 43 | 4 | 2016-02-18 17:23:15 | 500 |
| 44 | 5 | 2016-02-18 17:23:16 | 500 |
| 45 | 1 | 2016-02-18 17:23:16 | 500 |
| 46 | 2 | 2016-02-18 17:23:16 | 500 |
| 47 | 3 | 2016-02-18 17:23:17 | 500 |
| 48 | 4 | 2016-02-18 17:23:17 | 500 |
| 49 | 5 | 2016-02-18 17:23:18 | 500 |
| 50 | 1 | 2016-02-18 17:25:54 | 1000 |
| 51 | 1 | 2016-02-18 17:26:22 | 3000 |
| 52 | 2 | 2016-02-18 17:48:59 | 10 |
| 53 | 1 | 2016-02-18 17:48:59 | 10 |
| 55 | 1 | 2016-02-19 10:20:12 | 1000 |
+------+------+---------------------+--------+
HOW Can I Select current month or last month All Transactions record. All Month are not 30 days. So How can I Select January or other month record with SELECT query. I Try this query
SELECT * FROM con_transactions WHERE date > '2016-01-01 00:00:00' AND date < '2016-02-01 00:00:00';
This is not so smart .
Use month() and year() to directly find any given month of a year.
SELECT * FROM con_transactions WHERE year(date) = 2016 AND month(date) = 1;
One method is to use formats such as as YYYYMM for the date comparison:
SELECT *
FROM con_transactions
WHERE YEAR(date) * 100 + MONTH(date) = YEAR(CURDATE())*100 + MONTH(CURDATE());
However, that has the drawback that MySQL cannot use an index for the WHERE clause. So, another method is a variant of your method but using the first day of the month:
WHERE date >= str_to_date(concat_ws('-', year(curdate()), month(curdate()), '01'), '%Y-%m-%d) and
date < str_to_date(concat_ws('-', year(dateadd(curdate(), interval 1 month)), month(dateadd(curdate(), interval 1 month), '01'), '%Y-%m-%d)
Related
Table con_projects
mysql> SELECT * FROM con_projects;
+----+---------------+---------------------+--------+
| id | project_name | project_description | status |
+----+---------------+---------------------+--------+
| 1 | Project 1 | Description IS Here | 1 |
| 2 | Project 2 | Description IS Here | 0 |
| 3 | Project 3 | Description IS Here | 1 |
| 4 | Project 4 | Description IS Here | 0 |
| 5 | Project 5 | Description IS Here | 1 |
+----+---------------+---------------------+--------+
Here status 1=active and 0=inactive
mysql> SELECT * FROM con_transactions;
+------+------+---------------------+--------+
| t_id | p_id | date | amount |
+------+------+---------------------+--------+
| 10 | 1 | 2016-02-17 19:24:05 | 1800 |
| 12 | 2 | 2016-02-18 11:40:13 | 200 |
| 17 | 3 | 2016-02-18 11:42:04 | 100 |
| 19 | 4 | 2016-02-18 11:45:43 | 1 |
| 20 | 5 | 2016-02-18 11:45:54 | 999 |
| 21 | 1 | 2016-02-18 11:46:02 | 1500 |
| 41 | 2 | 2016-02-18 17:23:14 | 500 |
| 42 | 3 | 2016-02-18 17:23:14 | 500 |
| 43 | 4 | 2016-02-18 17:23:15 | 500 |
| 44 | 5 | 2016-02-18 17:23:16 | 500 |
| 45 | 1 | 2016-02-18 17:23:16 | 500 |
| 46 | 2 | 2016-02-18 17:23:16 | 500 |
| 47 | 3 | 2016-02-18 17:23:17 | 500 |
| 48 | 4 | 2016-02-18 17:23:17 | 500 |
| 49 | 5 | 2016-02-18 17:23:18 | 500 |
| 50 | 1 | 2016-02-18 17:25:54 | 1000 |
| 51 | 1 | 2016-02-18 17:26:22 | 3000 |
| 52 | 2 | 2016-02-18 17:48:59 | 10 |
| 53 | 1 | 2016-02-18 17:48:59 | 10 |
| 55 | 1 | 2016-02-19 10:20:12 | 1000 |
+------+------+---------------------+--------+
Now I want to SUM() all amount from con_trnsactions whose p_id from con_projects status = 1
I tried this and many more
mysql> SELECT (SELECT SUM(t.amount) FROM con_transactions t WHERE p.id=t.p_id) as total FROM con_projects p WHERE status='1'
+-------+
| total |
+-------+
| 14120 |
+-------+
AND
mysql> SELECT SUM(amount) from con_transactions;
+-------------+
| SUM(amount) |
+-------------+
| 14120 |
+-------------+
Both Are Same . But The subtraction will be like this.
mysql> SELECT 1800+100+999+1500+500+500+500+500+500+1000+3000+10+1000;
+---------------------------------------------------------+
| 1800+100+999+1500+500+500+500+500+500+1000+3000+10+1000 |
+---------------------------------------------------------+
| 11909 |
+---------------------------------------------------------+
here is the all active project (con_trsaction.p_id=con_projects.id AND con_projects.status=1) id (p_id) From con_trsanction.amount
Try this:
SELECT SUM(t.amount)
FROM con_transactions t
INNER JOIN con_projects AS p ON t.p_id = p.id
WHERE p.status = 1
It's a simple join operation. Only 'active' projects will be accounted for in the summation.
SELECT SUM(amount) from con_transactions LEFT JOIN con_projects ON con_projects.id = con_transactions.p_id WHERE con_projects.status = 1;
I Have a Table Name transactions
+-------+---------------------+--------+
| t_id | date | amount |
+-------+---------------------+--------+
| 10 | 2016-04-17 19:24:05 | 1800 |
| 12 | 2016-06-11 12:40:13 | 200 |
| 17 | 2016-04-13 14:42:04 | 100 |
| 19 | 2016-05-14 17:45:43 | 1 |
| 20 | 2016-08-15 19:45:54 | 999 |
| 21 | 2016-01-17 11:46:02 | 1500 |
| 41 | 2016-02-18 17:23:14 | 500 |
| 42 | 2016-07-19 13:26:14 | 500 |
| 43 | 2016-02-18 17:23:15 | 500 |
| 44 | 2016-02-18 17:23:16 | 500 |
| 45 | 2016-02-18 18:23:16 | 500 |
| 46 | 2016-02-18 17:23:16 | 500 |
| 47 | 2015-10-18 14:23:17 | 500 |
| 48 | 2015-11-18 17:23:17 | 500 |
| 49 | 2015-12-18 11:23:18 | 500 |
| 50 | 2015-05-18 11:25:54 | 1000 |
| 51 | 2015-09-18 12:26:22 | 3000 |
| 52 | 2015-05-18 13:48:59 | 10 |
| 53 | 2015-03-18 15:48:59 | 10 |
| 54 | 2015-01-18 17:49:13 | 5000 |
+-------+---------------------+--------+
I want to SELECT record with date
I Use
SELECT * FROM transactions WHERE date='2016-02-18';
I Also SELECT between 2 date And I Use
SELECT * FROM transactions WHERE date<'2016-02-18' AND date>'2016-02-01';
But Its Not Working.(I use php Mysql xampp)
Can you help me understand the concepts?
For the ist query you need to use DATE() function becuase your column type is DATETIME or TIMESTAMP so you can handle it as:
SELECT * FROM transactions WHERE DATE(date) = '2016-02-18';
For second query you can simply add the TIME as:
SELECT * FROM transactions WHERE date > '2016-02-01 00:00:00' AND date < '2016-02-18 23:59:59';
Is it a mysql DB ?
If it is than use DATE function :
SELECT * FROM transactions WHERE DATE(date)='2016-02-18';
OR
SELECT * FROM transactions WHERE date BETWEEN '2016-02-18' AND '2016-02-01';
use this
use % and like in your query . you will get all results in given date
SELECT * FROM transactions WHERE date like '2016-02-18%';
you should use BETWEEN ... AND ...
SELECT * FROM transactions WHERE `date` BETWEEN '2016-02-01' AND '2016-02-18';
This is a revised version of my question.
I am currently designing a simple power quality monitoring tool. I have managed to design a MySQL database and populate the table with the voltage status and the corresponding time stamp taken at particular times. Below is my table structure.
+----+---------------------+-------------+
| Id | Time_Stamp | Red_Ph_Volt |
+----+---------------------+-------------+
| 1 | 2015-02-01 17:33:45 | 250.00 |
| 2 | 2015-02-01 18:53:41 | 250.00 |
| 3 | 2015-02-01 18:54:39 | 25.00 |
| 4 | 2015-02-01 18:54:54 | 242.00 |
| 5 | 2015-02-01 18:55:11 | 222.00 |
| 6 | 2015-02-02 21:00:29 | 250.00 |
| 7 | 2015-02-02 21:00:45 | 220.00 |
| 8 | 2015-02-02 21:00:55 | 230.00 |
| 9 | 2015-02-02 21:03:01 | 230.00 |
| 10 | 2015-02-02 21:03:36 | 250.00 |
| 11 | 2015-02-02 21:03:46 | 50.00 |
| 12 | 2015-02-06 17:54:08 | 0.00 |
| 13 | 2015-02-06 23:04:04 | 220.00 |
| 14 | 2015-02-06 23:04:34 | 220.00 |
| 15 | 2015-02-06 23:05:51 | 250.00 |
| 16 | 2015-02-08 16:04:44 | 220.00 |
| 17 | 2015-02-08 16:06:29 | 220.00 |
| 18 | 2015-02-09 09:04:12 | 220.00 |
| 19 | 2015-02-09 10:42:39 | 203.00 |
| 20 | 2015-02-09 19:34:43 | 203.00 |
| 21 | 2015-02-09 21:57:02 | 203.00 |
| 22 | 2015-02-10 09:47:08 | 0.00 |
| 23 | 2015-02-10 11:15:34 | 250.00 |
| 24 | 2015-02-10 11:48:14 | 250.00 |
| 25 | 2015-02-10 13:18:14 | 220.00 |
| 26 | 2015-02-10 18:59:52 | 0.00 |
| 27 | 2015-02-10 22:44:14 | 250.00 |
| 28 | 2015-02-10 22:47:10 | 212.00 |
| 29 | 2015-02-14 00:02:10 | 212.00 |
| 30 | 2015-02-14 00:28:57 | 242.00 |
| 31 | 2015-02-14 00:35:56 | 21.00 |
| 32 | 2015-02-16 12:11:47 | 21.00 |
+----+---------------------+-------------+
32 rows in set (0.02 sec)
Any voltage below 50V is taken as an outage. I want to write a query that queries the table for the TOTAL OUTAGE TIME between 2015-02-01 17:33:45 and 2015-02-14 00:28:57.
Guys, kindly help me on this because I really don’t know how to approach it.
This should work:
SELECT sum(timestampdiff(SECOND, Time_Stamp, (SELECT Time_Stamp
FROM voltage AS V2
WHERE V2.Id > V1.Id
LIMIT 1))) AS T
FROM voltage AS V1
WHERE Red_Ph_Volt <= 50 AND Time_Stamp BETWEEN '2015-02-01 17:33:45' AND '2015-02-14 00:28:57';
I have assumed that a voltage of 50 is also considered as an outage and I have assigned the table name as voltage.
The first step to properly address this kind of queries is to join every row with its previous row.
SELECT *
FROM voltage AS V
LEFT JOIN voltage AS PRV_V ON (PRV_V.Id = V.Id-1)
Now you just have to keep those rows which its previous measure was below 50 and sum their timestamp differences.
SELECT SUM(TIMESTAMPDIFF(SECOND, PRV_V.Time_Stamp, V.Time_Stamp))
FROM voltage AS V
LEFT JOIN voltage AS PRV_V ON (PRV_V.Id = V.Id-1)
WHERE PRV_V.Red_Ph_Volt < 50
AND V.Time_Stamp BETWEEN '2015-02-01 17:33:45' AND '2015-02-14 00:28:57'
EDIT: If your data don't have autoincremental ids or you have gaps between your ids you can still join your measurements with its previous measurement using the timestamp:
SELECT *
FROM voltage AS V
LEFT JOIN voltage AS PRV_V
ON ( PRV_V.Time_Stamp < V.Time_Stamp
AND NOT EXISTS ( SELECT *
FROM voltage
WHERE Time_Stamp > PRV_V.Time_Stamp
AND voltage.Time_Stamp < V.Time_Stamp
)
)
I have the followng data:
+----+---------+---------------------+-------+
| id | site_id | datetime | views |
+----+---------+---------------------+-------+
| 1 | 1 | 2013-11-01 23:59:59 | 12 |
| 2 | 1 | 2013-11-02 23:59:59 | 15 |
| 3 | 1 | 2013-11-03 23:59:59 | 18 |
| 4 | 1 | 2013-11-04 23:59:59 | 29 |
| 5 | 1 | 2013-11-05 23:59:59 | 38 |
| 6 | 1 | 2013-11-05 12:59:59 | 40 |
| 7 | 1 | 2013-11-06 23:59:59 | 45 |
| 8 | 1 | 2013-11-07 23:59:59 | 49 |
| 9 | 1 | 2013-11-08 23:59:59 | 52 |
| 10 | 2 | 2013-11-04 23:59:59 | 25 |
| 11 | 2 | 2013-11-05 21:59:59 | 42 |
| 12 | 2 | 2013-11-06 23:59:59 | 60 |
| 13 | 2 | 2013-11-07 23:59:59 | 75 |
| 14 | 2 | 2013-11-08 23:59:59 | 86 |
| 15 | 2 | 2013-11-09 23:59:59 | 90 |
| 16 | 2 | 2013-11-10 23:59:59 | 92 |
| 17 | 2 | 2013-11-11 23:42:59 | 98 |
+----+---------+---------------------+-------+
I would pass a day and wish to get the rows with the highest time in the given day or if there`s no records for this site for this day, the last available row in the past.
e.g. for 2013-11-01
+----+---------+---------------------+-------+
| id | site_id | datetime | views |
+----+---------+---------------------+-------+
| 1 | 1 | 2013-11-01 23:59:59 | 12 |
+----+---------+---------------------+-------+
for 2013-11-05
+----+---------+---------------------+-------+
| id | site_id | datetime | views |
+----+---------+---------------------+-------+
| 5 | 1 | 2013-11-05 23:59:59 | 38 |
| 11 | 2 | 2013-11-05 21:59:59 | 42 |
+----+---------+---------------------+-------+
and for 2013-11-10
+----+---------+---------------------+-------+
| id | site_id | datetime | views |
+----+---------+---------------------+-------+
| 9 | 1 | 2013-11-08 23:59:59 | 52 |
| 16 | 2 | 2013-11-10 23:59:59 | 92 |
+----+---------+---------------------+-------+
Thanks in advance.
You can try this:
SELECT a.id,a.site_id,b.maxDate,a.views
FROM table1 a
INNER JOIN (
SELECT site_id ,MAX(datetime) as maxDate
FROM table1
WHERE datetime < DATEYOUWANTTOSEE + INTERVAL 1 DAY
GROUP BY site_id
) b ON a.site_id = b.site_id AND a.datetime = b.maxDate
The inner query will get you the MAX(datetime) for each site_id. Then you join it with your table to get the rest of the information.
sqlfiddle demo
SELECT * FROM <tablename> WHERE datetime = <datetime> ORDER BY datetime DESC LIMIT 2
You data and examples do not really match themselves, nor the description, but what you probably are looking for is this:
Select top 1 *
from table
where date(datetime) <= date(#PARAMETER)
order by datetime desc
I believe this should work:
SELECT SUBSTRING(`datetime`, 1, 10) AS date, MAX(`views`)
FROM table
GROUP BY SUBSTRING(`datetime`, 1, 10)
If you need the id/site_id as well, write it in your post, as it is not clear
SQLFiddle to show you the result:
http://sqlfiddle.com/#!2/e0ccb3/1
I have two tables in a tournament-related database and I need to know the most optimized SQL query to generate the correct overall results. The results must show the total points scored, minus any penalties, and scores that are tied should be broken based on the person who reached that score first.
In the database tables, I have an event log where each score is added as teams proceed through the tournament, and I have another table which shows which team is part of which tournament.
Table "xTournamentTeam" (connects a team to a tournament)
=======================
+-----+------------+--------+--------------+
| nID | Team Name | TeamID | TournamentID |
+-----+------------+--------+--------------+
| 1 | Team A | 12 | 25 |
| 2 | Team B | 13 | 25 |
| 3 | Team C | 14 | 25 |
| 4 | Team D | 15 | 25 |
| 3 | Team A | 12 | 32 |
| 4 | Team B | 13 | 32 |
+-----+------------+--------+--------------+
Table "nEventLog" (records scoring during a tournament)
=================
+-----+---------------+---------+----------+----------------+-----------------------+
| nID | nTournamentID | nTeamID | nPoints | nPointsPenalty | nEventTime |
+-----+---------------+---------+----------+----------------|-----------------------+
| 1 | 25 | 15 | 100 | 0 | 1/24/2013 6:05:14 AM |
| 2 | 25 | 14 | 100 | 0 | 1/24/2013 6:29:55 AM |
| 3 | 25 | 14 | 100 | 25 | 1/24/2013 7:09:34 AM |
| 4 | 25 | 12 | 100 | 0 | 1/24/2013 7:12:28 AM |
| 5 | 25 | 12 | 100 | 0 | 1/24/2013 8:42:59 AM |
| 6 | 25 | 12 | 100 | 50 | 1/24/2013 8:43:36 AM |
| 7 | 25 | 14 | 100 | 0 | 1/24/2013 9:15:24 AM |
| 8 | 25 | 15 | 100 | 0 | 1/24/2013 9:15:27 AM |
| 9 | 32 | 12 | 100 | 0 | 1/28/2013 8:33:49 AM |
| 10 | 32 | 13 | 100 | 25 | 1/28/2013 2:15:12 PM |
| 11 | 32 | 12 | 100 | 10 | 1/28/2013 7:12:25 AM |
| 12 | 32 | 13 | 100 | 0 | 1/29/2013 7:18:06 AM |
+-----+---------------+---------+----------+----------------+-----------------------+
In the case of the above data, the query I need should generate the following results for Tournament #25:
+-------+------------+--------+--------------+---------------+---------------------+-----------------------------+
| nRank | Team Name | TeamID | TournamentID | nTotalPoints | nTotalPointsPenalty | nLatestEventTime |
+-------+------------+--------+--------------+---------------+---------------------+-----------------------------+
| 1 | Team A | 12 | 25 | 300 | 50 | 1/24/2013 8:43:36 AM |
| 2 | Team C | 14 | 25 | 300 | 25 | 1/24/2013 9:15:24 AM |
| 3 | Team D | 15 | 25 | 200 | 0 | 1/24/2013 9:15:27 AM |
| 4 | Team B | 13 | 25 | 0 | 0 | |
+-------+------------+--------+--------------+---------------+---------------------+-----------------------------+
For load purposes, I'm trying to avoid sub-queries at all costs since the final query should be as optimized as possible. The "nRank" column can be generated programatically... MySQL shouldn't have to return it, but I'm shoing it for reference.
The query I have that is the closest is this one, but it doesn't return "Team B" because they don't have any records in the "nEventLog" table for nTournamentID #25:
SELECT xTournamentTeam.nTeamName
, sum(nEventLog.nPoints) AS nTotalPoints
, xTournamentTeam.nTeamID
, max(nEventLog.nEventTime) AS nLatestEventTime
, sum(nEventLog.nPointsPenalty) AS nTotalPenaltyPoints
, xTournamentTeam.nTournamentID
FROM
xTournamentTeam
LEFT OUTER JOIN nEventLog
ON xTournamentTeam.nTeamID = nEventLog.nTeamID
WHERE
xTournamentTeam.nTournamentID = 33
AND nEventLog.nTournamentID = 33
GROUP BY
xTournamentTeam.nID
, xTournamentTeam.nTournamentID
ORDER BY
nTotalPoints DESC
, nLatestEventTime DESC
I'm certainly no expert in MySQL queries, and I've been working on this for two days without much success, so any help would be greatly appreciated.
I change your logic a little bit, I think it's working:
SELECT
xTournamentTeam.TeamName
, sum(nEventLog.nPoints) AS nTotalPoints
, xTournamentTeam.TeamID
, max(nEventLog.nEventTime) AS nLatestEventTime
, sum(nEventLog.nPointsPenalty) AS nTotalPenaltyPoints
, xTournamentTeam.TournamentID
FROM
xTournamentTeam
LEFT OUTER JOIN nEventLog
ON xTournamentTeam.TournamentID = nEventLog.nTournamentID AND xTournamentTeam.TeamID = nEventLog.nTeamID
WHERE
xTournamentTeam.TournamentID = 25
GROUP BY
xTournamentTeam.TeamID
, xTournamentTeam.TournamentID
, xTournamentTeam.TeamName
ORDER BY
nTotalPoints DESC
If you need, you can format nulls to represent 0 or something else.