Skip to content
Advertisement

Converting access query to sql 2012

Hi i want to have this query in sql how can i change it to sql one.

SELECT Main.ID, Main.Name, Main.Family, Main.Mobile, Main.[Import Date],
 Main.Ostan, Main.City, Main.REP, Main.REP1, Main.ostan2, Main.City2, Main.Hospital,
  Main.Expert, Main.Status, Main.Type, Main.Time, Last(Report.Date) AS LastOfDate, 
  Last(Report.NameP) AS LastOfNameP, Last(Report.NEXTC) AS LastOfNEXTC, Report.Nurse, 
  Last(Report.Brand) AS LastOfBrand, Last(Report.Paste) AS LastOfPaste,
   Last(Report.Bag) AS LastOfBag, Last(Report.ACC) AS LastOfACC, Last(Report.BM) AS LastOfBM,
    Last(Report.PM) AS LastOfPM, Last(Report.Bimeh) AS LastOfBimeh, Main.[Dead/ Heald Date], Main.[Operation Date]
FROM Main INNER JOIN Report ON Main.ID = Report.ID
GROUP BY Main.ID, Main.Name, Main.Family, Main.Mobile, Main.[Import Date], Main.Ostan, Main.City, Main.REP,
 Main.REP1, Main.ostan2, Main.City2, Main.Hospital, Main.Expert, Main.Status, Main.Type, Main.Time, Report.Nurse, Main.[Dead/ Heald Date], Main.[Operation Date];

Advertisement

Answer

Go to http://www.w3schools.com/sql/sql_func_last.asp for a good description of LAST and possible workarounds. Using the information from that page I suggest you try the following code –

SELECT Main.ID,
       Main.Name,
       Main.Family,
       Main.Mobile,
       Main.[Import Date],
       Main.Ostan,
       Main.City,
       Main.REP,
       Main.REP1,
       Main.ostan2,
       Main.City2,
       Main.Hospital,
       Main.Expert,
       Main.Status,
       Main.Type,
       Main.Time,
       (SELECT TOP 1 Report.Date
        FROM Report
        ORDER BY Repoort.Date DESC) AS LastOfDate,
       (SELECT TOP 1 NameP
        FROM Report
        ORDER BY NameP DESC) AS LastOfNameP,
       (SELECT TOP 1 NextC
        FROM Report
        ORDER BY NextC DESC) AS LastOfNextC,
       Report.Nurse,
       (SELECT TOP 1 Brand
        FROM Report
        ORDER BY Brand DESC) AS LastOfBrand,
       (SELECT TOP 1 Paste
        FROM Report
        ORDER BY Paste DESC) AS LastOfPaste,
       (SELECT TOP 1 Bag
        FROM Report
        ORDER BY Bag DESC) AS LastOfBag,
       (SELECT TOP 1 ACC
        FROM Report
        ORDER BY ACC DESC) AS LastOfACC,
       (SELECT TOP 1 BM
        FROM Report
        ORDER BY BM DESC) AS LastOfBM,
       (SELECT TOP 1 PM
        FROM Report
        ORDER BY PM DESC) AS LastOfPM,
       (SELECT TOP 1 Bimeh
        FROM Report
        ORDER BY Bimeh DESC) AS LastOfBimeh,
       Main.[Dead/ Heald Date],
       Main.[Operation Date]
FROM Main INNER JOIN Report ON Main.ID = Report.ID
GROUP BY Main.ID,
         Main.Name,
         Main.Family,
         Main.Mobile,
         Main.[Import Date],
         Main.Ostan,
         Main.City,
         Main.REP,
         Main.REP1,
         Main.ostan2,
         Main.City2,
         Main.Hospital,
         Main.Expert,
         Main.Status,
         Main.Type,
         Main.Time,
         Report.Nurse,
         Main.[Dead/ Heald Date],
         Main.[Operation Date];

I hope that this proves helpful.

Please feel free to reply if you have any questions.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement