Solved

T-SQL Cannot Use DISTINCT for Some Reason

Posted on 2013-06-11
6
546 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
Comment Utility
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
Comment Utility
Does casting the text field as nvarchar(max) solve it for you?
0
 
LVL 48

Expert Comment

by:PortletPaul
Comment Utility
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 48

Expert Comment

by:PortletPaul
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction Earlier I wrote an article about the new lookup functions (http://www.experts-exchange.com/A_3433.html) that ship with SQL Server 2008 R2.  In this article I’m going to show you another new feature of SSRS 2008 R2, this time in the vis…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

763 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

12 Experts available now in Live!

Get 1:1 Help Now