一段时间以来,我一直在尝试编写一个代码,该代码可以同时从多个列计算条纹,对于一个表格,我需要在其中找到大于 0 的值的条纹。起初,我设法使用显示运行组的公式,该列指示在其各自行中与所讨论的数据不同的数据数。如下图:
select descrip,
`1.01`,
(select count(*)
from `all_data` dp
where dp.`1.01` <> dpo.`1.01`
and dp.descrip <= dpo.descrip) as rungroup,
`1.02`,
(select count(*)
from `all_data` dp
where dp.`1.02` <> dpo.`1.02`
and dp.descrip <= dpo.descrip) as rungroup_2
from `all_data` dpo;
1.01和1.02是列名,descrip是用来给数据排序的。这个模型到目前为止有效,但我不知道如何在另一个查询中使用它来显示两列的条纹。有这样的方法吗?
最佳答案
您肯定应该使用您的 2 列进行 UNION
查询。
类似这样的东西
SELECT descrip, thecol,
(select count(*)
from `all_data` dp
where (dp.`1.01` <> dpo.thecol AND dp.`1.02` <> dpo.thecol)
and dp.descrip <= dpo.descrip) as rungroup
FROM
(
SELECT *
FROM
(
SELECT '1.01' AS origcol, descrip, `1.01` AS thecol from `all_data`
UNION ALL
SELECT '1.02' AS origcol, descrip, `1.02` AS thecol from `all_data`
)
ORDER BY descrip, thecol
) dpo;
但是我对这部分完全不确定:
where (dp.`1.01` <> dpo.thecol AND dp.`1.02` <> dpo.thecol)
它可能是 OR
而不是 AND
。不看数据不容易。
只需尝试一下我的查询,分解为仅获得 UNION 子查询,修复...您就会得到它。
关于mysql - 有没有办法在 MYSQL 中同时定位/检测来自多个列的数据条纹?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39645342/