我的结果显示了我不想要的重复项。我们有一个列调用addresstypes,它返回一个B或L,具体取决于在db中输入的内容。如果选择B,则输入数据不正确,因为这是交付和合法地址。
在提取数据时,我得到了序列号等,但我得到了两次......那些在B& B中都有地址数据的数据。升。
这是我的查询 - 如何才能显示双行?
USE inventory
SELECT distinct
dbo.addressinfo.locationinfoid, dbo.equipmentlocationscurrent.serialnum, dbo.addressinfo.addresstype
FROM dbo.equipmentlocationscurrent
full join dbo.addressinfo
on dbo.equipmentlocationscurrent.locationinfoid = dbo.addressinfo.locationinfoid
where (clientName = 'cps lease')
and (locationtype = 'merchant')
and (addresstype = 'b' or addresstype = 'l')
order by serialnum
结果样本
locationinfoid serialnum
2887540 301-252-800 B
2887540 301-252-800 L
答案 0 :(得分:1)
根据您的评论,因为您要为每个addresstype
选择locationinfoid
的哪个值无关紧要,请使用GROUP BY locationinfoid, serialnum
与MAX
:< / p>
SELECT
a.locationinfoid,
e.serialnum,
MAX(a.addresstype)
FROM dbo.equipmentlocationscurrent AS e
full join dbo.addressinfo AS a on e.locationinfoid = a.locationinfoid
where clientName = 'cps lease'
and locationtype = 'merchant'
and addresstype = 'b' or addresstype = 'l'
GROUP BY a.locationinfoid, e.serialnum
order by serialnum;
这将为您提供locationinfoid
的明确值。
答案 1 :(得分:0)
Distinct返回选择列表中包含的任何列的所有不同记录。因此,如果addressinfo.locationinfoid
,serialnum
或addressinfo.addresstype
的值不同,那么您将获得重复记录
在这种情况下,如果addresstypes
和locationinfoid
对中的每一个都有serialnum
和addresstype
对,那么您可能会看到两个不同的USE inventory
SELECT distinct
dbo.addressinfo.locationinfoid, dbo.equipmentlocationscurrent.serialnum
FROM dbo.equipmentlocationscurrent
full join dbo.addressinfo
on dbo.equipmentlocationscurrent.locationinfoid = dbo.addressinfo.locationinfoid
where (clientName = 'cps lease')
and (locationtype = 'merchant')
and (addresstype = 'b' or addresstype = 'l')
order by serialnum
,因此您将获得2个“重复”记录。包含返回值的编辑确认了这一点。
如果您不关心{{1}},请不要将其包含在选择列表中。尝试:
{{1}}
答案 2 :(得分:0)
你有很多选择,
如果您不想获取地址类型并且想知道重复项,我会以这种方式重写查询:
SELECT A.locationinfoid, E.serialnum
FROM dbo.equipmentlocationscurrent E
inner join dbo.addressinfo A
on E.locationinfoid = A.locationinfoid
where (clientName = 'cps lease')
and (locationtype = 'merchant')
and (addresstype = 'b')
order by serialnum
INTERSECT
SELECT A.locationinfoid, E.serialnum
FROM dbo.equipmentlocationscurrent E
inner join dbo.addressinfo A
on E.locationinfoid = A.locationinfoid
where (clientName = 'cps lease')
and (locationtype = 'merchant')
and (addresstype = 'L')
order by serialnum
如果您只想获取它们,则不要在您的选择中使用不同的
包含AddressType SELECT DISTINCT A.locationinfoid, E.serialnum
FROM dbo.equipmentlocationscurrent E
FULL join dbo.addressinfo A
on E.locationinfoid = A.locationinfoid
where (clientName = 'cps lease')
and (locationtype = 'merchant')
and (addresstype IN('b', 'l'))
order by serialnum
你也可以使用像MAX或MIN这样的聚合函数给你的clausule,这样你就不需要使用你的独特clausule
SELECT A.locationinfoid, E.serialnum, MAX(A.addresstype ) AS addresstype
FROM dbo.equipmentlocationscurrent E
FULL join dbo.addressinfo A
on E.locationinfoid = A.locationinfoid
where (clientName = 'cps lease')
and (locationtype = 'merchant')
and (addresstype IN('b', 'l'))
Group by A.locationinfoid, E.serialnum
order by serialnum