• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 557
  • Last Modified:

T-SQL Cannot Use DISTINCT for Some Reason

Hello:

Below is T-SQL programming that I'm using, and this is working.  

The issue is that I want to place "DISTINCT" after "select", so that the "Item" field will be unique for every "VPAorDocNo" field.  In other words, I'm trying to eliminate duplicate item numbers.  There's only one "Item" for every "VPAorDocNo".

When I put in the word "DISTINCT", though, SQL gives me the following error upon hitting "Execute":  The text data type cannot be selected as DISTINCT because it is not comparable.

How do I eliminate this error?  

Thanks!  Very much appreciated!

TBSupport


select DTZ..SOP30300.ITEMNMBR AS [Item], 
DTZ..IV00101.ITEMDESC as [ItemDesc], DTZ..SOP30300.LOCNCODE AS [Site],
0 as [QtyAdjusted], 0 as [StockCountVariance], 0 as [QtyTransferred],
0 as [QtyRecd], 0 as [QtyReturned], DTZ..SOP30300.QTYPRINV * -1 as [QtySold],  
DTZ..SOP30300.FUFILDAT as [Date], DTZ..SOP30200.CSTPONBR as [VPAorDocNo], DTZ..IV00102.QTYONHND AS [QtyOnHand], SY03900.TXTFIELD AS [Comments]
--, --'' as [TransferFromReason], --'' as [TransferToReason],
--COALESCE (DTZ..SOP10106.CMMTTEXT, '') as [SalesComment]
from DTZ..SOP30300 
INNER JOIN DTZ..SOP30200 on 
DTZ..SOP30300.SOPTYPE = DTZ..SOP30200.SOPTYPE and
DTZ..SOP30300.SOPNUMBE = DTZ..SOP30200.SOPNUMBE
INNER JOIN DTZ..IV00101 ON DTZ..SOP30300.ITEMNMBR = DTZ..IV00101.ITEMNMBR
INNER JOIN DTZ..IV00102 ON DTZ..IV00101.ITEMNMBR = DTZ..IV00102.ITEMNMBR
LEFT OUTER JOIN DTZ..SOP10106 on 
DTZ..SOP30200.SOPTYPE = DTZ..SOP10106.SOPTYPE and
DTZ..SOP30200.SOPNUMBE = DTZ..SOP10106.SOPNUMBE
LEFT OUTER JOIN DTZ..SY03900 ON DTZ..SOP30200.NOTEINDX = SY03900.NOTEINDX
INNER JOIN vShip..SHP_ContainerItem on
DTZ..SOP30300.ITEMNMBR = vShip..SHP_ContainerItem.CoreItem and
DTZ..SOP30300.SOPNUMBE = vShip..SHP_ContainerItem.DocumentNumber
INNER JOIN vShip..SHP_Container on
vShip..SHP_ContainerItem.SHP_Container_ID = vShip..SHP_Container.SHP_Container_ID
WHERE (DTZ..SOP30300.SOPTYPE = '2'
AND DTZ..SOP30300.QTYPRINV  > 0
AND DTZ..SOP30200.CSTPONBR LIKE 'VPA%'
and DTZ..IV00101.ITMGEDSC LIKE '%OCE%'
AND DTZ..IV00102.LOCNCODE = ''
AND DTZ..SOP30200.CSTPONBR = 'VPA59282')

Open in new window

0
TBSupport
Asked:
TBSupport
1 Solution
 
Kevin CrossChief Technology OfficerCommented:
Hi.

Can you eliminate the column using the TEXT data type from the query?

Although, a better solution here is to identify the source of the duplication. i.e., which JOIN causes the multiple matches (or duplication of the same match)? If you know the JOIN, you can turn that portion into a derived table that aggregates or otherwise creates a unique match without having to complicate the outer query -- at least that is the hope. The data types of columns involved may determine that more than anything.

A sample of the data that is duplicate and the expected results may help.

Kevin
0
 
Daniel WilsonCommented:
Does casting the text field as nvarchar(max) solve it for you?
0
 
