i have the following table and im looking to create a query that will have 3 equations
sum "like%" and "%love%" count values together and call that Top 2 Box
sum the count of "dislike%" and "%hate%" as Bottom 2 Box
sum count of "%neutral%" together
for some reason my query is producing null and also placing the 3 values i want as their own individual columns when I want these 3 values to be in rows.
test 2 campaign table
values
count
like
5
like it
5
love
5
loveit
5
hate
1
hate it
0
dislike it
3
neutral
10
WIP (work in progress) query
SELECT
sum (CASE
WHEN VALUES ="%love%" OR VALUES ="like%" THEN count
ELSE
NULL
END
) AS Top2,
sum (CASE
WHEN VALUES ="%hate%" OR VALUES ="dislike%" THEN count
ELSE
NULL
END
) AS Bottom2,
sum (CASE
WHEN VALUES ="%neutral%" THEN count
ELSE
NULL
END
) AS Neutral
FROM
`test 2 campaign rating`
output from query
Top2
Bottom2
Neutral
null
null
null
Top2 Bottom2 Neutral
null null null
desired output
values
count
Top2
20
Bottom2
4
Neutral
10
Consider below query.
SELECT CASE
WHEN values LIKE "%love%" OR values LIKE "like%" THEN 'Top2'
WHEN values LIKE "%hate%" OR values LIKE "dislike%" THEN 'Bottom2'
ELSE 'Neutral'
END AS values,
SUM(count) AS count
FROM campaign_table
GROUP BY values;
Query results
Related
i am trying to do a count on null values however , it is not able to count null values.
example of table :
Country Id Id_typ Info
Us 123 NULL Testing
Us 124 NULL Testing
Us 125 Bob testing
this is my script to count null values
select count(id_typ) from combined_a where id_typ= 'NULL' limit 1
i have tried
select count(id_typ) from table_a where id_typ is null limit 1
however when i have change the condition to search id_typ = bob, it was able to do a count . i am unsure on what did i do wrong , any advice?
You need is null and count(*):
select count(*)
from table_a
where id_typ is null;
limit 1 is redundant. A SQL query with an aggregation function and no group by always returns one row.
Using SQL Server, I have a table as shown in the sample table below. I need to have sum of all the unique values per the columns "BookOrder, StationaryOrder, and Printing Order".
SAMPLE TABLE:
KeyIDCustomer BooksOrder StationaryOrder PrintingOrder
29945843 1070756 1891514 198876
29945843 1070756 1893827 198876
29945843 1070758 1891514 198876
29945843 1070758 1893827 198876
I am using the below coding to achieve this goal.
Select DISTINCT KeyIDCustomerID,
Sum(Case when BooksOrder is not null then 1 else 0 End) TotalBookOrders,
Sum(Case when StationaryOrder is not null then 1 else 0 End) TotalStationaryOrder,
Sum(Case when PrintingOrder is not null then 1 else 0 End)TotalPrintingOrder
With this coding in am getting the results as below
KeyIDCustomerID TotalBookOrders TotalStationaryOrder TotalPrintingOrder
29945843 4 4 4
I expect the results to be like this
KeyIDCustomerID TotalBookOrders TotalStationaryOrder TotalPrintingOrder
29945843 2 2 1
Is there a way i can accomplish this Goal in SQL?
Thanks
i think the proper term for
sum of all the unique values per the columns
is "count of unique values"
COUNT (DISTINCT column_name) returns the number of unique, non-null values in column_name
Select
KeyIDCustomerID,
COUNT(DISTINCT BooksOrder) as TotalBookOrders,
COUNT(DISTINCT StationaryOrder) as TotalStationaryOrder,
COUNT(DISTINCT PrintingOrder) as TotalPrintingOrder
FROM SAMPLE_TABLE
GROUP BY KeyIDCustomerID
Do a group by, count(distinct column) to count:
Select KeyIDCustomerID,
COUNT(distinct BooksOrder) TotalBookOrders,
COUNT(distinct StationaryOrder) TotalStationaryOrder,
COUNT(distinct PrintingOrder) TotalPrintingOrder
from tablename
group by KeyIDCustomerID
I have a simple problem i think. I have in sql Server one table with this :
Name : Sum : CNP
Andrey 100 120
Marius 20 100
George 20 200
Popescu Nulled 300
Antal Nulled 100
I use this comand to show duplicate :
SELECT SUM, Name,CNP
FROM dbo.database
where SUM IN ( Select SUM from dbo.asigpag group by SUM HAVING Count(*)> 1)
Everything work ok.
In this Case show :
Name : Sum : CNP
Marius 20 100
George 20 200
Popescu Nulled 300
Antal Nulled 100
This is the problem . I want to display duplicate but with not Nulled.
I want to display this with all the other field not only Sum.
Name : Sum : CNP
Marius 20 100
George 20 200
You need to add another condition to exclude records that have a null value in the sum field:
SELECT SUM, Name,CNP
FROM dbo.database
where SUM IN ( Select SUM from dbo.asigpag group by SUM HAVING Count(*)> 1)
AND SUM is not NULL
SQL Server treats NULLS differently from values, because they have no value at all. They're special case that need to be selected using [Field] IS NULL or [Field] = NULL, or their reverse, as in this case.
In SQL NULL <> NULL always. You can use IS NULL or IS NOT NULL or predefined function ISNULL(SUM, 0) - with last statement you will prepare NULL values to default 0 value. For example:
SELECT SUM, Name,CNP
FROM dbo.database
where ISNULL(SUM, 0) IN ( Select ISNULL(SUM, 0) from dbo.asigpag group by SUM HAVING Count(*) > 1)
Update:
Sorry, I misunderstood what you want. In order to eliminate NULL from the list you need to update sub-query as:
Select SUM from dbo.asigpag where SUM IS NOT NULL group by SUM HAVING Count(*) > 1
Whole query will be:
SELECT SUM, Name, CNP
FROM dbo.database
where SUM IN ( Select SUM from dbo.asigpag Where SUM IS NOT NULL group by SUM HAVING Count(*) > 1)
I have a table
num
----
NULL
NULL
NULL
NULL
55
NULL
NULL
NULL
99
when I wrote
select COUNT(*)
from tbl
where num is null
the output was 7
but when I wrote
select COUNT(num)
from tbl
where num is null
the output was 0
what's the difference between these two queries ??
Difference is in the field you select.
When counting COUNT(*) NULL values are taken into account (count all rows returned).
When counting COUNT(num) NULL values are NOT taken into account (count all non-null fields).
That is a standard behavior in SQL, whatever the DBMS used
Source. look at COUNT(DISTINCT expr,[expr...])
count(*) returns number of rows, count(num) returns number of rows where num is not null. Change your last query to select count(*) from test where num is null to get the result you expect.
In second case first count values are eliminated and then where clause comes in picture. While in first case when you are using * row with null is not eliminated.
If you are counting on a coll which contains null and you want rows with null to be included in count than use
Count(ISNULL(col,0))
Count(*) counts the number of rows, COUNT(num) counts the number of not-null values in column num.
Considering the output given above, the result of the query count(num) should be 2.
I have a table with a column that allows nulls. If the value is null it is incomplete. I want to calculate the percentage complete.
Can this be done in MySQL through SQL or should I get the total entries and the total null entries and calculate the percentage on the server?
Either way, I'm very confused on how I need to go about separating the variable_value so that I can get its total results and also its total NULL results.
SELECT
games.id
FROM
games
WHERE
games.category_id='10' AND games.variable_value IS NULL
This gives me all the games where the variable_value is NULL. How do I extend this to also get me either the TOTAL games or games NOT NULL along with it?
Table Schema:
id (INT Primary Auto-Inc)
category_id (INT)
variable_value (TEXT Allow Null Default: NULL)
When you use "Count" with a column name, null values are not included. So to get the count or percent not null just do this...
SELECT
count(1) as TotalAll,
count(variable_value) as TotalNotNull,
count(1) - count(variable_value) as TotalNull,
100.0 * count(variable_value) / count(1) as PercentNotNull
FROM
games
WHERE
category_id = '10'
SELECT
SUM(CASE WHEN G.variable_value IS NOT NULL THEN 1 ELSE 0 END)/COUNT(*) AS pct_complete
FROM
Games G
WHERE
G.category_id = '10'
You might need to do some casting on the SUM() so that you get a decimal.
To COUNT the number of entries matching your WHERE statement, use COUNT(*)
SELECT COUNT(*) AS c FROM games WHERE games.variable_value IS NULL
If you want both total number of rows and those with variable_value being NULL in one statement, try GROUP BY
SELECT COUNT(variable_value IS NULL) AS c, (variable_value IS NULL) AS isnull FROM games GROUP BY isnull
Returns something like
c | isnull
==============
12 | 1
193 | 0
==> 12 entries have NULL in that column, 193 havn't
==> Percentage: 12 / (12 + 193)