每个用户选择1个随机项。我的查询中包含许多表

时间:2015-09-08 18:14:53

标签: sql sql-server random sql-server-2012

我已经看到了几种使用多种方法从表中选择随机记录的方法。但是,我的需要我肯定在这里,我只是找不到它。我有一个使用几个表的查询。我的最终目标是为每个返回的用户提供一条随机记录。

在我的结果集中,我获得了用户和他们拥有的工作项。我需要的是每个用户只返回一个随机工作项。这是我陷入困境的地方。非常感谢ANy的帮助。

这是我的代码。我需要的是每个用户1个随机的C.credentilaing_k。我正在使用SQL 2012。

select 
    U.FULLNAME as 'Chg_By',
    CONVERT(DATE,AL.AUDITDATETIME) as 'DE_Date',
    P.ID,
    P.LONGNAME,
    CONVERT(DATE,P.DATEOFBIRTH) as 'DOB',
    C.CREDENTIALING_K,
    C.entity_k,
    R.DESCRIPTION as 'CVI_TYPE',
    CG.GROUPDESCRIPTION,
    C.APPLICATION_RECEIVED,
    R1.DESCRIPTION as 'Cur_STATUS',
    CONVERT(DATE,C.USERDEF_D3) as 'MSO_DUE_DT'
from
    VisualCACTUS.AUDITLOG AL
        JOIN VisualCACTUS.USERS U
        on U.user_k = AL.USER_K
        join VisualCACTUS.CREDENTIALING C
            JOIN VisualCACTUS.PROVIDERS P
            on P.provider_k = C.PROVIDER_K
            JOIN visualcactus.CREDENTIALINGGROUP CG
            on CG.CREDENTIALINGGROUP_K = C.CREDENTIALINGGROUP_K
            JOIN VisualCACTUS.REFTABLE R
            on R.reftable_k = C.TYPE_RTK
            JOIN VisualCACTUS.REFTABLE R1
            ON R1.REFTABLE_K = C.CREDENTIALINGSTATUS_RTK
        on C.CREDENTIALING_K = AL.FILE_PRIMARYKEY
where
    AUDITLOG_K in (select AUDITLOG_K from VisualCACTUS.AUDITLOG_RECORDLEVEL where TABLE_NAME = 'CREDENTIALING                 ' 
                    and
                    AUDITLOG_RECORDLEVEL_K in (SELECT AUDITLOG_RECORDLEVEL_K from VisualCACTUS.AUDITLOG_FIELDLEVEL where NEWVALUE_SHORT = 'D2LC0YSXXW'))
    and
    CONVERT(DATE, AUDITDATETIME) = DATEADD(day, -1, convert(date, GETDATE()))

1 个答案:

答案 0 :(得分:0)

获取单个用户+凭据记录的最简单方法是进行子选择并加入其中。 NEWID的使用为行编号创建了一个随机ID。分区通过使编号在用户ID之间重新启动。最后,连接中的Row = 1应该意味着每个用户只能获得1条记录。

select 
    rec2get.FULLNAME as 'Chg_By',
    CONVERT(DATE,AL.AUDITDATETIME) as 'DE_Date',
    rec2get.ID,
    rec2get.LONGNAME,
    rec2get.DOB,
    rec2get.CREDENTIALING_K,
    rec2get.entity_k,
    rec2get.CVI_TYPE,
    rec2get.GROUPDESCRIPTION,
    rec2get.APPLICATION_RECEIVED,
    rec2get.Cur_STATUS,
    rec2get.MSO_DUE_DT
from VisualCACTUS.AUDITLOG AL
JOIN (
    SELECT
        Row_Number() OVER (PARTITION BY u.user_k ORDER BY NEWID() ASC) [Row],
        u.user_k,
        u.FULLNAME,
        c.CREDENTIALING_K,
        c.entity_k,
        c.APPLICATION_RECEIVED,
        c.USERDEF_D3  as 'MSO_DUE_DT',
        P.ID,
        CONVERT(DATE,P.DATEOFBIRTH) as 'DOB',
        P.LONGNAME,
        R.DESCRIPTION as 'CVI_TYPE',
        CG.GROUPDESCRIPTION,
        R1.DESCRIPTION as 'Cur_STATUS'
    FROM VisualCACTUS.AUDITLOG AL1
    JOIN VisualCACTUS.USERS U on U.user_k = AL1.USER_K
    join VisualCACTUS.CREDENTIALING C on C.CREDENTIALING_K = AL1.FILE_PRIMARYKEY
    JOIN VisualCACTUS.PROVIDERS P on P.provider_k = C.PROVIDER_K
    JOIN visualcactus.CREDENTIALINGGROUP CG on CG.CREDENTIALINGGROUP_K = C.CREDENTIALINGGROUP_K
    JOIN VisualCACTUS.REFTABLE R on R.reftable_k = C.TYPE_RTK
    JOIN VisualCACTUS.REFTABLE R1 ON R1.REFTABLE_K = C.CREDENTIALINGSTATUS_RTK
) rec2get on rec2get.user_k = AL.USER_K
    AND rec2get.CREDENTIALING_K=AL.FILE_PRIMARYKEY
    AND rec2get.[Row]=1
where
AL.AUDITLOG_K in (
    select AUDITLOG_K
    from VisualCACTUS.AUDITLOG_RECORDLEVEL
    where TABLE_NAME = 'CREDENTIALING                 ' 
    and
    AUDITLOG_RECORDLEVEL_K in (
        SELECT AUDITLOG_RECORDLEVEL_K
        from VisualCACTUS.AUDITLOG_FIELDLEVEL
        where NEWVALUE_SHORT = 'D2LC0YSXXW'
    )
)
and
CONVERT(DATE, AL.AUDITDATETIME) = DATEADD(day, -1, convert(date, GETDATE()))