PortletPaulCommented:
It's most likely to be that comments field that you need to cast to nvarchar, and to be honest I wouldn't recommend using distinct on such a field in any case. It may not even achieve what you are after.

Imagine you have one "Item"=1 for a specific "VPAorDocNo"=X, but these are the available comments:

X, 1, Hello, yes that's fine
X, 1, Roger that, deliver next Monday
X, 1, Ok, no problem

each of these rows "distinct" (distinct works across the whole row!)

In circumstance such as this a more likely scenario is to get only "the most recent" comment, and report only that. (there are other approaches, this is a common one)

There are quite possibly other sources of repeating Items in the overall query, but you may need to adopt a different approach to the comments.
SELECT
      DTZ..SOP30300.ITEMNMBR AS [Item]
    , DTZ..IV00101.ITEMDESC AS [ItemDesc]
    , DTZ..SOP30300.LOCNCODE AS [Site]
    , 0 AS [QtyAdjusted]
    , 0 AS [StockCountVariance]
    , 0 AS [QtyTransferred]
    , 0 AS [QtyRecd]
    , 0 AS [QtyReturned]
    , DTZ..SOP30300.QTYPRINV * - 1 AS [QtySold]
    , DTZ..SOP30300.FUFILDAT AS [Date]
    , DTZ..SOP30200.CSTPONBR AS [VPAorDocNo]
    , DTZ..IV00102.QTYONHND AS [QtyOnHand]

-- this is likley to the problem    
--    , SY03900.TXTFIELD AS [Comments]

      , notes.comments
      
--, --'' as [TransferFromReason], --'' as [TransferToReason],
--COALESCE (DTZ..SOP10106.CMMTTEXT, '') as [SalesComment]
FROM DTZ..SOP30300
INNER JOIN DTZ..SOP30200 ON DTZ..SOP30300.SOPTYPE = DTZ..SOP30200.SOPTYPE
    AND DTZ..SOP30300.SOPNUMBE = DTZ..SOP30200.SOPNUMBE
INNER JOIN DTZ..IV00101 ON DTZ..SOP30300.ITEMNMBR = DTZ..IV00101.ITEMNMBR
INNER JOIN DTZ..IV00102 ON DTZ..IV00101.ITEMNMBR = DTZ..IV00102.ITEMNMBR
LEFT JOIN DTZ..SOP10106 ON DTZ..SOP30200.SOPTYPE = DTZ..SOP10106.SOPTYPE
    AND DTZ..SOP30200.SOPNUMBE = DTZ..SOP10106.SOPNUMBE

-- and the problem comments file is from here
-- LEFT JOIN DTZ..SY03900 ON DTZ..SOP30200.NOTEINDX = SY03900.NOTEINDX

-- instead MAYBE something like this...
left join (
            select noteindex, cast(TXTFIELD as nvarchar(max)) as comments
            , row_number() over (partition by <<something>> order by <<a-date>> DESC) as row_ref
            from DTZ..SY03900
          ) as nnotes ON DTZ..SOP30200.NOTEINDX = notes.noteindex and row_ref=1

INNER JOIN vShip..SHP_ContainerItem ON DTZ..SOP30300.ITEMNMBR = vShip..SHP_ContainerItem.CoreItem
    AND DTZ..SOP30300.SOPNUMBE = vShip..SHP_ContainerItem.DocumentNumber
INNER JOIN vShip..SHP_Container ON vShip..SHP_ContainerItem.SHP_Container_ID = vShip..SHP_Container.SHP_Container_ID
WHERE (
        DTZ..SOP30300.SOPTYPE = '2'
        AND DTZ..SOP30300.QTYPRINV > 0
        AND DTZ..SOP30200.CSTPONBR LIKE 'VPA%'
        AND DTZ..IV00101.ITMGEDSC LIKE '%OCE%'
        AND DTZ..IV00102.LOCNCODE = ''
        AND DTZ..SOP30200.CSTPONBR = 'VPA59282'
        )

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
PortletPaulCommented:
btw, as the comments are tied to DTZ..SOP30200.NOTEINDX, that table [DTZ..SOP30200] is probably causing repitition, so a subquery that reduces both that table and the comments to a single row per item might help solve the overall need for "distinct".
0
 
