Unix Technical Forum

Nested

This is a discussion on Nested within the SQL Server forums, part of the Microsoft SQL Server category; --> Hi all, I have a query that looks like so: SELECT GLDCT AS [Doc Type], GLDOC AS DocNumber, GLALID ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 07:27 PM
igloo
 
Posts: n/a
Default Nested

Hi all,
I have a query that looks like so:
SELECT GLDCT AS [Doc Type], GLDOC AS DocNumber, GLALID AS
Person_Name
FROM F0911
WHERE (GLAID = '00181913')

However by stipulating that GLAID = GLAID I cannot get the person_name
as not all the GLALID fields are filled in. from my reading of the
helpdesk I have a felling that a nested query might be the way to go
or a self-join but beyond this I am lost!?
Many thanks for any pointers in advance.

Sam
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 07:27 PM
Simon Hayes
 
Posts: n/a
Default Re: Nested


"igloo" <igloo@spamhole.com> wrote in message
news:eed8672e.0401080527.78d5ba30@posting.google.c om...
> Hi all,
> I have a query that looks like so:
> SELECT GLDCT AS [Doc Type], GLDOC AS DocNumber, GLALID AS
> Person_Name
> FROM F0911
> WHERE (GLAID = '00181913')
>
> However by stipulating that GLAID = GLAID I cannot get the person_name
> as not all the GLALID fields are filled in. from my reading of the
> helpdesk I have a felling that a nested query might be the way to go
> or a self-join but beyond this I am lost!?
> Many thanks for any pointers in advance.
>
> Sam


It's not completely clear from your post what you mean - are there NULLs in
the GLALID column, or the GLAID column, or both? I've made a couple of
complete guesses below, but if they don't help then you should post some
more details, preferably including your table structure and some sample
data.

SELECT
GLDCT AS [Doc Type],
GLDOC AS DocNumber,
GLALID AS Person_Name
FROM F0911
WHERE GLAID = '00181913' OR
GLAID IS NULL

SELECT
GLDCT AS [Doc Type],
GLDOC AS DocNumber,
ISNULL(GLAID, GLALID) AS Person_Name
FROM F0911
WHERE GLAID = '00181913'

Simon


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 07:29 PM
igloo
 
Posts: n/a
Default Re: Nested

Sorry I realise that this is somewhat esoteric I'll try and explain it
better: If I had:

Doc_Type Doc_Number Person_Name GLAID

F 300 00181913
F 300 John 00265898


There are many more fields but by filtering on 00181913 I could never
see the name john I need to put his name in if it has the same
Doc_Type and Doc_Number.
In an ideal world I'd like to populate the Person_Name field with all
‘john' but this is not practical at the present.

Hope that's a bit less muddy now?

Thanks again.
IL
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-28-2008, 07:29 PM
Simon Hayes
 
Posts: n/a
Default Re: Nested


"igloo" <igloo@spamhole.com> wrote in message
news:eed8672e.0401090717.5c9eab9c@posting.google.c om...
> Sorry I realise that this is somewhat esoteric I'll try and explain it
> better: If I had:
>
> Doc_Type Doc_Number Person_Name GLAID
>
> F 300 00181913
> F 300 John 00265898
>
>
> There are many more fields but by filtering on 00181913 I could never
> see the name john I need to put his name in if it has the same
> Doc_Type and Doc_Number.
> In an ideal world I'd like to populate the Person_Name field with all
> 'john' but this is not practical at the present.
>
> Hope that's a bit less muddy now?
>
> Thanks again.
> IL


That's a little clearer, although I'm still not sure I understand
completely. But I guess you may want something like this:

select f.doc_type, f.doc_number, coalesce(f.person_name, dt.person_name),
f.GLAID
from
foo f
join
(
select distinct doc_type, doc_number, person_name
from foo
where person_name is not null) dt
on f.doc_type = dt.doc_type and
f.doc_number = dt.doc_number
where f.GLAID = '00181913'

Without knowing more about the table structure (ie the CREATE TABLE
statement), and which columns are NULLable, which are keys etc.this is just
a guess, and may not work correctly in all cases.

Simon


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 10:39 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com