Simple sql query, but stuck on it - sql

database mode
The only relevant table is 'employee' in the database model.
Asked: In which month are the most employee's birthdays?
By using
SELECT DATEPART(m, dateofbirth) AS month
FROM employee
I can actually see all the months for every employee and count it myself.
But how can I show the most common birthday month?
Thanks in advance!
recent output (for comment below)

You need to use GROUP BY. This groups up the separate month values. Once you've done that, you can apply COUNT, and then order the values in descending order on that statistic. Then you need to wrap that logic in a Common Table Expression, so you can select just the months that have the maximum COUNT.
WITH ranking AS (
SELECT
DATEPART(m, dateofbirth) AS month,
COUNT(*) as ct
FROM DM_MTA.dbo.employee
GROUP BY DATEPART(m, dateofbirth)
)
select
month
from
ranking
where ct = (select max(ct) from ranking)

This would give you exact month you're looking for:
SELECT TOP 1 DATEPART(m, dateofbirth) AS month
FROM employee
GROUP BY DATEPART(m, dateofbirth)
ORDER BY count(DATEPART(m, dateofbirth)) DESC

Related

Match month of outer query to month in subquery

I am attempting to get the total of a column (Amount) by Month:
SELECT
Date,
DATEPART(month, Date) AS Month,
DATEPART(year, Date) AS year,
(
SELECT
SUM(Amount)
FROM Transactions AS T
WHERE DATEPART(month, Date) = DATEPART(month, T.Date)
) AS Amount
FROM Transactions AS T
I know that this could be done in a straightforward manner using a group by clause as follows:
SELECT
DATEPART(month, Date) AS Month,
SUM(Amount) AS Total
FROM Transactions AS T
GROUP BY DATEPART(month, Date)
However I need to use subquery instead as the subquery will contain additional conditions and joins to assist in the calculation of a total based on rules from other tables.
I expected the first query to return all months and their respective totals, in the subquery I am matching Month of the transactions with the Month of the outer query row. However it simply returns the full SUM of all months for each month.
My guess is that my attempt to match the month is not quite correct, but I can't see how to fix.

Is there a way to count how many strings in a specific column are seen for the 1st time?

**Is there a way to count how many strings in a specific column are seen for
Since the value in the column 2 gets repeated sometimes due to the fact that some clients make several transactions in different times (the client can make a transaction in the 1st month then later in the next year).
Is there a way for me to count how many IDs are completely new per month through a group by (never seen before)?
Please let me know if you need more context.
Thanks!
A simple way is two levels of aggregation. The inner level gets the first date for each customer. The outer summarizes by year and month:
select year(min_date), month(min_date), count(*) as num_firsts
from (select customerid, min(date) as min_date
from t
group by customerid
) c
group by year(min_date), month(min_date)
order by year(min_date), month(min_date);
Note that date/time functions depends on the database you are using, so the syntax for getting the year/month from the date may differ in your database.
You can do the following which will assign a rank to each of the transactions which are unique for that particular customer_id (rank 1 therefore will mean that it is the first order for that customer_id)
The above is included in an inline view and the inline view is then queried to give you the month and the count of the customer id for that month ONLY if their rank = 1.
I have tested on Oracle and works as expected.
SELECT DISTINCT
EXTRACT(MONTH FROM date_of_transaction) AS month,
COUNT(customer_id)
FROM
(
SELECT
date_of_transaction,
customer_id,
RANK() OVER(PARTITION BY customer_id
ORDER BY
date_of_transaction ASC
) AS rank
FROM
table_1
)
WHERE
rank = 1
GROUP BY
EXTRACT(MONTH FROM date_of_transaction)
ORDER BY
EXTRACT(MONTH FROM date_of_transaction) ASC;
Firstly you should generate associate every ID with year and month which are completely new then count, while grouping by year and month:
SELECT count(*) as new_customers, extract(year from t1.date) as year,
extract(month from t1.date) as month FROM table t1
WHERE not exists (SELECT 1 FROM table t2 WHERE t1.id==t2.id AND t2.date<t1.date)
GROUP BY year, month;
Your results will contain, new customer count, year and month

Create new table with number of incidents per month

