SQL display duplicate field but not nulled field - sql

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)

Related

SQL: Checking value counts of a column

I'd like to check if a column in a table has values with a small number of value counts.
Consider the following table as an example:
RowID |Product
1 | A
2 | A
3 | B
...
200.000 | C
the following table is aggregated of the table above:
Product |Count
A |204
B |682
C |553
D |1402
E |30855
F |357
G |1
H |542
What I'd like to know of the column Product of my table is, whether or not a Product has a count that is less than 5%. And if so, the SQL statement should return: 'Some values of this field have a small number of value counts'
In other words: IF [MinValueCount]/[Count] <= .05 then 'Some values of this field have a small number of value counts' else 'null'
With the example above, I should get: 'Some values of this field have a small number of value counts'
as product G is less than 5% of the total count of products.
how should the SQL statement look like?
With kind regards,
Lazzanova
Use two levels of aggregation. You can get the total using window functions:
select max( 'Some values of this field have a small number of value counts')
from (select product, count(*) as cnt,
sum(count(*)) over () as total_cnt
from t
) t
where cnt < 0.05 * total_cnt;
The use of max() in the outer query is just to return one row. You could also use fetch or a similar clause (whatever your database supports):
select 'Some values of this field have a small number of value counts'
from (select product, count(*) as cnt,
sum(count(*)) over () as total_cnt
from t
) t
where cnt < 0.05 * total_cnt
fetch first 1 row only;

hoe to make sum of one sql table column with ignoring duplicate values?

quoteId price
1 50
1 50
2 10
3 40
3 40
3 40
4 10
In this table I always get the same price for each quoteId.
Example: quoteId = 1 has multiple entries with the price of 50.
By using SUM I get the total sum of the price column which is:50 + 50 + 10 + 40 + 40 + 40 + 10 = 240
However, I only want to sum the unique price for quoteId which is:
50+10+40+10 = 110
How can I approch this?
Another option is DISTINCT
Example
Select MyTotal = sum(price)
from (Select Distinct quoteId,price From YourTable) A
Returns
MyTotal
110
Following query will work:
select sum(price)
from yourTablename
group by quoteId,price;
You need a nested query to compute an intermediate value by quoteId using avg ( or max or min with your data)
But you need know why you have duplicate value by quotedId, may be you have a mistake before.
select sum(price) from (
select
quoteId,
avg(price) price,
from
your_table
group by
quoteId
) as x
This query is compliant with ISO standard SQL and will works with several database engine

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.

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;

Get percent of columns that completed by calculating null values

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)