连接2列,其中1列具有唯一记录,另一列具有重复记录

时间:2019-04-09 13:09:27

标签: sql sql-server ssms

我在一个有重复项的表中有2列Risk_Geo_ID,在另一个有唯一记录的表中有Geography_Identifier。进行内部联接会给我重复的记录,并且无法在多维数据集中处理此查询,因为Geography_Identifier是PK及其唯一。需要查询以获得唯一记录。 这是代码:

SELECT     
s.Geography_Identifier     
,s.State_Code    
,s.State_Name     
,s.County_Name    
,s.City_Name    
,s.ZIP_Code     
,a.Risk_ID      
,a.Risk_Address     
,a.Latitude    
,a.Longitude     
,a.Distance_to_Coast    
,a.Insurance_Score    
FROM [Policy].[Dim_Risk] AS a    
INNER JOIN [Policy].[Fact_Monthly_Policy_Snap] AS b    
ON b.Risk_ID = a.Risk_ID    
AND b.Insurance_score = a.Insurance_Score    
INNER JOIN [Common].[Dim_Geography] AS s    
ON b.Risk_Geo_ID =  s.Geography_Identifier

2 个答案:

答案 0 :(得分:0)

如果您只想将CROSS APPLYTOP 1一起使用,请尝试将IDSELECT q.Geography_Identifier ,s.State_Code ,s.State_Name ,s.County_Name ,s.City_Name ,s.ZIP_Code ,a.Risk_ID ,a.Risk_Address ,a.Latitude ,a.Longitude ,a.Distance_to_Coast ,a.Insurance_Score FROM [Policy].[Dim_Risk] AS a INNER JOIN [Policy].[Fact_Monthly_Policy_Snap] AS b ON b.Risk_ID = a.Risk_ID AND b.Insurance_score = a.Insurance_Score CROSS APPLY ( SELECT TOP 1 * FROM [Common].[Dim_Geography] AS s WHERE s.Geography_Identifier = b.Risk_Geo_ID )q 一起使用:

def start_requests(self):
    urls = (
        (self.parse_2, 'https://www.forever21.com/us/shop/catalog/category/f21/sale'),
    )
    for cb, url in urls:
        yield scrapy.Request(url, callback=cb)


def parse_2(self, response):
    for product_item_forever in response.css('div.pi_container'):
        forever_item = {
            'forever-title': product_item_forever.css('p.p_name::text').extract_first(),
            'forever-regular-price': product_item_forever.css('span.p_old_price::text').extract_first(),
            'forever-sale-price': product_item_forever.css('span.p_sale.t_pink::text').extract_first(),
            'forever-photo-url': product_item_forever.css('img::attr(data-original)').extract_first(),
            'forever-description-url': product_item_forever.css('a.item_slider.product_link::attr(href)').extract_first(),
        }
        yield forever_item

答案 1 :(得分:0)

尝试使用不同的

SELECT     
distinct s.Geography_Identifier     
,s.State_Code    
,s.State_Name     
,s.County_Name    
,s.City_Name    
,s.ZIP_Code     
,a.Risk_ID      
,a.Risk_Address     
,a.Latitude    
,a.Longitude     
,a.Distance_to_Coast    
,a.Insurance_Score    
FROM [Policy].[Dim_Risk] AS a    
INNER JOIN [Policy].[Fact_Monthly_Policy_Snap] AS b    
ON b.Risk_ID = a.Risk_ID    
AND b.Insurance_score = a.Insurance_Score    
INNER JOIN [Common].[Dim_Geography] AS s    
ON b.Risk_Geo_ID =  s.Geography_Identifier