查找一个月内的独特记录

时间:2015-11-10 07:27:20

标签: sql sql-server sql-server-2008

我有一个叫Id ConfigAccountId Shelf FileIdentifier Created 5356341 23 BSAS020006 C200094 28/01/2013 5356342 23 BSAS020006 C200095 28/01/2013 5356343 23 BSAS020006 C200096 28/01/2013 5356344 23 BSAS020006 C200097 28/01/2013 5356345 23 BSAS020006 C200098 28/01/2013 5356346 23 BSAS020006 C200099 28/01/2013 5356347 23 BSAS020006 C200100 28/01/2013 5356348 23 BSAS020006 C200101 28/01/2013 5356349 23 BSAS020006 C200102 28/02/2013 5356350 23 BSAS020006 C200103 28/02/2013 5356351 23 BSAS020006 C200104 28/02/2013 5356352 23 BSAS020006 C200105 28/02/2013 5356353 23 BSAS020006 C200106 28/02/2013 5356354 23 BSAS020006 C200107 28/02/2013 5356355 23 BSAS020007 C200108 28/02/2013 5356356 23 BSAS020007 C200109 28/02/2013 的表。这样可以保存我每天存储的所有数据记录。

此表中的数据如下所示:

shelf

如果您查看数据,shelf列只会在数据已满时更改数字,但我需要知道本月有多少个唯一shelf代码。问题是BSAS020006 February运行了两个月以上,所以如果我为shelf运行一个不同的,它会再次计算BSAS020006 Shelf(我希望我&# 39;有意义)。我每个月都需要一个独特的shelf计数。因此,如果Jan中已报告Feb个号码,并且该号码已计入Jan,则必须仅显示select distinct Shelf from DataFile where Created Between convert(datetime, '2015-10-01 00:00:01', 102) and convert(Datetime, '2015-10-31 23:59:59', 102) 的计数。

这是我到目前为止的代码:

Month    Shelf Count
January       15
February      16
March         10

我的输出必须显示,但请注意,货架可以运行到新月,因此也不得在该月显示。

.hgignore

3 个答案:

答案 0 :(得分:1)

如果我正确理解你的问题,我认为在计算之前将不同和min的组合对你有用:

SELECT COUNT(Shelf) AS UniqueCodesForShelf, FirstMonthForShelf AS Month
FROM
(
    SELECT Shelf, MIN(AllMonthsForShelf) AS FirstMonthForShelf
    FROM
    (
        SELECT DISTINCT
            Shelf,
            DATEADD(m, DATEDIFF(m, 0, Created), 0) AS AllMonthsForShelf
        FROM DataFile
    ) AS T1
    GROUP BY Shelf
) AS T2
GROUP BY FirstMonthForShelf

月份列的输出是该月第一天的日期。

答案 1 :(得分:0)

我woukld建议迭代一系列日期并仅在临时表中不存在临时表时使用“Shelf”填充它。

像这样的东西(不整洁和未经考验)......

DECLARE @fromDate DateTime = 'A date in the past'

DECLARE @startDateTime DateTime =  DATEADD(day, DATEDIFF(day, 0, @fromDate), 0) ;
DECLARE @periodEnd DateTime;

Create TABLE #tmpShelf ( perisodStart DateTime, PeriodEnd DateTime, shelf nvarchar(100));

WHILE @startDateTime <= GetDate()
BEGIN
  SET @periodEnd = DATEADD( MONTH, 1, @startDateTime)

  INSERT INTO #tmpShelf SELECT @startDateTime, @periodEnd, DISTINCT Shelf
  FROM DataFile D 
  WHERE D.Created BETWEEN @startDateTime AND @periodEnd
  AND NOT EXISTS (SELECT 1 FROM #tmpShelf WHERE shelf = D.shelf)

  SET @startDateTime = DATEADD( MONTH, 1, @startDateTime)
END

答案 2 :(得分:0)

试试这个,

Declare @To Datetime='2013-02-28 23:59:59'
DECLARE @t TABLE (
    Id INT
    ,ConfigAccountId INT
    ,Shelf VARCHAR(20)
    ,FileIdentifier VARCHAR(20)
    ,Created DATETIME
    )

INSERT INTO @t
VALUES

(5356341, 23,'BSAS020006','C200094','01/28/2013')
,(5356342, 23,'BSAS020006','C200095','01/28/2013')
,(5356343, 23,'BSAS020006','C200096','01/28/2013')
,(5356344, 23,'BSAS020006','C200097','01/28/2013')
,(5356345, 23,'BSAS020006','C200098','01/28/2013')
,(5356346, 23,'BSAS020006','C200099','01/28/2013')
,(5356347, 23,'BSAS020006','C200100','01/28/2013')
,(5356348, 23,'BSAS020006','C200101','01/28/2013')
,(5356349, 23,'BSAS020006','C200102','02/28/2013')
,(5356350, 23,'BSAS020006','C200103','02/28/2013')
,(5356351, 23,'BSAS020006','C200104','02/28/2013')
,(5356352, 23,'BSAS020006','C200105','02/28/2013')
,(5356353, 23,'BSAS020006','C200106','02/28/2013')
,(5356354, 23,'BSAS020006','C200107','02/28/2013')
,(5356355, 23,'BSAS020007','C200108','02/28/2013')
,(5356356, 23,'BSAS020007','C200109','02/28/2013')
SELECT *
FROM @t A
WHERE Created BETWEEN @From
        AND @To
    AND NOT EXISTS (
        SELECT Shelf
        FROM @t B
        WHERE B.Shelf = A.Shelf
            AND Created NOT BETWEEN @From
                AND @To
        )