Mysql变量不能合计

时间:2015-09-17 09:42:40

标签: mysql

我创建的http://sqlfiddle.com/#!9/8b8805/1也是:

Caused by: org.gradle.cache.internal.LockTimeoutException: Timeout waiting to lock buildscript class cache for build file '/Users/me/IdeaProjects/api/explore/build.gradle' (/Users/me/.gradle/caches/2.2/scripts/build_dr53ryug2vbnxp25bu47jmb6a/ProjectScript/buildscript). It is currently in use by another Gradle instance.
Owner PID: unknown
Our PID: 10869
Owner Operation: unknown
Our operation: Initialize cache
Lock file: /Users/me/.gradle/caches/2.2/scripts/build_dr53ryug2vbnxp25bu47jmb6a/ProjectScript/buildscript/cache.properties.lock
        at org.gradle.cache.internal.DefaultFileLockManager$DefaultFileLock.lock(DefaultFileLockManager.java:258)
        at org.gradle.cache.internal.DefaultFileLockManager$DefaultFileLock.<init>(DefaultFileLockManager.java:127)
        at org.gradle.cache.internal.DefaultFileLockManager.lock(DefaultFileLockManager.java:80)
        at org.gradle.cache.internal.DefaultCacheAccess.open(DefaultCacheAccess.java:99)
        at org.gradle.cache.internal.DefaultPersistentDirectoryStore.open(DefaultPersistentDirectoryStore.java:46)
        ... 44 more

这是另一个问题Never decreasing cumulative figure的背面。从那个问题我现在创建了一个包含中间结果的表,这样我就可以进行额外的计算了。我希望得到以下结果:

CREATE TABLE `simple_summary` (
  `RowCount` bigint(21) DEFAULT NULL,
  `dayTotal` bigint(21) NOT NULL DEFAULT '0',
  `Entry` varchar(20) DEFAULT NULL,
  `Date` date DEFAULT NULL,
  KEY `RowCount` (`RowCount`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

insert  into `simple_summary`(`RowCount`,`dayTotal`,`Entry`,`Date`) values(1,3,'text1','2015-09-01'),(2,4,'text2','2015-09-02'),(3,3,'text3','2015-09-03'),(4,6,'text4','2015-09-04'),(0,0,'','0000-00-00');

SET @total = 0;
SELECT
    s1.rowCount,
    CASE
        WHEN s1.dayTotal <= s2.dayTotal     THEN @total:= @total
        WHEN s1.dayTotal > s2.dayTotal      THEN @total:= @total + (s1.dayTotal - s2.dayTotal)
    END     AS res,
/* I've also tried (but it fails */
@total:= @total + IF (s1.dayTotal <= s2.dayTotal,0,s1.dayTotal - s2.dayTotal),
    s1.dayTotal,
    s2.dayTotal
FROM simple_summary s1
JOIN simple_summary s2 ON s2.RowCount = s1.RowCount - 1
ORDER BY s1.RowCount ASC

出于某种原因,我无法正确设置@total。为什么不呢?因为基于上述问题中的注释,我接受变量的顺序 - 但是现在只有1个变量被使用/设置,所以顺序不应该起作用。

0 个答案:

没有答案