Select from 2 tables only the new entries in table 2 - mysql

I got 2 tables, Customers and Payment. I'm trying to select only the new customers that have payments in the specified month and year, and no previous payments in another month.
table Customer
id - name
table Payment
id - id_customer - month - year - amount
SELECT * FROM customer, payment
WHERE Customer.id = Payment.id_customer
AND month = '$month'
AND year = '$year'
That gets me all the payments in a specific month and year, but I don't know how to exclude all the customers that had other previous payments.
Thank you for your time.

I don't think that you could achieve this without a third table. What you can do is create a third table with all the ids that you have selected in query and update it every time you run a select query.
Then the below query might work:
SELECT * FROM customer c, payment p WHERE c.id = p.id_customer
AND month = '$month'AND year = '$year'AND p.id NOT IN (SELECT id FROM
third_table)
Hope it answers your question.

To get the first date of payment, use GROUP BY. But, you will have to convert the value to something like a date first:
SELECT p.id_customer, MIN(CONCAT_WS, '-', p.year, p.month)) as first_yyyymm
FROM payment p
GROUP BY p.id_customer;
You should store the payment date as a date.

Related

counting occurrences between dates of different date intervals

I have a query that give me a table like this:
Person | Date_IN | Date_OUT | Structure
During a year a person ENTER and EXIT many times, ENTER and EXIT could be also the same day.
I'd like to count, for a specific day of year, how many person were IN each structure.
The final goal is to have, for a given period (1st march --> 31st march), the sum of total person for each day for each structure.
I believe the following would work. It assumes that you have a table of dates (consists of one column which contains all the dates between 1950 and 2050) and you simply join it with the person check in/out table:
SELECT dates.date, Structure, COUNT(DISTINCT Person) Persons_on_That_Date
FROM dates
LEFT JOIN turndata ON dates.date BETWEEN Date_IN AND Date_OUT
WHERE dates.date BETWEEN '2018-03-01' AND '2018-03-31'
GROUP BY dates.date, Structure
ORDER BY Structure, dates.date
Demo Here
Note: the above assumes that the out date is inclusive (the person is counted as inside on that date). If out date is exclusive then the ON clause becomes:
... ON Date_IN <= dates.date AND dates.date < Date_OUT
Please use below query, data is grouped by structure for particular timeframe.
SELECT structure, COUNT(DISTINCT person) as no_of_person
FROM table_name
WHERE DATE(Date_IN) BETWEEN '2018-08-01' AND '2018-08-31'
GROUP BY structure
You say there can be no multiple date_in for the same day and person, because a person is in at least one day. So for a given date we only must look at the latest event per person until then to see whether the person is/was in that day.
These are the steps:
create a data set for the requiered days on-the-fly
join with the table and get the last date_in until that day per person
join with the table again to get the last records
aggregate per day and count persons present
This is:
select
data.day
sum(t.date_in is not null and (t.date_out is null or t.date_out = data.day)) as count_in
from
(
select days.day, t.person, max(t.date_in) as max_date_in
from (select date '2018-03-01' as day union all ...) days
left join t on t.date_in <= days.day
group by days.day, t.person
) data
left join t on t.person = data.person and t.date_in = data.max_date_in
group by data.day
order by data.day;

Query Between two date fields

I have one table of employees with a column called Hire Date. And I have another table of orders and every order has a date when it was processed.
Now I want to figure out how many orders this employee made in the first 30 days from his hire date. I made a query with DateAdd function in access and made a column with all employees 30 days after hire date, now I want to make a query of orders between hire date and 30 days after hire date.query from 30 days after hire date
Assume Employees table has EmpCode and Orders table has EmpCode too
It will be something like this
SELECT *
FROM Orders INNER JOIN Employees ON Orders.EmpCode = Employees.EmpCode
WHERE EmpCode = 'ABCD' AND DateAdd ( d, 30, Employees.HireDate) > Orders.OrdDate
Expand your query to include the EmployeeId. Then it could be:
Select
YourQuery.FirstName, YourQuery.LastName, Count(OrderTable.*) As OrderCount
From
YourQuery,
OrderTable
Where
OrderTable.EmplyoyeeId = YourQuery.EmplyoyeeId
And
OrderTable.OrderDate Between YourQuery.[Hire Date] And YourQuery.[30 days after]
Group By
YourQuery.FirstName,
YourQuery.LastName

How to filter out records from one table where its id occurs in a column of another table

