-1

我有下表:

    CREATE TABLE ta91 (
    id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, 
    date INTEGER, 
    _520  INTEGER, 
    _530 INTEGER,
    _540 INTEGER,
    _550 INTEGER,
    _560 INTEGER,
    _570 INTEGER,
    _580 INTEGER,
    _590 INTEGER,
    _600 INTEGER,
    _610 INTEGER,
    _620 INTEGER,
    _630 INTEGER,
    _640 INTEGER,
    _650 INTEGER,
    _660 INTEGER,
    _670 INTEGER,
    _680 INTEGER,
    _690 INTEGER,
    _700 INTEGER,
    _710 INTEGER,
    _720 INTEGER,
    _730 INTEGER,
    _740 INTEGER,
    _750 INTEGER,
    _760 INTEGER,
    _770 INTEGER,
    _780 INTEGER,
    _790 INTEGER,
    _800 INTEGER,
    _810 INTEGER,
    _820 INTEGER,
    _830 INTEGER,
    _840 INTEGER,
    _850 INTEGER
)

具有以下值:

INSERT INTO "someTable" VALUES ("10","1355480306018","1","0","0","0","0","1","0","0","0","0","1","1","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0");
INSERT INTO "someTable" VALUES ("11","1355480307044","1","0","0","0","0","0","0","0","1","0","0","0","1","0","0","0","0","0","1","1","1","1","0","0","0","0","0","0","0","0","0","0","0","0");
INSERT INTO "someTable" VALUES ("12","1355480308027","1","0","0","0","0","0","0","0","1","0","0","0","1","0","0","0","0","0","1","1","1","1","0","0","0","0","0","0","0","0","0","0","0","0");
INSERT INTO "someTable" VALUES ("13","1355480309033","1","0","0","0","0","1","0","0","0","0","1","1","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0");
INSERT INTO "someTable" VALUES ("14","1355480310038","1","0","0","0","0","1","0","0","0","0","1","1","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0");
INSERT INTO "someTable" VALUES ("15","1355480311043","1","0","0","0","0","0","0","0","1","0","0","0","1","0","0","0","0","0","1","1","1","1","0","0","0","0","0","0","0","0","0","0","0","0");
INSERT INTO "someTable" VALUES ("16","1355480312043","1","0","0","0","0","0","0","0","1","0","0","0","1","0","0","0","0","0","1","1","1","1","0","0","0","0","0","0","0","0","0","0","0","0");
INSERT INTO "someTable" VALUES ("17","1355480313048","1","0","0","0","0","1","0","0","0","0","1","1","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0");

我如何计算值从 0/1 切换的次数。1/0 直接在 sql 中的每一列?

我正在使用带有 System.Data.SQLite.dll 1.0.82.0 的 c# 来访问数据库

4

1 回答 1

1

假设

  1. 在 sqlite 之外没有可移植性要求;
  2. 连续id值;
  3. 需要计算(每列) 0 到 1 和 10 开关的次数;
  4. 表名是someTable,就像在你的INSERT陈述中一样;

你可以使用这个:

SELECT SUM(a._520<>b._520) AS s_520,
       SUM(a._530<>b._530) AS s_530,
       SUM(a._540<>b._540) AS s_540,
       SUM(a._550<>b._550) AS s_550,
       SUM(a._560<>b._560) AS s_560,
       SUM(a._570<>b._570) AS s_570,
       SUM(a._580<>b._580) AS s_580,
       SUM(a._590<>b._590) AS s_590,
       SUM(a._600<>b._600) AS s_600,
       SUM(a._610<>b._610) AS s_610,
       SUM(a._620<>b._620) AS s_620,
       SUM(a._630<>b._630) AS s_630,
       SUM(a._640<>b._640) AS s_640,
       SUM(a._650<>b._650) AS s_650,
       SUM(a._660<>b._660) AS s_660,
       SUM(a._670<>b._670) AS s_670,
       SUM(a._680<>b._680) AS s_680,
       SUM(a._690<>b._690) AS s_690,
       SUM(a._700<>b._700) AS s_700,
       SUM(a._710<>b._710) AS s_710,
       SUM(a._720<>b._720) AS s_720,
       SUM(a._730<>b._730) AS s_730,
       SUM(a._740<>b._740) AS s_740,
       SUM(a._750<>b._750) AS s_750,
       SUM(a._760<>b._760) AS s_760,
       SUM(a._770<>b._770) AS s_770,
       SUM(a._780<>b._780) AS s_780,
       SUM(a._790<>b._790) AS s_790,
       SUM(a._800<>b._800) AS s_800,
       SUM(a._810<>b._810) AS s_810,
       SUM(a._820<>b._820) AS s_820,
       SUM(a._830<>b._830) AS s_830,
       SUM(a._840<>b._840) AS s_840,
       SUM(a._850<>b._850) AS s_850
FROM "someTable" a INNER JOIN "someTable" b
ON a.id = b.id-1;

当然,我已经生成了这个东西。你也可以。

于 2013-01-26T14:17:17.387 回答