sqlite | pandas | python:选择任何列中包含值的行?

发布于 2025-02-10 17:11:34 字数 2754 浏览 1 评论 0原文

我有一个带有13500行的SQLite表,具有以下SQL模式:

PRAGMA foreign_keys = false;

-- ----------------------------
-- Table structure for numbers
-- ----------------------------
DROP TABLE IF EXISTS "numbers";
CREATE TABLE "numbers" (
  "RowId" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  "Date" TEXT NOT NULL,
  "Hour" TEXT NOT NULL,
  "N1" INTEGER NOT NULL,
  "N2" INTEGER NOT NULL,
  "N3" INTEGER NOT NULL,
  "N4" INTEGER NOT NULL,
  "N5" INTEGER NOT NULL,
  "N6" INTEGER NOT NULL,
  "N7" INTEGER NOT NULL,
  "N8" INTEGER NOT NULL,
  "N9" INTEGER NOT NULL,
  "N10" INTEGER NOT NULL,
  "N11" INTEGER NOT NULL,
  "N12" INTEGER NOT NULL,
  "N13" INTEGER NOT NULL,
  "N14" INTEGER NOT NULL,
  "N15" INTEGER NOT NULL,
  "N16" INTEGER NOT NULL,
  "N17" INTEGER NOT NULL,
  "N18" INTEGER NOT NULL,
  "N19" INTEGER NOT NULL,
  "N20" INTEGER NOT NULL,
  UNIQUE ("RowId" ASC)
);

PRAGMA foreign_keys = true;

每行包含1到80的非重复数字,按升序排序。

我只想从此表中选择仅包含数字的行:10,20,30,40,50,50,60,70,80,但不超过3个(我的意思是完全3,而不是更多而不是更少)。

我做了以下操作:

第一步:

eg仅选择列中的任何一个数字的行,我做了此命令:

SELECT * FROM numbers WHERE N1 IN (10,20,30,40,50,60,70,80);

当然是只用这些数字之一给我行,但也以我不想要的所有这些数字的排名,我想要在任何列上的这些数字中的3个数字。

第二步:

用于选择列N1和N2上包含这些数字中的任何一个的行我们刚刚运行此命令:

SELECT * FROM numbers WHERE N1 IN (10,20,30,40,50,60,70,80) AND N2 IN (10,20,30,40,50,60,70,80);

但是,这还将为我不想要的2个或更多(甚至所有数字)提供列因为这并不是任何此列上的此数字中的3个。

第三步:

检索包含n1,n2和n3上任何这些数字中的任何一个的行

SELECT * FROM numbers WHERE N1 IN (10,20,30,40,50,60,70,80) AND N2 IN (10,20,30,40,50,60,70,80) AND N3 IN (10,20,30,40,50,60,70,80);

。这些数字中有3个以上,例如4、5甚至我不需要的所有数字。

另外,一个想法是通过在(10,20,30,40,50,50,60,70,80)中添加而不是N4来修改此命令,而不是N5(10,20,30,40,50, )等直到达到N20。

60,70,80 ,n3,以及在任何给定的列中,例如N1,N12,N18和任何其他列组合,这意味着我应该创建从20列中获得的3列的任何可能组合,以便获得我的需求。

有什么聪明的方法吗?

先感谢您!

ps

  1. 我已经阅读 this 这是我需要的东西 我想避免由于多种组合而避免 java语言部分),在做我需要的事情(我 想想),但它是在Python和Pandas而不是Sqlite语法中,我认为 这个熊猫也要保留 请记住,最后两个不寻找任何可能的组合 但仅仅是为了在任何给定的列中寻找组合 我需要哪个部分。
  2. 另外,如果您可以在Python和Pandas中做到这一点,那也很好 因为我也可以使用它(所以,我正在按顺序添加标签 也许有人在寻找那个 如果您不介意,解决方案也是如此)。

I have an SQLite table with 13500 rows with the following SQL schema:

PRAGMA foreign_keys = false;

-- ----------------------------
-- Table structure for numbers
-- ----------------------------
DROP TABLE IF EXISTS "numbers";
CREATE TABLE "numbers" (
  "RowId" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  "Date" TEXT NOT NULL,
  "Hour" TEXT NOT NULL,
  "N1" INTEGER NOT NULL,
  "N2" INTEGER NOT NULL,
  "N3" INTEGER NOT NULL,
  "N4" INTEGER NOT NULL,
  "N5" INTEGER NOT NULL,
  "N6" INTEGER NOT NULL,
  "N7" INTEGER NOT NULL,
  "N8" INTEGER NOT NULL,
  "N9" INTEGER NOT NULL,
  "N10" INTEGER NOT NULL,
  "N11" INTEGER NOT NULL,
  "N12" INTEGER NOT NULL,
  "N13" INTEGER NOT NULL,
  "N14" INTEGER NOT NULL,
  "N15" INTEGER NOT NULL,
  "N16" INTEGER NOT NULL,
  "N17" INTEGER NOT NULL,
  "N18" INTEGER NOT NULL,
  "N19" INTEGER NOT NULL,
  "N20" INTEGER NOT NULL,
  UNIQUE ("RowId" ASC)
);

