加入问题?

时间:2014-02-06 15:30:30

标签: sql sql-server

我按照教程here创建了一个日历表,并在累计周加入。这是我正在运行的另一个查询,但这给了我一周不准确的计数。有人看到我错误的地方吗?

--Creating table to house the data
CREATE TABLE Test
(
Count int,
name varchar(2000),
salesman varchar(100),
datesold DATETIME
)
--Inserting the data into the table
INSERT INTO Test(Count, name, salesman, datesold)
SELECT COUNT(ItemSold)
     , name
     , salesman
     , datesold
  FROM saleinfodatabase
 WHERE datesold IS NOT NULL
   AND item IN ('mink coat', 'persian rug')
   AND salesman IN ('Mike', 'Joe', 'Hale')
 GROUP BY 
       name
     , salesman
     , datesold


--Selecting data from the table
SELECT a.name
     , COUNT(a.Count) As Count, dt.CumulativeWeek 
  FROM Test a
 INNER JOIN 
       dim_Date dt
    ON a.datesold = dt.FullDate
 WHERE a.datesold IS NOT NULL
   AND a.salesman IN ('Mike', 'Joe', 'Hale')
 GROUP BY 
       dt.CumulativeWeek
     , a.name

1 个答案:

答案 0 :(得分:0)

感谢@Gareth为我提供了SUM而不是Count,这使我的结果出现偏差