1

下面是我写的一段 SQL 代码。我想将两条记录合并到一条记录中,其中第一条记录域显示为“来自源”,第二条记录的域变为“到域”。我需要做更多的过滤,但为什么这个简单的语句不起作用?

我收到错误“输入末尾的语法错误”

*SELECT 
   "ID" , "Time",  "Source Domain", "To Domain" From
   (SELECT "RecordID" As "ID","UTCTimestamp" As "Time","Domain" As "Source Domain" FROM public."Traffic - Mobile")T1
   Inner Join Lateral
   (SELECT "Domain" As "To Domain" FROM public."Traffic - Mobile" where "RespondentID"="T1"."RespondentID" )T2*

提前致谢

PostgreSQL 版本 9.3

4

2 回答 2

1

我认为你过于复杂了。一个简单的表自连接就足够了。您不需要派生表来重命名列

SELECT "ID", "Time", t1."Domain" as "Source Domain", t2."Domain" as "To Domain" 
from public."Traffic - Mobile" as t1
  join public."Traffic - Mobile" as t2 on t2."RespondentID" = t1."RespondentID";
于 2018-01-03T07:29:22.210 回答
0

我认为您仍然需要一个ON子句,因为它是一个INNER JOIN. 如果您不想指定ON子句,我认为您可以CROSS JOIN改用。

例如

SELECT 
   "ID" , "Time",  "Source Domain", "To Domain" From
   (SELECT "RecordID" As "ID","UTCTimestamp" As "Time","Domain" As "Source Domain"
          FROM public."Traffic - Mobile")T1
   Inner Join Lateral
   (SELECT "Domain" As "To Domain" FROM public."Traffic - Mobile"
           where "RespondentID"="T1"."RespondentID" )T2
    ON true
于 2018-01-03T07:18:32.980 回答