January 8, 2015 at 3:30 am
How can I count the rows when I make new table. Here is my code:
SELECT EMPLOYEE_ID, LAST_NAME, SALARY, JOB_ID, COMMISSION_PCT, DEPARTMENT_ID, COUNT(TO_CHAR(HIRE_DATE, 'DAY')) AS NUM_EMPLOYEES
FROM EMPLOYEES
WHERE TO_CHAR(HIRE_DATE, 'Day') = 'Monday'
GROUP BY EMPLOYEE_ID, LAST_NAME, SALARY, JOB_ID, COMMISSION_PCT, DEPARTMENT_ID;
Here is the table: https://www.dropbox.com/s/03m2o6fzn1j8f8t/efewfewfewfew.PNG?dl=0
I have 10 rows, so I would like under NUM_EMPLOYEES to be displayed 10 on each row.
Please help!
January 8, 2015 at 3:58 am
Try this
SELECT
EMPLOYEE_ID
, LAST_NAME
, SALARY
, JOB_ID
, COMMISSION_PCT
, DEPARTMENT_ID
, COUNT(*) OVER() AS NUM_EMPLOYEES
FROM EMPLOYEES
WHERE TO_CHAR(HIRE_DATE, 'Day') = 'Monday'
January 8, 2015 at 5:22 am
Thank you very much.
January 8, 2015 at 5:50 pm
Emil Bialobrzeski (1/8/2015)
Try this
SELECT
EMPLOYEE_ID
, LAST_NAME
, SALARY
, JOB_ID
, COMMISSION_PCT
, DEPARTMENT_ID
, COUNT(*) OVER() AS NUM_EMPLOYEES
FROM EMPLOYEES
WHERE TO_CHAR(HIRE_DATE, 'Day') = 'Monday'
Certainly that will work but may not be the fastest way:
https://www.simple-talk.com/sql/t-sql-programming/the-performance-of-the-t-sql-window-functions/
My thought question: Have you ever been told that your query runs too fast?
My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.
Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
January 9, 2015 at 1:04 am
dwain.c (1/8/2015)
Emil Bialobrzeski (1/8/2015)
Try this
SELECT
EMPLOYEE_ID
, LAST_NAME
, SALARY
, JOB_ID
, COMMISSION_PCT
, DEPARTMENT_ID
, COUNT(*) OVER() AS NUM_EMPLOYEES
FROM EMPLOYEES
WHERE TO_CHAR(HIRE_DATE, 'Day') = 'Monday'
Certainly that will work but may not be the fastest way:
https://www.simple-talk.com/sql/t-sql-programming/the-performance-of-the-t-sql-window-functions/
Hi Dwain,
Thanks for this, very useful. I knew there is a performance difference but wasn't sure how big. On the other hand i just wanted to keep it simple as well.
January 9, 2015 at 1:17 am
Emil Bialobrzeski (1/9/2015)
dwain.c (1/8/2015)
Emil Bialobrzeski (1/8/2015)
Try this
SELECT
EMPLOYEE_ID
, LAST_NAME
, SALARY
, JOB_ID
, COMMISSION_PCT
, DEPARTMENT_ID
, COUNT(*) OVER() AS NUM_EMPLOYEES
FROM EMPLOYEES
WHERE TO_CHAR(HIRE_DATE, 'Day') = 'Monday'
Certainly that will work but may not be the fastest way:
https://www.simple-talk.com/sql/t-sql-programming/the-performance-of-the-t-sql-window-functions/
Hi Dwain,
Thanks for this, very useful. I knew there is a performance difference but wasn't sure how big. On the other hand i just wanted to keep it simple as well.
Yes, the simplicity of that COUNT OVER is great when you already have a really complex query and you need to add this to it. For simpler cases, I tend to go with the pre-aggregation.
My thought question: Have you ever been told that your query runs too fast?
My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.
Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply