Solved

T-SQL Cannot Use DISTINCT for Some Reason

Posted on 2013-06-11
6
551 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Question has a verified solution.

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

Written by Valentino Vranken. Introduction: The first step of creating a SQL Server Reporting Services (SSRS) report involves setting up a connection to the data source and programming a dataset to retrieve data from that data source.  The data…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

830 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