If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Correct syntax on this SQL INNER JOIN Query.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-29-04, 03:14
Mirador Mirador is offline
Registered User
 
Join Date: Jan 2004
Location: Oslo
Posts: 45
Question Correct syntax on this SQL INNER JOIN Query.

Hi

Trying to get this sql - query to run under Query Analyzer but not sure how to correct anything in it to the right

______INFO_______
Table : iptable
Fields : ip_start, ip_end, location

Table : PageLog
Fields : pl_ipaddress, pl_sessionid, pl_remotehost
_________________

______________CODE_______________
select
iptable.location
, count(pl_ipaddress)
from (
select distinct
pl_sessionid
, pl_ipaddress
, pl_remotehost
from PageLog
where pl_datetime
between '2003-12-25 00:00:00'
and '2003-12-25 23:59:59'
and pl_ipaddress <> ''
) as dt_pagelog
inner
join iptable
on dt_pagelog.pl_ipaddress
between iptable.ip_start
and iptable.ip_end
group
by iptable.location
order
by count(pl_ipaddress) desc
___________END CODE_________________

1) If i run this i get :
--> "Column dt_pagelog.pl_ipaddress is invalid in the select list because it's not contained in a aggregate function or in the GROUP BY clause.

2) If i include it in the GROUP BY i get :
--> The text, ntext and image datatypes cannot be used in WHERE, HAVING or ON clause, except with the LIKE or IS NULL predicate.

Soo.. how on earth should i put this right to get to use it with a INNER JOIN, since that has to have a ON to it ?

Not very familiar with INNER JOIN's so any help will be very much appreciated..

Best regards
Mirador.
Reply With Quote
  #2 (permalink)  
Old 01-29-04, 03:31
Rushi Rushi is offline
Registered User
 
Join Date: Jan 2004
Location: India
Posts: 62
Thumbs up

select a.col1, a.col2, b.col1, b.colxxxx
from table1 as a
inner join table2 as b
on a.col1 = b.col1
where a.col2 = value
__________________
Regards,

Rushi
Reply With Quote
  #3 (permalink)  
Old 01-29-04, 03:45
Mirador Mirador is offline
Registered User
 
Join Date: Jan 2004
Location: Oslo
Posts: 45
Hi Rushi and thanx for your reply..

What am i to do with the SELECT DISTINCT ........... FROM PageLog ?

Should i add PageLog.fieldname to each of the sentences in the select there to ?

Mirador.
Reply With Quote
  #4 (permalink)  
Old 01-29-04, 04:12
Rushi Rushi is offline
Registered User
 
Join Date: Jan 2004
Location: India
Posts: 62
select distinct a.location, ...........
from pagelog as a
inner join iptable as b
on a.--------- = b.========


In inner join syntax
you have to join the common fields of the 2 tables in inner join clause and the actual where condition in where clause.
__________________
Regards,

Rushi
Reply With Quote
  #5 (permalink)  
Old 01-29-04, 06:32
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
the error message doesn't make sense

"dt_pagelog.pl_ipaddress is invalid in the select list because it's not contained in a aggregate function"

dt_pagelog.pl_ipaddress is contained in a aggregate function -- the COUNT()

maybe it's the subquery's DISTINCT, although i seriously doubt it

try this:
Code:
select iptable.location
     , count(dt_pagelog.pl_ipaddress) 
from (
     select pl_sessionid
          , pl_ipaddress
          , pl_remotehost 
       from PageLog 
      where pl_datetime 
            between '2003-12-25 00:00:00'
                and '2003-12-25 23:59:59'
        and pl_ipaddress <> '' 
     group
         by pl_sessionid
          , pl_ipaddress
          , pl_remotehost   
     ) as dt_pagelog
inner
  join iptable
    on dt_pagelog.pl_ipaddress 
       between iptable.ip_start 
           and iptable.ip_end
group 
    by iptable.location
order 
    by count(dt_pagelog.pl_ipaddress) desc
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #6 (permalink)  
Old 01-29-04, 07:17
Mirador Mirador is offline
Registered User
 
Join Date: Jan 2004
Location: Oslo
Posts: 45
Smile

Hi again Rudy

This is indeed a bit weird...
Tried the exact query u posted and got this errormsg :


---------------Error-----------------

Server: Msg 306, Level 16, State 1, Line 1
The text, ntext, and image data types cannot be used in the WHERE, HAVING, or ON clause, except with the LIKE or IS NULL predicates.

Server: Msg 306, Level 16, State 1, Line 1
The text, ntext, and image data types cannot be used in the WHERE, HAVING, or ON clause, except with the LIKE or IS NULL predicates.
--------------------------------------
Reply With Quote
  #7 (permalink)  
Old 01-29-04, 07:49
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
okay, see if you can understand where i'm going with this...

which one of your columns is text, ntext, or image?

and just to give you a little advnace notice, my next question will be why

session id, ip address, remote host, location -- those all sound like varchars to me
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #8 (permalink)  
Old 01-29-04, 07:56
Mirador Mirador is offline
Registered User
 
Join Date: Jan 2004
Location: Oslo
Posts: 45
Wink

Oh my... i finally got it....

The ip_start and ip_end was text while the others were varchar..

As soon as i put all of them to varchar it worked...

Well.. ended up with a easy solution after all!... I thought this had to be something really really tricky stuff..

Now i know hehe.. varchars dont match very good with text when it comes to comparing..

Thanx for all your help Rudy...

Best regards
Terje.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On