Hue is not capturing null values - sql

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.

Related

Need a Logic to solve the task

How to skip the null values
Example Table Structure: Table Name : T1
ID A_Comments A_ts B_Comments B_ts C_Comments C_ts
1 Approved 20 Simple null fine null
1 Approved null Simple 10 fine null
1 Approved null Simple null fine 30
Expecting result:
ID A_Comments A_ts B_Comments B_ts C_Comments C_ts
1 Approved 20 Simple 10 fine 30
Use aggregation:
select id, max(a_comments) as a_commments, max(a_ts) as a_ts, . . .
from t1
group by id;
I am guessing that your table was created by an aggregation query with one too many columns in the group by.

sql count mismatch

I am not able to understand the SQL query output :
SQL> select distinct(STATUS) from TMP_ORDER_ACTION_PSTN_CP_11035;
InDelivery_SOMBe
In Delivery
Complete
Amended
Cancelled
Failed InComplete
1 SQL> select count(*) from TMP_ORDER_ACTION_PSTN_CP_11035 where
STATUS='Complete';
1484
2 SQL> select count(*) from TMP_ORDER_ACTION_PSTN_CP_11035 where STATUS
!= 'Complete';
3167
3 SQL> select count(*) from TMP_ORDER_ACTION_PSTN_CP_11035;
5091
The sum of count for the 1 and 2 queries should be same as the total count(3 query).Why is the sum differing from the whole count?
It seems like a dump question but i dont know why is this happening.
Please note that My question is not related to null check at all.It is the that
sum(1+2)=3.1484+3167 !=5091.Why is the result different?
My guess is NULL values, which match none of your WHERE clauses, including the last one. Try
select count(*) from TMP_ORDER_ACTION_PSTN_CP_11035 where STATUS is null;
where status = null is never true, nor is where null = null. You have to use is null.
The sum of count for the 1 and 2 queries should be same as the total count(3 query).Why is the sum differing from the whole count?
No, because the records with NULL are not matching query 1 or query 2, but they are counted in query 3.
1 + 2 + IS NULL should equal 3.
WHERE STATUS = NULL won't work. Nothing equals NULL.
Try IS instead of =...
select count(*) from TMP_ORDER_ACTION_PSTN_CP_11035 where STATUS IS null
Try this:
Assuming p_key is a primary key for the table,
select count(p_key) from TMP_ORDER_ACTION_PSTN_CP_11035 where STATUS='Complete';
select count(p_key) from TMP_ORDER_ACTION_PSTN_CP_11035 where STATUS <> 'Complete';
select count(p_key) from TMP_ORDER_ACTION_PSTN_CP_11035 ;

SQL select COUNT issue

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.

copy column values to another part of the table

I have a SELECT statement along with a join that uses two tables. We have tables Shell and table Tanker.
It looks something like this select S.*, T.* from shell S left outer join tanker T on S.id = T.id. Note that Tanker has about 183 fields.
So I get the following data
====Shell table==== =========Tanker table columsn======================
orgid org eli lang orgid org Lang {Other columns start}
906875 s 1 1 NULL NULL NULL NULL NULL NULL NULL
906876 s 2 2 NULL NULL NULL NULL NULL NULL NULL
906877 b 2 1 NULL NULL NULL NULL NULL NULL NULL
906878 s 1 1 NULL NULL NULL NULL NULL NULL NULL
906879 b 2 1 NULL NULL NULL NULL NULL NULL NULL
Now I want to select orgid, org, lang, and the other 183 fields. So right now I do
select T.* FROM ....
but the problem with this is that when it selects the orgid, org, lang, and eli it gets the NULLS only. Which makes sense because I am only selecting T.* and its an left join so naturally these will be null.
However, for my purpose I need them to be the values from the shell table. Ie, I want to select the orgid, org, eli, lang from Shell and then the rest from Tanker. I know the one way I do this is the following
select s.orgid, s.org, s.eli, s.lang, t.column, t.column2, t.column3
BUT RECALL that Tanker T has almost 200 columns. I do not want to write 200 columns on my script.
So here is the question.
Is there any way I can "copy" the values of orgid, org, eli, lang (ie, the actual not null values) to the columns which are null? That way I can just do select T.* and select all 200 columns.
so really if you are confused the end result I am looking for is
====Shell table==== =========Tanker table columsn======================
orgid org eli lang orgid org Eli Lang {Other columns start}
906875 s 1 1 906875 s 1 1 NULL NULL
906876 s 2 2 906876 s 2 2 NULL NULL NULL
906877 b 2 1 906877 b 2 1 NULL NULL
906878 s 1 1 906878 s 1 1 NULL NULL NULL
906879 b 2 1 906879 b 2 1 NULL NULL NULL
In this case, I would recommend just writing a view that would perform this select and you can reuse it wherever you need this kind of functionality. Should you ever need to modify it, it's a simple matter of updating the view (although you'll have to be careful you don't have any fundamental logic working against that view).
In order to do this easily, you can actually click-drag a table from SSMS into the query window. Just click-drag the Columns folder under the table in question into the query window and it will copy ALL the columns into it. That way you can do the select manually without having to type everything in.
you can use Isnull (SQL server). It allows you to provide a value to use incase column you are selecting is null.
select isnull(t.org, s.org) as org
if T has a value, it gives you that, else it give you the one from S
EDIT:
SPFiredrake pointed I focused on the wrong part. Another thing you could do (if all your worried about is a HUGE SQL script) is to select * from T and then just the few columns from S that may be needed and then handle the logic of comparrision in whatever lanaguage will be playing with your data.

SQL Query Help: Returning distinct values from Count subquery

I've been stuck for quite a while now trying to get this query to work.
Here's the setup:
I have a [Notes] table that contains a nonunique (Number) column and a nonunique (Result) column. I'm looking to create a SELECT statement that will display each distinct (Number) value where the count of the {(Number), (Result)} tuple where Result = 'NA' is > 25.
Number | Result
100 | 'NA'
100 | 'TT'
101 | 'NA'
102 | 'AM'
100 | 'TT'
200 | 'NA'
200 | 'NA'
201 | 'NA'
Basically, have an autodialer that calls a number and returns a code depending on the results of the call. We want to ignore numbers that have had an 'NA'(no answer) code returned more than 25 times.
My basic attempts so far have been similar to:
SELECT DISTINCT n1.Number
FROM Notes n1
WHERE (SELECT COUNT(*) FROM Notes n2
WHERE n1.Number = n2.Number and n1.Result = 'NA') > 25
I know this query isn't correct, but in general I'm not sure how to relate the DISTINCT n1.Number from the initial select to the Number used in the subquery COUNT. Most examples I see aren't actually doing this by adding a condition to the COUNT returned. I haven't had to touch too much SQL in the past half decade, so I'm quite rusty.
you can do it like this :
SELECT Number
FROM Notes
WHERE Result = 'NA'
GROUP BY Number
HAVING COUNT(Result) > 25
Try this:
SELECT Number
FROM (
SELECT Number, Count(Result) as CountNA
FROM Notes
WHERE Result = 'NA'
GROUP BY Number
)
WHERE CountNA > 25
EDIT: depending on SQL product, you may need to give the derived table a table correlation name e.g.
SELECT DT1.Number
FROM (
SELECT Number, Count(Result) as CountNA
FROM Notes
WHERE Result = 'NA'
GROUP
BY Number
) AS DT1 (Number, CountNA)
WHERE DT1.CountNA > 25;