0

我有以下行:

 ID             fk_id       type    comment user    ticket
-----------------------------------------------------------
000000658       135         notes   afdads  abas1   0000000000
000000658       999999      admin           NULL    0000000000
000000659       136         notes   afadsf  admin   0000000001
000000659       999999      admin           NULL    0000000001
000000660       999999      admin           NULL    0000000000
000000661       999999      admin           NULL    0000000006

我想返回 ID 为 000000658 的行,仅返回用户不为空的单个结果,但相同的结果还应返回 ID = 000000659 的用户为空,如下所示:

 ID             fk_id       type    comment user    ticket
-----------------------------------------------------------
000000658       135         notes   fdads   abas1   0000000000
000000659       999999      admin           NULL    0000000001
000000660       999999      admin           NULL    0000000000
000000661       999999      admin           NULL    0000000006

我的案例是在仪表板上显示所有投诉并向用户显示他们自己的评论。

我的案例是:我有两个表投诉和投诉详细信息,无论登录到我的页面,我都必须显示所有投诉,但只向用户显示他在票号上输入的评论。为此,我查看了查询的位置:select * from (

SELECT 
            COMPLAINT.COMP_TICKET_NUM,
            999999 COMPLAINT_DETAIL_ID ,
            'admin' FLAG,--TO be discuss
            ' ' NOTES,
            null LOGIN_USER,
            CURRENT_STATE, CURRENT_ACTOR,CALCULATED_ACTOR,CURRENT_ORG_UNIT--,
            --TEMP_FLAG
FROM         dbo.COMPLAINT 
left outer join COMPLAINT_DETAIL on COMPLAINT_DETAIL.COMP_TICKET_NUM = COMPLAINT.COMP_TICKET_NUM 
--where COMPLAINT_DETAIL.LOGIN_USER   in ('admin')
union
SELECT 
            COMPLAINT.COMP_TICKET_NUM,
            COMPLAINT_DETAIL.COMPLAINT_DETAIL_ID,
            isnull(COMPLAINT_DETAIL.FLAG,'admin') FLAG,--TO be discuss
            COMPLAINT_DETAIL.NOTES,
            COMPLAINT_DETAIL.LOGIN_USER,

        CURRENT_STATE, CURRENT_ACTOR,CALCULATED_ACTOR,CURRENT_ORG_UNIT--,
            --TEMP_FLAG
FROM         dbo.COMPLAINT 

inner join  COMPLAINT_DETAIL 
on COMPLAINT_DETAIL.COMP_TICKET_NUM =COMPLAINT.COMP_TICKET_NUM
4

4 回答 4

1

DISTINCT 将为您提供 ID。这是一个你可以尝试的想法:

 SELECT DISTINCT(ID), othercolumns
    FROM
    (
        SELECT DISTINCT(ID), othercolumns
        FROM TABLE_NAME
        WHERE user IS NOT NULL -- All distinct ids where user not null
        UNION
        SELECT DISTINCT(ID), othercolumns
        FROM TABLE_NAME
        WHERE user IS NULL -- All distinct ids where user IS null
    )
于 2013-08-05T05:17:44.877 回答
0

I think What you need is roqw level access, this can be achived as below. IF you are using it from front end, then you must be asking a user to log in. What i am saying is you have user name. Then thing is you need to Identify which record will be accessed from a perticular user. You can include two columns. Defining rights of that user on perticular row and rights of other users. All those records where user is not the current user and comment is null are available for every one. AND Where comment is not null its available for that user only.

This required more complex work, I am trying to give you an idea. please update me if more details required.

Regards

Ashutosh Arya

于 2013-08-05T05:40:22.990 回答
0

简单的答案是这样的:

SELECT * 
FROM   TABLE1 
WHERE  ( ID = '000000658' 
         AND USER IS NOT NULL ) 
        OR ( ID = '000000659' 
             AND USER IS NULL ) 

但我有一种感觉,你想要更复杂的东西。
只要让我知道您还想要什么,我会添加它:-)

编辑 经过一些澄清后,这是一个更好的解决方案:

DECLARE @curr_user VARCHAR(20) = '000000658' 

SELECT ID, 
       FK_ID, 
       TYPE, 
       CASE 
         WHEN @curr_user = USER THEN COMMENT 
         ELSE '' 
       END comment, 
       USER, 
       TICKET 
FROM   TABLE1 

根据要求,当前用户(其 ID 是在参数中传递的)可以看到所有的投诉,但只能看到他自己的评论。
如果您需要添加更多详细信息,请告诉我。

于 2013-08-05T05:08:42.763 回答
0

你可以试试

Select * From TableName Where User='abas1' or User is null
于 2013-08-05T05:11:04.057 回答