membership table
membership start date 2011-01-10
membership end date 2012-09-08
membership monthly amount £120.00
member_Id
member table
member_id
the member will pay the money on 10 th of every month...can i get the total amount that the member has paid up to this date 2011-05-15 and is it possible using mysql query.
this is the sql query
suggested by you guys
SELECT TIMESTAMPDIFF(MONTH, membertomships.memberToMship_StartDate, MIN(membertomships.memberToMship_EndDate,'2011-7-06'))* memberToMship_ChargePerPeriod FROM membertomships WHERE membertomships.member_Id = '1';
but it was giving error at memberToMship_EndDate,'2011-7-06' between dates memberToMship_EndDate and this date '2011-7-06'
would you pls explain why it was giving error
SELECT TIMESTAMPDIFF(MONTH, start_date, MIN(end_date,'2011-05-15')) * amount from membership where membershipId=#memberdshipId
for include current date also.......
SELECT TIMESTAMPDIFF(MONTH, start_date, MIN(end_date,'2011-05-15')) * amount ,TIMESTAMPDIFF(MONTH, start_date, MIN(end_date,CURDATE())) * amount from membership where membershipId=#memberdshipId
this is working fine for me in ms sqlserver please change for mysql syntex
select datediff(Month,startdate,enddate)*amount as Enddateamount,
datediff(Month,startdate,'12/6/2010')*amount as amountongivendate,
datediff(Month,startdate,getdate())*amount as amounttoday
from membership
Your question isn't very clear, I don't think you've explained the schema properly. If you want to get a sum of everything each has paid:
select sum(amount) from membership group by member_id
Feel free to add a where clause should you wish to restrict it by date or member_id(s):
http://dev.mysql.com/doc/refman/5.0/en/select.html
I guess you have only subscription data not all payment table, right?
Then you need such statement
SELECT TIMESTAMPDIFF(MONTH, '2011-01-10', MIN('2012-09-08', '2011-05-15')) * 120.00
Of course you should replace the constants with column names and parameters.
This of course assumes that the payment day 10 is dependent on the subscription start date and the customer has paid on time. :)
Related
i am facing an issue with my query i store users login time_in and time_out and a column name called break where i manually enter how long user has been on break supposing an hour then 01:00:00 i store it on the break column and to get total login hours of a user i use
TIME_TO_SEC(TIMEDIFF(time_out, time_in)) AS totalhours
now from the above query how can i subtract the value i store it on break column and then show the total hours? only if break column has some value then subtract else show the correct total hours.
e.g if say users total login hours is 9hrs but then in break i added 1hr then the totalhours instead of showing 9hrs should rather display 8hrs.
here is my complete query
SELECT hours_id,
member_id,
username,
team,
time_in,
time_out,
break,
activity,
comments,
TIME_TO_SEC(TIMEDIFF(time_out, time_ink)) AS totalhours
FROM login_hours
WHERE DATE_FORMAT(date, '%Y-%m-%d') LIKE '".$filter_date."'
ORDER BY activity DESC
Really appreciate your help.
thanks
I sorted it by
TIME_TO_SEC(TIMEDIFF(time_out, time_in)) - TIME_TO_SEC(break) AS totalhours
it does gives the result im looking for. can someone tell me if its right?
I've just started a job and my boss wants me to learn mySQL so please bear with me, i've been learning for only 2 days and i'm not that good at it yet.
So i've been given 3 tables and several tasks to do.
The tables are:
mobile_log_messages_sms
mobile_providers
service_instances
And in them i've got to:
Find out how many messages there were in the last 25 days and how
much income did they make
Then i need to group them by day (so per day, exclude hours) and
provider name.
Also i need to ignore all the messages that have an empty string
under the service column
Also i need to ignore the messages that made 0 income and count only
those that have the column service_enabled = 1
And then i need to sort it descending, by date.
in the tables
mobile_log_messages_sms:
message_id - used to count the messages
price - using for price obviously, exlude those with 0
time - date in yyyy/mm/dd hh:mm:ss format
service - exclude all those that have an empty string (or null)
mobile_providers
provider_name - to use to group with
service_instances
enabled - only use if value is 1
I've started with:
SELECT message_id, price, time
FROM mobile_log_messages_sms
WHERE time BETWEEN '2017-02-26 00:00:00'
AND time AND '2017-03-22 00:00:00'
But i need to change the date format and then use the JOIN commands but i don't know how, and i know i need to add more to it, but i'm stumped even at the start. Also the starting just lists the messages but i need to count the total sum of the income (price) per day.
Can anyone point me in the right direction at least since i'm still a noob? Many thanks in advance and sorry if i worded something badly, english is not my first language.
Find out how many messages there were in the last 25 days and how much income did they make
1.
SELECT COUNT(message_id), SUM(price)
FROM mobile_log_messages_sms
WHERE CAST(time AS DATE) BETWEEN DATE_SUB(CURRENT_DATE,INTERVAL 25 DAY)
AND CURRENT_DATE;
2.
SELECT COUNT(message_id), SUM(price)
FROM mobile_log_messages_sms
WHERE CAST(time AS DATE) BETWEEN DATE_SUB(CURRENT_DATE,INTERVAL 25 DAY)
AND CURRENT_DATE
GROUP BY CAST(time AS DATE);
3.
SELECT COUNT(message_id), SUM(price)
FROM mobile_log_messages_sms
WHERE CAST(time AS DATE) BETWEEN DATE_SUB(CURRENT_DATE,INTERVAL 25 DAY)
AND CURRENT_DATE AND service IS NULL
GROUP BY CAST(time AS DATE);
rest can't done with join so make sure that at least one column should be common in tables.
I am sure this is pretty simple but I cannot figure out what I am doing incorrectly here. I am trying to get totals for certain fields over the last 7 days and group them by date and field.
Example:
The following table:
TABLE API_LOG
COLUMNS
customer
ip_address
date_logged
endpoint
So I would like to get the total endpoint calls for the last 7 days per customer and ip_address. I already have the daily totals organized but for some reason cannot get this to group correctly.
SELECT date_logged AS DATE_LOG, Customer,
COUNT(customer) AS CustomerCount, COUNT(ip_address) as IPCOUNT
FROM API_log
GROUP BY date_logged, Customer
The idea is then to bind this to a chart.
Sample data:
Expected output;
Current Output:
Appreciate assistance with this.
edit:
I group by date because I want to see the total for each day for the last 7 days. In other words, give me the total counts for each customer by day.
I think your date_logged column is datetime so it makes sense that you are getting separate dates when grouping. Try this
SELECT CAST(date_logged AS DATE) AS DATE_LOG, Customer,
COUNT(customer) AS CustomerCount, COUNT(ip_address) as IPCOUNT
FROM API_log
GROUP BY CAST(date_logged AS DATE), Customer
I have members that have to pay for their membership. And I store: payment date, and membership length (they can pay for 1 month or several).
Now I'd like to know which payments are overdue, or soon to be.
My logic was: get an expiration date for each membership (last payment date + membership length) and then just look at the highest value of that for each member.
Here's my query, but I did want to explain my reasoning, as you may want to question that or even the format of the DB (but please don't ask me to store the expiration date).
SELECT tbl.company AS company,
MAX(ADDDATE( paydate, INTERVAL paylength MONTH ))) AS expiration,
tbl.id
FROM tblPayments
JOIN tbl ON tblPayments.comp_id = tbl.id
GROUP BY expiration
ORDER BY expiration ASC
I've read that grouping by calculated fields may not be possible, but my knowledge of MySQL is not strong enough to understand the workarounds. I'd appreciate any help you can provide! Thanks!
You are grouping by the aggregated result of your group which is not possible and not what you intended. Based on your explanation I would think you are trying to do this
SELECT
tbl.company AS company,
MAX(ADDDATE( paydate, INTERVAL paylength MONTH )) AS expiration,
tbl.id
FROM tblPayments
JOIN tbl
ON tblPayments.comp_id = tbl.id
GROUP BY tbl.company , tbl.id
HAVING expiration < NOW()
ORDER BY expiration ASC
And #dleiftah brings up a good point, you cannot always use the alias although MySQL seems to let you in a GROUP BY whereas MSSQL never does. I forget exactly when...
I have a transaction table and I'm looking to generate a dataset to drive a line chart that shows the sum of all the sales happened on each day during a given period. I have never grouped results like that before and am scratching my head.
Let's say the table is called "transactions", the "datetime" field is called timestamp, and the sales amount on each transaction is "txn_amount". I want the result set to include each day: "1/2/10" and the sum of the transaction amounts.
I need to get a book and spend a week learning mysql... Thanks for helping me out.
select sum(txn_amount) ,timestamp from transactions where timestamp in (select distinct timestamp from transactions) group by timestamp
if datatype is datetime,Use this
select sum(amt) ,substring(dt,1,10) from transactions where substring(dt,1,10) in (select distinct substring(dt,1,10) from transactions) group by substring(dt,1,10)