如何使用子查询“更新选择”列中的某些值?

时间:2019-06-03 23:26:25

标签: sql sql-update subquery derby

我正在使用Apache Derby DB(SQL)10.14版,这是Derby参考手册:https://db.apache.org/derby/docs/10.14/ref/refderby.pdf

我正在尝试创建星型模式,目前正在研究区域尺寸表。

这些是我正在使用的表: DWH_PRICE_PAID_RECORDS enter image description here

TRANSACTION_ID                          PRICE   DATE_OF_TRANSFER    PROPERTY_TYPE   OLD_NEW     DURATION    TOWN_CITY               DISTRICT                COUNTY                  PPDCATEGORY_TYPE    RECORD_TYPE
{FDD12C8B-5A02-4B3F-8C67-9BC523DC780B}  71000   15.09.2000 00:00    D                   N       F           SCUNTHORPE              NORTH LINCOLNSHIRE      NORTH LINCOLNSHIRE      A                       A
{70F7F480-4A9A-4FEB-A58A-2B964605BFD2}  97000   01.08.2002 00:00    F                   N       L           MANCHESTER              SALFORD                 GREATER MANCHESTER      A                       A
{64D48FA9-8C85-49D6-AF5A-23FABDDB4FEB}  104000  17.01.2006 00:00    S                   N       L           ROCHDALE                ROCHDALE                GREATER MANCHESTER      A                       A
{F0316F65-E375-4DC4-BCDF-3FDC054ADE9C}  188500  18.05.2015 00:00    S                   N       F           KIDDERMINSTER           MALVERN HILLS           WORCESTERSHIRE          A                       A
{2EC5A85B-7BEF-4127-B3D0-6B416899CAEB}  180000  07.05.1999 00:00    S                   N       F           KINGSTON UPON THAMES    KINGSTON UPON THAMES    GREATER LONDON          A                       A
{21E5FEB7-A62E-2439-E050-A8C06205342E}  55000   28.08.2015 00:00    T                   N       F           MOUNTAIN ASH            RHONDDA CYNON TAFF      RHONDDA CYNON TAFF      B                       A
{3E0330F0-0F44-8D89-E050-A8C062052140}  77000   30.08.2016 00:00    T                   N       F           WALLASEY                WIRRAL                  MERSEYSIDE              A                       A
{D43A8B4A-6272-4706-9189-30F8E24EDF13}  210000  23.05.2007 00:00    S                   N       F           BRISTOL                 NORTH SOMERSET          NORTH SOMERSET          A                       A
{3575DAF5-0E80-408F-9970-FDF5D1475E73}  185000  16.11.2007 00:00    S                   N       F           CREWKERNE               SOUTH SOMERSET          SOMERSET                A                       A
{A4246390-61F4-4228-BC82-79D3F369CA34}  32700   12.12.1996 00:00    F                   N       L           SOUTHAMPTON             SOUTHAMPTON             SOUTHAMPTON             A                       A

CREATE TABLE "DWH_PRICE_PAID_RECORDS" ("TRANSACTION_ID" VARCHAR(50) NOT NULL, "PRICE" INTEGER, "DATE_OF_TRANSFER" DATE NOT NULL, "PROPERTY_TYPE" CHAR(1), "OLD_NEW" CHAR(1), "DURATION" CHAR(1), "TOWN_CITY" VARCHAR(50), "DISTRICT" VARCHAR(50), "COUNTY" VARCHAR(50), "PPDCATEGORY_TYPE" CHAR(1), "RECORD_TYPE" CHAR(1));

DWH_POSTCODES enter image description here

Postcode    Eastings    Northings   Latitude    Longitude           Town            Region          UK_Region   Country_Code    Country_String
AB10        392900      804900      5,713,514   -211,731        Aberdeen            Aberdeen City   Scotland    SCT             Scotland
AB13        385600      801900      5,710,801   -223,776        Milltimber          Aberdeen City   Scotland    SCT             Scotland
AB14        383600      801100      5,710,076   -227,073        Peterculter         Aberdeen City   Scotland    SCT             Scotland
AB21        387900      813200      572,096     -220,033        Aberdeen Airport    Aberdeen City   Scotland    SCT             Scotland
AB22        392800      810700      5,718,724   -211,913        Bridge of Don       Aberdeen City   Scotland    SCT             Scotland
AB30        370900      772900      5,684,678   -247,712        Laurencekirk        Aberdeenshire   Scotland    SCT             Scotland
AB31        368100      798300      5,707,479   -252,623        Banchory            Aberdeenshire   Scotland    SCT             Scotland
AB32        380800      807200      5,715,545   -231,742        Westhill            Aberdeenshire   Scotland    SCT             Scotland
AB33        355200      815100      5,722,464   -274,203        Alford              Aberdeenshire   Scotland    SCT             Scotland
AB34        350800      800600      5,709,393   -281,204        Aboyne              Aberdeenshire   Scotland    SCT             Scotland


