Row counts Sql query - html

I want result in Sql query
id1 id2 id3 Count Id
A001 A001 A001 3
A001 NULL A001 2
A001 NULL NULL 1

In SQL Server, i would use VALUES construct :
SELECT t.*,
(SELECT COUNT(tt.ids) FROM ( VALUES (t.id1), (t.id2), (t.id3) ) tt(ids)
) as Count_Id
FROM table t;
In standard SQL you can use CASE Expression :
SELECT t.*,
( (CASE WHEN ID1 IS NOT NULL THEN 1 ELSE 0 END) +
(CASE WHEN ID2 IS NOT NULL THEN 1 ELSE 0 END) +
(CASE WHEN ID3 IS NOT NULL THEN 1 ELSE 0 END)
) AS Count_Id
FROM table t

are you find something like below
select id1,id2,id3,
case when id1 is not null then 1 else 0 end+
case when id2 is not null then 1 else 0 end+
case when id3 is not null then 1 else 0 end as val

Related

Distinct Count Query

I have a table below:
Item Status1 Status2
-----------------------------
A Good NULL
A Good NULL
A Good NULL
A Bad Good
B Bad Good
B Good NULL
C Good NULL
C Good NULL
C Good NULL
D Bad Good
Now, I'm thinking off writing a query which gives me the result below:
Item Good Bad
-----------------------------
A 4 1
B 2 1
C 3 0
D 1 1
Distinct in the Item column and the count of Good and Bad for each Item where NULL is not counted.
The column name can be of anything (I just kept it as Good and Bad in my second table).
Any suggestions/ideas on how to achieve my desired results?
Use UNION ALL & do aggregation :
select item, sum(status = 'good'), sum(status = 'bad')
from (select item, status1 as status
from table t
union all
select item, status2
from table t
) t
group by item;
You can use union all and conditional aggregation
select item, count(case when status1='good' then 1 end) as good,
count(case when status1='bad' then 1 end) as bad
from
(
select item , status1 from tablename
union all
select item , status2 from tablename
)A group by item
use union and case when
select Item, sum(case when status = 'good' then 1 else 0 end) as good,
sum ( case when status = 'bad' then 1 else 0 end) as bad
from (select Item, Status1 as status
from table_name
union all
select Item, Status2
from table_name
) t
group by Item;
There's no need for UNION, simply apply some logic.
select Item
,sum(case when Status1 = 'Good' then 1 else 0 end +
case when Status2 = 'Good' then 1 else 0 end) as good
,sum(case when Status1 = 'Bad' then 1 else 0 end +
case when Status2 = 'Bad' then 1 else 0 end) as bad
from tab
group by Item
or
select Item
,count(case when Status1 = 'Good' then 1 end) +
count(case when Status2 = 'Good' then 1 end) as good
,count(case when Status1 = 'Bad' then 1 end) +
count(case when Status2 = 'Bad' then 1 end) as good
from tab
group by Item
You can use sub-query and then apply sum function in outer query
select distinct(item) as item, sum(S1G+S2G) as Good,sum(S1B+S2B) as Bad from ( select item, CASE WHEN status1 ='Good' THEN 1 ELSE 0 END as S1G, CASE WHEN status2 ='Good' THEN 1 ELSE 0 END as S2G, CASE WHEN status2 ='Bad' THEN 1 ELSE 0 END as S2B, CASE WHEN status1 ='Bad' THEN 1 ELSE 0 END as S1B from t1 ) as b group by item
Here is demo

How to count non null fields?

I need to get the count of fields where the value is not null.
My table
city id_no no1 no2 no3
chn A12 2158
chn A13 8181 8182 8183
chn A14 19138
I need to get the count of fields set for no1, ..., no3
My query
SELECT
count(id_no) as total_id,
(count(no1) +
count(no2) +
count(no3)) as c_count
FROM table
WHERE city='chn';
My output
total_id c_count
3 9
Expected:
total_id c_count
3 5
I am expecting 5 instead of 9, since 5 fields are not null.
OR you can simply do this to avoid NULL or ' ' data
SELECT
count(id_no) as total_id,
(count(CASE WHEN no1 > 0 THEN no1 ELSE NULL END) +
count(CASE WHEN no2 > 0 THEN no2 ELSE NULL END) +
count(CASE WHEN no3 > 0 THEN no3 ELSE NULL END)) as c_count
FROM table
WHERE city='chn';
SELECT
count(id_no) as total_id,
(case when count(no1)='' or count(no1) is null then 0 else count(no1) end +
case when count(no2)='' or count(no2) is null then 0 else count(no2) end +
case when count(no3)='' or count(no3) is null then 0 else count(no3) end +
case when count(no4)='' or count(no4) is null then 0 else count(no4) end +
case when count(no5)='' or count(no5) is null then 0 else count(no5) end +
case when count(no6)='' or count(no6) is null then 0 else count(no6) end +
case when count(no7)='' or count(no7) is null then 0 else count(no7) end +
case when count(no8)='' or count(no8) is null then 0 else count(no8) end +
case when count(no9)='' or count(no9) is null then 0 else count(no9) end +
case when count(no10)='' or count(no10) is null then 0 else count(no10) end
) as c_count
FROM table
WHERE city='chn';
I am getting same output as you want please check here i provide my screenshot
SELECT count(id_no) as total_id,
count(CASE WHEN `nol`!="" THEN 1 END) as no1
FROM `table` where city='chn'
try like this
select count(distinct a.`id_no`),count(*) from
(
select `id_no`,`no1` as `non` from table WHERE city='chn'
union all
select `id_no`,`no2` as `non` from table WHERE city='chn'
union all
select `id_no`,`no3` as `non` from table WHERE city='chn'
)a where a.`non` is not null

