mySQL query between two dates issue with sum - mysql

I have the next table named expenses_partial_payment.
My query is the next, i need the sum of amount_paid as total_paid:
SELECT id_partial_payment, epp.id_billing, epp.id_user, epp.month_of_payment,
COALESCE(SUM(epp.amount_paid),0) AS total_paid,
(be.total - amount_paid) AS pending_total , be.total as total
FROM expenses_partial_payment epp
INNER JOIN BillingExpenses be ON epp.id_billing = be.id_billing
WHERE epp.id_user = 23
but when i add in the clause where
WHERE epp.id_user = 23 AND (Month_of_payment BETWEEN '2015-01-01' AND LAST_DAY('2016-01-01'))
I have an error with the total_piad, the query returns only the last row (300) and don't sum the amount_paid , the correct sum must be 501.00
What can i do to get the correct sum adding the clause where to filter the month_of_payment
(month_of_payment BETWEEN '2015-01-01' AND LAST_DAY('2016-01-01'))

month_of_payment BETWEEN '2015-01-01' AND LAST_DAY('2016-01-01')
last day() returns the last day of the Month not year. So it will not pull up data in the month greater than January.

Try to replace your date with this
STR_TO_DATE('01/01/2015', '%m/%d/%Y')

Related

Substract the last value of the day from the first value of the day

I have an MySQL database with the following structure:
The following statement will get the max value of the day
select a1.*
from powerUsage a1
inner join
(
select
max(timestamp) as max
from powerUsage
group by date(timestamp)
) a2
on a1.timestamp = a2.max
However I need to substract the last value of the day from the first value of that day.
Any help is welcome.
Thank you in advance for your help.
You can get the result you need by using TIMESTAMPDIFF and SEC_TO_TIME MySQL functions.
The idea is to use the max and min MySQL functions to get the last and the first timestamp values of the day and then use them as arguments for the TIMESTAMPDIFF function, telling the function to return the result in seconds:
TIMESTAMPDIFF(SECOND,min(timestamp),max(timestamp))
We'll then use the result above as argument to the SEC_TO_TIME function to get the time difference between the last and the first timestamp values of the day in the format of hh:mm:ss
SEC_TO_TIME(TIMESTAMPDIFF(SECOND,min(timestamp),max(timestamp)))
All the above can be achieved be modifying your original query to:
select
a1.*
,a2.delta_time
from
powerUsage a1
inner join
(
select
max(timestamp) as max
,SEC_TO_TIME(TIMESTAMPDIFF(SECOND,min(timestamp),max(timestamp))) as delta_time
from powerUsage
group by date(timestamp)
) a2
on a1.timestamp = a2.max

ORDER BY datetime depending on the day AND time

I have a database field of type datetime.
The name of this field is "recallDate"
I would like to order the results in the following way:
The results must be chronological in the time: from newest to oldest
The results must be grouped by date: in other words, result having the same date are together, grouped
For every day, the results must be chronological according to the hour: earliest to latest
The results having no hour ( 00:00:00 ) have to be at the end of the results of the day
This is my actual query :
SELECT a.recallDate, a.id, a.id_company, com.name, a.recallType
FROM PDT_CRM.actions a
INNER JOIN PDT_CRM.traders as trad on trad.id=a.id_traders
WHERE DATE(a.recallDate) > DATE(NOW() + INTERVAL 30 DAY)
ORDER BY TIME(a.recallDate) , a.recallType
It is very likely that I have to use CASE but I don't understand how to use it.
You can use the following code to create a specific order that will put times '00:00:00' at the very end of the day:
...
ORDER BY date(a.rappelDate),
case when time(a.rappelDate) = 0 then 1 else 0 end,
time(a.rappelDate)

select rows as month according to timestamp

I have a table - accounts, and it has 3 columns: id , timestamp and value.
I need to create a query that returns a table which in each row it will be the month (2, 3, etc.) and the sum of the values from this month.
for example: if my table will have 3 rows from January and one row from February, the query will return a two-rows table within the first row it'll be 1 and the sum of January's values, and the second will be 2 and the sum of February's values.
I have no idea how to begin. can anyone help me?
all you need to do is sum the value and also use mysql's MONTH() function to pull out the month from your timestamp
SELECT SUM(value) as total_amount, MONTH(timestamp) as month_num
FROM table
GROUP BY month_num
GROUP BY is used when you have an aggregate function (in our case its SUM) to know how to group your common fields. without a group by you will have all rows summed together
SQL Server can use the DATEPART function.
SELECT DATEPART(MONTH, your_date_column) AS month_
, SUM(your_value) AS value_
FROM your_table
GROUP BY DATEPART(MONTH, your_date_column)

How do I get the max date and stage name for a certain value?

I am working on a query to pull the max date that is greater than a given period of time and have it return the value that is in that date.
I'm working from a salesforce table and want to pull the max date that an opportunity is in and return the stage name that it is in. The date has to be greater than 5-1-14. So do I have to break this up into multiple queries? Give me the max date then return the stage name that its in. Any help would be appreciated!! Thanks!
SELECT OpportunityID,
Max(case when CREATEDDATE < '2014-05-01' THEN STAGENAME END) as Q1_FY15_Stage
FROM [BVSFWarehouse].[dbo].[sf_OPPORTUNITYHISTORY]
GROUP by OPPORTUNITYID
You can filter your SELECT statement with a WHERE clause to limit the dates. Try:
SELECT
OpportunityID
,STAGENAME
,MAX(CREATEDATE)
FROM BVSWarehouse.dbo.sf_OPPORTUNITYHISTORY
WHERE
CREATEDATE > '2015-05-01'
GROUP BY
OpportunityID
,STAGENAME

mysql query how to sum attendance status values

I am new to mysql. In the above table i have to SUM attendance_status(column) depending on the date like
01/05/2013 att_stat=0
01/05/2013 att_stat=1
02/05/2013 att_stat=0
02/05/2013 att_stat=1
It's working pretty well when thr is only 3 record when I insert 4th one its not working
SELECT SUM(Attendance_Status) as total FROM student_attendance1 Where Attendance_Status='1' and Date= '"+datevalue+"'";'
Basically you are close just have to put GROUP BY
SELECT
SUM(Attendance_Status) AS total
FROM student_attendance1
WHERE Attendance_Status = '1'
AND DATE = '"+datevalue+"'"
GROUP BY DATE