Unix Technical Forum

SEO

vBulletin Search Engine Optimization


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-29-2008, 03:54 AM
kieran
 
Posts: n/a
Default SQL Join Statement problem

Hi,

I have the following SQL statement which is pulling a few details from
a database. As you can see, there is only the one table from which i
am creating a temporary copy.
The reason I do this is because in the table i only have the 'standIn'
listed by integer and i want to return the 'standIn' by name.

I hope this is clear enough.

The statement works but i am now noticing that it lists multiple
returns in SQL Analyser e.g it is listing three different rows for one
user and these have all been past StandIns for the user in question.
It is not a problem at the moment but it may be and i would like to
know why it is doing this. Can i change the statement to stop this, i
have been messing with the join part but no luck.

Any help greatly appreciated.

-----------------------------------------------------


SELECT T2.FirstName AS StandIn_FirstName, T2.LastName AS
StandIn_LastName
FROM tblStaff AS T1
LEFT OUTER JOIN tblStaff AS T2
ON T1.StaffNo = T2.StandIn
WHERE (T1.NTUserName = 'auser')
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-29-2008, 03:54 AM
Hugo Kornelis
 
Posts: n/a
Default Re: SQL Join Statement problem

On 11 Aug 2004 04:57:11 -0700, kieran wrote:

>Hi,
>
>I have the following SQL statement which is pulling a few details from
>a database. As you can see, there is only the one table from which i
>am creating a temporary copy.
>The reason I do this is because in the table i only have the 'standIn'
>listed by integer and i want to return the 'standIn' by name.
>
>I hope this is clear enough.
>
>The statement works but i am now noticing that it lists multiple
>returns in SQL Analyser e.g it is listing three different rows for one
>user and these have all been past StandIns for the user in question.
>It is not a problem at the moment but it may be and i would like to
>know why it is doing this. Can i change the statement to stop this, i
>have been messing with the join part but no luck.
>
>Any help greatly appreciated.
>
>-----------------------------------------------------
>
>
>SELECT T2.FirstName AS StandIn_FirstName, T2.LastName AS
>StandIn_LastName
>FROM tblStaff AS T1
>LEFT OUTER JOIN tblStaff AS T2
>ON T1.StaffNo = T2.StandIn
>WHERE (T1.NTUserName = 'auser')


Hi Kieran,

Hard to tell without knowing anything about the table structure and data
in your database. Please post the following:

1. Table structure, in the form of DDL (CREATE TABLE statements, including
all constraints; irrelevant columns may be omitted);
2. Sample data (in the form of INSERT stattements);
3. The output you'd like to see, based on the sample data provided;
4. A description of the business problem you're trying to solve.

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-29-2008, 03:54 AM
kieran h
 
Posts: n/a
Default Re: SQL Join Statement problem



Hi Hugo,

Here is the create table, update and select statements - im not sure
what u mean by ddl, but all the staements you will need to test it are
here.
This is really frustrating me now because it returns a single row with
the select statement. However when you do an update after this
(changing the standIn integer) it returns two rows sometimes. It does
not seem to follow a certain pattern. It doesnt do it if you put all
the updates in together and then do a select. Doing an update and then
trying the select statement sometimes brings back one row and sometimes
two.

Maybe I am missing something really obvious

Thanks for all help.

-------------------------------------

CREATE TABLE [dbo].[tblStaff] (
[StaffNo] [int] IDENTITY (1, 1) NOT NULL ,
[FirstName] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[LastName] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[StandIn] [int] NULL ,

) ON [PRIMARY]
GO


-------------------------------------
Insert into tblstaff
values ('fname1', 'lname2', 2)

Insert into tblstaff
values ('fname1', 'lname2', 1)

Insert into tblstaff
values ('fname1', 'lname2', 1)


----Do individually after here----


UPDATE tblstaff
SET StandIn = 3
WHERE StaffNo = 2

------------------------------------

SELECT T2.FirstName AS StandIn_FirstName, T2.LastName AS
StandIn_LastName
FROM tblStaff AS T1
LEFT OUTER JOIN tblStaff AS T2
ON T1.StaffNo = T2.StandIn
WHERE (T1.StaffNo = 2)


-------------------------------------

UPDATE tblstaff
SET StandIn = 3
WHERE StaffNo = 2



*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-29-2008, 03:55 AM
Erland Sommarskog
 
Posts: n/a
Default Re: SQL Join Statement problem

kieran h (kieran5405@hotmail.com) writes:
> Here is the create table, update and select statements - im not sure
> what u mean by ddl, but all the staements you will need to test it are
> here.


DDL = Data Definition Language. Hugo hasn't learnt to speak to less
experienced users. Or he just being snobbish. Anyway, CREATE TABLE
is what he was after, so you got it right to far.

> This is really frustrating me now because it returns a single row with
> the select statement. However when you do an update after this
> (changing the standIn integer) it returns two rows sometimes. It does
> not seem to follow a certain pattern. It doesnt do it if you put all
> the updates in together and then do a select. Doing an update and then
> trying the select statement sometimes brings back one row and sometimes
> two.


Yeah, but the rule is that you provide the script that demonstrates
the problem, and leave the analysis to the group. Also, for a case
like this it's a good idea to supply the desired output.

Also, for the sake of the example, it's probably better to not have
StaffNo as an IDENTITY column, so you know which value is which.

--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-29-2008, 03:55 AM
Hugo Kornelis
 
Posts: n/a
Default Re: SQL Join Statement problem

On Wed, 11 Aug 2004 22:01:46 +0000 (UTC), Erland Sommarskog wrote:

>kieran h (kieran5405@hotmail.com) writes:
>> Here is the create table, update and select statements - im not sure
>> what u mean by ddl, but all the staements you will need to test it are
>> here.

>
>DDL = Data Definition Language. Hugo hasn't learnt to speak to less
>experienced users. Or he just being snobbish.


Am not. This is what I posted:

.....
1. Table structure, in the form of DDL (CREATE TABLE statements, including
all constraints; irrelevant columns may be omitted);
.....

By the way, I'm glad you replied to kiera, as his message didn't show up
on my news service. I have now found it on google, so I can look at it.

I'll let the supernews guys know about this disappearing post.

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 02-29-2008, 03:55 AM
Hugo Kornelis
 
Posts: n/a
Default Re: SQL Join Statement problem

On 11 Aug 2004 04:57:11 -0700, kieran wrote:

(snip)

Hi Kieran,

As I just wrote in a reply to Erland's message, I didn't catch your
message before as it was somehow blocked or dropped by me news service.
But after reading Erlands message, I managed to find your message on
google.

I copied and pasted the script you posted and it worked just fine. I get
one row of output consistently. Somehow, I don't manage to reproduce the
behaviour you describe (returning sometimes 1, sometimes 2 rows).

Maybe it would help if you could post the expected output as well, in
addition to the CREATE TABLE and INSERT statements you already provided. I
am not sure what you are trying to accomplish; seeing the output you try
to get might help me get a better understanding of your problem.

(Fingers crossed, hoping your next reply will get through...)

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 02-29-2008, 03:55 AM
K Finegan
 
Posts: n/a
Default Re: SQL Join Statement problem

Hi Kieran,

When I ran the sql you provided and then ran

UPDATE tblstaff
SET StandIn = 2
WHERE StaffNo = 3

& then:

SELECT * FROM tblstaff
SELECT T2.StaffNo, T2.FirstName AS StandIn_FirstName, T2.LastName AS
StandIn_LastName
FROM tblStaff AS T1 LEFT OUTER JOIN tblStaff AS T2
ON T1.StaffNo = T2.StandIn
WHERE (T1.StaffNo = 2)


I get:

StaffNo|FirstName|LastName|StandIn
1 fname1 lname2 2
2 fname1 lname2 3
3 fname1 lname2 2


StaffNo|StandIn_FirstName|StandIn_LastName
1 fname1 lname2
3 fname1 lname2


Anytime the WHERE clause has T1.StaffNo = X & X is a stand-in twice
then two rows appear.

Your query is returning all the times that the StaffNo is a standin &
who they are a standin to not who is a stand in for a particular
StaffNo.

I'm guessing that you want the latter rather than the former.

To do that either reverse the ON T1.StaffNo = T2.StandIn to ON
T1.StandIn = T2.StaffNo or change the where to (T2.StaffNo = 2)

Hope this helps,

K Finegan
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 02-29-2008, 03:55 AM
kieran
 
Posts: n/a
Default Re: SQL Join Statement problem

Hi Guys,

I think I have an example of where it happens now. If you paste all
the first statement into Query Analyser and run it. Then paste the
second select statement into query analyser you will see two rows
returned.

I think this is the easiest way to see what I am talking about. RE:
where it is used - the tblStaff is a large table with many fields (i
detailed the basic for claity) where all staff details are pulled
from. There is many users on it and I was worried when I saw this why
it was happening. And at this stage im also very curious why this is
happening. Hope you can see what I mean as i know im not going crazy.

Cheers.

-----------------------------------------

CREATE TABLE [dbo].[tblStaff] (
[StaffNo] [int] IDENTITY (1, 1) NOT NULL ,
[FirstName] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
,
[LastName] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[StandIn] [int] NULL ,

) ON [PRIMARY]
GO

Insert into tblstaff
values ('fname1', 'lname2', 2)

Insert into tblstaff
values ('fname1', 'lname2', 1)

Insert into tblstaff
values ('fname1', 'lname2', 1)


UPDATE tblstaff
SET StandIn = 1
WHERE StaffNo = 2


UPDATE tblstaff
SET StandIn = 1
WHERE StaffNo = 2


UPDATE tblstaff
SET StandIn = 2
WHERE StaffNo = 2


UPDATE tblstaff
SET StandIn = 2
WHERE StaffNo = 2

-------------------------------------------

SELECT T2.FirstName AS StandIn_FirstName, T2.LastName AS
StandIn_LastName
FROM tblStaff AS T1
LEFT OUTER JOIN tblStaff AS T2
ON T1.StaffNo = T2.StandIn
WHERE (T1.StaffNo = 2)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 02-29-2008, 03:55 AM
Hugo Kornelis
 
Posts: n/a
Default Re: SQL Join Statement problem

On 12 Aug 2004 03:47:15 -0700, kieran wrote:

>Hi Guys,
>
>I think I have an example of where it happens now. If you paste all
>the first statement into Query Analyser and run it. Then paste the
>second select statement into query analyser you will see two rows
>returned.
>
>I think this is the easiest way to see what I am talking about. RE:
>where it is used - the tblStaff is a large table with many fields (i
>detailed the basic for claity) where all staff details are pulled
>from. There is many users on it and I was worried when I saw this why
>it was happening. And at this stage im also very curious why this is
>happening. Hope you can see what I mean as i know im not going crazy.
>
>Cheers.


(snip DDL, sample data and query - thanks for providing it!)

Yes, this will indeed return two rows. They are two DIFFERENT rows,
though, not two copies of the same row. If you want to see more clearly
what's happening, change your query to read:

SELECT T2.FirstName AS StandIn_FirstName, T2.LastName AS
StandIn_LastName, T2.StaffNo
FROM tblStaff AS T1
LEFT OUTER JOIN tblStaff AS T2
ON T1.StaffNo = T2.StandIn
WHERE (T1.StaffNo = 2)

(That is: add T2.StaffNo to the select-list).

You'll see that the two rows returned are for staffno 1 and 2.

First, let's find out what exactly the contents of the table is after the
updates but before the select (leaving out the names - they are the same
on each row and won't influence the results)

SELECT StaffNo, StandIn
FROM tblStaff

StaffNo StandIn
----------- -----------
1 2
2 2
3 1


Here's what the query does (logically speaking - the exact order of
evaluation chosen by SQL Server may differ as long as the results remain
the same).

First, two copies of tblStaff are made in a working area; they are joined
so that a row from T1 will be combined with a row from T2 if the person in
T1 can be a standin for T2. If a row from T1 has no matching row in T2, it
is combined with a bunch of NULL values (as a result of the LEFT OUTER
JOIN). The intermediate results will be (agian leaving out the names):

<----- T1 -----> <----- T2 ----->
StaffNo StandIn StaffNo StandIn
----------- ----------- ----------- -----------
1 2 3 1
2 2 1 2
2 2 2 2
3 1 NULL NULL

The first row denotes that StaffNo 1 is standin for StaffNo 3. The second
and third row denote that StaffNo 2 is standin for StaffNo 2 (him/herself)
and 1. The final row denotes that StaffNo 3 is standin for nobody.

The WHERE clause filters the intermediate results above. Only the rows
with T1.StaffNo = 2 are retained. These are the two rows denoting that
StaffNo 2 is standin for StaffNo 1 and 2.

Finally, the SELECT clause defines what should be returned. In the case of
your original query, this will return the first and last name of the two
employees that have employee 2 as a standin.

While the above (hopefully) clarifies why you get two rows, it doesn't
solve your problem. In order to do that, I really must now what output you
would expect and why you expect that output. Once you post that, I (and
the other regular posters in this group) can try to find you a better
query.

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 02-29-2008, 03:55 AM
kieran
 
Posts: n/a
Default Re: SQL Join Statement problem

Thanks for your reply Hugo.

Based on ur detailed explanation, i think i see it clearer and also
see where i was going wrong. Basically each person can only have one
standin but u can be a standin to many people. I wanted to get who
the standin was for that particular user. I was getting the two or
more rows returned bcause of the last line of the statement
WHERE (T1.StaffNo = 2)
it should have been
WHERE (T2.StaffNo = 2) and this brings me back the standin for
that person - a single value.

I hope this is right, i will start testing it in the system but i
think thats it.

Cheers for all your help.
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 09:36 AM.


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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588