I have 2 MySQL tables
tableRooms contains the rooms of a hotel
tableRoomsBooked contains the booked dates of the rooms
I need an SQL query that returns the rooms that have no bookings between 2 given dates. This is what I have got so far:
SELECT * FROM `tableRooms`
LEFT JOIN `tableRoomsBooked`
ON `tableRooms`.`id` = `tableRoomsBooked`.`room_id`
WHERE (date BETWEEN '2015-01-02' AND '2015-01-30')
....?
The query should only get the room_id 2 because room 2 has no bookings in this period.
What should my query be like?
select *
from tableRooms
where id not in (
select distinct room_id
from tableRoomsBooked
where date between '2015-01-02' and '2015-01-30'
)
This will select the list of existing IDs in a sub request, then exclude them from the main request.
Anyway, you should change the name of "date" column, because "date" can be confusing as soon as it is a data type too.

query for succesive customer orders

How do I write a query to display the cust_id and cust_name_last for each customer who had orders in two successive months in the current year. (successive meaning they follow each other 'may, june')
for example: customer 3 has orders in May and June of this year.
select cust_id, cust_name_last
from customer
where date_sub (order_date, interval 1 month)
and date_sub (order_date, interval 2 months)
"I just want to know how to find customers with orders in consecutive months in a year"
Could you try this?
SELECT DISTINCT month1.cust_id, month1.cust_name_last
FROM customer month1 INNER JOIN customer month2
ON month1.cust_id = month2.cust_id
AND YEAR(month2.order_date) = YEAR(month1.order_date)
AND MONTH(month2.order_date) - MONTH(month1.order_date) = 1;
If you want to find consecutive orders including another years (e.g 2013-12 => 2014-01), need to check overflows something like as follows
SELECT DISTINCT month1.cust_id, month1.cust_name_last
FROM customer month1 INNER JOIN customer month2
ON month1.cust_id = month2.cust_id
AND (YEAR(month2.order_date) - YEAR(month1.order_date)) * 12 + (MONTH(month2.order_date) - MONTH(month1.order_date)) = 1;
If preceding SQL does not work for you, We are highly appreciated when you post your schema and sample data on sqlFiddle http://www.sqlfiddle.com/.

MySql SUM and JOIN

I am trying to count sales made by a list of sales agents, this count is made every few minutes and updates a screen showing a 'sales leader board' which is updates using a Ajax call in the background.
I have one table which is created and populated every night containing the agent_id and the total sales for the week and month. I create a second, temporary table, on the fly which counts the sales for the day.
I need to combine the two tables to create a current list of sales for all agents in agent_count.
Table agent_count;
agent_id (varchar),
team_id (varchar),
name (varchar),
day(int),
week(int),
month(int)
Table sales;
agent_id (varchar),
day(int)
I can't figure out how to combine these tables. I think I need to use a join as all agents must be returned - even if they don't appear in the agent_count table.
First I make a simple call to get the week and month totals for all agents
SELECT agent_id, team_id, name, week, month FROM agent_count;
the I create a temporary table of todays sales, and then I count the sales for each agent for the day
CREATE TEMPORARY TABLE temp_todays_sales
SELECT s.id, s.agent_id
FROM sales s
WHERE DATEDIFF(s.uploaded, NOW()) = 0
AND s.valid = 1;
SELECT tts.agent_id, COUNT(tts.id) as today
FROM temp_todays_sales tts
GROUP BY tts.agent_id;
What is the best/easiet way to combine these to end up with a resultset such as
agent_id, team_id, name, day, week, month
where week and month also include the daily totals
thanks for any help!
Christy
SELECT s.agent_id, ac.team_id, ac.name,
s.`day` + COALESCE(ac.`day`, 0) AS `day`,
s.`day` + COALESCE(ac.`week`, 0) AS `week`,
s.`day` + COALESCE(ac.`month`, 0) AS `month`
FROM sales s
LEFT JOIN
agent_count ac
ON ac.agent_id = s.agent_id
team_id and name will be NULL if there is no record in agent_count for an agent.
If the agents can be missing from both tables, you normally would need to make a FULL JOIN but since MySQL does not support the latter you may use its poor man's substitution:
SELECT agent_id, MAX(team_id), MAX(name),
SUM(day), SUM(week), SUM(month)
FROM (
SELECT agent_id, NULL AS team_id, NULL AS name, day, day AS week, day AS month
FROM sales
UNION ALL
SELECT *
FROM agent_count
) q
GROUP BY
agent_id