需要返回找到的第一个实例,而不是全部

时间:2017-08-03 23:07:23

标签: sql-server

我有一个查询,我在过去几年中做了很多事情,这些查询严重依赖用户以一种正确的顺序输入数据(我知道,第一个错误)。

这个想法是用户在前7行中输入一个CRT类别的项目......只有一个。有时可能会有其他类别,但我只想要第一个。现在,用户有时会在前7行之后输入项目...导致另一个问题。这是我原来的疑问:

Select distinct
        d.Job_Number as Rig,
    case d.Customer_Name
        when 'case' then 'diff case'
        else '******'
    end as Parent_Customer,
        d.Customer_Name,
    case d.Reference_Location2
        when 'case' then 'diff case'
        else '******'
    end as Size,
    case d.Office_code
        when 'case' then 'diff case'
        else '****'
    end as Office,
        d.Rental_Ticket,
    case d.job_type
        when 'case' then 'diff case'
        else '0'
    end as Billable,
    d.Reference_Location5 as TT

from HP_View_DEL_Ticket_Header_Master as d
left join CSView_INVC_Header_Master as i --left join to report jobs that have not been invoiced
on d.Rental_Ticket = i.Rental_Ticket_or_Tag_Number
join CSView_DEL_Ticket_Lines_Master as l
on d.Rental_Ticket = l.Rental_Ticket
where (d.Ticket_Month between 7 and 7 and d.Ticket_Year = 2017)-- or (d.Ticket_Month between 12 and 12 and d.Ticket_Year = 2016))

and d.Rental_Ticket not in (select dticket from deltick_void)
and d.Ticket_Type = 'D'
and d.Posted_Flag = -1
and l.Row_Counter between 1 and 7 --Currently how I get the correct record, problem is its sometimes not entered in the first 7 rows.
and l.Category_Code = 'CRT'
order by Parent_Customer

我需要一个Ticket号码,一个CRT项目(CRT第一项)。我已经尝试在where子句中做一些事情来找到顶级的CRT项目但是没有像我想象的那样工作。

任何帮助都会很棒。

由于 BD

编辑: 预期的输出

Rig Parent_Customer Customer_Name   Size    Office  Rental_Ticket   Billable    TT
RIG 642 A   A   5 to 5.5    City    12627   0   YES
RIG 525 B   B   5 1/2   City 2  12628   0   YES
RIG 603 C   C   9 5/8   City 3  12634   0   NO

OUTPUT:

不正确
Rig Parent_Customer Customer_Name   Size    Office  Rental_Ticket   Billable    TT
RIG 642 A   A   5 to 5.5    City    12627   0   YES
RIG 642 A   A   5 to 5.5    City    12627   0   YES

因为查询找到了多个CRT'在Rental Ticket上,它返回两个与我选择的列相同的记录。我只想使用它找到的第一个。

0 个答案:

没有答案