将访问查询转换为sql具有最后日期和最大值

时间:2015-09-20 11:05:46

标签: sql sql-server sql-server-2012

我想转换此查询。 在sql中使用它。 我不知道如何使用las和max与组

SELECT     main.id, 
           main.rep1, 
           main.NAME, 
           main.family, 
           main.ostan, 
           main.city, 
           main.telhome, 
           Max(report.nextc) AS maxofnextc, 
           main.status, 
           Last(report.date)    AS lastofdate, 
           Last(report.connect) AS lastofconnect, 
           main.hospital, 
           main.[Bimeh-Not], 
           main.tamin, 
           main.khadamat, 
           main.niroo, 
           main.emdad, 
           main.takmili, 
           main.sayer, 
           main.darmangar 
FROM       main 
INNER JOIN report 
ON         main.id = report.id 
GROUP BY   main.id, 
           main.rep1, 
           main.NAME, 
           main.family, 
           main.ostan, 
           main.city, 
           main.telhome, 
           main.status, 
           main.hospital, 
           main.[Bimeh-Not], 
           main.tamin, 
           main.khadamat, 
           main.niroo, 
           main.emdad, 
           main.takmili, 
           main.sayer, 
           main.darmangar 
HAVING     ((( main.rep1) LIKE "BEN") 
           AND (( Max(report.nextc))<=[تاريخ روز جاري را وارد نماييد]) 
           AND (  main.status)<>4 
           AND (  main.status)<>1 
           AND (  main.status)<>5))
ORDER BY   max(report.nextc) DESC , 
           main.status, 
           last(report.connect);

0 个答案:

没有答案