Retrieving Date function in sql - sql

I have an employee table with the hire_date column.
I am stuck with one query related to the date function, where I have used data type 'DATE' to insert date of hiring and using DATE_FORMAT fun. to retrieve no. of employees hired in every month, but in SQL-server it is not supporting the date_format function.
I'm using SQL -server
Query: - list of the no.of employee hired every month in ascending order.
select date_format(hire_date,'%b') month, count(*)
from employee
group by DATE_FORMAT(hire_date,'%b')
order by month

date_format(hire_date,'%b') in MySQL will return abbreviated monthname. However, you can still have this functionality by combining MONTHNAME with LEFT in SQL Server.
select LEFT(DATENAME(MONTH,hire_date),3) month, count(*)
from employee
group by LEFT(DATENAME(MONTH,hire_date),3)
order by month

select Month(Hire_Date),count('x') 'count' from employee
group by Month(Hire_Date)
order by Month(Hire_Date) asc

Instead, you can directly use:
select MONTH(hire_date), count(*)
from employee
group by MONTH(hire_date)
order by MONTH(hire_date)
or
select hire_date.MONTH, count(*)
from employee
group by hire_date.MONTH
order by hire_date.MONTH

Related

Use of Group BY in more than 1 column

I have made an table which has 3 columns
Table
In this above table there are 3 columns i.e Trandsaction, EmpID, Date having some data. I need to form a table using some queries so that in the result table I will get how many transactions done in each month by a particular EmpID.
So the result table must be like:
So how to get month wise number of transactions per EmpID?
Get all employees (I assume you have an employee table).
Get all months (you could get them from the transaction table).
Cross join the two in order to get all combinations, because you want to show these in your result.
Outer join the transaction counts per month and employee (an aggregation subquery). SQL Server's date to string conversion is a bit awkward compared to other DBMS. You need to convert to a predefined format and use a substring of that.
Use COALESCE to turn null (for no count for the employee and month) to zero.
The query:
select m.month, e.empid, coalesce(t.cnt, 0) as transaction_count
from employees e
cross join (select distinct convert(varchar(7), t.date, 126) as month from transactions) m
left join
(
select convert(varchar(7), t.date, 126) as month, empid, count(*) as total
from transactions
group by convert(varchar(7), t.date, 126), empid
) t on t.empid = e.empid and t.month = m.month
order by m.month, e.empid;
If you don't want all employees, but only those that have at least one transaction in some month, then replace from employees e with from (select distinct empid from transactions) e.
SELECT Date, EmpID, COUNT(transaction)
FROM your_table
GROUP BY Date, EmpID
To get the date like "YYYY-MM" use
for MySQL (replace the String with the date column):
DATE_FORMAT("2019-11-25 10:49:30.000", "%Y - %m")
for oracle:
to_char(date, 'YYYY-MM')

SQL Server data search with date range

I have a table with the following columns:
Date
Skills,
Customer ID
I want to find out Date(x), Customers, Count of Customers in between Date(x) and Date(x)+6
Can somebody guide me how to make this query, or can I create this function in SQL Server?
If I understand you correctly, you want something like this:
(take care, can be bad syntax, because i "work" only with oracle. But I think that it should work)
select date, customer_id, COUNT(*)
from your_table --add your table
where date between getdate() and DATEADD(day, 6, getdate())
-- between current database system date and +6 day
group by date, customer id
order by COUNT (*) desc -- if you want, you can order your result - ASC||DESC
If you have data on each date, then perhaps this is what you want:
select date, count(*),
sum(count(*)) over (order by date rows between 6 preceding and current row) as week_count
from t
group by date;

SQL query for multiple values of a column

I have db with names etc with date of birth. How can I get count of columns for all 12 months of the dates?
Exact code depends on the database you use; you should, somehow, "extract" month from date of birth in order to GROUP BY it.
In Oracle, you might have done it as
select to_char(date_of_birth), 'mon') dob_month,
count(*)
from your_table
group by to_char(date_of_birth, 'mon');
or
select extract(month from date_of_birth) dob_month,
count(*)
from your_table
group by extract(month from date_of_birth);

Basic SQL Query Request

SQL: There are 3 columns in the table and they are employee_id, month, and working hour. How do you create a SQL query to return monthly total of working hour for each employee?
select employee_id
, month
, sum(working_hour) as [monthly total of working hour]
from table_name
group by employee_id, month
you can use something like below:
"select sum(working_hour) from emplyee_table group by employee_id,month"

SQL - select max of datetime by day

Using the below example, I want to get a count of records grouped by Person and Product, for each day.
The example on the left is the data in the SQL Server 2008 database. The data on the right is my desired query result.
I could just substring off the time portion of the date values, then just do a Group By... but ideally the Max(date) would contain the full timestamp...
Is this what you want?
select t.person, t.product, count(*) as cnt, max(t.date) as date
from table t
group by t.person, t.product, cast(t.date as date);