TSQL query to retrieve flagged/total rows - sql

I'm struggling to find a solution to the following problem.
Assume, one has a table like this
COL1 FLAG
aaa 1
aaa 0
aaa 1
bbb 0
I need to write a query to get the following output:
COL1_VALUE FLAGGED TOTAL
aaa 2 3
bbb 0 1
where FLAGGED column contains the total count of the 'aaa' row values for which FLAG=1, and TOTAL column is the total number of rows containing 'aaa', in other words find how many rows containing 'aaa' are flagged in relation to total number of rows containing 'aaa'. Is it possible with a single query? (i.e. without using temp tables etc.)
(MSSQL2008)

SELECT COL1 AS COL1_VALUE,
COUNT(CASE WHEN FLAG = 1 THEN 1 END) AS FLAGGED,
COUNT(*) AS TOTAL
FROM YourTable
GROUP BY COL1

SELECT COL1, SUM(FLAG) AS FLAGGED, Count(*) AS TOTAL from tbl GROUP BY COL1

SELECT Tab.COL1 AS COL1_VALUE,
SUM(CASE WHEN Tab.FLAG = 1 THEN 1 ELSE 0 END) AS FLAGGED,
COUNT(*) AS TOTAL
FROM Tab
GROUP BY Tab.COL1

Related

Count Values From Column

I am trying to create a script in SQL Server that will count values under a column but I want it to still report missing values not counted.
Currently, I have the following setup with a group by, but it cuts the results in half:
select count(ID) as Count, Building, ID
from table
group by Building, ID
I want my output to show the count per ID as well as null values if there was nothing to count per ID.
Building ID
1234 1
1234 2
4567 3
4567 4
8910 5
0 6
Want the Output To Be:
Building ID Count
1234 1 2
1234 2 2
4567 3 2
4567 4 2
8910 5 1
0 6 0
The total population is 200,000. I want to see 200,000 records with the total counts per name or null values. When I run the above script, I obtain 1's per record.
Example: If ID 1 has a count of 2 and ID 2 has a count of 2, I want both IDs to show up as separate counts per ID.
You need to get the count from a sub-query and then join that sub-query
SELECT
CASE WHEN t1.building is null THEN 0
ELSE t1.building END AS Building,
t1.id,
CASE WHEN t1.building is null THEN 0
ELSE t2.count END AS Count
FROM table t1
JOIN (SELECT building, COUNT(*) as count
FROM table
GROUP BY building) AS t2 ON t2.building = t1.building OR (t2.building is null AND t1.building is null)
try
select count(name) as Count, ID, Building
from table
group by ID, Building

One column condition in sql

I have a table:
[letter] [Name] [status] [price]
A row1 1 11
A row1 1 15
B row2 2 9
B row2 3 23
B row2 3 30
And want to select data something like this:
SELECT letter, Name,
COUNT(*),
CASE WHEN price>10 THEN COUNT(*) ELSE NULL END
GROUP BY letter, Name
the result is:
A row1 2 2
B row2 1 null
B row2 2 2
But I want this format:
A row1 2 2
B row2 3 2
Please, help me to modify my query
Close. Probably want this instead:
SELECT letter, Name,
COUNT(*),
SUM(CASE WHEN price>10 THEN 1 ELSE 0 END)
FROM TableThatShouldHaveAppearedInTheQuestionInTheFromClause
GROUP BY letter, Name
should work. Assuming that the intention of the fourth column is to return the count of the number of rows, within each group, with a price greater than 10. It's also possible to do this as a COUNT() over a CASE then returns non-NULL and NULL results for the rows that should and should not be counted, but I find the above form easier to quickly reason about.
Since nulls are not used in aggregate functions:
SELECT letter
, name
, count(*)
, count(
case when price > 10 then 1
end
)
FROM t
GROUP BY letter, name
You were very close.
Looking to the other answers, probably this is not the best way, but it will work.
The count of the prices over 10 is made with a subquery which has a condition on price > 10 and which is joined to the current TAB record with the alias A for the same letter and name.
SELECT letter,
Name,
COUNT(*),
(SELECT COUNT(*) FROM TAB WHERE letter = A.letter and Name = A.Name WHERE price>10)
FROM TAB A
GROUP BY letter, Name

To find total number of rows

I have a table like this
Table1
=======
A B
8 5
2 9
null 4
2 5
How to find total number of rows from table1,note if any column value is null in a row then that row should be considered as 2 rows?
I have tried with count(*)*2 and nvl function it doesn't work
Try this
SELECT SUM(CASE WHEN A IS NULL OR B IS NULL THEN 2 ELSE 1 END) AS CountVal
FROM TABLE1
Fiddle Demo
O/P:
COUNTVAL
--------
5
COUNT() is rowbased.. you can tweak it using SUM() instead..
select sum(NVL2(a,NVL2(b,1,2),2)) FROM TABLE1
CASE as suggested by #Vignesh is the simplest and more readable !!
COUNT() can also done like this.. But NOT a optimal solution at all!
SELECT COUNT(1) FROM
(
SELECT NVL(a,NVL(b,1)) FROM TABLEA
UNION ALL
SELECT NVL(a,NVL(b,1)) FROM TABLEA
WHERE A OR B iS NULL
)

How do I use group by on one column which is having many entries?

I want to use group by on one column which is having many entries
table_a
Name price
AAA 12
BBB 13
AAA 0
CCC 24
AAA 0
DDD 0
Now I want to find out Name which is having Price as 0
but as I'm having entries AAA 3 times I can't directly write simple sql with condition
NOT Equal to 0
Please help me I want to print result for above table_a should be
only D as it is having 0 as price.
I assumed price cannot be lower than zero:
SELECT Name
FROM TableName
GROUP BY Name
HAVING SUM(price) = 0
Or with additional condition of only one price, which is zero
SELECT Name
FROM TableName
GROUP BY Name
HAVING COUNT(*) = 1 AND SUM(price) = 0
You can achive this by using aggregate functions GROUP BY and HAVING clause as:
SQLFiddle Demo
SELECT name,SUM(price) as price
FROM table
GROUP BY name
HAVING SUM(price) = 0
select name,max(price) as price from table group by name having max(price) =0
select name,max(price)
group by name
having max(price)=0

Multiple row Sums into a Total Column

I have a temp table populated in a sproc that is similar to this:
Company Col1 Col2 Col3 Total
Company1 4 3 2 9
Company2 1 0 3 4
Total ? ? ? ?
Is there a slick way to get the bottom total column populated with SUM of each row in one shot without having to do each column individually using sub-queries?
select sum(col1), sum(col2), sum(col3), sum(col1+col2+col3)
FROM CompanyTable
If the DBMS you use is MS SQL than be aware of how to reference the temporary table:
SELECT SUM(Col1) as TotalOfCol1,...
FROM #Company
If you are using MySQL, you can get the whole table in one query using the WITH ROLLUP feature:
SELECT Company, SUM(Col1), SUM(Col2), SUM(Col3), SUM(Col1 + Col2 + Col3)
FROM Table1
GROUP BY Company
WITH ROLLUP
Results:
'Company1' 4 3 2 9
'Company2' 1 0 3 4
'' 5 3 5 13 <--- This extra row is the column totals