TBSupportAuthor Commented:
Good Morning:

I see what you all are saying about the TXTFIELD.  But, I need to have the "repetition" of the comments field.  I can't just have only one comment per SOP30200.SOPNUMBER.  

Actually, Daniel Wilson's suggestion on using cast(nvarchar(max)) was the solution.  More specifically, I used a variation of the solution as follows:

convert(varchar(max), SY03900.TXTFIELD) as [Comments]

Thanks, Daniel!  And, I do want to thank the others that offered suggestions.  It's always appreciated!!!  :)

TBSupport
0
 
PortletPaulCommented:
this needs help only you can give as my crystal ball does not extend to knowing all the fields of all your tables - please read the comments below
SELECT DISTINCT -- would love to get rid of this distinct
      DTZ..SOP30300.ITEMNMBR AS [Item]
    , DTZ..IV00101.ITEMDESC AS [ItemDesc]
    , DTZ..SOP30300.LOCNCODE AS [Site]
    , 0 AS [QtyAdjusted]
    , 0 AS [StockCountVariance]
    , 0 AS [QtyTransferred]
    , 0 AS [QtyRecd]
    , 0 AS [QtyReturned]
    , DTZ..SOP30300.QTYPRINV * - 1 AS [QtySold]
    , DTZ..SOP30300.FUFILDAT AS [Date]
    , DTZ..SOP30200.CSTPONBR AS [VPAorDocNo]
    , DTZ..IV00102.QTYONHND AS [QtyOnHand]
    , notes.comments
      
--, --'' as [TransferFromReason], --'' as [TransferToReason],
--COALESCE (DTZ..SOP10106.CMMTTEXT, '') as [SalesComment]

FROM DTZ..SOP30300
INNER JOIN DTZ..SOP30200 ON DTZ..SOP30300.SOPTYPE = DTZ..SOP30200.SOPTYPE
    AND DTZ..SOP30300.SOPNUMBE = DTZ..SOP30200.SOPNUMBE
INNER JOIN DTZ..IV00101 ON DTZ..SOP30300.ITEMNMBR = DTZ..IV00101.ITEMNMBR
INNER JOIN DTZ..IV00102 ON DTZ..IV00101.ITEMNMBR = DTZ..IV00102.ITEMNMBR
LEFT JOIN DTZ..SOP10106 ON DTZ..SOP30200.SOPTYPE = DTZ..SOP10106.SOPTYPE
    AND DTZ..SOP30200.SOPNUMBE = DTZ..SOP10106.SOPNUMBE
left join (
            select noteindex, cast(TXTFIELD as nvarchar(max)) as comments
-- need your help here
-- what can field can be used to order these notes? a date would be good, below if have guessed 'ID'            
            , row_number() over (partition by NOTEINDX order by ID DESC) as row_ref
            from DTZ..SY03900
          ) as nnotes ON DTZ..SOP30200.NOTEINDX = notes.noteindex and row_ref = 1

INNER JOIN vShip..SHP_ContainerItem ON DTZ..SOP30300.ITEMNMBR = vShip..SHP_ContainerItem.CoreItem
    AND DTZ..SOP30300.SOPNUMBE = vShip..SHP_ContainerItem.DocumentNumber
INNER JOIN vShip..SHP_Container ON vShip..SHP_ContainerItem.SHP_Container_ID = vShip..SHP_Container.SHP_Container_ID
WHERE (
        DTZ..SOP30300.SOPTYPE = '2'
        AND DTZ..SOP30300.QTYPRINV > 0
        AND DTZ..SOP30200.CSTPONBR LIKE 'VPA%'
        AND DTZ..IV00101.ITMGEDSC LIKE '%OCE%'
        AND DTZ..IV00102.LOCNCODE = ''
        AND DTZ..SOP30200.CSTPONBR = 'VPA59282'
        )

Open in new window

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now