CREATE TABLE "DWH_POSTCODES" ("POSTCODE_ID" INTEGER generated always as identity (start with 1 increment by 1), "POSTCODE" VARCHAR(10), "EASTINGS" VARCHAR(50), "NORTHINGS" VARCHAR(50), "LATITUDE" VARCHAR(10), "LONGITUDE" VARCHAR(10), "TOWN" VARCHAR(50) NOT NULL, "REGION" VARCHAR(50), "UK_REGION" VARCHAR(50), "COUNTRY_CODE" VARCHAR(20), "COUNTRY_STRING" VARCHAR(20));

和DIM_REGION enter image description here

CREATE TABLE "DIM_REGION" ("REGION_ID" INTEGER generated always as identity (start with 1 increment by 1), "TOWN" VARCHAR(30), "COUNTY" VARCHAR(30), "DISTRICT" VARCHAR(30), "LATITUDE" VARCHAR(10), "LONGITUDE" VARCHAR(10), "COUNTRY_STRING" VARCHAR(20));

首先我插入了县(区)的城镇(唯一):

INSERT INTO DIM_REGION (TOWN, County, District) SELECT town_city, MAX(county), MAX(district) FROM DWH_PRICE_PAID_RECORDS GROUP BY town_city;

enter image description here

镇必须是唯一的,因为我没有其他要用作匹配/链接表的标识符。 DIM_REGION表现在具有938个唯一的镇记录,我想“更新”剩余的列“纬度”,“经度”和“ COUNTRY_STRING”。 DWH_POSTCODES表具有1637个唯一的镇记录,这意味着镇是不同的!只有532个城镇匹配,并且只有那些城镇应在DIM_REGION表中进行更新。

我仅从一列“纬度”开始,这是我希望在Update语句后显示的样子: enter image description here

UPDATE DIM_REGION SET DIM_REGION.LATITUDE = (SELECT DWH_POSTCODES.LATITUDE from dim_region join dwh_postcodes on dim_region.town = dwh_postcodes.town where dim_region.town = dwh_postcodes.town);

但是当我这样做时,我收到此错误消息(该错误消息是德语的,抱歉,我不得不翻译):

ERROR 21000 a scalar subquery returned more than one value

我不明白为什么。如果我只提交select语句:

SELECT DWH_POSTCODES.LATITUDE from dim_region join dwh_postcodes on dim_region.town = dwh_postcodes.town where dim_region.town = dwh_postcodes.town

我得到的正是我期望得到的,这就是所有专栏文章的所有纬度。

非常感谢您!

4 个答案:

答案 0 :(得分:1)

当您需要更新多行时,请使用以下查询。

UPDATE A SET A.LATITUDE = B.LATITUDE 
FROM DIM_REGION as A JOIN DWH_POSTCODES B  
ON A.TOWN = B.TOWN 
WHERE A.TOWN = B.TOWN

答案 1 :(得分:1)

您收到的错误是因为您的子查询返回的记录多于1条。您需要将子查询的结果限制为1。否则,它将无法正常工作。

或者,您也可以将子查询与 TOP 1

一起使用
UPDATE DIM_REGION SET DIM_REGION.LATITUDE = (SELECT TOP 1 DWH_POSTCODES.LATITUDE from dim_region join dwh_postcodes on dim_region.town = dwh_postcodes.town where dim_region.town = dwh_postcodes.town);

希望这会有所帮助。

答案 2 :(得分:1)

@flippi

一种在一个块中执行多个UPDATE语句的方法:

     BEGIN
       UPDATE ...
       UPDATE ...
       UPDATE ...
       ...
     END;

答案 3 :(得分:1)

IBM Db2Apache Derby DB是完全不同的产品。因此,删除db2标签。
您在两个表中都提供了绝对不匹配的城镇列表。准备匹配的数据集这么难吗?

如果dwh_postcodes.town列中没有重复项,则update应该看起来像这样(可能与{中的subselect谓词中的EXISTS相同) {1}}):

WHERE

否则,您必须使用一些聚合函数(如果Derby支持,则使用UPDATE DIM_REGION SET DIM_REGION.LATITUDE = ( SELECT --MAX ( DWH_POSTCODES.LATITUDE --) from dwh_postcodes where dim_region.town = dwh_postcodes.town -- fetch first 1 row only ) /* WHERE EXISTS ( SELECT 1 from dwh_postcodes where dim_region.town = dwh_postcodes.town ) */ ; 子句)以使此fetch first仅返回1行,如上例中所示。

或者,您可以执行1次INSERT而不是INSERT和UPDATE来填充所有4列:

subselect