插入新表获取更多记录,CROSS JOIN,UPDATE

时间:2016-07-26 14:03:13

标签: sql-server ssms

在我当前的数据结构中,我的OFFENSE表没有我需要的所有列。出于这个原因,我将其与其他几个表列连接起来,并将其插入到由我创建的名为TexasCCHPublicRecords的新表中。这是我的问题:

INSERT INTO TexasCCHPublicRecords (OFF_IDN, TRS_IDN, AGY_TXT, DOO_DTE, AON_COD, AOL_TXT, LDA_CODE, GOC_COD, ADN_COD, ADD_TXT, ADA_DTE, REF_TXT, 
IPN_NBR, ICA_NBR, DMV_COD, TRS_CODE, TRN_CODE, PERSON_ID, FIRST_NAME, LAST_NAME, DATE_OF_BIRTH, CDN, OffenseCode, OffenseName, CDNCode, ArrestingAgency,
ArrestingAgencyORI, ProsecutionAgency, ProsecutionAgencyORI)

SELECT o.* , trs.TRS_COD as 'TRS_CODE', trn.TRN_NBR as 'TRN_CODE', p.PER_IDN as 'PERSON_ID', nam.FNA_TXT as 'FIRST_NAME', nam.LNA_TXT as 'LAST_NAME', 
birth.DOB_DTE as 'DATE_OF_BIRTH', cdnCode.CDN_VAL_TXT as 'CDN', offenseCode.OFF_COD as 'OffenseCode', offenseCode.LIT_TXT as 'OffenseName', 
cdnCode.CDN_VAL_COD as 'CDNCode', arrestingAgency.ATR_TXT as 'ArrestingAgency', arrestingAgency.ORI_TXT as 'ArrestingAgencyORI', 
prosecutionAgency.ATR_TXT as 'ProsecutionAgency',prosecutionAgency.ORI_TXT as 'ProsecutionAgencyORI'
    FROM OFFENSE o
    inner join CCH_PUBLIC.dbo.TRS  trs on trs.TRS_IDN = o.TRS_IDN 
    inner join CCH_PUBLIC.dbo.TRN  trn on trn.TRN_IDN = trs.TRN_IDN 
    inner join CCH_PUBLIC.dbo.PERSON  p on p.IND_IDN = trn .IND_IDN 
    inner join CCH_PUBLIC.dbo.NAME nam on nam.PER_IDN = p.PER_IDN
    inner join CCH_PUBLIC.dbo.BRTHDATE birth on birth.PER_IDN = p.PER_IDN
    inner join CCH_PUBLIC.dbo.PROSECUTION prose on prose.TRS_IDN = o.TRS_IDN
    inner join CCH_PUBLIC.dbo.AGENCY arrestingAgency on arrestingAgency.ORI_TXT = o.AGY_TXT
    inner join CCH_PUBLIC.dbo.AGENCY prosecutionAgency on prosecutionAgency.ORI_TXT = o.REF_TXT
    inner join CCH_PUBLIC.dbo.CRT_STAT crtStat on crtStat.TRS_IDN = o.TRS_IDN
    inner join CCH_PUBLIC.dbo.CDN_COD cdnCode on cdnCode.CDN_VAL_COD = crtStat.CDN_COD
    inner join CCH_PUBLIC.dbo.OFF_CODE offenseCode on offenseCode.OFF_COD = o.AON_COD

我会得出结论,这个表会选择OFFENSE表内的每个进程将其与其他表连接起来并将其插入TexasCCHPublicRecords

然而,在运行此简单计数后

select count(*) as 'Offense Table Record Count' FROM OFFENSE
select count(*) as 'CCHPublicRecords Table Record Count' FROM TexasCCHPublicRecords

我以这些结果结束:

Offense Table Record Count
11372377

CCHPublicRecords Table Record Count
49666836

我的新表中有超过3800万条记录。这怎么发生的?我的查询是否插入了相同攻击行的重复实例?

我的目标是选择一个OFFENSE内部连接它与它相关联的表并将其插入我的空表。我做错了什么?

  

更新

在阅读其中一条评论后,我意识到其中一张表给了我7列。现在我只想选择第一个结果。我正在尝试做一个交叉申请但是我不确定在交叉申请的外面放置什么。这是我的选择查询:

SELECT o.*, trs.TRS_COD as 'TRS_CODE', trn.TRN_NBR as 'TRN_CODE', p.PER_IDN as 'PERSON_ID', nam.FNA_TXT as 'FIRST_NAME', nam.LNA_TXT as 'LAST_NAME', 
birth.DOB_DTE as 'DATE_OF_BIRTH', cdnCode.CDN_VAL_TXT as 'CDN', offenseCode.OFF_COD as 'OffenseCode', offenseCode.LIT_TXT as 'OffenseName', 
cdnCode.CDN_VAL_COD as 'CDNCode', arrestingAgency.ATR_TXT as 'ArrestingAgency', arrestingAgency.ORI_TXT as 'ArrestingAgencyORI', 
prosecutionAgency.ATR_TXT as 'ProsecutionAgency',prosecutionAgency.ORI_TXT as 'ProsecutionAgencyORI'
    FROM OFFENSE o
    inner join CCH_PUBLIC.dbo.TRS  trs on trs.TRS_IDN = o.TRS_IDN 
    inner join CCH_PUBLIC.dbo.TRN  trn on trn.TRN_IDN = trs.TRN_IDN 
    inner join CCH_PUBLIC.dbo.PERSON  p on p.IND_IDN = trn.IND_IDN 
    inner join CCH_PUBLIC.dbo.BRTHDATE birth on birth.PER_IDN = p.PER_IDN
    inner join CCH_PUBLIC.dbo.PROSECUTION prose on prose.TRS_IDN = o.TRS_IDN
    inner join CCH_PUBLIC.dbo.AGENCY arrestingAgency on arrestingAgency.ORI_TXT = o.AGY_TXT
    inner join CCH_PUBLIC.dbo.AGENCY prosecutionAgency on prosecutionAgency.ORI_TXT = o.REF_TXT
    inner join CCH_PUBLIC.dbo.CRT_STAT crtStat on crtStat.TRS_IDN = o.TRS_IDN
    inner join CCH_PUBLIC.dbo.CDN_COD cdnCode on cdnCode.CDN_VAL_COD = crtStat.CDN_COD
    inner join CCH_PUBLIC.dbo.OFF_CODE offenseCode on offenseCode.OFF_COD = o.AON_COD
    CROSS APPLY (
        SELECT TOP 1 *
        FROM CCH_PUBLIC.dbo.NAME as nam
        WHERE nam.PER_IDN = p.PER_IDN
    ) nam

CROSS APPLY中的nam是否正确?

0 个答案:

没有答案