Whats up mates , i have already started to learn SQL database thing and i am confused here . i have to create a table with number of incidents per month.
I already know how to create table but the rest ?
SELECT
EXTRACT(month FROM dateofcall) AS x,
incidentnumber,
dateofcall
FROM
incidents
GROUP BY
incidentnumber,
x
ORDER BY
x ASC;
But its not giving me the results of incidents number per month . =(
It looks like you are grouping by too many items in your GROUP BY clause, and you are not COUNTing your incidents, just showing their details.
Try this:
SELECT EXTRACT(month FROM dateofcall) AS x,
COUNT(*) AS incidents
FROM
incidents
GROUP BY
EXTRACT(month FROM dateofcall)
ORDER BY
EXTRACT(month FROM dateofcall)
SELECT
EXTRACT(month FROM dateofcall) AS theMonth,
COUNT(*) AS theNumberOfIncidents
FROM
incidents
GROUP BY
EXTRACT(month FROM dateofcall)
ORDER BY
theMonth
Your original query wasn't counting anything. You were also grouping by incidentNumber which I assume is your primary-key, which is a nonsensical operation.
Due to a quirk in the SQL language you cannot use a column alias in GROUP BY statements, which is why you need to duplicate the EXTRACT(month FROM dateofcall) code.

How do I correctly use the SQL Sum function with multiple variables and grouping?

I am trying to write an SQL statement based on the following code.
CREATE TABLE mytable (
year INTEGER,
month INTEGER,
day INTEGER,
hoursWorked INTEGER )
Assuming that each employee works multiple days over each month in a 3 year period.
I need to write an sql statement that returns the total hours worked in each month, grouped by earliest year/month first.
I tried doing this, but I don't think it is correct:
SELECT Sum(hoursWorked) FROM mytable
ORDER BY(year,month)
GROUP BY(month);
I am a little confused about how to operate the sum function in conjunction with thee GROUP BY or ORDER BY function. How does one go about doing this?
Try this:
SELECT year, month, SUM(hoursWorked)
FROM mytable
GROUP BY year, month
ORDER BY year, month
This way you will have for example:
2014 December 30
2015 January 12
2015 February 40
Fields you want to group by always have be present in SELECT part of query. And vice-versa - what you put in SELECT part, need be also in GROUP BY.
SELECT year, month, Sum(hoursWorked)as workedhours
FROM mytable
GROUP BY year,month
ORDER BY year,month;
You have to group by year and month.
Is this what you are trying to do. This will sum by Year/Month and Order by Year/Month.
Select [Year], [Month], Sum(HoursWorked) as WorkedHours
From mytable
Group By [Year], [Month]
Order by [Year], [Month]
You have to group by year and month, otherwise you will have the hours you worked on March 2014 and 2015 in one record :)
SELECT Sum(hoursWorked) as hoursWorked, year, month
FROM mytable
GROUP BY(year, month)
ORDER BY(year,month)
;

Logical error in my SqlCode

I need to run a report that give the count of record with specific createria Per Month.For each Month my query display the month more than once. Could it be that am doing something wrong: My script:
Select DATEPART(mm,DatePrinted),COUNT(ReceiptNo)As CardPrinted
from mytble where ReceiptNo like'990%'
Group by DatePrinted
posible receipts:800,75.
Am expected something like:
January totalcount
Feb totalcount etc.
Use Group by DATEPART(month,DatePrinted).
Select DATEPART(month,DatePrinted) As MyMonth, COUNT(ReceiptNo) As CardPrinted
From mytble
Where ReceiptNo like '990%'
Group by DATEPART(month,DatePrinted)
If you need name of the month, then use DATENAME() function:
Select DATENAME(month,DatePrinted) As MyMonth, COUNT(ReceiptNo) As CardPrinted
From mytble
Where ReceiptNo like '990%'
Group by DATENAME(month,DatePrinted)
Note: May be you need to group by year to get correct results. Otherwise, you will get the count of similar months regardless of the year. If you are looking for a particular year, add this filter to the WHERE clause Year(DatePrinted) = yourYear
Your group by statement is wrong, it must be on DATEPART(mm,DatePrinted)
SELECT DATEPART(mm, DatePrinted) AS [Month], COUNT(ReceiptNo) As CardPrinted
FROM mytble
WHERE ReceiptNo LIKE '990%'
GROUP BY DATEPART(mm, DatePrinted)
You can also replace COUNT(ReceiptNo) by COUNT(*).
Also note that as it is right now, all months of different years will be grouped together.
If that isin't the desired behaviour you can SELECT and GROUP BY DATEPART(yyyy, DatePrinted), DATEPART(mm, DatePrinted)