T-SQL:选择在两个时间段内趋势向下的帖子

时间:2018-05-23 22:58:28

标签: sql sql-server tsql sql-server-2016

我想避免在表格上进行子选择来比较两个时期以及趋势是否向下。

此选择获取一个期间的百分比

SELECT user, (SUM(value1)/SUM(value2)) AS percentage1
FROM table
WHERE (date BETWEEN @start1 AND @end1)
GROUP BY user
ORDER BY 2

此查询获取我之后的结果,但效率不高,因为有超过1亿行。

SELECT t1.user, (SUM(t1.value1)/SUM(t1.value2)) AS percentage1,
(SELECT (SUM(t2.value1)/SUM(t2.value2)) AS percentage2
FROM table AS t2
WHERE t2.userID = t1.userID
AND (t2.date BETWEEN @start2 AND @end2)
)
FROM table AS t1
WHERE (t1.date BETWEEN @start1 AND @end1)
AND (SUM(t1.value1)/SUM(t1.value2)) < (SELECT (SUM(t2.value1)/SUM(t2.value2))
FROM table AS t2
WHERE t2.userID = t1.userID
AND (t2.date BETWEEN @start2 AND @end2)
)
GROUP BY t1.user

有更好的方法吗?一种解决方案可能是只有一个日期,然后在月份(日期),年份(日期)上进行分组,并与上个月进行比较,而不是有两个确切的日期。但是对月份和年份进行分组只会为每个用户提供几行,我想避免这样做。

只想要一个干净的结果:

Adam, 43%, 47%
Lisa, 22%, 25%
John, 18%, 34%

排除这样的行,因为趋势是较低的百分比

Bill, 24%, 18%
Nina, 84%, 56%

SQL-Server 2016 Enterprise是数据库。

2 个答案:

答案 0 :(得分:3)

你可以试试这个。

SELECT G1.user, G1.percentage1, G2.percentage2 
FROM 
    ( SELECT t1.user, t1.userID
        (SUM(t1.value1)/SUM(t1.value2)) AS percentage1,
      FROM table AS t1
      WHERE 
        (t1.date BETWEEN @start1 AND @end1)
      GROUP BY t1.user, t1.userID
    ) AS G1
    INNER JOIN (
        SELECT t2.userID, (SUM(t2.value1)/SUM(t2.value2)) AS percentage2
        FROM table AS t2
        WHERE 
            (t2.date BETWEEN @start2 AND @end2)
        GROUP BY t2.userID
    ) AS G2 ON G1.userID = G2.userID
WHERE 
    G1.percentage1 < G2.percentage2

但是如果你只想从表中选择一个,那么你也可以尝试这个。

DECLARE @start DATE
DECLARE @end DATE

SET @start = CASE WHEN @start1 < @start2 THEN @start1 ELSE @start2 END
SET @end = CASE WHEN @end1 > @end2 THEN @end1 ELSE @end2 END

SELECT * FROM (
    SELECT t.userID, 
        SUM( CASE WHEN t.date BETWEEN @start1 AND @end1 THEN t.value1 END ) / SUM( CASE WHEN t.date BETWEEN @start1 AND @end1 THEN t.value2 END ) AS percentage1, 
        SUM( CASE WHEN t.date BETWEEN @start2 AND @end2 THEN t.value1 END ) / SUM( CASE WHEN t.date BETWEEN @start2 AND @end2 THEN t.value2 END ) AS percentage2, 
    FROM table AS t
    WHERE 
        (t.date BETWEEN @start AND @end)
    GROUP BY t.userID
) AS SQ WHERE percentage1 < percentage2

答案 1 :(得分:0)

我们可以尝试使用CTE方法,如果将来需要,提供更多的可读性和灵活的修改。我已将索引添加到RequiredDate列以提高性能。希望它有所帮助。

IF OBJECT_ID('dbo.InputUsers') IS NULL
BEGIN
CREATE TABLE dbo.InputUsers (
UserNameID INT NOT NULL,
UserName NVARCHAR(MAX),
RequiredDate DATETIME,
Value1 DECIMAL,
Value2 DECIMAL
)
CREATE NONCLUSTERED INDEX IX_Users_RequiredDate   
    ON dbo.InputUsers (RequiredDate);   
END

DECLARE @Start1 NVARCHAR(20), @End1 NVARCHAR(20), @Start2 NVARCHAR(20), @End2 NVARCHAR(20)
SET @Start1 = '2018-05-26'
SET @End1 = '2018-05-27'

SET @Start2 = '2018-05-28'
SET @End2 = '2018-05-29'

INSERT INTO InputUsers(UserNameID, UserName, RequiredDate, Value1, Value2) VALUES
(1, 'Adam', '2018-05-29', 13, 25),
(1, 'Adam', '2018-05-28', 12, 25),
(1, 'Adam', '2018-05-27', 11, 25),
(1, 'Adam', '2018-05-26', 10, 25),

(2, 'Lisa', '2018-05-29', 19, 25),
(2, 'Lisa', '2018-05-28', 18, 25),
(2, 'Lisa', '2018-05-27', 17, 25),
(2, 'Lisa', '2018-05-26', 16, 25),

(3, 'John', '2018-05-29', 16, 25),
(3, 'John', '2018-05-28', 17, 25),
(3, 'John', '2018-05-27', 18, 25),
(3, 'John', '2018-05-26', 19, 25),

(4, 'Bill', '2018-05-29', 10, 25),
(4, 'Bill', '2018-05-28', 11, 25),
(4, 'Bill', '2018-05-27', 12, 25),
(4, 'Bill', '2018-05-26', 13, 25)

;WITH PercentageValues1 AS (SELECT UserNameID, UserName, SUM(Value1)*100 / SUM(Value2) AS Percentage
FROM dbo.InputUsers
WHERE RequiredDate >= @Start1 AND RequiredDate <= @End1
GROUP BY UserNameID, UserName
),
PercentageValues2 AS (SELECT UserNameID, UserName, SUM(Value1)*100 / SUM(Value2) AS Percentage
FROM dbo.InputUsers
WHERE RequiredDate >= @Start2 AND RequiredDate <= @End2
GROUP BY UserNameID, UserName
)
SELECT pv2.UserName, pv1.Percentage, pv2.Percentage
FROM PercentageValues2 pv2
LEFT JOIN PercentageValues1 pv1 ON 
pv2.UserNameID = pv1.UserNameID
WHERE pv2.Percentage > pv1.Percentage