Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.databases.ms-sqlserver > #1382

Re: How do I write this sql statement

X-Received by 10.66.82.103 with SMTP id h7mr16016pay.11.1359441786050; Mon, 28 Jan 2013 22:43:06 -0800 (PST)
MIME-Version 1.0
X-Received by 10.50.163.67 with SMTP id yg3mr17147igb.12.1359441785779; Mon, 28 Jan 2013 22:43:05 -0800 (PST)
Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!nntp.club.cc.cmu.edu!newsfeed.news.ucla.edu!usenet.stanford.edu!f6no14955925pbd.1!news-out.google.com!s9ni16233pbb.0!nntp.google.com!f6no14955919pbd.1!postnews.google.com!nh8g2000pbc.googlegroups.com!not-for-mail
Newsgroups comp.databases.ms-sqlserver
Date Mon, 28 Jan 2013 22:43:05 -0800 (PST)
Complaints-To groups-abuse@google.com
Injection-Info nh8g2000pbc.googlegroups.com; posting-host=24.251.224.56; posting-account=71gC0woAAACe89aka1uuzzYro-KCCKcN
NNTP-Posting-Host 24.251.224.56
References <k9v5ml$7ln$1@dont-email.me> <XnsA123AB61FDE8CYazorman@127.0.0.1>
User-Agent G2/1.0
X-HTTP-UserAgent Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0),gzip(gfe)
Message-ID <15114049-8790-49e4-840b-e3fef7cf892d@nh8g2000pbc.googlegroups.com> (permalink)
Subject Re: How do I write this sql statement
From bill <billmaclean1@gmail.com>
Injection-Date Tue, 29 Jan 2013 06:43:06 +0000
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
Xref csiph.com comp.databases.ms-sqlserver:1382

Show key headers only | View raw


On Dec 8 2012, 8:50 am, Erland Sommarskog <esq...@sommarskog.se>
wrote:
> Tony Johansson (johansson.anders...@telia.com) writes:
> > In a field in the database called Cid we have a format like year-35-X
> > So in the database we can have numbers that can look like this.
> >  2012-35-1
> > 2012-35-2
> > ....
> > 2012-35-56
> > 2012-35-0145
>
> > If I want a select statement that return the largest number for X how
> > can I write this ?
> > So if we have 2012-35-0194 in the database field Cid I want to get back
> > 195 ?
>
> RJA and Bob has already answered your question as posted, and they have
> also hinted that you need to restructure the design. For one thing, what does the existence of 2012-35-145 and 2012-35-0145 signify?
>
> Whatever, to retrieve the MAX value of the last part, SQL Server needs to scan the table. Or at least scan all entries for the year in question. Which may be acceptable. Or just a plain disaster.
>
> It seems to me that it would be better to have physical column that controls this number, and you would run MAX on that column. Then you have a computed column which holds the string.
>
> --
> Erland Sommarskog, SQL Server MVP, esq...@sommarskog.se
>
> Links for SQL Server Books Online:
> SQL 2008:http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
> SQL 2005:http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx

Just to follow up on Bob's point about keys:  SQL 2012 has a SEQUENCE
object that works like the SEQUENCE object in Oracle.  If you need to
know the value of a surrogate prior to insertion, that is probably the
way to go, if you are using 2012.

That said, I am not a big fan of surrogate keys.  I also agree with
Erland that the 'X' portion of the column should be on its own.  As a
general principle, column values should be atomic, meaning that
substringing a column shouldn't be required for queries.

Back to comp.databases.ms-sqlserver | Previous | NextPrevious in thread | Find similar


Thread

How do I write this sql statement "Tony Johansson" <johansson.andersson@telia.com> - 2012-12-08 11:44 +0100
  Re: How do I write this sql statement rja.carnegie@gmail.com - 2012-12-08 03:37 -0800
    Re: How do I write this sql statement "Tony Johansson" <johansson.andersson@telia.com> - 2012-12-08 14:31 +0100
      Re: How do I write this sql statement "Bob Barrows" <reb01501@NOSPAMyahoo.com> - 2012-12-08 09:15 -0500
  Re: How do I write this sql statement Erland Sommarskog <esquel@sommarskog.se> - 2012-12-08 16:50 +0100
    Re: How do I write this sql statement bill <billmaclean1@gmail.com> - 2013-01-28 22:43 -0800

csiph-web