mysql

时间:2018-03-22 06:58:54

标签: mysql

首先,我不是数据库专家。我自己创建一个数据库并成功运行几个查询。我现在想要计算一个月中最高的ach%,然后使用结果将其与下个月进行比较。

例如,我有7%ach%和aug Ach%,这两个月的结果将与sep ach%进行比较。我现在正在使用查询的内容在帖子中。我想要的是如何在查询中的所有月份执行此操作?

代码:

`associate_monthly_ach_percentage`.`Team Name` AS `Team Name`,
`associate_monthly_ach_percentage`.`CCRS ID` AS `CCRS ID`,
`associate_monthly_ach_percentage`.`Associate Name` AS `Associate Name`,
`associate_monthly_ach_percentage`.`Desgination` AS `Desgination`,
`associate_monthly_ach_percentage`.`Date of Joining` AS `Date of Joining`,
`associate_monthly_ach_percentage`.`Ach % Jul` AS `Ach % Jul`,
`associate_monthly_ach_percentage`.`Ach % Aug` AS `Ach % Aug`,
( CASE
WHEN ( `associate_monthly_ach_percentage`.`Ach % Aug` >=
`associate_monthly_ach_percentage`.`Ach % Jul` )
THEN `associate_monthly_ach_percentage`.`Ach % Aug`
ELSE `associate_monthly_ach_percentage`.`Ach % Jul` END ) AS `Highest Ach Jul to Aug`,
`associate_monthly_ach_percentage`.`Ach % Sep` AS `Ach % Sep`

我卡在这里,无法想到如何将这个结果与Sep Ach%的月份进行比较。以上查询运行完美并正确显示所需数据。 任何有关这方面的帮助将不胜感激。在此先感谢。

以上查询结果:

Team Name   CCRS ID Associate Name  Desgination DOJ Ach%Jul Ach%Aug Highest AchJultoAug Ach%Sep

Cardio Hospital 102 Musawar Jaan    H.C.R   4/1/2011    122 118 122 103
Cardio Hospital 103 Zulfiqar Ali Khan   H.C.R   7/11/2016   118 118 118 87
Cardio Hospital 104 Hameed Ur Rehman    H.C.R   6/7/2009    90  79  90  103
Cardio Hospital 1101    Muhammad Ahmed  H.C.R   9/3/2011    85  96  96  79

SQLFIDDLE LINK

http://www.sqlfiddle.com/#!9/a93816

添加虚拟数据以检查查询。

1 个答案:

答案 0 :(得分:0)

试试这个,

我从您生成的表中查询如下,

我希望这能解决你的问题,

    SELECT
    `associate_monthly_ach_percentage`.`CCRS ID` AS `CCRS ID`,
    `associate_monthly_ach_percentage`.`Associate Name` AS `Associate Name`,
    `associate_monthly_ach_percentage`.`Date of Joining` AS `Date of Joining`,
    `associate_monthly_ach_percentage`.`Ach % Jul` AS `Ach % Jul`,
    `associate_monthly_ach_percentage`.`Ach % Aug` AS `Ach % Aug`,
    GREATEST(`associate_monthly_ach_percentage`.`Ach % Jul`, `associate_monthly_ach_percentage`.`Ach % Aug`) AS `Highest Ach Jul to Aug`,
    `associate_monthly_ach_percentage`.`Ach % Sep` AS `Ach % Sep`,
    GREATEST(`associate_monthly_ach_percentage`.`Ach % Aug`,`associate_monthly_ach_percentage`.`Ach % Sep`) AS `Highest Ach Aug to Sep`,
    `associate_monthly_ach_percentage`.`Ach % Oct` AS `Ach % Oct`,
    GREATEST(`associate_monthly_ach_percentage`.`Ach % Sep`,`associate_monthly_ach_percentage`.`Ach % Oct`) AS `Highest Ach Sep to Oct`,
    `associate_monthly_ach_percentage`.`Ach % Nov` AS `Ach % Nov`,
    GREATEST(`associate_monthly_ach_percentage`.`Ach % Oct`,`associate_monthly_ach_percentage`.`Ach % Nov`) AS `Highest Ach Sep to Nov`,
    `associate_monthly_ach_percentage`.`Ach % Dec` AS `Ach % Dec`,
    GREATEST(`associate_monthly_ach_percentage`.`Ach % Nov`,`associate_monthly_ach_percentage`.`Ach % Dec`) AS `Highest Ach Sep to Dec` FROM
`associate_monthly_ach_percentage`;