Join with Distinct
  (5-replies, Power system - SQL (general/non-embedded))
Post your reply | Return to Forum  Refresh | ascending | descending
Author: SteveCCNJ Return to Forum  Refresh 2010-07-28 08.32.59
In a programmer frame of mind it may seem right to get the headers you want 
and bring in the detail.  In an SQL data set frame of mind it is more correct 
to use the detail as the driver and bring in the related header.  It may seem 
like a picky point, but there is frequently a different mind set needed in SQL 
programming, because it works on returning entire data sets, rather than 
reading row by row as a program would.  If an HLL programmer only uses SQL to 
declare cursors for single row fetch, there may not really be much of a 
difference.  But if you start doing real SQL programming, not wrapped in an 
HLL sandbox, it can make a great deal of difference.  I have seen a lot of bad 
SQL programming from HLL programmers who don't seem to see the difference.
Author: TDaly Return to Forum  Refresh 2010-07-27 16.01.06
DISTINCT seems to have no effect.  At least when JOIN is involve.d  Thanks for 
the ideas.
Author: Muehe Return to Forum  Refresh 2010-07-27 15.02.51
I think it is interrupting it as a left outer.
try,

SELECT DISTINCT HEADER.SALESID, DETAIL.SALESID 
  FROM SALES AS HEADER 
  JOIN SALESLINE AS DETAIL 
         ON HEADER.SALESID = DETAIL.SALESID 
 WHERE HEADER.SALESID = '654444' 


Of course that will only produce 1 record.
654444  654444
Because they are equal and any other matches would be identical

unless you meant to do a right outer, which would give you every detail -- i 
hate right, make it a left

SELECT DISTINCT HEADER.SALESID, DETAIL.SALESID 
  FROM SALESLINE AS DETAIL  
  LEFT OUTER JOIN  SALES AS HEADER
         ON HEADER.SALESID = DETAIL.SALESID 
 WHERE DETAIL.SALESID = '654444'
Author: SteveCCNJ Return to Forum  Refresh 2010-07-27 14.11.23
An inner join will do what you want as long as the Detail table is the driver 
table and there is only 1 Header row for each SALESID.

SELECT DISTINCT 
       HEADER.SALESID, 
       DETAIL.SALESID 
  FROM SALESLINE AS DETAIL ,
       SALES     AS HEADER 
 WHERE HEADER.SALESID = DETAIL.SALESID 
 AND   HEADER.SALESID = '654444'
Author: Captndjc Return to Forum  Refresh 2010-07-27 13.04.25
WITH CT AS (SELECT DISTINCT SALESID
                   FROM SALES 
                   GROUP BY SALESID)
  SELECT CT.SALESID, DETAIL.SALESID
     FROM CT LEFT JOIN SALESLINE AS DETAIL
             ON CT.SALESID = DETAIL.SALESID
     WHERE CT.SALESID = '654444'

CT is a Common Table Expressions.
Author: TDaly Return to Forum  Refresh 2010-07-27 11.27.08
Is there some limitation on usnig Join and Distinct?  I have a header & detail 
records. What I want is a single line for each detail record but also include 
fields from the header.  The output I get though repeats the result set for 
however many header records there are.


SELECT DISTINCT HEADER.SALESID, DETAIL.SALESID 
  FROM SALES AS HEADER 
  LEFT JOIN SALESLINE AS DETAIL 
         ON HEADER.SALESID = DETAIL.SALESID 
 WHERE HEADER.SALESID = '654444' 


I'd do it in RPG but the user wants to use their sql query tool.
Your reply | top | Return to Forum | ascending | descending
      Name:
    
By pressing you agree to the
forum guidelines
      Note: Your message is limited to 3500 characters.