1. Welcome! Please take a few seconds to create your free account to post threads, make some friends, remove a few ads while surfing and much more. ClutchFans has been bringing fans together to talk Houston Sports since 1996. Join us!

SQL Help

Discussion in 'BBS Hangout' started by Lil Pun, Jul 18, 2011.

  1. Lil Pun

    Lil Pun Member

    Joined:
    Oct 6, 1999
    Messages:
    34,143
    Likes Received:
    1,038
    OK, I have just received search access to use SQL to search our database instead of waiting for the deathly slow Discoverer to run.

    I was given the following script by our IT department and I was hoping somebody could explain certain sections to me so it's a little clearer what it does.

    Script:

    Code:
    SELECT DISTINCT(sgbstdn_pidm),
    [COLOR="Red"]substr(frkiden.f_get_spriden_id(sgbstdn_pidm),1,9) ID,
    substr(frkiden.f_get_spriden_first_name(sgbstdn_pidm),1,15) FIRST,
    substr(frkiden.f_get_spriden_last_name(sgbstdn_pidm),1,60) LAST,
    SGBSTDN_TERM_CODE_EFF[/COLOR]
    
    FROM sgbstdn
    
    WHERE SGBSTDN_TERM_CODE_EFF='201130'AND
    
    sgbstdn_stst_code = 'IG';
    I know what the select, from and where statements are doing. What I am not sure of is those 3 substring lines and how they are working. Also, why does a field name come right after those substring lines?

    Also, how would I do a comparison between 2 different tables with 2 different field names?

    Example: I have the table orders_current and the field from this table pers_last_order. I also have a table called orders_previous and a field from this table called last_order.

    I want a comparison of anybody in these table where pers_last_order <> last_order. Is that complicated?

    Example of what I tried:

    Code:
    
    SELECT DISTINCT(orders_pidm),
    substr(frkiden.f_get_spriden_id(orders_pidm),1,9) ID,
    substr(frkiden.f_get_spriden_first_name(orders_pidm),1,15) FIRST,
    substr(frkiden.f_get_spriden_last_name(orders_pidm),1,60) LAST,
    
    FROM orders_current, orders_previous
    
    WHERE pers_last_order<>last_order;
    
    
    It gives me an error in the from statement saying table does not exist. What am I doing wrong?

    TIA
     
  2. DarkHorse

    DarkHorse Member

    Joined:
    Oct 9, 1999
    Messages:
    6,756
    Likes Received:
    1,303
    The substr calls are giving you the first 9 characters of ID, the first 15 characters of the first name, and the first 60 characters of the last name.

    I guess I should also clarify that those values are assigned to the fields "ID", "FIRST", and "LAST" respectively.
     
  3. DrLudicrous

    DrLudicrous Member

    Joined:
    May 9, 2002
    Messages:
    3,936
    Likes Received:
    203
    Substring is just telling it to return the first x number of characters from a column. The column name afterwards is an alias so you can work with easier to write names, so you can reference "frkiden.f_get_spriden_first_name" as "first" in the rest of your statement.

    The error is probably because you're still referencing frkiden in the select statement when it's not present in the from clause.

    Also, generally if you want to get related data from two different tables you'd use a join statement. Something like:

    Code:
    Select columns
    From orders_current
    Left Outer Join orders_previous
    On orders_current.pers_last_order <> orders_previous.last_order
    That seems like it would give you some odd data though, what are you trying to accomplish by getting data from those two tables?
     
    1 person likes this.
  4. pirc1

    pirc1 Member

    Joined:
    Dec 9, 2002
    Messages:
    14,137
    Likes Received:
    1,882
    substring is text function returning a portion of the text field. The Name after the substring functions are aliases. There are joins you can perform, inner, left, right and outer depend what you are looking for. Search for join on google or provide more details on the tables that needs to be joined for comparison.
     
    1 person likes this.
  5. Lil Pun

    Lil Pun Member

    Joined:
    Oct 6, 1999
    Messages:
    34,143
    Likes Received:
    1,038

    Thanks for the input. Any idea why this field (SGBSTDN_TERM_CODE_EFF) is listed at the end of the substring lines?
     
  6. Lil Pun

    Lil Pun Member

    Joined:
    Oct 6, 1999
    Messages:
    34,143
    Likes Received:
    1,038
    That was just an example. What I am trying to do is look on two separate tables for people whose last orders do not match up. So if the ID on a person had one table with Smith and the other was Brown it would be pulled by the script. Does that make sense?
     
  7. Lil Pun

    Lil Pun Member

    Joined:
    Oct 6, 1999
    Messages:
    34,143
    Likes Received:
    1,038
    Here is another one a coworker who just received the same access is trying to run:

    Table: SPRIDEN_CURRENT
    Field: PERS_LAST_NAME

    Table: SPRIDEN_PREVIOUS
    Field: SPRIDEN_LAST_NAME
    Field: SPRIDEN_NTYP_CODE

    He is trying to find where the last names do not match on people (ex. married women) where NTYP_CODE is specified as TRANS.

    So if SPRIDEN_CURRENT had John Doe and SPRIDEN_PREVIOUS had John Deere under the NTYP_CODE of TRANS it would be pulled so it could be corrected.

    Thanks.

    (I have and will continue to rep where I can.)
     
  8. DrLudicrous

    DrLudicrous Member

    Joined:
    May 9, 2002
    Messages:
    3,936
    Likes Received:
    203
    That's just another column that is being selected.

    You would have to have the user's ID in both tables.

    Code:
    Select columns
    From SPRIDEN_CURRENT
    Inner Join SPRIDEN_PREVIOUS
    On SPRIDEN_CURRENT.userID = SPRIDEN_PREVIOUS.userID
    And SPRIDEN_CURRENT.PERS_LAST_NAME <> SPRIDEN_PREVIOUS.SPRIDEN_LAST_NAME
    And SPRIDEN_PREVIOUS.SPRIDEN_NTYP_CODE= 'TRANS'
     
    #8 DrLudicrous, Jul 18, 2011
    Last edited: Jul 18, 2011
  9. OldManBernie

    OldManBernie Old Fogey

    Joined:
    May 5, 2000
    Messages:
    2,851
    Likes Received:
    221
    That comma after "LAST" will throw an error. Can you include the error message if that doesn't solve it?
     
  10. pirc1

    pirc1 Member

    Joined:
    Dec 9, 2002
    Messages:
    14,137
    Likes Received:
    1,882
    You need a common field like ID for something like this.

    For example:

    Select SPRIDEN_CURRENT.ID, SPRIDEN_Previous.ID
    from SPRIDEN_CURRENT inner join SPRIDEN_PREVIOUS
    on SPRIDEN_CURRENT.ID = SPRIDEN_PREVIOUS.ID
    and SPRIDEN_CURRENT.NTYP_CODE = SPRIDEN_PREVIOUS.NTYP_CODE
    where SPRIDEN_PREVIOUS.Name <> SPRIDEN_CURRENT.Name

    Would give you names different under the same ID and NTYP_CODE
     
  11. Supermac34

    Supermac34 President, Von Wafer Fan Club

    Joined:
    Mar 31, 2000
    Messages:
    7,110
    Likes Received:
    2,457
    If you are still a beginner with SQL and need a handy reference book, I really like the SAMS: "Teach Yourself SQL in 10 Minutes" book.

    Broken up into a bunch of 10 minute lessons about SQL. Creates a good foundation, etc.
     
  12. OldManBernie

    OldManBernie Old Fogey

    Joined:
    May 5, 2000
    Messages:
    2,851
    Likes Received:
    221
    If you need to learn on the fly, http://www.w3schools.com/sql/default.asp is a good reference tool as well.
     
  13. Lil Pun

    Lil Pun Member

    Joined:
    Oct 6, 1999
    Messages:
    34,143
    Likes Received:
    1,038
    Just tried to run this as:

    Code:
    Select columns
    From SPRIDEN_CURRENT
    Inner Join SPRIDEN_PREVIOUS
    On SPRIDEN_CURRENT_PIDM = SPRIDEN_PREVIOUS_PIDM
    And SPRIDEN_CURRENT.PERS_LAST_NAME <> SPRIDEN_PREVIOUS.SPRIDEN_LAST_NAME
    And SPRIDEN_PREVIOUS.SPRIDEN_NTYP_CODE='TRANS';
    And I received the following error:

     
  14. OldManBernie

    OldManBernie Old Fogey

    Joined:
    May 5, 2000
    Messages:
    2,851
    Likes Received:
    221
    One more thing, I'm not sure what you're trying to accomplish with "WHERE pers_last_order<>last_order", but if "pers_last_order" is on one table and "last_order" is on another, you will need to join those two tables so you would be querying horizontal records that contain both items.

    So it'd be something like this:

    Code:
    
    SELECT DISTINCT(orders_pidm),
    substr(frkiden.f_get_spriden_id(orders_pidm),1,9) ID,
    substr(frkiden.f_get_spriden_first_name(orders_pidm),1,15) FIRST,
    substr(frkiden.f_get_spriden_last_name(orders_pidm),1,60) LAST,
    
    FROM orders_current t1 INNER JOIN orders_previous t2
    ON t1.pers_last_order = t2.last_order
    
    WHERE pers_last_order<>last_order;
    
    
    There might be some deviations depending on the relationship between the two tables (whether they are 1-to-1, 1-to-many or many-to-many).
     
    1 person likes this.
  15. DrLudicrous

    DrLudicrous Member

    Joined:
    May 9, 2002
    Messages:
    3,936
    Likes Received:
    203
    You'll need to replace "columns" with the list of columns you want to get. To make sure you can actually select data from the table try something simple like:

    Code:
    Select top 1 *
    From SPRIDEN_CURRENT
     
  16. OldManBernie

    OldManBernie Old Fogey

    Joined:
    May 5, 2000
    Messages:
    2,851
    Likes Received:
    221
    Try this:


    Code:
    Select columns
    From SPRIDEN_CURRENT
    Inner Join SPRIDEN_PREVIOUS
    On SPRIDEN_CURRENT_PIDM = SPRIDEN_PREVIOUS_PIDM
    WHERE SPRIDEN_CURRENT.PERS_LAST_NAME <> SPRIDEN_PREVIOUS.SPRIDEN_LAST_NAME
    And SPRIDEN_PREVIOUS.SPRIDEN_NTYP_CODE='TRANS';
     
  17. Supermac34

    Supermac34 President, Von Wafer Fan Club

    Joined:
    Mar 31, 2000
    Messages:
    7,110
    Likes Received:
    2,457
  18. Lil Pun

    Lil Pun Member

    Joined:
    Oct 6, 1999
    Messages:
    34,143
    Likes Received:
    1,038
    I tried this:

    Code:
    Select DISTINCT (SPRIDEN_CURRENT_ID),
    From SPRIDEN_CURRENT
    Inner Join SPRIDEN_PREVIOUS
    On SPRIDEN_CURRENT_ID = SPRIDEN_PREVIOUS_ID
    And SPRIDEN_CURRENT_PERS_LAST_NAME <> SPRIDEN_PREVIOUS.SPRIDEN_LAST_NAME
    And SPRIDEN_PREVIOUS.SPRIDEN_NTYP_CODE='TRANS';
    Error I received:

    Looks like progress but I am still doing something wrong but your assistance is greatly appreciated.

    Thanks again. :)
     
  19. OldManBernie

    OldManBernie Old Fogey

    Joined:
    May 5, 2000
    Messages:
    2,851
    Likes Received:
    221
    You'll get a syntax error if you put a comma after the last column you're selecting. Take out the comma at the end of, "Select DISTINCT (SPRIDEN_CURRENT_ID),"
     
  20. DrLudicrous

    DrLudicrous Member

    Joined:
    May 9, 2002
    Messages:
    3,936
    Likes Received:
    203
    You don't need the comma at the end of the select clause. The comma is only needed to separate columns.
     

Share This Page