Finding those employees who had joined in 1st quarter of the year - mysql

Finding record of those employees who had joined in 1st quarter of the year,I have tried the following query but I want the query without specifying the explicit date.
select * from employee where DOJ between '2015-01-01' and '2015-03-31';

Use quarter
select * from employee where year(DOJ) = 2015 and quarter(DOJ) = 1;

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;

related to query using SQL

In oracle sql, how to get the count of newly added customers only for the month of april and may and make sure they werent there in the previous months
SELECT CUSTOMER ID , COUNT(*)
FROM TABLE
WHERE DATE BETWEEN '1-APR-2018' AND '31-MAY-2018' AND ...
If we give max (date) and min(date), we can compare the greater date to check if this customer is new , correct?
expected output is month count
april ---
may ---
should show the exact count how many new customers joined in these two months
One approach is to use aggregation:
select customer_id, min(date) as min_date
from t
group by customer_id
having min(date) >= date '2018-04-01 and
min(date) < date '2018-06-01';
This gets the list of customers (which your query seems to be doing). To get the count, just use count(*) and make this a subquery.

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

Select from 2 tables only the new entries in table 2

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.

Is this query possible?

Here is my table:
table employee (
char(50) name
datetime startdate
datetime finishdate
}
Assume that at least one employee started everyday since the start of the business so that
select distinct startdate from employee
would return every day the business was open. I've already provided a query to get every day the business was open, but would it be pair each day with the number of employees that were employed on that day? Essentially I am asking whether it is possible to count the number of employees for which (startdate <= day AND finishdate >= day) is true for each day and return that relation in one query.
SELECT x.startDate, COUNT(e.startDate)
FROM (SELECT DISTINCT startDate FROM employee) AS x
INNER JOIN employee AS e
ON x.startDate BETWEEN e.startDate AND e.finishDate
GROUP BY x.startDate
SELECT e1.startdate, COUNT(*) AS num_employed_on_date
FROM employee e1 INNER JOIN employee e2
ON e1.startdate BETWEEN e2.startdate and e2.finishdate
GROUP BY e1.startdate
'startdate' holds the date an employee joined the company.
If you get MIN for startdate and MAX for finishdate, you'll have the entire span of time the company was open.
What you suggested would return every date at least one employee joined the company / started working.