如何从sqlite列获取唯一的子字符串数?

时间:2018-02-02 08:58:28

标签: sql sqlite

根据以下数据,我想获取唯一日期的计数,忽略时间值。

"01.09.2017 06.16"
"01.09.2017 06.17"
"01.09.2017 06.17"
"01.09.2017 06.32"
"26.10.2017 13.58"
"26.10.2017 13.59"
"26.10.2017 13.59"
"26.10.2017 14.02"
"18.01.2018 16.26"
"18.01.2018 16.26"
"18.01.2018 16.29"
"18.01.2018 16.29"
"18.01.2018 16.29"
"29.01.2018 15.45"
"29.01.2018 15.57"
"29.01.2018 15.57"
"29.01.2018 15.58"
"29.01.2018 15.58"

sql查询的结果输出应为 4

请忽略双引号,我从列名PWhen和表名PData中获取此数据。

2 个答案:

答案 0 :(得分:1)

希望这有助于你

  SELECT SUBSTR(PWhen,1,INSTR(PWhen,' ')),COUNT(*) from PDate GROUP BY
  SUBSTR(PWhen,1,INSTR(PWhen,' '));

获取唯一的日期列表

  SELECT COUNT(DISTINCT SUBSTR(PWhen,1,INSTR(PWhen,' '))) from PDate;

答案 1 :(得分:1)

为了获得样本输入的唯一日期(4)的计数,
计算不同的日期,由子字符串反映直到第一个空白。

select count(distinct substr(info, 1, instr (info,' '))) from strings;

输出:

4

MCVE我是根据您的无语法样本数据构建的:

CREATE TABLE strings (info varchar(20));
INSERT INTO "strings" VALUES('01.09.2017 06.16');
INSERT INTO "strings" VALUES('01.09.2017 06.17');
INSERT INTO "strings" VALUES('01.09.2017 06.17');
INSERT INTO "strings" VALUES('01.09.2017 06.32');
INSERT INTO "strings" VALUES('26.10.2017 13.58');
INSERT INTO "strings" VALUES('26.10.2017 13.59');
INSERT INTO "strings" VALUES('26.10.2017 13.59');
INSERT INTO "strings" VALUES('26.10.2017 14.02');
INSERT INTO "strings" VALUES('18.01.2018 16.26');
INSERT INTO "strings" VALUES('18.01.2018 16.26');
INSERT INTO "strings" VALUES('18.01.2018 16.29');
INSERT INTO "strings" VALUES('18.01.2018 16.29');
INSERT INTO "strings" VALUES('18.01.2018 16.29');
INSERT INTO "strings" VALUES('29.01.2018 15.45');
INSERT INTO "strings" VALUES('29.01.2018 15.57');
INSERT INTO "strings" VALUES('29.01.2018 15.57');
INSERT INTO "strings" VALUES('29.01.2018 15.58');
INSERT INTO "strings" VALUES('29.01.2018 15.58');