简体   繁体   English

SQL Server:仅当一列的计数大于另一列的计数时才返回数据

[英]SQL Server: only return data if count of one column is greater than another

What I want to do: 我想做的事:

If the total number of occurrences of a serial number in table OITM ( OITM.AssetSerNo ) is greater than the total number of occurrences of the same serial number in RDN1 ( RDN1.SerialNum ), then I want to return a few fields from the OITM record with the matching serial number where the Asset Number OITM.ItemCode (PKey) is greatest. 如果表中的序列号出现的总次数OITMOITM.AssetSerNo )比相同序列号的出现的总数越大RDN1RDN1.SerialNum ),那么我想从返回几场OITM使用资产编号OITM.ItemCode (PKey)最大的匹配序列号进行记录。

The code I have is as follows 我的代码如下

select 

oitm.itemcode as 'Asset No', 
oitm.itemname as 'Asset Description',
oitm.assetSerNo as 'Serial No'

from oitm
inner join rdn1 on oitm.assetserno = rdn1.serialnum

WHERE
OITM.itemtype = 'F'
and OITM.asststatus = 'A'

HAVING count(oitm.assetserno)>count(rdn1.serialnum)

Unfortunately, I'm getting an error because the majority of my items are neither in a group by or an aggregate function. 不幸的是,我遇到了错误,因为我的大部分商品都不在group by中或不在汇总函数中。 I'm not even sure the HAVING clause is the best way to approach the problem (in fact, I'm fairly sure that it's not). 我什至不确定HAVING子句是解决该问题的最佳方法(实际上,我很确定不是)。

EDIT: Sample data (I guess?) 编辑:样本数据(我猜是?)

OITM OITM

ItemCode        ItemName        AssetSerNo        ItemType       AsstStatus
123             Object 1        QW                F              I
234             Object 2        ER                F              A
345             Object 3        RT                F              I
456             Object 4        TY                F              A
567             Object 1        QW                F              I
678             Object 5        YU                F              I
789             Object 3        RT                F              A
890             Object 1        QW                F              A
901             Object 2        UI                F              A

This is an item master list data record. 这是项目主清单数据记录。 A = active, I = inactive. A =有效,I =无效。

RDN1 RDN1

DocID           Object Name     Serial Num 
1               Object 1        QW
2               Object 3        RT
3               Object 1        QW
4               Object 5        YU
5               Object 4        TY
6               Object 3        RT

This is a list of return records, for when the rented items return to the warehouse. 这是退货记录的列表,用于租借物品何时返回仓库。

Expected output: 预期产量:

ItemCode        ItemName        AssetSerNo 
789             Object 3        RT
456             Object 4        TY

I need a list of all items where a return has been created, but the item has not been made inactive yet. 我需要创建退货的所有项目的列表,但该项目尚未变为非活动状态。 I can't just do an inner join on RDN1 because I may have items that are returned, the item record is changed to I, but then it's sent back out and a new item record is created. 我不能仅仅在RDN1上进行内部联接,因为我可能有返回的项目,项目记录更改为I,然后又将其发送回并创建了新的项目记录。

20 different ways to do this and they are all right. 20种不同的方法可以做到这一点。 Here is the way using most of your code and implementing an EXISTS() command. 这是使用大多数代码并实现EXISTS()命令的方法。

select 
 oitm.itemcode as 'Asset No', 
 oitm.itemname as 'Asset Description',
 oitm.assetSerNo as 'Serial No'
from oitm x
WHERE
 EXISTS(
  select TOP 1 1 
  from oitm y
   inner join rdn1 z on y.assetserno = z.serialnum
  WHERE
   y.itemtype = 'F'
   and y.asststatus = 'A'
   and y.assetSerNo = x.assetSerNo 
  HAVING count(y.assetserno)>count(z.serialnum)
)

here's an option using some sub queries 这是使用一些子查询的选项

SELECT 
    item.itemcode AS 'Asset No',
    item.itemname AS 'Asset Description',
    item.assetSerNo AS 'Serial No'
FROM
(
    SELECT
        oitm.itemcode,
        oitm.itemname,
        oitm.assetSerNo,
        COUNT() OVER (PARTITION BY assetSerNo) assetSerNoCount,
        ROW_NUMBER() OVER (PARTITION BY assetSerNo ORDER BY oitm.itemcode DESC) rn
    FROM
        oitm
    WHERE
        OITM.itemtype = 'F'
        AND OITM.asststatus = 'A'
) item
LEFT JOIN (
    SELECT 
        serialnum, COUNT(*) serialnumCount 
    FROM 
        rdn1 
    GROUP BY 
    serialnum
) serial ON serial.serialnum = item.assetSerNo
WHERE
    ISNULL(serial.serialnumCount,0) < item.assetSerNoCount
    AND item.rn = 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM