将访问查询转换为 sql 2012

Posted

技术标签:

【中文标题】将访问查询转换为 sql 2012【英文标题】:Converting access query to sql 2012 【发布时间】:2015-10-07 12:47:26 【问题描述】:

您好,我想在 sql 中使用此查询,如何将其更改为 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];

【问题讨论】:

唯一的区别是LAST,搜索如何在 ACCESS 中执行LAST 中的操作以及如何在 SQL 2012 中执行相同操作,如果遇到任何问题,请发布您尝试过的操作。跨度> 【参考方案1】:

转至http://www.w3schools.com/sql/sql_func_last.asp 以获得有关 LAST 和可能解决方法的详细说明。使用该页面中的信息,我建议您尝试以下代码 -

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];

我希望这会有所帮助。

如果您有任何问题,请随时回复。

【讨论】:

嗨。 Tnx 为您的回复。但它没有用,所以我试图澄清我想要什么。谢谢 ***.com/questions/33051258/…这是问题的链接

以上是关于将访问查询转换为 sql 2012的主要内容,如果未能解决你的问题,请参考以下文章

将 SQL 查询转换为访问查询 - SELECT 中的 SELECT

如何将选择 sql 查询的结果转换为 ms 访问中的新表

如何将访问查询转换为MS SQL

将 sql 查询转换为 linq 查询

如何将 MS SQL 查询转换为 MySQL 查询

将 SQL 转换为 LINQ 查询