我正在尝试从下面的示例表中提取所有地址号值,但每个组的最低地址号除外。首先需要按帐号对它们进行分组。 下表是主要结果集的结果,因此我需要子查询或cte或其他内容。如何做到这一点。光标?
addressnumber accountnumber
3 15847
4 15847
5 15847
6 15847
8 15847
3 4310
4 4310
3 4350
4 4350
6 4350
3 5076
4 5076
SELECT
addressnumber,
a.accountnumber [accountnumber],
a.accountid accountid,
a.accountratingcode [accountsegment],
a.address1_addresstypecode,
a.address1_city,
a.address1_country,
a.address1_line1,
a.address1_line2,
a.address1_line3,
a.address1_postalcode,
a.address1_stateorprovince,
a.address2_city,
a.address2_country,
a.address2_line1,
a.address2_line2,
a.address2_line3,
a.address2_postalcode,
a.address2_stateorprovince,
a.creditlimit,
a.creditonhold,
a.customertypecode,
a.industrycode,
a.name [name],
a.ownerid,
a.parentaccountid,
a.paymenttermscode,
a.sik_billingemail,
a.sik_billingfax,
a.sik_billingphone,
a.sik_copyprimaryadd,
a.sik_deliverymethodid,
a.sik_warehouseid,
a.telephone1 [main phone],
ca.customeraddressid,
ca.addressnumber,
ca.city ca_city,
ca.country ca_country,
ca.line1 ca_line1,
ca.line2 ca_line2,
ca.line3 ca_line3,
ca.postalcode ca_postalcode,
ca.sik_addressemail ca_email,
ca.sik_scribeexternalid [ca_location_id],
ca.stateorprovince ca_state,
ca.telephone1 ca_telephone,
dm.sik_methodid,
osm.localizedlabel [address type],
pa.name [parentaccountname],
w.sik_code [warehouse code],
w.sik_name [warehouse name]
FROM account a
LEFT JOIN account pa ON a.parentaccountid = pa.accountid
JOIN customeraddress ca ON a.accountid = ca.parentid
JOIN optionsetmetadata osm ON osm.entityname = 'customeraddress'
AND osm.optionsetname = 'addresstypecode'
AND ca.addresstypecode = osm.[option]
LEFT JOIN sik_warehouse w ON a.sik_warehouseid = w.sik_warehouseid
LEFT JOIN sik_deliverymethods dm ON a.sik_deliverymethodid = dm.sik_deliverymethodsid
WHERE a.accountnumber IN ('002-100455','58398',
'4350',
'5076',
'4310',
'15847')
AND addresstypecode = 2
AND ca.jms_status = 100000000
ORDER BY [accountnumber],
ca.addressnumber ASC
答案 0 :(得分:1)
我会使用row_number()
:
select t.*
from (select t.*, row_number() over (partition by accountnumber order by addressnumber) as seqnum
from t
) t
where seqnum > 1;
答案 1 :(得分:0)
select *
from primaryresult as p
left outer join (select min(addressnumber)
,accountnumber
from primaryresult
group by accountnumber
) as iq
on p.result.account number = iq.accountnumber
and p.addressnumber = iq.minaddress
where minaddess is null
答案 2 :(得分:0)
如果您对此感兴趣,这是我的决赛:
WITH shiptorows AS
(
SELECT ROW_NUMBER() OVER (PARTITION BY a1.accountnumber ORDER BY ca1.addressnumber ASC) rn,
ca1.customeraddressid ca1id
FROM customeraddress ca1
JOIN account a1 ON ca1.parentid = a1.accountid
WHERE a1.accountnumber IN ('002-100455', '58398',
'4350',
'5076',
'4310',
'15847')
AND addresstypecode = 2
AND ca1.jms_status = 100000000
)
SELECT shiptorows.rn,
addressnumber,
a.accountnumber [accountnumber],
a.accountid accountid,
a.accountratingcode [accountsegment],
a.address1_addresstypecode,
a.address1_city,
a.address1_country,
a.address1_line1,
a.address1_line2,
a.address1_line3,
a.address1_postalcode,
a.address1_stateorprovince,
a.address2_city,
a.address2_country,
a.address2_line1,
a.address2_line2,
a.address2_line3,
a.address2_postalcode,
a.address2_stateorprovince,
a.creditlimit,
a.creditonhold,
a.customertypecode,
a.industrycode,
a.name [name],
a.ownerid,
a.parentaccountid,
a.paymenttermscode,
a.sik_billingemail,
a.sik_billingfax,
a.sik_billingphone,
a.sik_copyprimaryadd,
a.sik_deliverymethodid,
a.sik_warehouseid,
a.telephone1 [main phone],
ca.customeraddressid,
ca.addressnumber,
ca.city ca_city,
ca.country ca_country,
ca.line1 ca_line1,
ca.line2 ca_line2,
ca.line3 ca_line3,
ca.postalcode ca_postalcode,
ca.sik_addressemail ca_email,
ca.sik_scribeexternalid [ca_location_id],
ca.stateorprovince ca_state,
ca.telephone1 ca_telephone,
dm.sik_methodid,
osm.localizedlabel [address type],
pa.name [parentaccountname],
w.sik_code [warehouse code],
w.sik_name [warehouse name]
FROM account a
LEFT JOIN account pa ON a.parentaccountid = pa.accountid
JOIN customeraddress ca ON a.accountid = ca.parentid
JOIN optionsetmetadata osm ON osm.entityname = 'customeraddress'
AND osm.optionsetname = 'addresstypecode'
AND ca.addresstypecode = osm.[option]
LEFT JOIN sik_warehouse w ON a.sik_warehouseid = w.sik_warehouseid
LEFT JOIN sik_deliverymethods dm ON a.sik_deliverymethodid = dm.sik_deliverymethodsid
JOIN shiptorows ON shiptorows.ca1id = ca.customeraddressid
WHERE a.accountnumber IN ('002-100455', '58398',
'4350',
'5076',
'4310',
'15847')
AND addresstypecode = 2
AND ca.jms_status = 100000000
AND rn > 1
ORDER BY [accountnumber],
ca.addressnumber ASC