Showing posts with label null. Show all posts
Showing posts with label null. Show all posts

Sunday, March 25, 2012

Basic "RANK" question: how to stop null records appearing.

i have a query that runs just fine, returning the turnover per sector, where the sector has non-null turnover.

I'd like to add ranking into the query, but when I do, I now get ALL sectors, with those sectors having null turnover all ranking equal bottom.

This kinda makes sense, but essentially I'd like to exclude the null turnover ones. I've tried various combinations of nonempty, NON EMPTY and Exists without success. What's the answer to this, and more importantly, how should I be thinking this through to get the right answer myself?

1. First query: i have defined the ranking measure, but not used it. I get only non empty sectors.

WITH

member measures.[turnover rank] as rank([SECTOR3].[SECTOR3 ID].CurrentMember, [SECTOR3].[SECTOR3 ID].[SECTOR3 ID].ALLMEMBERS)

SELECT {[Measures].[TURNOVER]} on columns,

nonempty {[SECTOR3].[SECTOR3 ID].[SECTOR3 ID].ALLMEMBERS} ONROWS

FROM [WmCube4]

where [TIME].[Date].&[2007-06-04T00:00:00]

2. Second query: i include the ranking measure in the output - now null records appear.

WITH

member measures.[turnover rank] as rank([SECTOR3].[SECTOR3 ID].CurrentMember, [SECTOR3].[SECTOR3 ID].[SECTOR3 ID].ALLMEMBERS)

SELECT {[Measures].[TURNOVER],measures.[turnover rank]} on columns,

nonempty {[SECTOR3].[SECTOR3 ID].[SECTOR3 ID].ALLMEMBERS} ONROWS

FROM [WmCube4]

where [TIME].[Date].&[2007-06-04T00:00:00]

Not sure how ranking could occur without using a 3rd parameter for Rank() - but you can return null sectors with empty turnover, like:

member measures.[turnover rank] as

iif(IsEmpty([Measures].[TURNOVER]), Null,

rank([SECTOR3].[SECTOR3 ID].CurrentMember,

[SECTOR3].[SECTOR3 ID].[SECTOR3 ID].ALLMEMBERS,

[Measures].[TURNOVER]))

|||

works perfectly, thanks!

Tuesday, March 20, 2012

bad table design