Query select all fields are not null

I have a table that I want to count number of fields which their field is not null. How I can return the result?
SELECT * FROM `fakelos2` WHERE fields are not Null
Instead of are use is, while to count rows for particular field, you use count function like below:
SELECT count(*)
FROM fakelos2
WHERE fields is not null
Now you have to count each and every field then you have to use case when then like below:
SELECT
((CASE WHEN field1 IS NULL THEN 1 ELSE 0 END)
+ (CASE WHEN field2 IS NULL THEN 1 ELSE 0 END)
+ (CASE WHEN field3 IS NULL THEN 1 ELSE 0 END)
...
...
+ (CASE WHEN field10 IS NULL THEN 1 ELSE 0 END)) AS sum_of_nulls
FROM fakelos2

convert Rows to column

Looking for the way to change row to column. (The comflag is of type bit and not null). Help appreciated
Table1
Id Commflag value
122 0 Ce
125 1 Cf
122 0 Cg
125 1 cs
Here is what I want in result
id ce cf cg cs cp
122 0 null 0 null null
125 null 1 null 1 null
The below query shows error-
SELECT ID , [CE],[CF],[CG],[CS],[CP]
FROM TABLE1
PIVOT ((convert((Commflag)as varchar()) FOR value IN [CE],[CF],[CG],[CS],[CP] as pvt
ORDER BY date
This query does what you want:
select Id, pvt.Ce, pvt.Cf, pvt.CG, pvt.Cs, pvt.Cp
from
(
select Id, cast(Commflag as tinyint) Commflag, value
from Table1
) t
pivot (max(Commflag) for value in ([Ce],[Cf],[CG],[Cs],[Cp])) pvt
SQL Fiddle
Here's another way to do it, without using PIVOT:
select Id,
max(case value when 'Ce' then CAST(Commflag as tinyint) else null end) Ce,
max(case value when 'Cf' then CAST(Commflag as tinyint) else null end) Cf,
max(case value when 'Cg' then CAST(Commflag as tinyint) else null end) Cg,
max(case value when 'Cs' then CAST(Commflag as tinyint) else null end) Cs,
max(case value when 'Cp' then CAST(Commflag as tinyint) else null end) Cp
from Table1
group by Id
order by Id
SQL Fiddle

sql server null value in where clause

There is one table which is having only one row with 4 date columns ,initially all date value are null so
if Exists(select 1 from rep_master where pacdt_1=null OR
pacdt_2=null OR
pacdt_3=null OR
pacdt_4=null)
select 0
else
select 1
this one is returning 1
if Exists(select 1 from rep_master where ISNULL(pacdt_1,0)=0 or
ISNULL(pacdt_2,0)=0 or
ISNULL(pacdt_3,0)=0 or
ISNULL(pacdt_4,0)=0 )
select 0
else
select 1
this one is returning 0 ,which is correct result
I m unable to figure out what is wrong with first query?
use IS NULL rather than = NULL
so:
if Exists(select 1 from rep_master
where pacdt_1 is null OR pacdt_2 is null OR
pacdt_3 is null OR pacdt_4 is null)
select 0
else
select 1
You can't equal null, you have to use IS
if Exists(select 1 from rep_master where pacdt_1 IS null OR
pacdt_2 IS NULL OR
pacdt_3 IS NULL OR
pacdt_4 IS NULL)
select 0
else
select 1
to get where the value is not null you use IS NOT
if Exists(select 1 from rep_master where pacdt_1 IS NOT null OR
pacdt_2 IS NOT NULL OR
pacdt_3 IS NOT NULL OR
pacdt_4 IS NOT NULL)
select 0
else
select 1