Results 1 to 6 of 6

Thread: Join of Views w/ SQL problem!!!

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2000
    Location
    bebenia, PA, USA
    Posts
    241
    I am getting an Incorrect syntax near the keyword 'FROM'.

    heres the code:

    SELECT 'Reciept ID' = Receipt_id, 'Customer Type ID '= Customer_type_id,
    FROM database.dbo.vw_receipt_item ri join database.dbo.vw_LaLa l
    ON ri.receipt_item_id = l.receipt_item_id
    Where created_dt between @TodayDate and @HourBackDate AND Customer_type_id = -1000 or Custome_type_id = -1015

    this is directly being done in SQL

  2. #2
    Frenzied Member
    Join Date
    Aug 1999
    Location
    Santa Clara, Ca , 95058
    Posts
    1,105
    What platform/dbms are you running this on?

  3. #3
    Hyperactive Member
    Join Date
    Nov 1999
    Location
    Leavenworth KS USA
    Posts
    482
    I know I'm at risk of clearly proving I'm more than a few bricks short of a full load, but based on what I understand from your previous questions, something like this may help.

    Since earlier you said you want records that are an hour or less old, you can skip the BETWEEN penalty and clean-up your SARG with a single GT.

    I've taken the liberty of trying to read between the lines of your OR clause and instead used a "control values" table. By this I'm implying that you insert/delete specific Customer_type_ids that you want to return. If you intend getting all values, then drop the "AND Customer_type_id IN" part of the search argument.

    My curly brackets {} are to be ignored, if you're setup on the same server... and, if it's obvious that I still don't have any clue as to what you're trying to do -- then please just ignore moi too!
    Code:
    SELECT Receipt_id AS 'Reciept ID'
    , Customer_type_id AS 'Customer Type ID'
    FROM {<server_name1>.}<database_name1>.<owner_name>.vw_receipt_item
    WHERE created_dt > DATEADD('mi', -61, GETDATE())
    AND Customer_type_id IN (SELECT ID FROM Cust_ID)
    UNION
    SELECT Receipt_id AS 'Reciept ID'
    , Customer_type_id AS 'Customer Type ID'
    FROM {<other_server_name?>.}<database_name2>.<owner_name>.vw_LaLa
    WHERE created_dt > DATEADD('mi', -61, GETDATE())
    AND Customer_type_id IN (SELECT ID FROM Cust_ID)

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Mar 2000
    Location
    bebenia, PA, USA
    Posts
    241
    what does the 'mi' command do? and could you explain this to me in why it is a better way; I would appreciate it...

  5. #5
    Hyperactive Member
    Join Date
    Nov 1999
    Location
    Leavenworth KS USA
    Posts
    482
    It's taking 61 minutes away from the current server time. I feared you might wrinkle your nose at using greater than one hour prior, so I got extravagant and threw in an extra minute. You can spell out minutes, if you are uncomfortable with the 'mi' abreviation. Otherwise, if DATEADD('hh', -1, GETDATE()) meets your needs, use it.
    I meant to aim you away from using a less efficient BETWEEN, GE(>=) or an OR clause. Your goal is to write optimal cost-based, flexible code. Where & when possible avoid hard-coding specific values. In the business world, the most important client's code changes from one year (or one minute) to the next.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Mar 2000
    Location
    bebenia, PA, USA
    Posts
    241
    how do you check and make sure the code is optomized. What are the tools to do this? I would love to know,thanks for all your help. I will take out the between clause and use the other and I am thankful for the tip. I hope others can use this too for their coding!!!

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