Is there any method to achieve DISTINCT with text datatype columns.
Sql doesnt allow DISTINCT with text columns.Even though I dont
need to use distinct directly to a TEXT column, text columns which is
part of a join is creating problems
SELECT recep.id AS rID,
recep.NUM, recep.remark,
recep.name, recep.year
from recep
where typeid in (
select id from receptype)
and exists( select * from Calrecep
where recep.year = Calrecep.year
AND recep.NUM = Calrecep.NUM )
union all
SELECT recep.id AS rID,
recep.NUM, recep.remark,
recep.name, recep.year
from recep
where typeid in (
select id from receptype)
and not exists( select * from Calrecep
where recep.year = Calrecep.year
AND recep.NUM = Calrecep.NUM )
No columns are selected from Calrecep and the JOIN is a LEFT JOIN so a matching row in the table is not needed. Drop the LEFT JOIN and the Calrecep table from the query.
No columns are selected from recepType so the INNER JOIN is unneeded. You can use WHERE EXISTS instead. Modify the query as follows.
SELECT
recep.id AS rID,
recep.NUM,
recep.remark,
recep.name,
recep.year
FROM recep
WHERE Exists
(Select * From recepType
Where recep.TypeID = receptype.id)
I suspect you may tell us that the query was simplified for posting here and you actually select columns from the two additional tables. In that case, I recommend posting a complete query rather than a stripped down model that is incomplete.
If the query is complete then my revision should work unless multiple rows exist in the Recep table for the selected columns and you want distinct rows. If you don't need to return more than 8000 characters in the remark column, you can try the following.
SELECT
recep.id AS rID,
recep.NUM,
Convert(varchar(8000), recep.remark) As remark,
recep.name,
recep.year
FROM recep
WHERE Exists
(Select * From recepType
Where recep.TypeID = receptype.id) Terry L. Broadbent - DBA
SQL Server Page:
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.