如何在SQL Server中获取内部联接的第一个实例以进行“选择入”?

时间:2018-10-01 18:08:59

标签: sql sql-server

到目前为止,这是我的SQL语句

INSERT INTO RegisteredDonors (firstname,
                              lastname,
                              organization,
                              OrganizationType,
                              OrganizationGroup,
                              OrganizationField1,
                              OrganizationField2,
                              OrganizationOther,
                              Website,
                              PrimaryEmail,
                              AltEmail,
                              PrimaryPhone,
                              PrimaryPhoneName,
                              AltPhone,
                              AltPhoneName,
                              FaxNumber,
                              BillingFirstName,
                              BillingLastName,
                              BillingCompany,
                              BillingAddressLine1,
                              BillingAddressLine2,
                              BillingCity,
                              BillingPostalCode,
                              BillingState,
                              BillingCountry,
                              ShippingFirstName,
                              ShippingLastName,
                              ShippingCompany,
                              ShippingAddressLine1,
                              ShippingAddressLine2,
                              ShippingCity,
                              ShippingPostalCode,
                              ShippingState,
                              ShippingCountry,
                              DateAdded,
                              DonorStatusCode,
                              qbId,
                              CreatedBy)

SELECT firstname,
       lastname,
       organization,
       organization_type,
       organization_group,
       organization_field_1,
       organization_field_2,
       organization_other,
       organization_website,
       email,
       email_alt,
       telephone,
       telephone_name,
       telephone_alt,
       telephone_alt_name,
       fax,
       payment_firstname,
       payment_lastname,
       payment_company,
       payment_address_1,
       payment_address_2,
       payment_city,
       payment_postcode,
       payment_zone,
       payment_country,
       shipping_firstname,
       shipping_lastname,
       shipping_company,
       shipping_address_1,
       shipping_address_2,
       shipping_city,
       shipping_postcode,
       shipping_zone,
       shipping_country,
       GETDATE()  as CreateDate,
       1          as DonorStatusCode,
       qb.Id,
       'Me' as CreatedBy
FROM 
    cart_order co
INNER JOIN
    qb_customers qb ON (co.organization + ' (' + co.payment_zone + ')') = qb.FullyQualifiedName
ORDER BY
    co.order_id DESC

如何获取内部联接以从cart_order返回结果的第一行?我正在使用SQL Server(Azure),但无法弄清楚。我看着这个StackOverflow答案,不确定在这里是否正确应用。

SQL Left Join first match only

谢谢!

2 个答案:

答案 0 :(得分:2)

您可以使用top (1)

select top (1) . . .
from cart_order co inner join
    qb_customers qb
    on co.organization + ' (' + co.payment_zone + ')') = qb.FullyQualifiedName
order by co.order_id desc

答案 1 :(得分:1)

您必须使用排名功能。

;with co as (
    select *, rank() over(partition by organization + ' (' + co.payment_zone + ')' order by organization + ' (' + co.payment_zone + ')') rnk
    from cart_order
)
    INSERT INTO RegisteredDonors (firstname,
                              lastname,
                              organization,
                              OrganizationType,
                              OrganizationGroup,
                              OrganizationField1,
                              OrganizationField2,
                              OrganizationOther,
                              Website,
                              PrimaryEmail,
                              AltEmail,
                              PrimaryPhone,
                              PrimaryPhoneName,
                              AltPhone,
                              AltPhoneName,
                              FaxNumber,
                              BillingFirstName,
                              BillingLastName,
                              BillingCompany,
                              BillingAddressLine1,
                              BillingAddressLine2,
                              BillingCity,
                              BillingPostalCode,
                              BillingState,
                              BillingCountry,
                              ShippingFirstName,
                              ShippingLastName,
                              ShippingCompany,
                              ShippingAddressLine1,
                              ShippingAddressLine2,
                              ShippingCity,
                              ShippingPostalCode,
                              ShippingState,
                              ShippingCountry,
                              DateAdded,
                              DonorStatusCode,
                              qbId,
                              CreatedBy)

SELECT firstname,
       lastname,
       organization,
       organization_type,
       organization_group,
       organization_field_1,
       organization_field_2,
       organization_other,
       organization_website,
       email,
       email_alt,
       telephone,
       telephone_name,
       telephone_alt,
       telephone_alt_name,
       fax,
       payment_firstname,
       payment_lastname,
       payment_company,
       payment_address_1,
       payment_address_2,
       payment_city,
       payment_postcode,
       payment_zone,
       payment_country,
       shipping_firstname,
       shipping_lastname,
       shipping_company,
       shipping_address_1,
       shipping_address_2,
       shipping_city,
       shipping_postcode,
       shipping_zone,
       shipping_country,
       GETDATE()  as CreateDate,
       1          as DonorStatusCode,
       qb.Id,
       'Me' as CreatedBy

FROM co
       inner join qb_customers qb on (co.organization + ' (' + co.payment_zone + ')') = qb.FullyQualifiedName
where co.rnk = 1
order by co.order_id desc

我不确定rank() over(partition by organization + ' (' + co.payment_zone + ')' order by organization + ' (' + co.payment_zone + ')') rnk的百分百。如果结果不是预期的结果,则仅运行CTE部分以确保每个客户rnk = 1。