Solved

T-SQL Cannot Use DISTINCT for Some Reason

Posted on 2013-06-11
6
553 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 60

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 49

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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 49

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 49

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 learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
It is helpful to note: This is a cosmetic update and is not required, but should help your reports look better for your boss.  This issue has manifested itself in SSRS version 3.0 is where I have seen this behavior in.  And this behavior is only see…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

689 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