SQL Server - 将前5个月的销售数据汇总到当月

时间:2018-06-06 13:33:37

标签: sql-server sql-server-2012 sum aggregation

我需要将每个记录的前五个月的销售额汇总到当月。例如,在下表中,

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>Framework</groupId>
    <artifactId>AdvancedFramework</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>AdvancedFramework</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.6</maven.compiler.source>
        <maven.compiler.target>1.6</maven.compiler.target>
    </properties>

    <build>

        <!-- Source directory configuration -->
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.18.1</version>
                <configuration>
                    <!-- Suite testng xml file to consider for test execution -->
                    <suiteXmlFiles>
                        <suiteXmlFile>testng.xml</suiteXmlFile>
                    </suiteXmlFiles>
                </configuration>
            </plugin>
            <!-- Compiler plugin configures the java version to be usedfor compiling 
                the code -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${maven.compiler.source}</source>
                    <target>${maven.compiler.target}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>




    <dependencies>
        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.7.0</version>
            <type>maven-plugin</type>
        </dependency>
        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.21.0</version>
        </dependency>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>gherkin</artifactId>
            <version>2.12.2</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.11</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-server</artifactId>
            <version>3.12.0</version>
        </dependency>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>1.2.5</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>1.2.5</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-core</artifactId>
            <version>1.2.5</version>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.14.3</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-testng</artifactId>
            <version>1.2.5</version>
        </dependency>
    </dependencies>
</project>

StoreID 119119的201804月累计销售额应为50000 + 62500 + 93750。

201803年119119的总销售额应为62500 + 50000,依此类推。

我需要这样做,这样我才能在时间序列图上绘制这个聚合列。

我不能在这里使用ASC ROWS 5 PRECEEDING,因为可能每个月都没有销售数据,我不应该汇总前5个记录,但应该严格基于之前5个月的销售数据。

是否可以在不使用游标的情况下执行此操作?有人可以提出解决方案吗?

1 个答案:

答案 0 :(得分:0)

您可以使用自联接来获取较旧的值。即:

from pyquery import PyQuery as pq
import urllib3

urllib3.disable_warnings()
# http = urllib3.PoolManager()
http = urllib3.HTTPSConnectionPool('www.eneldistribucion.cl')
# Code
codigo = "000000-1"

pr = http.request('GET', "/hogares")
print('++++++++++++++++++++++++++++++++++++++++++++++++++++')
print('Root URL: HTTP/GET : {}'.format(pr.status))
print('++++++++++++++++++++++++++++++++++++++++++++++++++++')
page = pq(pr.data)

# Form Parameters (get credentials)
form_parameter_test = page('input[name="test"]').val()
form_parameter_segment = page('input[name="segment"]').val()
form_parameter_from = page('input[name="from"]').val()
form_parameter_num_supply = codigo
form_parameter_ct = 'S'

print('++++++++++++++++++++++++++++++++++++++++++++++++++++')
print(pr.headers) # Need to pass this as well the cookies to the R request
print('++++++++++++++++++++++++++++++++++++++++++++++++++++')

r = http.request('POST', '/pago-linea',
                headers=pr.headers,
                fields={'test': str(form_parameter_test),
                        'segment': str(form_parameter_segment),
                        'from': str(form_parameter_from),
                        'numSupply': str(form_parameter_num_supply),
                        'ct': str(form_parameter_ct)})

print('++++++++++++++++++++++++++++++++++++++++++++++++++++')
print('/pago-linea URL: HTTP/POST status: {}'.format(r.status))
print(http)
print('++++++++++++++++++++++++++++++++++++++++++++++++++++')
if "Actualmente no tiene ninguna factura pendiente de pago" in page.text():
    print('No hay pago pendiente')
else:
    print('Si hay pago pendiente')