我如何在最小行中获得DISTINCT值? SQL

时间:2016-11-07 12:04:19

标签: sql sql-server

我有表Orders_Header,Order_Sequence,Orders_Samples有值。我想从3个表中获取唯一值:

我在Orders_Header中有一行:

enter image description here

我在Orders_Sequence中有两行: enter image description here

我在Orders_Samples中有4行:

enter image description here

我的Sql代码是:

SELECT orders_header.plant, 
       orders_header.shortyear, 
       orders_header.orderno, 
       orders_header.orderdate, 
       orders_header.closingdate, 
       orders_header.requester, 
       orders_header.projectnumber, 
       orders_header.costcenter, 
       orders_header.batchnumber, 
       orders_header.partname, 
       orders_header.partnumber, 
       orders_header.costcategory, 
       orders_header.status, 
       orders_header.overallproductrating, 
       orders_sequence.activitynumber, 
       orders_sequence.dimension, 
       orders_sequence.description, 
       orders_sequence.activityname, 
       orders_sequence.activityresource, 
       orders_sequence.requirementnom, 
       orders_sequence.requirementplus, 
       orders_sequence.requirementminus, 
       orders_sequence.units, 
       orders_samples.samplenumber, 
       orders_samples.samplevalue 
FROM   orders_header 
       INNER JOIN orders_sequence 
               ON orders_header.orderid = orders_sequence.orderid 
       INNER JOIN orders_samples 
               ON orders_samples.orderid = orders_sequence.orderid 
WHERE  ( orders_header.orderno = 79148 ) 
       AND ( orders_header.shortyear = 16 ) 

为什么我得到8行而不是4行,我想要唯一的行。我在Orders_Samples中有4行,我希望最后4行包含所有日期。

我尝试使用不同但不起作用

注意*我尝试使用DISTINCT但不起作用

由于

3 个答案:

答案 0 :(得分:0)

怎么样:

select orders_header.plant, ...

FROM   orders_header

inner join (select distinct orderid from orders_sequence) ord_sequence
on orders_header.orderid = ord_sequence.orderid 
inner join orders_samples ON orders_samples.orderid = ord_sequence.orderid

答案 1 :(得分:0)

试试这个,

SELECT orders_header.plant, 
   orders_header.shortyear, 
   orders_header.orderno, 
   orders_header.orderdate, 
   orders_header.closingdate, 
   orders_header.requester, 
   orders_header.projectnumber, 
   orders_header.costcenter, 
   orders_header.batchnumber, 
   orders_header.partname, 
   orders_header.partnumber, 
   orders_header.costcategory, 
   orders_header.status, 
   orders_header.overallproductrating, 
   orders_sequence.activitynumber, 
   orders_sequence.dimension, 
   orders_sequence.description, 
   orders_sequence.activityname, 
   orders_sequence.activityresource, 
   orders_sequence.requirementnom, 
   orders_sequence.requirementplus, 
   orders_sequence.requirementminus, 
   orders_sequence.units, 
   orders_samples.samplenumber, 
   orders_samples.samplevalue 
FROM   orders_samples
   LEFT JOIN orders_sequence 
           ON orders_samples.orderid = orders_sequence.orderid 
   LEFT JOIN orders_header  
           ON orders_header.orderid = orders_sequence.orderid 
WHERE  ( orders_header.orderno = 79148 ) 
   AND ( orders_header.shortyear = 16 ) 

答案 2 :(得分:0)

从这个最小查询开始并添加字段,直到找到导致重复的字段。

SELECT
  oh.order_id 
  osam.samplenumber, 
  osam.samplevalue,
  oseq.activitynumber
FROM orders_samples osam
  INNER JOIN orders_header oh
    ON osam.order_id = oh.order_id
  LEFT JOIN orders_sequence oseq
    ON oh.order_id = oseq.order_id
WHERE  ( oh.orderno = 79148 ) 
 AND ( oh.shortyear = 16 )