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"
Related
I have a table with the following attributes, "id_commande, id_client, id_agent, D_commande, Heure_commande, prix_total".
D_commande represents DATE OF SALE
prix_total represents the price of a sale.
when I tried to create a Query to Get the "prix_total" per month. I struggled.
notice that I tried this query but I don't get the correct result:
SELECT sum(prix_total)
FROM commande
GROUP BY (
SELECT strftime('%y','D_commande')
)
You can use
SELECT strftime('%Y-%m',D_commande) AS month,
SUM(prix_total) AS prix_total
FROM commande
GROUP BY strftime('%Y-%m',D_commande)
ORDER BY month
in order to get the result partitioned by months
SELECT sum(prix_total)
FROM commande
GROUP BY strftime('%Y',D_commande)
, strftime('%m',D_commande)
Here is a demo
And also you can order by year and month:
ORDER BY strftime('%Y',D_commande)
, strftime('%m',D_commande)
my table name is CustomerDetails and it has the following columns:
customer_id, login_id, session_id, login_date
i am trying to write a query that calculates the average number of customers login in per day.
i tried this:
select avg(session_id)
from CustomerDetails
where exists (select count(session_id) from CustomerDetails as 'no_of_entries')
.
but then i realized it was going straight to the column and just calculating the average of that column but that's not what i want to do. can someone help me?
thanks
The first thing you need to do is get logins per day:
SELECT login_date, COUNT(*) AS loginsPerDay
FROM CustomerDetails
GROUP BY login_date
Then you can use that to get average logins per day:
SELECT AVG(loginsPerDay)
FROM (
SELECT login_date, COUNT(*) AS loginsPerDay
FROM CustomerDetails
GROUP BY login_date
)
If your login_date is a DATE type you're all set. If it has a time component then you'll need to truncate it to date only:
SELECT AVG(loginsPerDay)
FROM (
SELECT CAST(login_date AS DATE), COUNT(*)
FROM CustomerDetails
GROUP BY CAST(login_date AS DATE)
)
i am trying to write a query that calculates the average number of customers login in per day.
Count the number of customers. Divide by the number of days. I think that is:
select count(*) * 1.0 / count(distinct cast(login_date as date))
from customerdetails;
I understand that you want do count the number of visitors per day, not the number of visits. So if a customer logged twice on the same day, you want to count him only once.
If so, you can use distinct and two levels of aggregation, like so:
select avg(cnt_visitors) avg_cnt_vistors_per_day
from (
select count(distinct customer_id) cnt_visitors
from customer_details
group by cast(login_date as date)
) t
The inner query computes the count of distinct customers for each day, he outer query gives you the overall average.
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
select
id
,id_name
, MAX(last_login_date)
, SUM(transaction_count)
, mAX(last_transaction_date)
from sales;
hi I am looking for the results to only include a transaction count for the sales made in the last 12 months. what can I do?
I have max and sum because there are multiple instances of the same ids so they are not unique.
I don't have individual transaction dates. I only have a last transaction date field
You may use months_between function to have 12 months directly :
select id,id_name, MAX(last_login_date), SUM(transaction_count), mAX(last_transaction_date)
from sales
where months_between(trunc(sysdate),last_transaction_date) <= 12
group by id, id_name;
if you need to select all transactions in a month (with current month), you can use this construction:
select id
, id_name
, Max(last_login_date)
, Sum(transaction_count)
, Max(last_transaction_date)
from sales
where last_transaction_date >= add_months(trunc(sysdate,'mm'),-11)
group by id, id_name;
I'm trying to display the average number of counts/records for each month in 2016. The following code does not display each month, rather displays only the monthly average for 2016:
SELECT AVG(DISTINCT DayCnt) AS AvgCnt
FROM
(
SELECT COUNT(*) As DayCnt
FROM table
WHERE YEAR(Insert_Date) = '2016'
GROUP BY MONTH(Insert_Date)
)
AS AvgCnt
You first should be group your result per month and days and count daily inserted records, after that to get average of per month inserted records try this:
SELECT monthGroup, AVG(DayCnt) AS AvgCnt
FROM
(
SELECT MONTH(Insert_Date) monthGroup, DAY(Insert_Date) dayGroup, COUNT(*) As DayCnt
FROM table
WHERE YEAR(Insert_Date) = '2016'
GROUP BY MONTH(Insert_Date), DAY(Insert_Date)
)
AS AvgCnt
GROUP BY monthGroup
I'm not sure since you didn't post the data but if I understand correctly, this should work for you:
SELECT AVG(DayCnt) AS AvgCnt, mth
FROM
(
SELECT COUNT(*) As DayCnt, MONTH(Insert_Date) as mth
FROM table
WHERE YEAR(Insert_Date) = '2016'
GROUP BY MONTH(Insert_Date), DAY(Insert_Date) )
AS AvgCnt
GROUP BY mth