Close

Results 1 to 8 of 8

Thread: sql help

  1. #1
    DF VIP Member Nikki's Avatar
    Join Date
    Dec 2002
    Location
    Walsall
    Posts
    12,413
    Thanks
    559
    Thanked:        148
    Karma Level
    898

    Default sql help

    SELECT OrdD1.OrderID AS OrderID,
    SUM(OrdD1.Quantity) AS "Units Sold",
    SUM(Ord01.UnitPrice * OrdD1.Quantity) AS Revenue
    FROM [Order Details] AS OrdD1
    WHERE OrdD1.OrderID in (SELECT DISTINCT OrdD2.OrderID
    FROM [Order Details] AS OrdD2
    WHERE OrdD2.UnitPrice > $100)
    GROUP BY OrdD1.OrderID
    HAVING SUM(OrdD1.Quantity) > 100

    above is my sql code, but i am having problems, i keep getting the error messege

    The column prefix 'Ord01' does not match with a table name or alias name used in the query.

    and i cant find out why i still keep getting this error, as its in the table

    TIA

  2. #2
    DF VIP Member Nikki's Avatar
    Join Date
    Dec 2002
    Location
    Walsall
    Posts
    12,413
    Thanks
    559
    Thanked:        148
    Karma Level
    898

    Default Re: sql help

    have managed to figure it out, just after i posted it. Theere was a spelling error in the unit price code line DOH!

  3. #3
    DF VIP Member Q-Ball's Avatar
    Join Date
    Mar 2005
    Location
    In a House
    Posts
    1,576
    Thanks
    56
    Thanked:        2
    Karma Level
    341

    Default Re: sql help

    Nikki - try and format the sql a bit better as i find it helps readability
    e.g
    Code:
    SELECT OrdD1.OrderID AS OrderID
    ,          SUM(OrdD1.Quantity) AS "Units Sold"
    ,          SUM(Ord01.UnitPrice * OrdD1.Quantity) AS Revenue
    FROM [Order Details] AS OrdD1
    WHERE OrdD1.OrderID in (SELECT DISTINCT OrdD2.OrderID
                                      FROM [Order Details] AS OrdD2
                                      WHERE OrdD2.UnitPrice > $100)
                                      GROUP BY OrdD1.OrderID
                                      HAVING SUM(OrdD1.Quantity) > 100
                                      )
    also, to improved readability, keep everything lower case. some people keep keys word upper case, so you could try that.
    If I'm not back in five minutes... wait longer!

  4. #4
    DF VIP Member Nikki's Avatar
    Join Date
    Dec 2002
    Location
    Walsall
    Posts
    12,413
    Thanks
    559
    Thanked:        148
    Karma Level
    898

    Default Re: sql help

    i have always thought the caps for say 'select' wouldnt work in lower case, or that me being a dumb ass and need to try it first lol

  5. #5
    DF VIP Member Nikki's Avatar
    Join Date
    Dec 2002
    Location
    Walsall
    Posts
    12,413
    Thanks
    559
    Thanked:        148
    Karma Level
    898

    Default Re: sql help

    another little problem,

    use Pubs
    select Pub_name
    from Publishers
    where EXISTS
    (
    select * from titles
    where titles.pub_id - publishers.pub_id
    AND Type = 'business'
    )

    i am running the sql code but i keep getting a syntax for the 'and' used within the statement? any ideas pleeeeeeeeeeesssseee

  6. #6
    DF Member keifster's Avatar
    Join Date
    Aug 2006
    Location
    UK
    Posts
    48
    Thanks
    0
    Thanked:        0
    Karma Level
    0

    Default Re: sql help

    "type" is probably a key word. I assume it's supposed to be a fieldname. Try putting the tablename before it (and a dot) eg. titles.type

  7. #7
    Argyll's Apprentice TwoPlAnKs's Avatar
    Join Date
    May 2003
    Location
    Aberdeenshire
    Posts
    5,191
    Thanks
    0
    Thanked:        0
    Karma Level
    620

    Default Re: sql help

    Quote Originally Posted by Nikki View Post
    where titles.pub_id - publishers.pub_id
    is that not meant to be a =? if it is meant to be a - then you shouldnt be doing maths on IDs, they might be integer types but they arent numbers (even though they are represented by numbers)

    i dont know sql well enough to know if minus returns a value that can become the boolean that where needs, but i imagine it cant.

    tbh i'm just being a pedantic cunt and trying to show off that i know big words. its cos the minus should be an equals
    "The Net interprets censorship as damage and routes around it." - John Gilmore

  8. #8
    DF VIP Member Nikki's Avatar
    Join Date
    Dec 2002
    Location
    Walsall
    Posts
    12,413
    Thanks
    559
    Thanked:        148
    Karma Level
    898

    Default Re: sql help

    Quote Originally Posted by TwoPlAnKs View Post
    is that not meant to be a =? if it is meant to be a - then you shouldnt be doing maths on IDs, they might be integer types but they arent numbers (even though they are represented by numbers)
    erm..... no

    i have just managed to fig the problem out, thanks for the replies

Social Networking Bookmarks

Posting Permissions

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