Group by null and not null values - sql-server-2008

I have a Table containing facebook Ids of my users and I have to make a report on who is using facebook or not.
For the facebook user, the datarow contains a number else it contains null
My result have to be like that :
NbUsers Facebook
1000 no
500 yes
I don't know how to construct my query!
Any help will be helpfull!
Thanks a lot!!

you can do it easily by case statement :
SELECT SUM(CASE WHEN FACEBOOKID IS NULL THEN 1 ELSE 0 END) AS NbUsers , 'NO' AS FACEBOOK
FROM USERTABLE
UNION ALL
SELECT SUM(CASE WHEN FACEBOOKID IS NULL THEN 0 ELSE 1 END) AS NbUsers , 'YES' AS FACEBOOK
FROM USERTABLE

Related

MySQL Count multiple columns at once or separate transactions

I have a daily cron that counts the number of non-null and non-empty string rows for a specific column, such as:
SELECT count(first_name) FROM users WHERE first_name IS NOT NULL and first_name IS != ''
SELECT count(last_name) FROM users WHERE last_name IS NOT NULL and first_name IS != ''
Would the performance be improved if I did this in one MySQL call?
Use a CASE condition for 1 query
SELECT SUM(CASE WHEN first_name IS NOT NULL and first_name != '' THEN 1 ELSE 0 END),
SUM(CASE WHEN last_name IS NOT NULL and last_name != '' THEN 1 ELSE 0 END) FROM users
I believe that this query is enough:
SELECT COUNT(DISTINCT first_name), COUNT(DISTINCT last_name) FROM users
As other developers pointed this COUNT will count even '' empty value.
IMHO that is acceptable. If not you can:
SELECT COUNT(DISTINCT first_name)-1, COUNT(DISTINCT last_name)-1 FROM users
It is not exactly what you need, but if you are sure that you have those empty value that will work :
If you want clear query that return correct COUNT you should better use #Mihai query:
SELECT SUM(CASE WHEN first_name IS NOT NULL and first_name != '' THEN 1 ELSE 0 END),
SUM(CASE WHEN last_name IS NOT NULL and last_name != '' THEN 1 ELSE 0 END) FROM user
But this one can hit the performance a lot on big numbers of records.

Reduce the number of queries

I have seperate queries but i need to reduce the no so put all in one
select count(applicant_id) as registered from student_application where filter_status=0 AND
select count(applicant_id) as filer_select from student_application where filter_status=1 AND
select count(applicant_id) as filter_reject from student_application where filter_status=2
but this shows some errors
Use CASE expression.
Query
select
count(case when filter_status = 0 then applicant_id else null end) as registered,
count(case when filter_status = 1 then applicant_id else null end) as filer_select,
count(case when filter_status = 2 then applicant_id else null end) as filer_reject
from student_application;
SQL Fiddle
You could also use group_by, with the where clause if you're looking for a subset rather than all possible values of filter_status:
SELECT filter_status, COUNT(*)
FROM student_application
WHERE filter_status in (0, 1, 2)
GROUP BY filter_status;

MYSQL - Showing status input as it's own column

I'm having trouble writing a SQL query to show the status of a row as it's own column as shown in the picture.
I was thinking of using an alias for Status as:
SELECT 'Table A.Date', 'Table A.Status' as ... FROM Table A;
But this doesn't resolve the issue on how to display each status type as their own column and number value.
Can someone point out how to do this?
Try this one. I used CASE statement to conditionally count the status as one depending on the status type given.
SELECT
Date,
SUM(CASE WHEN Status='Pending' THEN 1 ELSE 0 END)Pending,
SUM(CASE WHEN Status='Completed' THEN 1 ELSE 0 END)Completed,
SUM(CASE WHEN Status='Cancelled' THEN 1 ELSE 0 END)Cancelled
FROM Table A
WHERE Date='2014-01-01'
GROUP BY Date
Try this:
select A.date,
count(
case
when A.Status='Pending'
Then 1
Else NULL
End
) as Pending,
count(
case
when A.Status='Completed'
Then 1
Else NULL
End
) as Completed,
count(
case
when A.Status='Cancelled'
Then 1
Else NULL
End
) as Cancelled
From A
group by A.date

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

MySQL Combining Multiple Count Queries

I have the following query:
SELECT COUNT(package) as advanced_count FROM users WHERE package = '2' AND site_url is NOT NULL;
What I want to do is have 2 more queries to get the basic_count and then the total_count which is basic + advanced.
My other basic query is:
SELECT count(package) as basic_count FROM users WHERE package = '1' AND site_url is NOT NULL
But I'm not sure how to combine the two so it is just one query, and then plus adding the total number in there as well.
I hope someone can point me in the right directions.
Thank you!
SELECT SUM(CASE WHEN package = '2' THEN 1 ELSE 0 END) AS advanced_count,
SUM(CASE WHEN package = '1' THEN 1 ELSE 0 END) AS basic_count,
COUNT(*) AS total_count
FROM users
WHERE site_url IS NOT NULL
AND package IN ('1', '2');