我正在尝试找出如何根据通过和失败列来复制行。到目前为止,这是我已经完成查询的表。
查询代码如下所示。我正在从json查询。
DataClasses1DataContext()
因此,假设我想将其作为重复项进行查询,以分隔 Pass 和 Fail 值,就像:
SELECT
to_date(LotSummary ->> 'Start', 'HH24:MI:SS DD/MM/YY')AS "Date",
Machine AS "Machine",
LotSummary ->> 'Pass' AS "Pass",
LotSummary ->> 'Fail' AS "Fail"
FROM
(
SELECT
CASE
WHEN jsonb_typeof(json_data->'OEESummary'->'LotSummary'->'Lot') = 'array'
THEN
jsonb_array_elements(cast(json_data->'OEESummary'->'LotSummary'->'Lot' AS JSONB))
ELSE
json_data->'OEESummary'->'LotSummary'->'Lot'
END
AS LotSummary,
json_data->'OEESummary'->>'MachineID' AS Machine
FROM
(
SELECT
jsonb_array_elements(cast(json_data->>'body' AS JSONB)) AS json_data
FROM data
)t
WHERE
json_data ->> 'file_name' = 'OEE.xml'
)a
WHERE
to_date(LotSummary ->> 'Start', 'HH24:MI:SS DD/MM/YY') IS NOT NULL
以防万一,您需要json格式(请注意,它不是确切的格式,但是格式正确):
+----------------------------+--------------+------+------+
| Date | Machine | Pass | Fail |
+----------------------------+--------------+------+------+
| "2019-08-04T16:00:00.000Z" | TRS1000i-082 | 5 | NULL |
| "2019-08-04T16:00:00.000Z" | TRS1000i-082 | NULL | 2 |
| "2019-07-01T16:00:00.000Z" | TRS1000i-001 | 0 | NULL |
| "2019-07-01T16:00:00.000Z" | TRS1000i-001 | NULL | 0 |
| "2019-07-01T16:00:00.000Z" | TRS1000i-001 | 20 | NULL |
| "2019-07-01T16:00:00.000Z" | TRS1000i-001 | NULL | 0 |
+----------------------------+--------------+------+------+
如果您知道我可以用来解决此问题的任何技术,请提供帮助。非常感谢您的帮助!谢谢你。
答案 0 :(得分:1)
在表中,可以使用横向联接:
select t.date, t.machine, v.pass, v.fail
from t cross join lateral
(values (t.pass, null), (null, t.fail)) v(pass, fail);
我不太确定您的查询与该问题有什么关系。但是您可以将其定义为CTE,然后将结果用于t
。
答案 1 :(得分:1)
在结果集的顶部,它只是联合查询:
$result = DB::connection('mysql2')
->query()
->select('CC AS CountOfVisits', DB::raw('count(CC) AS Users'))
->fromSub(function($query) use ($startdate) {
$query->select('user_id', DB::raw('count(user_id) AS CC '))
->from('mytable')
->where('created_at','>=',$startdate)
->groupBy('user_id');
}, 'temp')
->groupBy('CC')
->get();