Results 1 to 7 of 7

Thread: LEFT JOIN issue

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2006
    Posts
    592

    Question LEFT JOIN issue

    Code:
    SELECT 
    		Ord.orderID, Ord.DateCreated, Ord.DateShipped, 
    		Ord.OrderTotal, Ord.Subtotal, Ref.UrlReferrer AS Referrer
    	FROM 
    		customer_Orders AS Ord
    	INNER JOIN
    		customer_WebStatistics AS Ref
    	ON
    		Ord.IPAddress = Ref.IPAddress
    	WHERE
    		(Ord.DateShipped >= @StartDate AND Ord.DateShipped <= @EndDate)
    	AND 
    		Ord.IPAddress 
    	IN 
    		(
    			SELECT 
    				IPAddress 
    			FROM 
    				customer_WebStatistics 
    			WHERE 
    				UrlReferrer <> @CurrentDomain 
    			AND 
    				Ref.EnterDate = Ord.DateCreated
    		)

    I am aware that this will not work as expected. However i wanted to give you an idea of what i am trying to achive here.


    Basically i want to returns all orders no matter if there is Referrer or NO.

    The referrer should be included ONLY if it happened on the same date with the order AND if UrlReferrer contains the Request.ServerVariables("SERVER_NAME").

    Thanks for any ideas
    Last edited by selanec; Jun 25th, 2010 at 10:55 AM.

  2. #2
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Didn't know how to name it :(

    Quote Originally Posted by selanec View Post

    Basically i want to returns all orders no matter if there is Referrer or NO.
    So use LEFT JOIN instead of INNER JOIN.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2006
    Posts
    592

    Re: Didn't know how to name it :(

    Yeah i just recalled that you advised me the same some time ago. I have asked very similar question and you also told me to use LEFT JOIN. But, i have forgotten it unfortunately.

    Thank you again. It is much appreciated!

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2006
    Posts
    592

    Re: [RESOLVED] Didn't know how to name it :(

    Hmm it seems that this time i celebrated too soon

    Code:
    CREATE PROCEDURE SalesReport
    	(
    		@Store int,
    		@StartDate datetime,
    		@EndDate datetime,
    		@Domain varchar(1024)
    	)
    AS
    	SET NOCOUNT ON 
    	
    	DECLARE @IPAddress varchar(20);
    	
    	SELECT 
    		Ord.order_ID, Ord.DateCreated, Ord.DateShipped, 
    		Ord.OrderTotal, Ord.Subtotall, Ord.StoreStatus, 
    		Ord.StoreProfit, Ord.OrderStatus, Ref.UrlReferrer AS Referrer
    	FROM 
    		customer_Orders AS Ord
    	LEFT OUTER JOIN
    		customer_TrafficStatistics AS Ref
    	ON
    		Ord.IPAddress = Ref.IPAddress
    	WHERE
    		Ord.StoreOwner = @Store
    	AND
    		Ord.OrderCurrentStatus <> 4 
    	AND 
    		(Ord.StoreStatus <> 3 AND Ord.StoreStatus <> 6)
    	AND
    		(Ord.DateShipped >= @StartDate AND Ord.DateShipped <= @EndDate)
    	AND 
    		Ord.StoreOwner = @Store
    	AND
    		Ref.UrlReferrer <> @Domain 
    	AND 
    		DATEPART(day, Ord.DateCreated) = DATEPART(day, Ref.EnterDate)
    If you throw out the last two conditions it works like a charm but AS IS it does not return anything. return 0


    Any ideas? Thanks

  5. #5
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: LEFT JOIN issue

    What happens if you substitute "WHERE" with "AND" ?
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2006
    Posts
    592

    Re: LEFT JOIN issue

    It underlines the code as syntax error

  7. #7
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: LEFT JOIN issue

    Quote Originally Posted by selanec View Post
    It underlines the code as syntax error
    It doesn't compile? Or you get errors when executing the stored procedure?
    what is the error?
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width