Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如何进行查询以比较今年与去年的销售额?_Sql_Sql Server 2012 - Fatal编程技术网

Sql 如何进行查询以比较今年与去年的销售额?

Sql 如何进行查询以比较今年与去年的销售额?,sql,sql-server-2012,Sql,Sql Server 2012,我对SQL比较陌生,我试图做一个简单的查询,看看今年同一天与去年同一天或理想情况下每周同一天的销售情况 我已经创建了以下查询,它当前基于单个集合日。我需要把这个移到今天的约会上,但我想我会在以后处理这个问题 - 有人能告诉我如何在同一行上显示每个商店今年和去年的数据吗 i、 e 先谢谢你 迈克 好的,请耐心听我说,这是我第一次尝试回答问题,而且我对SQL也很陌生。 我正在努力学习,但我想更多地参与其中。我相信这不是最有效的方式来完成你想做的事情,但就像我说的,我是个新手,这是我唯一知道的方式。我

我对SQL比较陌生,我试图做一个简单的查询,看看今年同一天与去年同一天或理想情况下每周同一天的销售情况

我已经创建了以下查询,它当前基于单个集合日。我需要把这个移到今天的约会上,但我想我会在以后处理这个问题

-

有人能告诉我如何在同一行上显示每个商店今年和去年的数据吗

i、 e

先谢谢你

迈克


好的,请耐心听我说,这是我第一次尝试回答问题,而且我对SQL也很陌生。

我正在努力学习,但我想更多地参与其中。我相信这不是最有效的方式来完成你想做的事情,但就像我说的,我是个新手,这是我唯一知道的方式。我完全愿意接受纠正和帮助。谢谢

-- 1. Began by declaring the dates. 
-- 2. Then created a temp table.
-- 3. Inserted into the temp table
-- 4. Created JOINS to separate the dates you wanted. 
-- 5. SELECTED fields from the temp table for the desired output.

-- Step 1

DECLARE  @startDate1   DATETIME2 = '01/01/2014', -- Creating previous year
         @endDate1     DATETIME2 = '12/31/2014', -- Creating previous year
         @startDate2   DATETIME2 = '01/01/2015', -- Creating this year
         @endDate2     DATETIME2 = '12/31/2015'; -- Creating this year
-- Step 2

DECLARE @yearFigures TABLE (    -- Created temp table with your desired output
       StoreLocation VARCHAR(20),
       TransactionDt VARCHAR(10),
       PreviousYear VARCHAR(20),
       CurrentYear VARCHAR(20))

-- Step 3

INSERT INTO @yearFigures        -- Inserting into your temp table
SELECT DISTINCT
       strtrdecode,
       CONVERT( VARCHAR(10), dtmtradedate, 101),
       sales1.cs1,
       sales2.cs2
FROM DAILYSALES dls

-- Step 4

LEFT OUTER JOIN(
    SELECT cursales AS cs1
    FROM DAILYSALES dls
    WHERE dtmtradedate BETWEEN @startDate1 AND @endDate1
    GROUP BY cursales ) AS sales1 ON sales1.cursales = dls.cursales -- Year one
LEFT OUTER JOIN (
    SELECT cursales AS cs2
    FROM DAILYSALES dls
    WHERE dtmtradedate BETWEEN @startDate2 AND @endDate2
    GROUP BY cursales ) AS sales2 ON sales2.cursales = dls.cursales; -- Year two

-- Step 5

SELECT StoreLocation,
       TransactionDt,
       SalesValue,
       SalesValue2
FROM @yearFigures -- From here you're pulling from your temp table (your output)
你应该得到你想要的东西。左侧外部联接的独立销售值为年,并命名为年。我在我的一个dbo上运行了1行,得到了这个

StoreLocation  TransactionDt  PreviousYear   CurrentYear
AESKAGGS         03/14/2014     828.35         400.00

再次对我放松点,伙计们。。我是个笨蛋

向我们展示您的表架构,并请使用您正在使用的SQL的相应版本进行标记。这似乎是一个非常愚蠢的问题,我在哪里可以找到表架构?没有愚蠢的问题,只有愚蠢的答案:-)。。。仅显示表名和列即可。表名:DAILYSALES,列:STRSALETYPE STRTRADECODE CURSALES DTMTRADEDATE INTTRANSNUMI已将此添加到上述问题中
TABLE_CATALOG   TABLE_SCHEMA    TABLE_NAME  COLUMN_NAME
> DRSData   dbo DAILYSALES  STRSALETYPE 
> DRSData   dbo DAILYSALES  STRTRADECODE
> DRSData   dbo DAILYSALES  CURSALES
> DRSData   dbo DAILYSALES  DTMTRADEDATE
> DRSData   dbo DAILYSALES  INTTRANSNUM



 *SELECT        strtradecode, dtmtradedate, sum(cursales) as [Actual SALES TY], '' as Target
    FROM            DAILYSALES 
                             where (STRSALETYPE = 'H' )and (DTMTRADEDATE BETWEEN CONVERT(DATETIME, '2015-06-01 00:00:00', 102) AND CONVERT(DATETIME, '2015-06-01 00:00:00', 
                             102))
                             group by strtradecode, dtmtradedate*
-- 1. Began by declaring the dates. 
-- 2. Then created a temp table.
-- 3. Inserted into the temp table
-- 4. Created JOINS to separate the dates you wanted. 
-- 5. SELECTED fields from the temp table for the desired output.

-- Step 1

DECLARE  @startDate1   DATETIME2 = '01/01/2014', -- Creating previous year
         @endDate1     DATETIME2 = '12/31/2014', -- Creating previous year
         @startDate2   DATETIME2 = '01/01/2015', -- Creating this year
         @endDate2     DATETIME2 = '12/31/2015'; -- Creating this year
-- Step 2

DECLARE @yearFigures TABLE (    -- Created temp table with your desired output
       StoreLocation VARCHAR(20),
       TransactionDt VARCHAR(10),
       PreviousYear VARCHAR(20),
       CurrentYear VARCHAR(20))

-- Step 3

INSERT INTO @yearFigures        -- Inserting into your temp table
SELECT DISTINCT
       strtrdecode,
       CONVERT( VARCHAR(10), dtmtradedate, 101),
       sales1.cs1,
       sales2.cs2
FROM DAILYSALES dls

-- Step 4

LEFT OUTER JOIN(
    SELECT cursales AS cs1
    FROM DAILYSALES dls
    WHERE dtmtradedate BETWEEN @startDate1 AND @endDate1
    GROUP BY cursales ) AS sales1 ON sales1.cursales = dls.cursales -- Year one
LEFT OUTER JOIN (
    SELECT cursales AS cs2
    FROM DAILYSALES dls
    WHERE dtmtradedate BETWEEN @startDate2 AND @endDate2
    GROUP BY cursales ) AS sales2 ON sales2.cursales = dls.cursales; -- Year two

-- Step 5

SELECT StoreLocation,
       TransactionDt,
       SalesValue,
       SalesValue2
FROM @yearFigures -- From here you're pulling from your temp table (your output)
StoreLocation  TransactionDt  PreviousYear   CurrentYear
AESKAGGS         03/14/2014     828.35         400.00