Home Forums SQL Server 7,2000 T-SQL Group by with MAX then other columns - how to control what is returned if there are multiple results RE: Group by with MAX then other columns - how to control what is returned if there are multiple results

  • Whoa there.

    It would really help if you had DDL that had keys, followed basic standards for dates, used a DATE data type, etc. This disaster is not even a table! Here is a wild guess as to how to make this mess into a schema.

    Of course its not the actual table its just an example of the pertinent bits of it it doesn't need to be fully formed for this EXAMPLE

    You did not know the ANSI/ISO syntax for insertion or the ISO-8601 date format. It is very useful:

    I know full well the structure of various ISO date formats, as I live in the UK its automatic for me to type out UK dates, as far as I understand it the dates are all stored as seconds since whenever anyway so ANY format you use is converted before its stored as a matter of course.

    Using alphabetic ordering for aliases is a hangover from the days of tape drives which had single letter names; an SQL programmer would use a meaningful name because we care about maintaining code. You can avoid the self join with current syntax:

    Its an EXAMPLE!

    SELECT X.emp_id, X.job_id, X.posting_date

    FROM (SELECT emp_id, job_id, posting_date,

    MAX(posting_date) OVER (PARTITION BY emp_id)

    AS last_posting_date

    FROM Timesheets) AS X

    WHERE X.posting_date = X.last_posting_date;

    Correct me if I am wrong but OVER doesn't exist in Sql2000,7

    No, DISTINCT gets rid of redundant duplicates; this is a basic term in RDBMS; you need to learn it. Then you need to learn why SQL programmers would never have this problem in the first place. Keys? Normal forms?

    Please do explain to me why using DISTINCT would remove ONE of these rows

    MANID, JOBID, DATE

    1,1,2013-01-01

    1,2,2013-01-01

    So much, so wrong. Why do you wish to destroy truth?

    If you have nothing constructive to say please don't say anything. I'm shocked to receive such a response on such a friendly and genuinely expert forum, I'm not a DBA but I have to administrate and run a database as well as run a web server, write php, .net, t-sql, handle DNS and network infrastructure and I rely on picking expert brains in each of these fields when my jack-of-all trades knowledge fails me.