Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count the total records containing specific values

I have a question and hope you guys can assist me.

I have a table containing two columns:

type           // contains 2 different values: "Raid" and "Hold"
authorization  // contains 2 different values: "Accepted" or "Denied"

I need to make a view that returns values like this:

TYPE:RAID     ACCEPTED:5          DENIED:7

Basically I want to know how many of the values in TYPE are "Raid" and then how many of them are "Accepted" and "Denied".

Thank you in advance!!

like image 514
user1311030 Avatar asked Apr 03 '12 17:04

user1311030


People also ask

How count rows with specific value in SQL?

The SQL COUNT() function returns the number of rows in a table satisfying the criteria specified in the WHERE clause. It sets the number of rows or non NULL column values.

How do I count total records in SQL?

To counts all of the rows in a table, whether they contain NULL values or not, use COUNT(*). That form of the COUNT() function basically returns the number of rows in a result set returned by a SELECT statement.

How do you count unique records?

SELECT COUNT (DISTINCT item_num) FROM items; If the COUNT DISTINCT function encounters NULL values, it ignores them unless every value in the specified column is NULL. If every column value is NULL, the COUNT DISTINCT function returns zero (0).

What does count (*) do in SQL?

COUNT(*) returns the number of rows in a specified table, and it preserves duplicate rows. It counts each row separately. This includes rows that contain null values.

How to count the number of rows that contain specific values?

To get the number of rows that contain specific values, the generic syntax is: Array formula, should press Ctrl + Shift + Enter keys together. X: The specific value you use to count the rows. 1. Please enter or copy the below formula into a blank cell where you want to put the result: 2.

How to count the number of cells with specific text?

For example, to get the number of cells in A2:A10 that contain the text in D1 and handle uppercase and lowercase as different characters, use this formula: =SUMPRODUCT (--EXACT (D1, A2:A10)) Case-sensitive formula to count cells with specific text (partial match)

How to count the number of specific values in an array?

To get the number of rows that contain specific values, the generic syntax is: Array formula, should press Ctrl + Shift + Enter keys together. X: The specific value you use to count the rows. 1.

How do I Count specific values in Excel VBA?

Specific Value: Select the value that you want to count for by changing the 'Exceldome' value in the VBA code. Worksheet Selection: Select the worksheet which captures a range of cells from which you want to count for the specific value by changing the Analysis worksheet name in the VBA code.


2 Answers

You can use COUNT in combination with a CASE statement

SELECT COUNT(CASE authorization WHEN 'denied' THEN 1 ELSE NULL END) as denied,
  COUNT(CASE authorization WHEN 'authorized' THEN 1 ELSE NULL END) as authorized
FROM table
WHERE type = 'RAID'

SUM(CASE …) is also possible, but you'll have to return 0 in the ELSE clause instead of NULL

like image 96
knittl Avatar answered Sep 18 '22 09:09

knittl


SELECT
   Type
  ,sum(case Authorization when 'Accepted' then 1 else 0 end) Accepted
  ,sum(case Authorization when 'Denied' then 1 else 0 end) Denied
 from MyTable
 where Type = 'RAID'
 group by Type
like image 22
Philip Kelley Avatar answered Sep 19 '22 09:09

Philip Kelley