且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何仅计算值的首次出现?

更新时间:2023-02-05 10:08:20

您必须获取每个屏幕宽度的DISTINCT用户数,这是将获取结果的示例查询.

You have to get the DISTINCT count of users per screenwidth and here is the sample query that will fetch the results.

点击此处查看SQL Fiddle中的演示

脚本:

CREATE TABLE screenwidth 
(
    id INT NOT NULL
  , user_id INT NOT NULL
  , screenwidth INT NOT NULL
);

INSERT INTO screenwidth (id, user_id, screenwidth) VALUES
  (1, 1, 1366),
  (2, 1, 1366),
  (3, 1, 1366),
  (4, 1, 1366),
  (5, 2, 1920),
  (6, 2, 1920),
  (7, 3, 1920),
  (8, 4, 1280),
  (9, 5, 1280),
  (10, 6, 1280);

SELECT      screenwidth
        ,   COUNT(DISTINCT user_id) AS screenwidthcount
FROM        screenwidth
GROUP BY    screenwidth
ORDER BY    screenwidthcount;

输出:

SCREENWIDTH SCREENWIDTHCOUNT
----------- ----------------
1366               1
1920               2
1280               3