Solved

T-SQL Cannot Use DISTINCT for Some Reason

Posted on 2013-06-11
6
549 Views
Last Modified: 2013-06-12
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
Comment
Question by:TBSupport
6 Comments
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 39239699
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
 
LVL 32

Accepted Solution

by:
Daniel Wilson earned 500 total points
ID: 39239808
Does casting the text field as nvarchar(max) solve it for you?
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39239837
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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 48

Expert Comment

by:PortletPaul
ID: 39239856
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
 
LVL 1

Author Comment

by:TBSupport
ID: 39240960
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
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39241191
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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

29 Experts available now in Live!

Get 1:1 Help Now