我正在BigQuery上访问GDELT全球新闻数据库。我有一些SQL代码已生成了共同提到的国家/地区的邻接列表。此结果适用于所有全球新闻。我的问题是我想将结果限制在一个国家的新闻报道中,这样我就可以比较两个或多个国家的同一个国家的网络。
这是查询输出:
https://docs.google.com/spreadsheets/d/1BBxUC9SRFAeBEo5AyX4EYgtK92VMmcEK9DLxte2Xhhk/edit?usp=sharing
仅供参考,这是输出的网络图:
https://drive.google.com/file/d/101h4_kgbfOBGAakqPR-x6dVvObxKwvI6/view?usp=sharing
SELECT c.Source SourceCountryCode, d.country Source, c.Target TargetCountryCode, e.country Target, Count RawCount, "Undirected" Type, RATIO_TO_REPORT(Count) OVER () Weight FROM (
SELECT a.name Source, b.name Target, COUNT(*) as Count
FROM (FLATTEN(
SELECT GKGRECORDID, UNIQUE(REGEXP_EXTRACT(SPLIT(V2Locations,';'),r'^[1-
5]#.*?#(.*?)#')) name
FROM [gdelt-bq:gdeltv2.gkg]
WHERE DATE > 20190101000000 and DATE < 20190605000000 AND V2Locations like '%United States%United States%' and (V2Themes like '%CYBER_ATTACK%' or DocumentIdentifier like '%cyber_attack%') ignore case
,name)) a
JOIN EACH (
SELECT GKGRECORDID, UNIQUE(REGEXP_EXTRACT(SPLIT(V2Locations,';'),r'^[1-5]#.*?#(.*?)#')) name
FROM [gdelt-bq:gdeltv2.gkg]
WHERE DATE > 20190101000000 and DATE < 20190605000000 AND V2Locations like '%United States%United States%' and (V2Themes like '%CYBER_ATTACK%' or DocumentIdentifier like '%cyber_attack%') ignore case
) b
ON a.GKGRECORDID=b.GKGRECORDID
WHERE a.name<b.name
GROUP EACH BY 1,2
ORDER BY 3 DESC
LIMIT 1000
) c
JOIN EACH (
select fips, country from [gdelt-bq:extra.countryinfo]) d
ON c.Source = d.fips
JOIN EACH (
select fips, country from [gdelt-bq:extra.countryinfo]) e
ON c.Target = e.fips
order by Count Desc
答案 0 :(得分:1)
只需在查询底部添加以下内容(但显然要在ORDER BY
上方)
WHERE c.Source = 'US'
最后两个联接也可以重写为不太复杂
JOIN EACH [gdelt-bq:extra.countryinfo] d
ON c.Source = d.fips
JOIN EACH [gdelt-bq:extra.countryinfo] e
ON c.Target = e.fips