Counting the 1's and 2's in a Table - sql

I have this table
SECTION
Which consist of field called
Semester
2
1
1
1
2
1
1
2
2
2
1
2
I need sql to count how many 1's and how many 2's are there
And out put like this
semester 1 | semester 2
6 | 6

Demo here:After testing
select
sum(Case when semester=1 then 1 else 0 end) as 'Semester1',
sum(Case when semester=2 then 1 else 0 end) as 'Semester2'
from section

Try out this:
Select sum(case when semster =1 then 1 else 0 end) as semster1 ,
sum(case when semster =2 then 1 else 0 end) as semster2
from section;

Assuming there are more values than 1 and 2, and that the values are integers:
SELECT semester, count(*)
FROM section
WHERE semester = 1 OR semester = 2
GROUP BY semester

Related

Sql conditional count

Can anyone help me with the sql and sqlite query for the below condition. To be specific, i wanted to show the merged count of 2 categories.
**Table**
Category Sub_category
A 1
A 2
A 2
B 1
C 1
C 1
C 2
D 1
D 1
D 1
D 2
D 3
**Required Output**
Category Sub_category Count **condition(not part of o/p just instruction)**
A 1 1 -
A 2+ 2 -
B 1 1 -
C 1 2 -
C 2+ 1 -
D 1 3 -
D 2+ 1 should contain the count of 2 and more
i am able to achieve the same using below:
select Category,
count(CASE WHEN Sub_category = 1 THEN Sub_category END) AS '1',
count(CASE WHEN Sub_category >= 2 THEN Sub_category END) AS '2+'
from table
however output is little different hence looking for the first output only.
**Output**
Category 1 2+
A 1 2
B 1 0
C 1 2
D 3 2
Thanks in advance!
You want:
select Category,
(case when sub_category = 1 then '1' else '2+' end) as sub_category,
count(*)
from table
group by Category,
(case when sub_category = 1 then '1' else '2+' end);
That is, you want to put the sub category groups on rows, not as columns.
You are just missing one column. Add the last count without a filtering to get the total:
select Category,
count(CASE WHEN Sub_category = 1 THEN Sub_category END) AS '1',
count(CASE WHEN Sub_category >= 2 THEN Sub_category END) AS '2+',
count(*)
from table
group by Category, count(CASE WHEN Sub_category = 1 THEN Sub_category END), count(CASE WHEN Sub_category >= 2 THEN Sub_category END), count(*)

SQL Server - Show All possible Values and Count Them

I have the following table:
IdSce Year NoIte Value
1 0 1 1
1 0 2 5
1 0 3 1
1 1 1 2
1 1 2 3
1 1 3 2
2 0 1 4
2 0 2 4
2 0 3 1
2 1 1 2
2 1 2 4
2 1 3 3
I want to group by IdSce and Year, and show each possible value and count how many time each value appears like this:
IdSce Year Value1 Value2 Value3 Value4 Value5
1 0 2 0 0 0 1
1 1 0 2 1 0 0
2 0 1 0 0 2 0
2 1 0 1 1 1 0
Thanks !
EDIT
shawnt00 is really close to what I want, but I'm looking to do it as dynamic as possible, meaning if I have 10 different values for the column value, I will be missing information in my table. Therefore, if I have 10 different values, I want 10 new columns (value1, value2, ... , value10)
This is what I've tried so far:
SELECT IdSce
,Year
,SUM(CASE WHEN Value >= 0 and Value < 1 THEN 1 else 0 end) Zero
,SUM(CASE WHEN Value >= 1 and Value < 2 THEN 1 else 0 end) One
,SUM(CASE WHEN Value >= 2 and Value < 3 THEN 1 else 0 end) Two
,SUM(CASE WHEN Value >= 3 and Value < 4 THEN 1 else 0 end) Three
,SUM(CASE WHEN Value >= 4 and Value < 5 THEN 1 else 0 end) Four
,SUM(CASE WHEN Value >= 5 THEN 1 else 0 end) FiveMore
,SUM(CASE WHEN Value >= 0 THEN 1 else 0 end) Total
FROM Table
GROUP BY IdSce
,Year
Thanks for the help again!
Ok, I'll do it!
select IdSce, "Year"
count(case when Value = 1 then 1 end) as "1",
count(case when Value = 2 then 1 end) as "2",
count(case when Value = 3 then 1 end) as "3",
count(case when Value = 4 then 1 end) as "4",
count(case when Value = 5 then 1 end) as "5"
from T
group by IdSce, "Year"
I think you'll often find this filed under "conditional aggregation". SQL Server has a proprietary syntax that uses pivot if you want to look into that also.

