Я впервые работаю с sql / bigquery. Я не могу объяснить проблему ясно, потому что я не понимаю логи c из sql.
У меня есть «table_a», который я хотел бы объединить с «table_b», а затем запросить «TransactionType», « BusinessDay "и" ExitStationID ". Однако в 'table_b' "ExitStationID" - это "SiteID", который мне нужен для соединения влево с таблицей "MP_001_Station".
Я могу запросить обе таблицы. Однако ExitStationID, принадлежащий table_b, не преобразуется в правильное значение.
Вот код:
WITH bss AS (
SELECT
TransactionType,
BusinessDay,
ExitStationID
FROM
`table_a`
WHERE
EntryStationID IS NOT NULL AND ExitStationID IS NOT NULL AND
CAST(EntryStationID as INT64) < 100 AND CAST(ExitStationID AS INT64) < 100 AND
BusinessDay BETWEEN '2017-05-01' AND '2017-12-31'
UNION ALL
SELECT
CAST(MessageTypeValue AS STRING) AS TransactionType,
BusinessDay,
SiteID AS ExitStationID
FROM
`table_b`
WHERE
MessageTypeValue = 20 AND BusinessDay BETWEEN '2017-05-01' AND '2017-12-31'
),
dimdate AS (
SELECT
EnglishDayNameOfWeek AS Day,
DayType,
FullDateAlternateKey
FROM
`DimDate`
),
dimstation AS (
SELECT
LineCode AS ExitLine,
StationKey
FROM
`DimStation`
),
mappingstation AS (
SELECT
StationCodeBLU
FROM
`MP_001_Station`
),
legacy AS (
SELECT
SiteID
FROM `table_b`
)
SELECT
EXTRACT(DAYOFYEAR FROM BusinessDay) AS DayOfYear,
EXTRACT(DATE FROM BusinessDay) AS Date,
EXTRACT(MONTH FROM BusinessDay) AS Month,
Day,
DayType,
ExitStationID,
COUNT(*) AS Ridership
FROM bss
LEFT JOIN dimdate
ON (
CAST(bss.BusinessDay AS date) = dimdate.FullDateAlternateKey
)
LEFT JOIN dimstation
ON (
CAST(bss.ExitStationID AS INT64) = CAST(dimstation.StationKey AS INT64)
)
LEFT JOIN mappingstation
ON (
CAST(bss.ExitStationID AS INT64) = CAST(mappingstation.StationCodeBLU AS INT64)
)
GROUP BY
DayOfYear,
Date,
Month,
Day,
DayType,
ExitStationID
ORDER BY
DayOfYear,
ExitStationID