A developer has just gave me a few tables table to put
into production, with all character columns defined as
varchar(255), null.
As I am absolutely positive that the majority of these
columns will not be storing this amount of data in each
of the columns, as some of them are code columns, types,
and one description field.
After asking him to review all of the columns and the
maximum estimated data stored in each one, he said that
it shouldn't make any difference because A) they're
varchar and B) they're defined as null.
What can I say to him to make him do the needful?
Thanks,
MarcusMarcus
Well, variablle-length characters ( as in your case) is stored 1 byte per
character.Declared but unused characters don't consume storage.
On the other hand I'll be conserned about using NULL's when you will be
querieng the tables.
Using IS NULL/NOT NULL clause will not allow Query Optimizer to use an index
and respectively your query will suffer from perfomance hit.
Also consider using DEFAULT values in those columns.
"Marcus" <anonymous@.discussions.microsoft.com> wrote in message
news:8b7101c404e1$04e07c70$a401280a@.phx.gbl...
> A developer has just gave me a few tables table to put
> into production, with all character columns defined as
> varchar(255), null.
> As I am absolutely positive that the majority of these
> columns will not be storing this amount of data in each
> of the columns, as some of them are code columns, types,
> and one description field.
> After asking him to review all of the columns and the
> maximum estimated data stored in each one, he said that
> it shouldn't make any difference because A) they're
> varchar and B) they're defined as null.
> What can I say to him to make him do the needful?
> Thanks,
> Marcus|||Pick up one of Joe Celko's post from here regarding how important it is to
get the database design right and where he state that he almost never have
to use NULL. If he question Joe's credibility, search for instance Amazon of
the books he has been writing (or go to www.celko.com).
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
"Marcus" <anonymous@.discussions.microsoft.com> wrote in message
news:8b7101c404e1$04e07c70$a401280a@.phx.gbl...
> A developer has just gave me a few tables table to put
> into production, with all character columns defined as
> varchar(255), null.
> As I am absolutely positive that the majority of these
> columns will not be storing this amount of data in each
> of the columns, as some of them are code columns, types,
> and one description field.
> After asking him to review all of the columns and the
> maximum estimated data stored in each one, he said that
> it shouldn't make any difference because A) they're
> varchar and B) they're defined as null.
> What can I say to him to make him do the needful?
> Thanks,
> Marcus|||Right. I've brought that up also and he's come back and
said that the table is for importing data only and it
shouldn't matter.
You're thoughts?
Thanks!
Marcus
>--Original Message--
>Marcus
>Well, variablle-length characters ( as in your case) is
stored 1 byte per
>character.Declared but unused characters don't consume
storage.
>On the other hand I'll be conserned about using NULL's
when you will be
>querieng the tables.
>Using IS NULL/NOT NULL clause will not allow Query
Optimizer to use an index
>and respectively your query will suffer from perfomance
hit.
>Also consider using DEFAULT values in those columns.
>
>"Marcus" <anonymous@.discussions.microsoft.com> wrote in
message
>news:8b7101c404e1$04e07c70$a401280a@.phx.gbl...
types,
>
>.
>|||Marcus
Look, we can discuss a lot on this topic.
In outer-join operation you should carefully account for NULL's values that
are generated to preserve rows that don't have a match in the table being
joined.
Also dealing with NULL's adding complexity to the storage engine because SQL
Server keeps a special bitmap in every row to indicate which nullable
columns actually are NULL. SQL Server must decode the bitmap for every row
accessed.
Again I'd recommed you/your opponent to make all nullable columns as NOT
NULL and DEFAULT values.
"Marcus" <anonymous@.discussions.microsoft.com> wrote in message
news:8c2301c404f2$255e24a0$a501280a@.phx.gbl...
> Right. I've brought that up also and he's come back and
> said that the table is for importing data only and it
> shouldn't matter.
> You're thoughts?
> Thanks!
> Marcus
> stored 1 byte per
> storage.
> when you will be
> Optimizer to use an index
> hit.
> message
> types,|||"Marcus" <anonymous@.discussions.microsoft.com> wrote in message
news:8b7101c404e1$04e07c70$a401280a@.phx.gbl...
> A developer has just gave me a few tables table to put
> into production, with all character columns defined as
> varchar(255), null.
Two aspects :
varchar(255), there is nothing wrong defining a varchar
type with a number of characters. Varchar(20) and varchar(255)
both occupie the same number of bytes if the strings are the same.
255 doesn't look like a 'natural' number, it is typical a number
which comes out of the head of a 'computer' oriented mind.
(80, 100, 200, 400 are more natural). But if the data comes
from another 'computer' system 255 could be the right number.
Null,
Never prefered and should be avoided. But for only importing
data (on the way to another table) and for text holding fields
on which no selection and no join is done a 'logical' choice.
If the text is human generated and a 'non' text is one of
the posibilities, I think that null is ok.
(When indexed, joined, used in a where clause one should
avoid a null).
Also concatenating with <null> strings should be avoided,
because this is handled differently in different databases.
(One could choose as wel for an empty string instead of
a null, this is supported in SQL-server, but be aware there
are RDBMSses which consider an empty string equal to
a Null string.)
I would not know of a default in a text string which is a
good replacement for a null string.
(Considering other factors as usage by programs, utilities
and other databases. Empty string is not supported
in all databases.
Using a single character as a default might conflict
with a 'real' single character even if it is a space.)
vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
vvvvvvvvvv
Does anybody know of a good 'default' for character strings
instead of using a null ?
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ben brugman

> As I am absolutely positive that the majority of these
> columns will not be storing this amount of data in each
> of the columns, as some of them are code columns, types,
> and one description field.
> After asking him to review all of the columns and the
> maximum estimated data stored in each one, he said that
> it shouldn't make any difference because A) they're
> varchar and B) they're defined as null.
> What can I say to him to make him do the needful?
> Thanks,
> Marcus

Sunday, March 11, 2012

bad case statement?

