SQL help...

Results 1 to 5 of 5

Thread: SQL help...

  1. #1
    Join Date
    Dec 1969
    Posts
    158

    Default SQL help...

    I need to write a stored procedue in SQL Server that will select the minimum service date for each participantfrom a current service table UNION an archived service table.<BR><BR>First, I use the following to put all the records together:<BR> <BR>select partID, service_date from current_service_table<BR>UNION<BR>select partID, service_date from service_archive_table<BR><BR>then I don&#039;t know how to select the min date. Can we do this inside one stored procedue or do it using two?<BR><BR>Thanks for any suggestions!<BR><BR><BR>

  2. #2
    Join Date
    Dec 1969
    Posts
    3,195

    Default RE: SQL help...

    SELECT MIN(service_date) AS mindate<BR>FROM<BR> (SELECT service_date AS service_date<BR> FROM current_service_table <BR> UNION <BR> SELECT service_date AS service_date<BR> FROM service_archive_table) service

  3. #3
    Join Date
    Dec 1969
    Posts
    1,032

    Default RE: SQL help...

    select max(partID), min(service_date) <BR>from current_service_table<BR>group by partID<BR>UNION<BR>select max(partID), min(service_date) <BR>from service_archive_table<BR>group by partID<BR><BR><BR>????<BR><BR>Jon.

  4. #4
    Join Date
    Dec 1969
    Posts
    3,195

    Default You know I have to read

    juts a little better there. I missed that he wanted the min date for EACh part. Then he could simply do<BR><BR>SELECT partid,<BR> MIN(service_date) AS mindate <BR>FROM <BR>(SELECT partid AS partid,<BR> service_date AS service_date <BR> FROM current_service_table <BR> UNION <BR> SELECT partid AS partid,<BR> service_date AS service_date <BR> FROM service_archive_table) service <BR>GROUP BY partid


  5. #5
    Join Date
    Dec 1969
    Posts
    158

    Default RE: You know I have to read

    When I copy all your code into the SQL Server stored procedure, it says "Incorrect syntax near the keyword &#039;group&#039; ".<BR>I know in Access, the query is fine, but what&#039;s wrong in SQL Server? Thanks.

Posting Permissions

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