How to count rows by group in a SQL query

John_Matrix

I have a SQL query that returns data which includes quote types along with customer information. There are 4 types of quotes (Open, Dead, Requote, Project). I want to be able to count each type for each customer. And also count the total. I have not found a way to accomplish this.

Ultimately I want this to go into a gauge on an SSRS report so that we can tell how many of the quotes (percentage) eventually turn into a project.

I haven't found anything in my searches that works. Thanks in advance for any advice.

Juan Carlos Oropeza

Using CTE's

 WITH quoteTotal as (
      Select customer, count(*) as customer_total
      from customer
      group by customer
 ),
 typeQuote as (
      Select customer, quote_type, count(*) as quote_total
      from customer
      group by customer, quote_type
 )
 SELECT T.customer, T.quote_type, T.quote_total, Q.customer_total
 FROM typeQuote T
 INNER JOIN quoteTotal Q
    ON T.customer = Q.customer

I think using window functions can be easy.

SELECT DISTINCT 
       customer, 
       quote_type,
       COUNT(*) OVER (partition by customer, quote_type order by customer) as type_total,
       COUNT(*) OVER (partition by customer order by customer) as customer_total
FROM customers

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to return count of updated rows in SQL query?

From Dev

SQL query using group by and count on some rows in a join

From Dev

Laravel Eloquent. How to count rows on query with group_by and having

From Dev

How to create an sql string to count selected rows with group by

From Dev

How do I count specific rows in GROUP BY SQL

From Dev

SQL Server query with group as a count

From Dev

Excel: how to count and group rows

From Dev

How to display the number of rows using the SQL query COUNT

From Dev

How do I return multiple columns and rows in a SQL Count Query?

From Dev

get whole count of rows in a query with group condition

From Dev

JPA count query to count total result rows of a group by query

From Dev

How do I get the count for a SQL query containing a GROUP BY statement?

From Dev

How to write the sql query to select and group by type and count per type?

From Dev

How to enumerate a continuous group of rows based on order in result of sql query

From Dev

SQL query - how to get 2 or more rows with group by

From Dev

How to group or marge SQL rows via PHP after query

From Dev

SQL Count + Left join + Group by ... Missing rows

From Dev

SQL Server GROUP BY COUNT Consecutive Rows Only

From Dev

SQL - Display all empty rows with Count() Group By

From Dev

How can I correctly implement an Hibernate SQL query starting from an SQL query that count the number of rows?

From Dev

sql query: pivot but with different count or rows

From Dev

SQL query to return count of monthly rows is slow

From Dev

sql query: pivot but with different count or rows

From Dev

SQL query to count rows that contain value

From Dev

SQL Query to Count Distinct with Group By with a Join

From Dev

Django COUNT with JOIN and GROUP BY SQL query

From Dev

SQL Query, GROUP/COUNT issue with INNER JOIN

From Dev

SQL query help in Yii framework(count, group by)!

From Dev

SQL query to get count and group by output values

Related Related

  1. 1

    How to return count of updated rows in SQL query?

  2. 2

    SQL query using group by and count on some rows in a join

  3. 3

    Laravel Eloquent. How to count rows on query with group_by and having

  4. 4

    How to create an sql string to count selected rows with group by

  5. 5

    How do I count specific rows in GROUP BY SQL

  6. 6

    SQL Server query with group as a count

  7. 7

    Excel: how to count and group rows

  8. 8

    How to display the number of rows using the SQL query COUNT

  9. 9

    How do I return multiple columns and rows in a SQL Count Query?

  10. 10

    get whole count of rows in a query with group condition

  11. 11

    JPA count query to count total result rows of a group by query

  12. 12

    How do I get the count for a SQL query containing a GROUP BY statement?

  13. 13

    How to write the sql query to select and group by type and count per type?

  14. 14

    How to enumerate a continuous group of rows based on order in result of sql query

  15. 15

    SQL query - how to get 2 or more rows with group by

  16. 16

    How to group or marge SQL rows via PHP after query

  17. 17

    SQL Count + Left join + Group by ... Missing rows

  18. 18

    SQL Server GROUP BY COUNT Consecutive Rows Only

  19. 19

    SQL - Display all empty rows with Count() Group By

  20. 20

    How can I correctly implement an Hibernate SQL query starting from an SQL query that count the number of rows?

  21. 21

    sql query: pivot but with different count or rows

  22. 22

    SQL query to return count of monthly rows is slow

  23. 23

    sql query: pivot but with different count or rows

  24. 24

    SQL query to count rows that contain value

  25. 25

    SQL Query to Count Distinct with Group By with a Join

  26. 26

    Django COUNT with JOIN and GROUP BY SQL query

  27. 27

    SQL Query, GROUP/COUNT issue with INNER JOIN

  28. 28

    SQL query help in Yii framework(count, group by)!

  29. 29

    SQL query to get count and group by output values

HotTag

Archive