2015-10-21 101 views
0

我对SQL比较陌生,试图做一个简单的查询来看看今年的销售情况,与去年同一日期或理想的一周同一天。如何查询比较今年和去年的销售额?

我创建了以下查询,当前基于单个设置日。我将需要移动这个来获得今天的日期,但后来认为id会解决这个问题。

-

Below is the query 
-strtrdecode = store location 
-dtmtradedate = transaction date 
-cursales = sales value 
-Target = will contain Targets but i haven't generated any yet but need to show -the field 

会有人请告诉我如何同时显示,今年和去年的数字在同一条线上取悦每个商店

liverpool, 01/06/2015, 4000,3000 
blackpool, 01/06/2015, 6000, 7500 etc.. 

谢谢你提前

迈克

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

向我们展示您的表架构,并标记您正在使用的适当版本的SQL。 –

+0

看起来真的很愚蠢的问题,我在哪里找到表格架构? – Michael

+0

没有愚蠢的问题,只有愚蠢的答案:-) ...只是向我们展示表名和列,就是这样。 –

回答

0

好的,请耐心等待,因为这是我第一次接受答案,而且我对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) 

你应该得到你正在寻找与此。 LEFT OUTER JOIN的分离销售价值年并命名它们。我跑了我的一个dbo的一排,并得到了这

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

再次轻松对我的家伙..我是一个noob。

相关问题