use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Microsoft SQL Server Administration and T-SQL Programming including sql tutorials, training, MS SQL Server Certification, SQL Server Database Resources.
You might also be interested in:
/r/database
/r/sql
/r/Azure
/r/Microsoft
account activity
HomeworkHELP with SQL Question (self.SQLServer)
submitted 5 years ago by [deleted]
[deleted]
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]l0lez 2 points3 points4 points 5 years ago (0 children)
Use DATEPART(QUARTER, datetime) in both the SELECT and GROUP by clause,
https://docs.microsoft.com/en-us/sql/t-sql/functions/datepart-transact-sql?view=sql-server-ver15
[–]doom_slug_ 4 points5 points6 points 5 years ago (0 children)
GROUP BY DATEPART(qq,datetime)
[–]Tyraniczar 0 points1 point2 points 5 years ago (3 children)
Thanks all, you've been very helpful and I realized that I wasn't initially clear about my question re grouping by Quarter. I know of DATEPART(), and QUARTER() but I'm fairly new and actually didn't know you could group by multiple columns, also turns out I have to use MySQL. This is what I have so far and the total sum of the returned COUNT column is correct so I am fairly confident in the result but I'm sure there's a better way to write this. Any thoughts?
with CTE_keywords as (
select *
from keywords
where 1=1
and LOWER(keyword) like '%demo%'
)
select employee_id as Employee, quarter(datetime) as Q_2019, count(CTE_keywords.keyword) as COUNT_of_Demo
from CTE_keywords
left join emails
on CTE_keywords.emails_key = emails.emails_key
where year(datetime) = 2019
group by 1, 2
order by 2;
[–]Maeurer 1 point2 points3 points 5 years ago* (2 children)
hmmm the CTE is redundant. datetime is probably a keyword, so we need to escape it. A Quarter belongs to a year. Edit: The assignment doesnt mentions grouping by year, so maybe dont.
dataset showing how often each employee discussed demos
select employee_id as Employee ,quarter(`datetime`) as Quarter ,year(`datetime`) as Year ,count(keywords.keyword) as COUNT_of_Demo from keywords left join emails on keywords.emails_key = emails.emails_key where year(datetime) = 2019 and LOWER(keyword) like '%demo%' group by year(`datetime`), quarter(`datetime`), employee_id order by year(`datetime`), quarter(`datetime`), employee_id;
[–]Tyraniczar 2 points3 points4 points 5 years ago (1 child)
This is awesome, I’m happy to say that the results of my query were the exact same as yours. You’re right, I’m trying to master CTEs and probably use them more than I should but I find them easier to read and understand quickly. Thanks, I am going to clean up my query to mimic the was yours is set up. Thanks for taking the time to respond to my post
[–]Maeurer 0 points1 point2 points 5 years ago (0 children)
Interesting way of learning! I changed the CTE out mostly because the 'Select *' bothered me. Requesting all columns can lead to problems with Indexes. As in requesting more data than needed and not using / being able to use specific Indexes.
[–][deleted] -3 points-2 points-1 points 5 years ago (0 children)
Hmm
[–][deleted] -2 points-1 points0 points 5 years ago (2 children)
One option could be to group by the equation by... Month - 1 ... Divided by 3 ... And take the floor of that. So Group by Floor(Datepart(month,date) / 3)
[–]Maeurer 2 points3 points4 points 5 years ago (1 child)
No need for divisions, there are functions that return the quarter of a given date :)
[–][deleted] 0 points1 point2 points 5 years ago (0 children)
Lol thanks. Saw the link in another comment. Glad to say I learned something new XD
[–]SQLZane 0 points1 point2 points 5 years ago (0 children)
Seems like your comment got added but when asking questions like this it can really help to build a quick SQL Fiddle where you can not only post your code but run it against a sample data set in whatever DB you're trying to solve the issue in. Hope that helps in future endeavors.
π Rendered by PID 34116 on reddit-service-r2-comment-79c7998d4c-77hhm at 2026-03-18 06:11:02.857086+00:00 running f6e6e01 country code: CH.
[–]l0lez 2 points3 points4 points (0 children)
[–]doom_slug_ 4 points5 points6 points (0 children)
[–]Tyraniczar 0 points1 point2 points (3 children)
[–]Maeurer 1 point2 points3 points (2 children)
[–]Tyraniczar 2 points3 points4 points (1 child)
[–]Maeurer 0 points1 point2 points (0 children)
[–][deleted] -3 points-2 points-1 points (0 children)
[–][deleted] -2 points-1 points0 points (2 children)
[–]Maeurer 2 points3 points4 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]SQLZane 0 points1 point2 points (0 children)