CASE WHEN u.sImage1_nm is NULL then 0
ELSEWHEN u.sImage2_nm is NULL then 1
ELSEWHEN u.sImage3_nm is NULL then 2
ELSEWHEN u.sImage4_nm is NULL then 3
ELSEWHEN u.sImage5_nm is NULL then 4
end
-- what's wrong here?
Looks good to me. Can you show the whole (simplified) query? Or, better yet, a repro?
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"n_o_s_p_a__m" <n_o_s_p_a__m@.mail.com> wrote in message
news:1faa8782.0408082134.496c2e97@.posting.google.c om...
> CASE WHEN u.sImage1_nm is NULL then 0
> ELSE WHEN u.sImage2_nm is NULL then 1
> ELSE WHEN u.sImage3_nm is NULL then 2
> ELSE WHEN u.sImage4_nm is NULL then 3
> ELSE WHEN u.sImage5_nm is NULL then 4
> end
>
> -- what's wrong here?
|||Tibor
It is wrong syntax assuming that below query (he missed addition CASE and
END respectively)
select CASE WHEN EndDate is NULL then 0
ELSE CASE WHEN InDate is NULL then 1
ELSE CASE WHEN Stardate is NULL then 2
end END END
from table
"Tibor Karaszi" <tibor_please.no.email_karaszi@.hotmail.nomail.com> wrote in
message news:e8UlrvdfEHA.2468@.TK2MSFTNGP12.phx.gbl...
> Looks good to me. Can you show the whole (simplified) query? Or, better
yet, a repro?
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
>
> "n_o_s_p_a__m" <n_o_s_p_a__m@.mail.com> wrote in message
> news:1faa8782.0408082134.496c2e97@.posting.google.c om...
>
|||The extra else's are what is killing you... Try
select
CASE
WHEN u.sImage1_nm is NULL then 0
WHEN u.sImage2_nm is NULL then 1
WHEN u.sImage3_nm is NULL then 2
WHEN u.sImage4_nm is NULL then 3
WHEN u.sImage5_nm is NULL then 4
end from yourtable
Wayne Snyder, MCDBA, SQL Server MVP
Mariner, Charlotte, NC
www.mariner-usa.com
(Please respond only to the newsgroups.)
I support the Professional Association of SQL Server (PASS) and it's
community of SQL Server professionals.
www.sqlpass.org
"n_o_s_p_a__m" <n_o_s_p_a__m@.mail.com> wrote in message
news:1faa8782.0408082134.496c2e97@.posting.google.c om...
> CASE WHEN u.sImage1_nm is NULL then 0
> ELSE WHEN u.sImage2_nm is NULL then 1
> ELSE WHEN u.sImage3_nm is NULL then 2
> ELSE WHEN u.sImage4_nm is NULL then 3
> ELSE WHEN u.sImage5_nm is NULL then 4
> end
>
> -- what's wrong here?

bad case statement?

CASE WHEN u.sImage1_nm is NULL then 0
ELSE WHEN u.sImage2_nm is NULL then 1
ELSE WHEN u.sImage3_nm is NULL then 2
ELSE WHEN u.sImage4_nm is NULL then 3
ELSE WHEN u.sImage5_nm is NULL then 4
end
-- what's wrong here?Looks good to me. Can you show the whole (simplified) query? Or, better yet, a repro?
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"n_o_s_p_a__m" <n_o_s_p_a__m@.mail.com> wrote in message
news:1faa8782.0408082134.496c2e97@.posting.google.com...
> CASE WHEN u.sImage1_nm is NULL then 0
> ELSE WHEN u.sImage2_nm is NULL then 1
> ELSE WHEN u.sImage3_nm is NULL then 2
> ELSE WHEN u.sImage4_nm is NULL then 3
> ELSE WHEN u.sImage5_nm is NULL then 4
> end
>
> -- what's wrong here?|||Tibor
It is wrong syntax assuming that below query (he missed addition CASE and
END respectively)
select CASE WHEN EndDate is NULL then 0
ELSE CASE WHEN InDate is NULL then 1
ELSE CASE WHEN Stardate is NULL then 2
end END END
from table
"Tibor Karaszi" <tibor_please.no.email_karaszi@.hotmail.nomail.com> wrote in
message news:e8UlrvdfEHA.2468@.TK2MSFTNGP12.phx.gbl...
> Looks good to me. Can you show the whole (simplified) query? Or, better
yet, a repro?
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
>
> "n_o_s_p_a__m" <n_o_s_p_a__m@.mail.com> wrote in message
> news:1faa8782.0408082134.496c2e97@.posting.google.com...
> > CASE WHEN u.sImage1_nm is NULL then 0
> > ELSE WHEN u.sImage2_nm is NULL then 1
> > ELSE WHEN u.sImage3_nm is NULL then 2
> > ELSE WHEN u.sImage4_nm is NULL then 3
> > ELSE WHEN u.sImage5_nm is NULL then 4
> > end
> >
> >
> > -- what's wrong here?
>|||The extra else's are what is killing you... Try
select
CASE
WHEN u.sImage1_nm is NULL then 0
WHEN u.sImage2_nm is NULL then 1
WHEN u.sImage3_nm is NULL then 2
WHEN u.sImage4_nm is NULL then 3
WHEN u.sImage5_nm is NULL then 4
end from yourtable
Wayne Snyder, MCDBA, SQL Server MVP
Mariner, Charlotte, NC
www.mariner-usa.com
(Please respond only to the newsgroups.)
I support the Professional Association of SQL Server (PASS) and it's
community of SQL Server professionals.
www.sqlpass.org
"n_o_s_p_a__m" <n_o_s_p_a__m@.mail.com> wrote in message
news:1faa8782.0408082134.496c2e97@.posting.google.com...
> CASE WHEN u.sImage1_nm is NULL then 0
> ELSE WHEN u.sImage2_nm is NULL then 1
> ELSE WHEN u.sImage3_nm is NULL then 2
> ELSE WHEN u.sImage4_nm is NULL then 3
> ELSE WHEN u.sImage5_nm is NULL then 4
> end
>
> -- what's wrong here?

