Hey guys,
I am trying to run a query that will return all rows where a column is
completely empty of data.
I try this query:
select * from patients_visitInsurers
where CompanyID is NULL
But it does not return anything for me. If I try the reverse
select * from patients_visitInsurers
where CompanyID is not NULL
It returns rows that contain CompanyId values and rows that are blank.
What am I missing here? Thanks!
Do you have those blank values for CompanyID being NULL or just blank
string? Assuming CompanyID is character format, you can check:
SELECT <columns>
FROM patients_visitInsurers
WHERE COALESCE(CompanyID, '') = ''
Or
SELECT <columns>
FROM patients_visitInsurers
WHERE CompanyID IS NULL
OR CompanyID = ''
HTH,
Plamen Ratchev
http://www.SQLStudio.com
|||Is CompanyID a string? "Empty of data" and "blank" are two different
things, in my opinion. Empty of data means NULL (and you have the correct
syntax, of that were the case). Blank means an empty string, e.g. ''. In
which case,
WHERE CompanyID = ''
You may also be safer to trim the data first, since it could contain a
space...
WHERE RTRIM(CompanyID) = ''
However, you should not insert a blank string when you really meant NULL.
They are different in implementation, and they are different on a conceptual
level, as well.
<alvinstraight38@.hotmail.com> wrote in message
news:40bfbcdf-5531-4350-9889-dee8af80b160@.d1g2000hsg.googlegroups.com...
> Hey guys,
> I am trying to run a query that will return all rows where a column is
> completely empty of data.
> I try this query:
> select * from patients_visitInsurers
> where CompanyID is NULL
> But it does not return anything for me. If I try the reverse
> select * from patients_visitInsurers
> where CompanyID is not NULL
> It returns rows that contain CompanyId values and rows that are blank.
> What am I missing here? Thanks!
|||Plamen Ratchev was thinking very hard :
> Do you have those blank values for CompanyID being NULL or just blank string?
> Assuming CompanyID is character format, you can check:
> SELECT <columns>
> FROM patients_visitInsurers
> WHERE COALESCE(CompanyID, '') = ''
> Or
> SELECT <columns>
> FROM patients_visitInsurers
> WHERE CompanyID IS NULL
> OR CompanyID = ''
>
and if that doesn't get it, then
OR LTRIM(RTRIM(CompanyID)) = ''
HTH,
Brad.
|||On Apr 4, 11:07Xam, "Aaron Bertrand [SQL Server MVP]"
<ten...@.dnartreb.noraa> wrote:
> Is CompanyID a string? X"Empty of data" and "blank" are two different
> things, in my opinion. XEmpty of data means NULL (and you have the correct
> syntax, of that were the case). XBlank means an empty string, e.g. ''. XIn
> which case,
> WHERE CompanyID = ''
> You may also be safer to trim the data first, since it could contain a
> space...
> WHERE RTRIM(CompanyID) = ''
> However, you should not insert a blank string when you really meant NULL.
> They are different in implementation, and they are different on a conceptual
> level, as well.
> <alvinstraigh...@.hotmail.com> wrote in message
> news:40bfbcdf-5531-4350-9889-dee8af80b160@.d1g2000hsg.googlegroups.com...
>
>
>
>
>
> - Show quoted text -
I think that is where I am getting confused. I looked at the data
type and it says varchar and not null. If I look at the database,
nothing shows in the column for some rows, but I want to exclude rows
that do contain values in this field.
Thanks!
|||>>
I think that is where I am getting confused. I looked at the data
type and it says varchar and not null. If I look at the database,
nothing shows in the column for some rows,[vbcol=seagreen]
Right. So you need to have two concepts very clear:
NULL is the absence of any data whatsoever.
'' is an empty string. It is data, even if it is zero-length.
There is little point in setting this column to NOT NULL if you can stick an
empty string in there. In this specific situation, it is apparent that the
two concepts are interchangeable...
Showing posts with label queryselect. Show all posts
Showing posts with label queryselect. Show all posts
Friday, March 30, 2012
Wednesday, March 28, 2012
Having condition
I have query:
SELECT name,company,sum(quantity) as quantity from table
GROUP BY name,company WITH ROLLUP HAVING name is not null
But in result set I get also resulting rows with name is null:
name company quantity
--
NULL NULL 100
NULL company1 2
NULL company2 5
...
...
name 1 company1 2
...
Any idea why?
How can I remove summarazing rows with name is null in result set?
I thought that having should work?
regards,Simonwhere name is not null
Having is intended for filtering aggregate values, not column values.
Admittedly, I would have expected it to work anyway. Maybe the rollup is
affecting it. I don't generally have a use for rollup, so I'm not really
sure how it functions...
"simonZ" <simon.zupan@.studio-moderna.com> wrote in message
news:%23N4vH%23HZGHA.2136@.TK2MSFTNGP05.phx.gbl...
> I have query:
> SELECT name,company,sum(quantity) as quantity from table
> GROUP BY name,company WITH ROLLUP HAVING name is not null
> But in result set I get also resulting rows with name is null:
> name company quantity
> --
> NULL NULL 100
> NULL company1 2
> NULL company2 5
> ...
> ...
> name 1 company1 2
> ...
> Any idea why?
> How can I remove summarazing rows with name is null in result set?
> I thought that having should work?
> regards,Simon
>|||Hi,
Check WITH ROLLUP clause in BOL.
The number of summary rows in the result set is determined by the number of
columns included in the GROUP BY clause. Each operand (column) in the GROUP
BY clause is bound under the grouping NULL and grouping is applied to all
other operands (columns). Because CUBE returns every possible combination of
group and subgroup, the number of rows is the same, regardless of the order
in which the grouping columns are specified.
Tomasz B.
"Jim Underwood" wrote:
> where name is not null
> Having is intended for filtering aggregate values, not column values.
> Admittedly, I would have expected it to work anyway. Maybe the rollup is
> affecting it. I don't generally have a use for rollup, so I'm not really
> sure how it functions...
> "simonZ" <simon.zupan@.studio-moderna.com> wrote in message
> news:%23N4vH%23HZGHA.2136@.TK2MSFTNGP05.phx.gbl...
>
>|||Thanks. I looked it up after making the last post, and tested it out.
On SQL 2000 the having clause works fine with this same logic. Granted, my
tables are different, but the following excluded null values from the result
set.
select description, bogusID, count(*), sum(random_id) from my_table
group by description, bogusID with rollup
having description is not null.
If I remove the having, then I get back rows where description is null, with
the having these rows are excluded. However, in my test the having
description is not null also removed the grand summary at the end of the
report. This is as expected, and the grand summary row is there if I use
where instead of having.
"Tomasz Borawski" <TomaszBorawski@.discussions.microsoft.com> wrote in
message news:2E2D3DBB-EA47-4E7F-BBB5-73C86261AA95@.microsoft.com...
> Hi,
> Check WITH ROLLUP clause in BOL.
> The number of summary rows in the result set is determined by the number
of
> columns included in the GROUP BY clause. Each operand (column) in the
GROUP
> BY clause is bound under the grouping NULL and grouping is applied to all
> other operands (columns). Because CUBE returns every possible combination
of
> group and subgroup, the number of rows is the same, regardless of the
order
> in which the grouping columns are specified.
> Tomasz B.
> "Jim Underwood" wrote:
>
is
really|||I meant to ask in my last response...
What version of SQL Server are you using, and could you post table DDL and
the full query that you are executing. If what you already posted is the
complete query, then just say so.
"simonZ" <simon.zupan@.studio-moderna.com> wrote in message
news:%23N4vH%23HZGHA.2136@.TK2MSFTNGP05.phx.gbl...
> I have query:
> SELECT name,company,sum(quantity) as quantity from table
> GROUP BY name,company WITH ROLLUP HAVING name is not null
> But in result set I get also resulting rows with name is null:
> name company quantity
> --
> NULL NULL 100
> NULL company1 2
> NULL company2 5
> ...
> ...
> name 1 company1 2
> ...
> Any idea why?
> How can I remove summarazing rows with name is null in result set?
> I thought that having should work?
> regards,Simon
>|||The only reliable test you can make in HAVING with ROLLUP/CUBE
is based on the GROUPING(column) test.So in this example you can test
HAVING GROUPING([name])=0/1
HAVING GROUPING(company)=0/1
HAVING GROUPING([name])=0/1 and/or GROUPING(company)=0/1
0 will test for null,1 a non null if i remember:)
Of course you make as complex a HAVING as you want using the GROUPING()
statements ie.
GROUPING([name])+GROUPING(company)=0
Of course the big implication of all this is that a temp table or derived
table
must be used for aggregate testing.Silly:)
"simonZ" <simon.zupan@.studio-moderna.com> wrote in message
news:%23N4vH%23HZGHA.2136@.TK2MSFTNGP05.phx.gbl...
>I have query:
> SELECT name,company,sum(quantity) as quantity from table
> GROUP BY name,company WITH ROLLUP HAVING name is not null
> But in result set I get also resulting rows with name is null:
> name company quantity
> --
> NULL NULL 100
> NULL company1 2
> NULL company2 5
> ...
> ...
> name 1 company1 2
> ...
> Any idea why?
> How can I remove summarazing rows with name is null in result set?
> I thought that having should work?
> regards,Simon
>
SELECT name,company,sum(quantity) as quantity from table
GROUP BY name,company WITH ROLLUP HAVING name is not null
But in result set I get also resulting rows with name is null:
name company quantity
--
NULL NULL 100
NULL company1 2
NULL company2 5
...
...
name 1 company1 2
...
Any idea why?
How can I remove summarazing rows with name is null in result set?
I thought that having should work?
regards,Simonwhere name is not null
Having is intended for filtering aggregate values, not column values.
Admittedly, I would have expected it to work anyway. Maybe the rollup is
affecting it. I don't generally have a use for rollup, so I'm not really
sure how it functions...
"simonZ" <simon.zupan@.studio-moderna.com> wrote in message
news:%23N4vH%23HZGHA.2136@.TK2MSFTNGP05.phx.gbl...
> I have query:
> SELECT name,company,sum(quantity) as quantity from table
> GROUP BY name,company WITH ROLLUP HAVING name is not null
> But in result set I get also resulting rows with name is null:
> name company quantity
> --
> NULL NULL 100
> NULL company1 2
> NULL company2 5
> ...
> ...
> name 1 company1 2
> ...
> Any idea why?
> How can I remove summarazing rows with name is null in result set?
> I thought that having should work?
> regards,Simon
>|||Hi,
Check WITH ROLLUP clause in BOL.
The number of summary rows in the result set is determined by the number of
columns included in the GROUP BY clause. Each operand (column) in the GROUP
BY clause is bound under the grouping NULL and grouping is applied to all
other operands (columns). Because CUBE returns every possible combination of
group and subgroup, the number of rows is the same, regardless of the order
in which the grouping columns are specified.
Tomasz B.
"Jim Underwood" wrote:
> where name is not null
> Having is intended for filtering aggregate values, not column values.
> Admittedly, I would have expected it to work anyway. Maybe the rollup is
> affecting it. I don't generally have a use for rollup, so I'm not really
> sure how it functions...
> "simonZ" <simon.zupan@.studio-moderna.com> wrote in message
> news:%23N4vH%23HZGHA.2136@.TK2MSFTNGP05.phx.gbl...
>
>|||Thanks. I looked it up after making the last post, and tested it out.
On SQL 2000 the having clause works fine with this same logic. Granted, my
tables are different, but the following excluded null values from the result
set.
select description, bogusID, count(*), sum(random_id) from my_table
group by description, bogusID with rollup
having description is not null.
If I remove the having, then I get back rows where description is null, with
the having these rows are excluded. However, in my test the having
description is not null also removed the grand summary at the end of the
report. This is as expected, and the grand summary row is there if I use
where instead of having.
"Tomasz Borawski" <TomaszBorawski@.discussions.microsoft.com> wrote in
message news:2E2D3DBB-EA47-4E7F-BBB5-73C86261AA95@.microsoft.com...
> Hi,
> Check WITH ROLLUP clause in BOL.
> The number of summary rows in the result set is determined by the number
of
> columns included in the GROUP BY clause. Each operand (column) in the
GROUP
> BY clause is bound under the grouping NULL and grouping is applied to all
> other operands (columns). Because CUBE returns every possible combination
of
> group and subgroup, the number of rows is the same, regardless of the
order
> in which the grouping columns are specified.
> Tomasz B.
> "Jim Underwood" wrote:
>
is
really|||I meant to ask in my last response...
What version of SQL Server are you using, and could you post table DDL and
the full query that you are executing. If what you already posted is the
complete query, then just say so.
"simonZ" <simon.zupan@.studio-moderna.com> wrote in message
news:%23N4vH%23HZGHA.2136@.TK2MSFTNGP05.phx.gbl...
> I have query:
> SELECT name,company,sum(quantity) as quantity from table
> GROUP BY name,company WITH ROLLUP HAVING name is not null
> But in result set I get also resulting rows with name is null:
> name company quantity
> --
> NULL NULL 100
> NULL company1 2
> NULL company2 5
> ...
> ...
> name 1 company1 2
> ...
> Any idea why?
> How can I remove summarazing rows with name is null in result set?
> I thought that having should work?
> regards,Simon
>|||The only reliable test you can make in HAVING with ROLLUP/CUBE
is based on the GROUPING(column) test.So in this example you can test
HAVING GROUPING([name])=0/1
HAVING GROUPING(company)=0/1
HAVING GROUPING([name])=0/1 and/or GROUPING(company)=0/1
0 will test for null,1 a non null if i remember:)
Of course you make as complex a HAVING as you want using the GROUPING()
statements ie.
GROUPING([name])+GROUPING(company)=0
Of course the big implication of all this is that a temp table or derived
table
must be used for aggregate testing.Silly:)
"simonZ" <simon.zupan@.studio-moderna.com> wrote in message
news:%23N4vH%23HZGHA.2136@.TK2MSFTNGP05.phx.gbl...
>I have query:
> SELECT name,company,sum(quantity) as quantity from table
> GROUP BY name,company WITH ROLLUP HAVING name is not null
> But in result set I get also resulting rows with name is null:
> name company quantity
> --
> NULL NULL 100
> NULL company1 2
> NULL company2 5
> ...
> ...
> name 1 company1 2
> ...
> Any idea why?
> How can I remove summarazing rows with name is null in result set?
> I thought that having should work?
> regards,Simon
>
Subscribe to:
Posts (Atom)