MS Access Query To Show Last Transaction - ms-access

I have a table called tblTransactions. On this table I log all Payments and Invoices for each customer Account. Each Payment or Invoice is its own record. In essence it's a simple accounting table showing debits and credits. My goal is to create a query which shows, by account, the last 'Payment' date and how many days ago that was. I'm sure this is fairly easy, but I've been out of Access for a while. Any help is appreciated!
Fields:
AccountID,
Transaction_Date,
Transaction_Type ("Payment" or "Invoice")

This should do the trick:
SELECT AccountID,
MAX(Transaction_Date) AS LastTranDate ,
Datediff("d",MAX(Transaction_Date),Date()) AS DaysAgo
FROM tblTransactions
WHERE Transaction_Type = 'Payment'
GROUP BY AccountID

Related

How to execute SQL query for selection of info from more than 1 table

I wanted to select values from one table according to another table
I have a booking system with 'user' information table and 'appointment table', they have columns:
user_id, name, surname, telephone_number
appointment_id, user_id, appointment_date_time
I want to make a SQL query so that I can choose a date, for example, 20th of May and retrieve all the booking's on 20th of May, but in different times, like 12:00, 9:00, 14:00, 15:00, 10:00 and so on, as they are not ordered in the table
Sort the time of the appointments so that it will be: 9:00, 10:00, 12:00, 14:00, 15:00
Select user_ids of the bookings from 'appointments'
Select the user credentials through 'user' table like their names, surname, and phone number
At the end I am going to implement this into the admin panel
So that when the admin will choose a specific date
They can see all the appointment bookings, their time, and who booked them.
Could you please tell me what kind of query I can use (preferable a complex one)
or direct me to useful and understandable resources?
SELECT appointments.appointment_index, appointments.appointment_time, users.name,
users.surname, users.telnumber
FROM users
INNER JOIN appointments
ON users.user_id = appointments.user_id
WHERE appointment_date = '$date'
ORDER BY appointment_time";

How to exclude total number of holidays from total days in MySQL query?

I'm working on ticketing system application. It consists of tickets and holidays tables.
I'm showing the ticket completed days by calculating the difference between the ticket created_date and ticket closed_date.
But my requirement is to EXCLUDE the holidays from completedInDays. The problem here is, I don't have any common column in tickets and holidays tables to join them both.
Here is the tickets table for the sample.
And, below is the holidays table.
Now, is it possible to exclude the holidays counts from ticket completed days using SQL or Javascript?
I've tried something like this.
SELECT *,(DATEDIFF('2020-07-16','2020-07-08') - SUM(DATEDIFF(end_date,holiday_date))) as diff
FROM holidays
where ((holiday_date BETWEEN '2020-07-08' AND '2020-07-16')
OR (end_date BETWEEN '2020-07-08' AND '2020-07-16'))
AND approval_date <> '0000-00-00';
The query to I've used to get the ticket completed days is,
SELECT *, DATEDIFF(ticket_closedOn,ticket_createdOn) as completedInDays from tickets;
The case is quite big to build a test case for writting a query. So the query shows the idea, I haven't checked it:
SELECT
DATEDIFF(ts.ticket_closedOn, ts.ticket_createdOn) total_time,
SUM(DATEDIFF(
GREATEST(ts.ticket_createdOn, hs.holiday_date),
LEAST(ts.ticket_closedOn, hs.end_date)
)) holidays,
DATEDIFF(ts.ticket_closedOn, ts.ticket_createdOn) -
SUM(DATEDIFF(
GREATEST(ts.ticket_createdOn, hs.holiday_date),
LEAST(ts.ticket_closedOn, hs.end_date)
)) pure_time_spent
FROM
tickets ts
LEFT JOIN
holidays hs ON
hs.holiday_date BETWEEN ts.ticket_createdOn AND ts.ticket_closedOn OR
hs.end_date BETWEEN ts.ticket_createdOn AND ts.ticket_closedOn
AND hs.approval_date <> '0000-00-00'
GROUP BY
ts.id, ts.ticket_createdOn, ts.ticket_closedOn

select total of field for the last 7 days and group by date and values

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

calculating the total money up to the mention date using mysql query

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. :)

Need mysql query to add values for each day in a time series... Need help

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)