How to count and rank within field? SQL

rank in sql w3schools
where rank = 1 sql
rank() over partition by sql
sql rank() over (partition by multiple columns)
rank() over sum sql
rank and dense_rank in sql server with examples
dense_rank function in sql
sql rank() over (partition by multiple columns)
| proj_id | list_date  | state ||
|   1     |   03/05/10 |   CA   |
|   2     |   04/05/10 |   WA   | 
|   3     |   03/05/10 |   WA   | 
|   4     |   04/05/10 |   CA   | 
|   5     |   03/05/10 |   WA   | 
|   6     |   04/05/10 |   CA   |

What query could I write so that I can count how many projects were listed in each month by state? Using MySQL

Maybe something like this? How many projects a month per a state.

SELECT project_id, list_date, state_name, COUNT(*) As Projects FROM projects GROUP BY MONTH(list_date), state_name;

Here is the sql fiddle:!9/194dd9/1


If you meant you want to count EACH project per month (meaning there could be multiple entries of the same project_id) then this fiddle does that:

SELECT project_id, list_date, state_name, COUNT(*) As Projects FROM projects GROUP BY MONTH(list_date), state_name, project_id;

and the fiddle for this one:!9/dd5803/4

Methods to Rank Rows in SQL Server: ROW_NUMBER(), RANK , The article describes methods to rank rows in SQL Server, including is Ranking Window Functions that are used to rank specific field values and used to calculate ROWID for the provided rows window in their own way. The rows within a partition that have the same values will receive the same rank. The rank of the first row within a partition is one. The RANK () function adds the number of tied rows to the tied rank to calculate the rank of the next row, therefore, the ranks may not be consecutive. The following shows the syntax of the RANK () function:

Please try this.

    SELECT 1 AS project_id,  Getdate() list_date , 'U.P' state_name  
    SELECT 2 AS project_id,  DATEADD(MONTH,-2,Getdate()) list_date , 'M.P' state_name  
    SELECT 3 AS project_id, Getdate() list_date , 'U.P' state_name  
    SELECT 4 AS project_id,  DATEADD(MONTH,-5,Getdate()) list_date , 'R.J' state_name  
) AS T


SELECT COUNT(T.project_id) AS TotalPorject, CONVERT(varchar(10),T.list_date) AS ProjectDate, t.state_name   FROM #temp T
GROUP BY CONVERT(varchar(10),T.list_date), t.state_name

How to Rank Rows in SQL: A Complete Guide, Ranking With COUNT(); Ranking With SUM(). Using partition by: RANK() OVER(​PARTITION BY )—Single Column; RANK()  SQL Server provides us with four ranking window functions that help us to rank the provided rows set according to specific column values. These functions are: ROW_NUMBER (), RANK (), DENSE_RANK () and NTILE ().

You can try below

SELECT MONTH(list_date), state_name, COUNT(proj_id) As projectcount
FROM projects 
GROUP BY MONTH(list_date), state_name

SQL RANK() Function Explained By Practical Examples, This tutorial shows how to use SQL RANK() function to find ranks of rows in a result set. It provides some The same column values receive the same ranks. As clearly shown in the output, the second and third rows share the same rank because they have the same value. The fourth row gets the rank 4 because the RANK() function skips the rank 3. Note that if you want to have consecutive ranks, you can use the DENSE_RANK() function. SQL RANK() function examples

RANK(), DENSE_RANK(), and COUNT() in SQL , If you want the ranking, perhaps rank() will do want you want: SELECT TOP 100 T.PGM AS Program, COUNT(*) AS ProgramCount, RANK() OVER (Order BY  SQL COUNT(*) with ORDER BY clause example. You can use the COUNT(*) function in the ORDER BY clause to sort the number of rows per group. For example, the following statement gets the number of employees for each department and sorts the result set based on the number of employees in descending order.

Overview of SQL RANK functions, We use DENSE_RANK() function to specify a unique rank number within the partition as per the specified column value. It is similar to the Rank  The SQL COUNT (), AVG () and SUM () Functions The COUNT () function returns the number of rows that matches a specified criterion. The AVG () function returns the average value of a numeric column. The SUM () function returns the total sum of a numeric column.

RANK (Transact-SQL), Examples. A. Ranking rows within a partition. The following example ranks the products in inventory the specified inventory locations according to  Returns the rank of each row within the partition of a result set. The rank of a row is one plus the number of ranks that come before the row in question. ROW_NUMBER and RANK are similar. ROW_NUMBER numbers all rows sequentially (for example 1, 2, 3, 4, 5).

  • What is the context? Field Name and Data Type of what?
  • It's a hypothetical table. The field names are project_id, list_date, and state_name. What query could i write to show how many projects were listed in each month by state?
  • When you write a question, be sure to be clear as to what you're asking. If you include more information in the original question, you might get an answer instead of being downvoted (i.e. edit your question above)
  • Share some input data with expected output. Also, what DBMS are you using? sql is just a language.
  • hey there! I just fixed it - I hope that's okay
  • I just tried this - it looks like the states are not aggregating together. For example, it will list out 2 Projects Nevada and another 3 Projects in Nevada. How do you make the query show 5 Projects in Nevada?
  • @JuniorSQL Have a look at this one!9/194dd9/1 thats showing how many projects per state.
  • @JuniorSQL Because those are different months some are in 2013 other in 2018. Your question asked how many each month by state.
  • this has been extremely helpful - last question, how could i get rid of the project_id column, and just show month, state and the number of Projects?
  • @JuniorSQL Thanks! If you feel this was an acceptable answer if you could click the checkmark next to the answer to help others if they see this as well.
  • Hi Sanjay - thanks for your quick response. How come we don't use a count function?
  • @JuniorSQL not sure, please visit on this link might it helps…