1

我的结果显示了我不想要的重复项。我们有一个列调用地址类型,它根据在 db 中输入的内容返回 B 或 L。如果选择 B,则输入数据是不正确的,因为这既是交货地址又是法定地址。

提取数据时,我得到序列号等,但我得到了两次……那些在 B 和 L 中都有地址数据的。

这是我的查询 - 如何使双行不显示?

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
4

3 回答 3

1

addresstype根据您的评论,由于您要为 each 选择哪个值并不重要,请locationinfoid使用 a GROUP BY locationinfoid, serialnumwith MAX

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

于 2013-03-15T15:10:06.647 回答
0

你有很多选择,

  • 从结果中排除地址类型并使用 distinct
  • 使用聚合函数并按函数分组
  • 知道重复项的子句的交叉并集

如果您不想获取地址类型并且想知道重复项,我会以这种方式重写查询:

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

如果您只想获取它们,请不要使用 distinct 在您的选择中包含 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 这样的聚合函数,这样您就不需要使用不同的分句

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
于 2013-03-15T15:14:06.367 回答
0

Distinct 返回选择列表中包含的任何列的任何不同记录。所以如果你有不同的值addressinfo.locationinfoidserialnum或者addressinfo.addresstype你会得到重复的记录

在这种情况下,看起来您正在提取两个不同addresstypes的记录,因此如果 alocationinfoidserialnumpair 各有一个,您将获得 2 个“重复”记录。您对包含返回值的编辑证实了这一点。

如果您不关心,则不要将其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
于 2013-03-15T15:08:22.453 回答