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)
Related
I have tried using the following query
select distinct Year (SaleDate) AS SaleYear,Max(SalePrice)
from Sale
group by SaleDate
The years 2010 and 2014 are showing twice,even though i used distinct and group by. the amounts in Maxprice are different as well. am i doing something wrong here?
You need to repeat year() in the group by:
select Year(SaleDate) AS SaleYear, Max(SalePrice)
from Sale
group by year(SaleDate);
SELECT DISTINCT with GROUP BY is almost never correct. All that your query does is aggregate by SaleDate and in the result set extract the year. That is why you see duplicates.
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
I have a table that sends out messages, I would like to get a total count of the messages that have been going out month by month over the last year . I am new to SQL so I am having trouble with it . I am using MSSQL 2012 this is my sql
SELECT sentDateTime, MessageID, status AS total, CONVERT(NVARCHAR(10), sentDateTime, 120) AS Month
FROM MessageTable
WHERE CAST(sentDateTime AS DATE) > '2017-04-01'
GROUP BY CONVERT(NVARCHAR(10), sentDateTime, 120), sentDateTime, MessageID, status
ORDER BY Month;
I think the month() and year() functions are more convenient than datepart() for this purpose.
I would go for:
select year(sentDateTime) as yr, month(sentDateTime) as mon, count(*)
from MessageTable
where sentDateTime > '2017-04-01'
group by year(sentDateTime), month(sentDateTime)
order by min(sentDateTime);
Additional notes:
Only include the columns in the select that you care about. This would be the ones that define the month and the count.
Only include the columns in the group by that you care about. Every combination of the expressions in the group by found in the data define a column.
There is no need to convert sentDateTime to a date explicitly for the comparison.
The order by orders the results by time. Using the min() is a nice convenience.
Including the year() makes sure you don't make a mistake -- say by including data from 2018-04 with 2017-04.
-- this selects the part of the date you are looking for, replace this with the date format you are using, this should give you what you are looking for
SELECT DATEPART(mm, GETDATE())
SELECT COUNT(DATEPART(mm, sentDateTime)), MessageID, status
From MessageTable where Cast(sentDateTime as date) > '2017-04-01'
group by DATEPART(mm, sentDateTime), MessageID, status
order by DATEPART(mm, sentDateTime)
You can group by the month number of the sentDateTime with the function DATEPART(MONTH, sentDateTime). The next select will also yield results if no message was sent for a particular month (total = 0).
;WITH PossibleMonths AS
(
SELECT
M.PossibleMonth
FROM
(VALUES
(1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12)) M(PossibleMonth)
),
MonthTotals AS
(
select
COUNT(1) AS Total,
DATEPART(MONTH, sentDateTime) [Month]
From
MessageTable
where
Cast(sentDateTime as date) > '2017-04-01'
group by
DATEPART(MONTH, sentDateTime)
)
SELECT
P.PossibleMonth,
Total = ISNULL(T.Total, 0)
FROM
PossibleMonths AS P
LEFT JOIN MonthTotals AS T ON P.PossibleMonth = T.Month
SELECT
COUNT(*) AS TotalGuestBook_TotalCount,
MONTH(GuestBook_CreatedDate) AS month
FROM
tbl_Guestbook
GROUP BY
MONTH (GuestBook_CreatedDate)
you need to describe your question not just on the title and give a simple query. what i can get from your script and try to understand is this :
SELECT count(*) AS TotalGuestBook_TotalCount, MONTH(GuestBook_CreatedDate) as months
from tbl_Guestbook
where MONTH(GuestBook_CreatedDate) in (the month you want to search)
group by MONTH (GuestBook_CreatedDate)
since you want the name, it's easy to be done:
SELECT DATENAME(month,datetime) from table.
the datename function will help you. but you have to change your query like this:
SELECT count(*) AS TotalGuestBook_TotalCount,
cast(datename(month,GuestBook_CreatedDate)as varchar(20)) as month_name
from tbl_Guestbook
where MONTH(GuestBook_CreatedDate) in (the month you want to search)
group by cast(datename(month,GuestBook_CreatedDate)as varchar(20))
if you just want a abbreviation of the month name just cast the varchar(20) to be 3. like now it will show January, varchar(3) will be Jan
i think You want to retrieve output as :-
select Mon,count(No) NO from(
select month(GuestBook_CreatedDate) Mon,'1' No from tbl_Guestbook
)x group by Mon
You can use having clause as below
declare #month int=1
SELECT
COUNT(*) AS TotalGuestBook_TotalCount,
MONTH(GuestBook_CreatedDate) AS month
FROM tbl_Guestbook
GROUP BY MONTH (GuestBook_CreatedDate)
having MONTH(GuestBook_CreatedDate)=#month
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)
;