Friday, February 10, 2012

Backup to NULL

Hi,
I have a database that was left with recovery set to full and receving a lot
of updates. Log file has become huge but there were no transaction log
backups being done. Until I needed to trim the transaction log file now
being 25 GB (data file is only 1.5GB) because there is no more space in the
drive.
To be able to shrink it I plan to back it up first, then change the recovery
from full to simple and then shrink the log file. My problem is I don't have
any more disk space to hold the transaction log backup. Is there a way to
just backup the 20GB+ transaction log to the great void? Null? or wherever?
Please share me your insights, comments and suggestion are welcome.
TIA.
CarloVino
...
You can specify WITH TRUNCATE_ONLY on the BACKUP LOG command so that
committed data are removed from the log without creating a backup file.
Then shrink the log file to the desired size.
Afterwards, backup the database and schedule regular transaction log backups
to keep your log size reasonable.
Hope this helps.
Dan Guzman
SQL Server MVP
"CarloVino" <CarloVino@.discussions.microsoft.com> wrote in message
news:06741C7C-D44A-4D7E-B65B-8F2E8C23B7B1@.microsoft.com...
> Hi,
> I have a database that was left with recovery set to full and receving a
> lot
> of updates. Log file has become huge but there were no transaction log
> backups being done. Until I needed to trim the transaction log file now
> being 25 GB (data file is only 1.5GB) because there is no more space in
> the
> drive.
> To be able to shrink it I plan to back it up first, then change the
> recovery
> from full to simple and then shrink the log file. My problem is I don't
> have
> any more disk space to hold the transaction log backup. Is there a way to
> just backup the 20GB+ transaction log to the great void? Null? or
> wherever?
> Please share me your insights, comments and suggestion are welcome.
> TIA.
> CarloVino
> --
> ...
|||BACKUP LOG MyDatabase with TRUNCATE_ONLY. Look it up in BOL.
Geoff N. Hiten
Senior Database Administrator
Microsoft SQL Server MVP
"CarloVino" <CarloVino@.discussions.microsoft.com> wrote in message
news:06741C7C-D44A-4D7E-B65B-8F2E8C23B7B1@.microsoft.com...
> Hi,
> I have a database that was left with recovery set to full and receving a
> lot
> of updates. Log file has become huge but there were no transaction log
> backups being done. Until I needed to trim the transaction log file now
> being 25 GB (data file is only 1.5GB) because there is no more space in
> the
> drive.
> To be able to shrink it I plan to back it up first, then change the
> recovery
> from full to simple and then shrink the log file. My problem is I don't
> have
> any more disk space to hold the transaction log backup. Is there a way to
> just backup the 20GB+ transaction log to the great void? Null? or
> wherever?
> Please share me your insights, comments and suggestion are welcome.
> TIA.
> CarloVino
> --
> ...

Backup to NULL

