• Visitors can check out the Forum FAQ by clicking this link. You 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. View our Forum Privacy Policy.
  • Want to receive the latest contracting news and advice straight to your inbox? Sign up to the ContractorUK newsletter here. Every sign up will also be entered into a draw to WIN £100 Amazon vouchers!

Reply to: SQL Help please

Collapse

You are not logged in or you do not have permission to access this page. This could be due to one of several reasons:

  • You are not logged in. If you are already registered, fill in the form below to log in, or follow the "Sign Up" link to register a new account.
  • You may not have sufficient privileges to access this page. Are you trying to edit someone else's post, access administrative features or some other privileged system?
  • If you are trying to post, the administrator may have disabled your account, or it may be awaiting activation.

Previously on "SQL Help please"

Collapse

  • Spacecadet
    replied
    (select distinct <primary key fields> from customer_snapshot where date in (<today>, <tomorrow>) as clist
    left outer join
    (select * from customer_snapshot where date = <today>) as today
    on clist.<pkey fields> = today.<pkey fields>
    left outer join
    (select * from customer_snapshot where date = <yesterday>) as yesterday
    on clist.<pkey fields> = yesterday.<pkey fields>

    it will hamper performance though.. there's probably a better way but with only 1 table to look at its difficult to tell
    Last edited by Spacecadet; 2 October 2008, 15:03.

    Leave a comment:


  • Ruprect
    replied
    One last qn - without a full outer join available to me in sybase is there any way to do this in one query or do I just have to do 2 queries?

    ta.

    Leave a comment:


  • Ruprect
    replied
    Thanks I'll give it a go

    Leave a comment:


  • Spacecadet
    replied
    use derived tables

    Can't be arsed writing out the full SQL, you can do that yourself.
    Heres a starter for 10 though
    select <stuff>
    from (select * from customer_snapshot where date = <today>) as Today
    full outer join
    (select * from customer_snapshot where date = <yesterday>) as Yesterday
    on today.<primary key fields> = yesterday.<primary key fields>
    where
    <add your where clauses here>

    Leave a comment:


  • Ruprect
    replied
    OK Smarty pantses

    how about this:

    table "customer_snapshot"

    fields customer_surname, customer_dob, snapshot_date, customer_email(other fields...)

    for the purposes of this exercise customer_surname, customer_dob and snapshot_date are a composite PK.

    What I want to know is:
    1) which customers exist today that didn't exist yesterday and
    2) which customers existed yesterday that don't exist today and
    3) which customers have changed their email addresses since yesterday.

    DB: Sybase, Number of records in table - about 2.5 million.

    Leave a comment:


  • gables
    replied
    Originally posted by Ruprect View Post
    I have 2 tables, e.g. customers and orders. Foreign key between is customerID. I want to know which customers have zero orders.

    What SQL will achieve this?

    Thanks
    On a more philosophical note can you have a customer with zero orders?

    Leave a comment:


  • xoggoth
    replied
    Customers, orders. Dull. If I ever designed a new language I would only sell it with a condition "Not to be used in boring commercial apps"

    Leave a comment:


  • richard-af
    replied
    Numpties.

    Leave a comment:


  • ElectricChair
    replied
    Originally posted by DimPrawn View Post
    A left join usually sorts it all out.
    whats wrong with a right join?

    Leave a comment:


  • richard-af
    replied
    Originally posted by DimPrawn View Post
    A left join usually sorts it all out.
    Aaaagggghhhh!!!!! Sob! <clinking of gin bottle against glass>

    Leave a comment:


  • DimPrawn
    replied
    A left join usually sorts it all out.

    Leave a comment:


  • richard-af
    replied
    Originally posted by Gonzo View Post
    I am not a coder but I have come across the phenomenon that some versions of some databases will do a tablescan when you are not expecting it, so you are earning your money by knowing when that will happen.

    I would vote for the subquery option because it is the one that I would write, but I can accept that in the real world that could trigger a tablescan when it shouldn't. The foreign key should be indexed in both tables otherwise the DBA should be shot.

    IMO

    EDIT - even if all the fields are indexed that is not always enough to stop a tablescan.
    What ARE you on about? Full Table Scans, Fast Full Scans, etc. are not associated, in particular, with either Joins or Subqueries. The CBO decides HOW it will gather the data. The only way to be sure is to run it and see. Then tweaks-tests-tweaks-tests, etc. Then you know. No need to go through it all again, unless the stats become significantly out-of-step with reality, or an index is added, a table is partitioned, etc., etc.


    Incidentally, a Full Table Scan can be the right thing to do.

    There are books on all this, as well as bizillions of lovely web sites.

    That old copy of "Oracle 7: Ye Goode Guyde To The Magicks and Mysteryes" is a bit redundant.
    Last edited by richard-af; 24 August 2007, 08:34.

    Leave a comment:


  • Gonzo
    replied
    I am not a coder but I have come across the phenomenon that some versions of some databases will do a tablescan when you are not expecting it, so you are earning your money by knowing when that will happen.

    I would vote for the subquery option because it is the one that I would write, but I can accept that in the real world that could trigger a tablescan when it shouldn't. The foreign key should be indexed in both tables otherwise the DBA should be shot.

    IMO

    EDIT - even if all the fields are indexed that is not always enough to stop a tablescan.
    Last edited by Gonzo; 23 August 2007, 20:58.

    Leave a comment:


  • TheFaQQer
    replied
    If only Milan were around to help you guys out, now that he's a DBA.

    I think I would do it with a "where not exists" clause, but haven't looked at the plan for it.

    Leave a comment:


  • richard-af
    replied
    Originally posted by King Cnvt View Post
    Couldn't you just ring up all the customers and ask them if they have any orders? FFS, kids of today....
    Generous of spirit and magnanimous in defeat. I salute you, sir.

    SQL, eh? It can be tricky, can't it?

    Leave a comment:

Working...
X