如何将数据从表B追加到表A,但仅当表C中有记录时

时间:2019-07-02 02:08:18

标签: postgresql sql-insert

我希望你能告诉我我在做什么错。我的Postgres函数有以下摘录:输入“ RawdataCsvs2”的部分无法正常工作。

如果在“地点”表的“ CalcType”字段中将地点标记为“新建”,我只想将记录插入StationMactemp和RawdataCsvs2中。

当我在“ CalcType”字段中未标记为“ New”的场所上运行脚本时,.StationMacTemp表中没有任何记录(这是完美的,并且是我期望的),但是由于某种原因,我得到了记录添加到RawdataCsv2表中,即使我也尝试使用where子句

where "StationMacTemp"."CalcType" = 'New' 
   and "csv_file"."StationMac" in (Select "StationMac" 
                                   from dbo."StationMacTemp");

我想,如果我输入必须在StationMacTemp表中找到StationMac的条件,那么如果该表中没有记录,则不应将任何记录添加到RawDataCsvs2表中,但是它们仍然存在,而我没有知道为什么。任何帮助将不胜感激。

我希望我可以在函数中使用If语句,或者在情况下使用Case,这将限制此脚本仅在Venues表中的Venue在“ CalcType”字段中标记为“ New”时运行。

我已经尝试过,但是没有运气,我一直认为我应该能够单独使用Where子句来做到这一点。我注意到,如果我反向尝试此操作,并且选择一个标记为“新建”的场地,则“ StationMacTemp”和“ RawDataCsvs2”表都将按需填充。

...
DROP TABLE IF EXISTS dbo."StationMacTemp";
create table dbo."StationMacTemp" as 
select "StationMac","RawDataCsvs"."VenueId", "Venues"."CalcType", "AreaOfInterestId" 
from dbo."RawDataCsvs" 
   INNER JOIN dbo."Venues" ON ("Venues"."Id"= "RawDataCsvs"."VenueId")
   inner join dbo."AreaOfInterests" on ("AreaOfInterests"."Id"= "RawDataCsvs"."AreaOfInterestId")
where "Venues"."Id" = venueid 
  and "Venues"."CalcType" ='New' 
  and ("Venues"."VenueType" = 'RestoBar' OR "AreaOfInterests"."Level" = 77)  
group by dbo."RawDataCsvs"."StationMac", "RawDataCsvs"."VenueId", "Venues"."CalcType", dbo."RawDataCsvs"."AreaOfInterestId"; 


INSERT INTO dbo."RawDataCsvs2"("TimeStr", "StationMac", "Level", "Lat", "Lng", "RssMacStation1", 
        "RssStation1", "RssMacStation2", "RssStation2", "RssMacStation3", 
        "RssStation3", "RssMacStation4", "RssStation4", "RssMacStation5", 
        "RssStation5", "RssMacStation6", "RssStation6", "RssMacStation7", 
        "RssStation7", "RssMacStation8", "RssStation8", "RssMacStation9", 
        "RssStation9", "RssMacStation10", "RssStation10", "LoadingGuid", 
        "LoadingDate",
        "SiteId", 
        "VenueId", 
        "LocalTime", "OperatingDay", "AreaOfInterestId")
SELECT "TimeStr", csv_file."StationMac", csv_file."Level", "Lat", "Lng", "RssMacStation1", 
       "RssStation1", "RssMacStation2", "RssStation2", "RssMacStation3", 
           "RssStation3", "RssMacStation4", "RssStation4", "RssMacStation5", 
           "RssStation5", "RssMacStation6", "RssStation6", "RssMacStation7", 
           "RssStation7", "RssMacStation8", "RssStation8", "RssMacStation9", 
           "RssStation9", "RssMacStation10", "RssStation10", "LoadingGuid", 
           "LoadingDate",
        dbo.csv_file."SiteId", 
        csv_file."VenueId", 
        "LocalTime", "OperatingDay", dbo."StationMacTemp"."AreaOfInterestId" as "AreaOfInterestId"
FROM dbo.csv_file
    INNER JOIN dbo."Venues" ON ("Venues"."Id"= csv_file."VenueId")
    INNER JOIN dbo."StationMacTemp" on ("StationMacTemp"."StationMac" = csv_file."StationMac")
    INNER JOIN dbo."AreaOfInterests" on ("AreaOfInterests"."Id"= "StationMacTemp"."AreaOfInterestId")
where "Venues"."Id" = venueid 
  and "Venues"."CalcType" ='New' 
  and ("Venues"."VenueType" = 'RestoBar' OR "AreaOfInterests"."Level" = 77); 

我希望,如果在Venues表中将Venue标记为“ New”,则将同时填充StationMacTemp和RawDataCsvs2表,并且如果未将场地标记为“ New”,则不会填充两个表。我没有收到任何错误消息,只是总是填充RawDataCsvs2表,而不管场地是否“新建”

0 个答案:

没有答案