Hi,
I have a database that was left with recovery set to full and receving a lot
of updates. Log file has become huge but there were no transaction log
backups being done. Until I needed to trim the transaction log file now
being 25 GB (data file is only 1.5GB) because there is no more space in the
drive.
To be able to shrink it I plan to back it up first, then change the recovery
from full to simple and then shrink the log file. My problem is I don't have
any more disk space to hold the transaction log backup. Is there a way to
just backup the 20GB+ transaction log to the great void? Null? or wherever?
Please share me your insights, comments and suggestion are welcome.
TIA.
CarloVino
--
...You can specify WITH TRUNCATE_ONLY on the BACKUP LOG command so that
committed data are removed from the log without creating a backup file.
Then shrink the log file to the desired size.
Afterwards, backup the database and schedule regular transaction log backups
to keep your log size reasonable.
--
Hope this helps.
Dan Guzman
SQL Server MVP
"CarloVino" <CarloVino@.discussions.microsoft.com> wrote in message
news:06741C7C-D44A-4D7E-B65B-8F2E8C23B7B1@.microsoft.com...
> Hi,
> I have a database that was left with recovery set to full and receving a
> lot
> of updates. Log file has become huge but there were no transaction log
> backups being done. Until I needed to trim the transaction log file now
> being 25 GB (data file is only 1.5GB) because there is no more space in
> the
> drive.
> To be able to shrink it I plan to back it up first, then change the
> recovery
> from full to simple and then shrink the log file. My problem is I don't
> have
> any more disk space to hold the transaction log backup. Is there a way to
> just backup the 20GB+ transaction log to the great void? Null? or
> wherever?
> Please share me your insights, comments and suggestion are welcome.
> TIA.
> CarloVino
> --
> ...|||BACKUP LOG MyDatabase with TRUNCATE_ONLY. Look it up in BOL.
Geoff N. Hiten
Senior Database Administrator
Microsoft SQL Server MVP
"CarloVino" <CarloVino@.discussions.microsoft.com> wrote in message
news:06741C7C-D44A-4D7E-B65B-8F2E8C23B7B1@.microsoft.com...
> Hi,
> I have a database that was left with recovery set to full and receving a
> lot
> of updates. Log file has become huge but there were no transaction log
> backups being done. Until I needed to trim the transaction log file now
> being 25 GB (data file is only 1.5GB) because there is no more space in
> the
> drive.
> To be able to shrink it I plan to back it up first, then change the
> recovery
> from full to simple and then shrink the log file. My problem is I don't
> have
> any more disk space to hold the transaction log backup. Is there a way to
> just backup the 20GB+ transaction log to the great void? Null? or
> wherever?
> Please share me your insights, comments and suggestion are welcome.
> TIA.
> CarloVino
> --
> ...

Backup to NULL

Hi,
I have a database that was left with recovery set to full and receving a lot
of updates. Log file has become huge but there were no transaction log
backups being done. Until I needed to trim the transaction log file now
being 25 GB (data file is only 1.5GB) because there is no more space in the
drive.
To be able to shrink it I plan to back it up first, then change the recovery
from full to simple and then shrink the log file. My problem is I don't hav
e
any more disk space to hold the transaction log backup. Is there a way to
just backup the 20GB+ transaction log to the great void? Null? or wherever?
Please share me your insights, comments and suggestion are welcome.
TIA.
CarloVino
--
...You can specify WITH TRUNCATE_ONLY on the BACKUP LOG command so that
committed data are removed from the log without creating a backup file.
Then shrink the log file to the desired size.
Afterwards, backup the database and schedule regular transaction log backups
to keep your log size reasonable.
Hope this helps.
Dan Guzman
SQL Server MVP
"CarloVino" <CarloVino@.discussions.microsoft.com> wrote in message
news:06741C7C-D44A-4D7E-B65B-8F2E8C23B7B1@.microsoft.com...
> Hi,
> I have a database that was left with recovery set to full and receving a
> lot
> of updates. Log file has become huge but there were no transaction log
> backups being done. Until I needed to trim the transaction log file now
> being 25 GB (data file is only 1.5GB) because there is no more space in
> the
> drive.
> To be able to shrink it I plan to back it up first, then change the
> recovery
> from full to simple and then shrink the log file. My problem is I don't
> have
> any more disk space to hold the transaction log backup. Is there a way to
> just backup the 20GB+ transaction log to the great void? Null? or
> wherever?
> Please share me your insights, comments and suggestion are welcome.
> TIA.
> CarloVino
> --
> ...|||BACKUP LOG MyDatabase with TRUNCATE_ONLY. Look it up in BOL.
Geoff N. Hiten
Senior Database Administrator
Microsoft SQL Server MVP
"CarloVino" <CarloVino@.discussions.microsoft.com> wrote in message
news:06741C7C-D44A-4D7E-B65B-8F2E8C23B7B1@.microsoft.com...
> Hi,
> I have a database that was left with recovery set to full and receving a
> lot
> of updates. Log file has become huge but there were no transaction log
> backups being done. Until I needed to trim the transaction log file now
> being 25 GB (data file is only 1.5GB) because there is no more space in
> the
> drive.
> To be able to shrink it I plan to back it up first, then change the
> recovery
> from full to simple and then shrink the log file. My problem is I don't
> have
> any more disk space to hold the transaction log backup. Is there a way to
> just backup the 20GB+ transaction log to the great void? Null? or
> wherever?
> Please share me your insights, comments and suggestion are welcome.
> TIA.
> CarloVino
> --
> ...