PRAGMA AnsiInForEmptyOrNullableItemsCollections;

USE hahn;

$pre = "{{pre_id}}";
$test = "{{test_id}}";
$cmp = "{{cmp_id}}";

$reports_path = "{{yt_prefix}}/";

$pre_path = $reports_path || $pre || "/primary_ammo";
$test_path = $reports_path || $test || "/primary_ammo";
$diff_path = $reports_path || $cmp || "/failed_test_ids";

$request_ids = SELECT RequestIDBinary FROM $diff_path;

$pre_response = (
    SELECT RequestID, HttpCode, Response, RequestIDBinary FROM (
        SELECT MAX_BY(row, path) FROM (
            SELECT TableRow() AS row, TablePath() AS path
            FROM RANGE($pre_path, "", "", "response")
            WHERE RequestID in $request_ids
        ) GROUP BY row.RequestID AS RequestID
    ) FLATTEN COLUMNS
);

$pre_request = (
    SELECT Request, RequestID FROM (
        SELECT MAX_BY(row, path) FROM (
            SELECT TableRow() AS row, TablePath() AS path
            FROM RANGE($pre_path, "", "", "request")
            WHERE RequestID in $request_ids
        )
        GROUP BY row.RequestID AS RequestID
    ) FLATTEN COLUMNS
);

$pre_event = (
    SELECT SSPID, PageID, ProductType, RequestID FROM (
        SELECT MAX_BY(row, path) FROM (
            SELECT TableRow() AS row, TablePath() AS path
            FROM RANGE($pre_path, "", "", "event")
            WHERE RequestID in $request_ids
        )
        GROUP BY row.RequestID AS RequestID
    ) FLATTEN COLUMNS
);

$test_response = (
    SELECT RequestID, HttpCode, Response, RequestIDBinary FROM (
        SELECT MAX_BY(row, path) FROM (
            SELECT TableRow() AS row, TablePath() AS path
            FROM RANGE($test_path, "", "", "response")
            WHERE RequestID in $request_ids
        ) GROUP BY row.RequestID AS RequestID
    ) FLATTEN COLUMNS
);

$test_event = (
    SELECT SSPID, PageID, ProductType, RequestID FROM (
        SELECT MAX_BY(row, path) FROM (
            SELECT TableRow() AS row, TablePath() AS path
            FROM RANGE($test_path, "", "", "event")
            WHERE RequestID in $request_ids
        )
        GROUP BY row.RequestID AS RequestID
    ) FLATTEN COLUMNS
);

$t1 =
SELECT
    pre_response.RequestID AS requestid,
    pre_event.SSPID AS sspid,
    pre_event.PageID AS pageid,
    pre_event.ProductType AS producttype_pre,
    test_event.ProductType AS producttype_test,
    pre_request.Request AS request,
    pre_response.Response AS response_pre,
    test_response.Response AS response_test
FROM
    $pre_response as pre_response
INNER JOIN
    $pre_request as pre_request
ON
    pre_response.RequestIDBinary == pre_request.RequestID
INNER JOIN
    $pre_event as pre_event
ON
    pre_response.RequestIDBinary == pre_event.RequestID
INNER JOIN
    $test_response as test_response
ON
    test_response.RequestID == pre_response.RequestID
INNER JOIN
    $test_event as test_event
ON
    test_response.RequestIDBinary == test_event.RequestID
WHERE
    pre_event.ProductType != test_event.ProductType;

$t2 =
SELECT *
FROM $t1
GROUP BY sspid, pageid;

SELECT *
FROM $t1;

SELECT *
FROM $t2
ORDER BY sspid, pageid;

SELECT sspid, COUNT(*) as count
FROM $t1
GROUP BY sspid;

SELECT pageid, COUNT(*) as count
FROM $t1
GROUP BY pageid;