PRAGMA foreign_keys = true;

Each row contain non repeating numbers from 1 to 80, sorted in ascending order.

I want to select from this table only the rows that contain numbers only these numbers: 10,20,30,40,50,60,70,80 but not more than 3 of them (I mean EXACTLY 3 and not more and not less).

I did the following:

First step:

e.g. for selecting only the rows that contains ANY of these numbers on the column N1 I did this command:

SELECT * FROM numbers WHERE N1 IN (10,20,30,40,50,60,70,80);

Of course that this is giving to me rows with just one of these numbers but also rows with let's say 5 or even all these numbers which I do not want, I want exactly 3 of these numbers on ANY column.

Second step:

For selecting rows which contain any of these numbers on columns N1 and N2 we just run this command:

SELECT * FROM numbers WHERE N1 IN (10,20,30,40,50,60,70,80) AND N2 IN (10,20,30,40,50,60,70,80);

But this will give also columns with 2 or more (even all numbers) which I do not want because this is not exactly 3 of this numbers on any of this columns.

Third step:

Retrieving rows that contain any of these numbers on N1, N2 and N3 with this command:

SELECT * FROM numbers WHERE N1 IN (10,20,30,40,50,60,70,80) AND N2 IN (10,20,30,40,50,60,70,80) AND N3 IN (10,20,30,40,50,60,70,80);

This is almost good because of giving the rows with any 3 of these numbers but also gives rows that could have more than 3 of these numbers like 4, 5 or even all numbers which I don't need.

Also, one idea is to modify this command by adding AND NOT N4 IN (10,20,30,40,50,60,70,80) AND NOT N5 IN (10,20,30,40,50,60,70,80) and so on until reach the N20.

On the other hand, any of these numbers (10,20,30,40,50,60,70,80) could be on N1, N2,N3 but also in any given column like N1, N12, N18 and any other combination of columns which means I should create any possible combination of 3 columns taken from 20 columns in order to get what I need.

Is there any smarter way to do this?

Thank you in advance!

P.S.

  1. I have already read this which is somehow something I need but
    I want to avoid because of to many combinations (and also it is in
    the Java language section), this which is doing what I need (I
    think) but it is in Python and pandas not SQLite syntax and I think
    this one is the same but also in Python and pandas, also, keep
    in mind that the last two do not look for any possible combination
    but just for a give combination to look for in any given column
    which partially what I need.
  2. Also, If you can do it in Python and pandas it is very good too
    because I could use that too (so, I'm adding tags for these in order
    to be seen as well maybe there is someone which is looking for that
    solution too, if you don't mind).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野味少女 2025-02-17 17:11:34

这是一个sqlite查询,它将为您提供所需的结果。它创建所有感兴趣的值的CTE,然后将您的数字表加入CTE,如果任何列包含来自CTE的值,则仅选择ROWID> ROWID 数字,即加入中的行数正好为3(使用 和具有),然后最终从行中选择所有数据符合该标准的哪个:

WITH CTE(n) AS (
  VALUES (10),(20),(30),(40),(50),(60),(70),(80)
),
rowids AS (
  SELECT RowId
  FROM numbers
  JOIN CTE ON n IN (n1, n2, n3, n4, n5, n6, n7, n8, n9, n10,
                    n11, n12, n13, n14, n15, n16, n17, n18, n19, n20)
  GROUP BY RowId
  HAVING COUNT(*) = 3
)
SELECT n.*
FROM numbers n
JOIN rowids r ON n.RowId = r.RowId

我做了一个小

Here's an SQLite query that will give you the results you want. It creates a CTE of all the values of interest, then joins your numbers table to the CTE if any of the columns contain the value from the CTE, selecting only RowId values from numbers where the number of rows in the join is exactly 3 (using GROUP BY and HAVING) and then finally selecting all the data from the rows which match that criteria:

WITH CTE(n) AS (
  VALUES (10),(20),(30),(40),(50),(60),(70),(80)
),
rowids AS (
  SELECT RowId
  FROM numbers
  JOIN CTE ON n IN (n1, n2, n3, n4, n5, n6, n7, n8, n9, n10,
                    n11, n12, n13, n14, n15, n16, n17, n18, n19, n20)
  GROUP BY RowId
  HAVING COUNT(*) = 3
)
SELECT n.*
FROM numbers n
JOIN rowids r ON n.RowId = r.RowId

I've made a small demo on db-fiddle.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文