How to return one single row from groupped by columns

I have table like this
ID Specified TIN
-----------------
1 0 tin1
2 0 tin1
3 1 tin1
4 0 tin2
5 0 tin3
6 1 tin3
7 1 tin3
I need to count rows groupped by TIN, Specified columns - but result should one row for each TIN:
TIN ZEROSpecified NOTZEROSpecified
tin1 2 1
tin2 0 1
tin3 1 2
Important notice - i have only 2 values for Specified column - 0 and 1
SELECT TIN,
SUM(case when Specified=0 then 1 else 0 end) as ZeroSpecified,
SUM(case when Specified<>0 then 1 else 0 end) as NOTZEROSpecified
FROM table
GROUP BY TIN
Pretty Simple;
SELECT
TIN
,SUM(CASE WHEN Specified = 0 THEN 1 ELSE 0 END) ZEROSpecified
,SUM(CASE WHEN Specified <> 0 THEN 1 ELSE 0 END) NotZEROSpecified
FROM TableName
GROUP BY TIN

How can we count sub query use group by without pivot?

I need to create query that has below result.
I add my table for example for better understanding.
QTable:
QID Name
-------
1 x
1 y
2 z
FTable:
QID FID
-------
1 1
1 2
2 3
Inner join with :
FID Type
-------
1 1
1 2
1 3
1 4
2 2
2 3
2 3
3 1
3 3
3 3
I need to result like this:
result:
QID FID-Count Type(1)-count Type(2)-count Type(3)-count Type(4)-count
-----------------------------------------------------------------
1 2 1 2 3 1
2 1 1 0 2 0
Thank u for help.
Ususally you can emulate a pivot with GROUP BY and SUM+CASE:
SELECT
T1.QID,
COUNT(DISTINCT T1.FID) as FIDCOUNT,
SUM(CASE WHEN Type=1 THEN 1 ELSE 0 END) as Type1Cnt,
SUM(CASE WHEN Type=2 THEN 1 ELSE 0 END) as Type2Cnt,
SUM(CASE WHEN Type=3 THEN 1 ELSE 0 END) as Type3Cnt,
SUM(CASE WHEN Type=4 THEN 1 ELSE 0 END) as Type4Cnt
FROM QFTable as T1
JOIN T2 ON
T1.FID=T2.FID
GROUP BY T1.QID
ORDER BY QID
SQLFiddle demo

Counting values from a column and grouping and sorting by a second value

Let's say I have this table:
NAME YEAR SCORE
_____________________
User1 2010 1
User2 2011 3
User3 2012 2
User4 2013 1
User5 2012 1
User6 2011 3
User7 2010 4
User8 2011 1
I would like to create a query whose output looks like this from this data:
YEAR 1 2 3 4
________________________
2010 1 0 0 1
2011 1 0 2 0
2012 1 1 0 0
2013 1 0 0 0
Where the values in the numbered columns is the count of how many times that score appears in the given year. This seems as if it should be easy but I am having trouble figuring out how to wrap my head around writing this query in a way that is static. Ideas?
select YEAR,
count(CASE WHEN SCORE = 1 THEN 1 END) AS `1`,
count(CASE WHEN SCORE = 2 THEN 1 END) AS `2`,
count(CASE WHEN SCORE = 3 THEN 1 END) AS `3`,
count(CASE WHEN SCORE = 4 THEN 1 END) AS `4`
from TABLE
group by year
For a set of Four values
SELECT YEAR, SUM(CASE WHEN SCORE = 1 THEN 1 ELSE 0 END) AS `1`,
SUM(CASE WHEN SCORE = 2 THEN 1 ELSE 0 END) AS `2`,
SUM(CASE WHEN SCORE = 3 THEN 1 ELSE 0 END) AS `3`,
SUM(CASE WHEN SCORE = 4 THEN 1 ELSE 0 END) AS `4`
FROM TABLE
GROUP BY YEAR