Hi all is there any way that i can use multiple values in between clause as
column_name between 0 and 100 or 200 and 300 like this
Any help would be appreciated
here is my query SELECT CASE WHEN ISNUMERIC(value_text) = 1 THEN CAST(value_text AS INT) ELSE -1 END) between 0 and 100
i just want to append multiple values in between clause
This is full query
SELECT ROW_NUMBER() OVER
(
order by Vendor_PrimaryInfo.Vendor_ID asc
)AS RowNumber
, Unit_Table.Unit_title, Vendor_Base_Price.Base_Price, Vendor_Base_Price.showprice, Category_Table.Title, Vendor_Registration.Business_Name,
Vendor_PrimaryInfo.Street_Address, Vendor_PrimaryInfo.Locality, Vendor_PrimaryInfo.Nearest_Landmark, Vendor_PrimaryInfo.City, Vendor_PrimaryInfo.State,
Vendor_PrimaryInfo.Country, Vendor_PrimaryInfo.PostalCode, Vendor_PrimaryInfo.Latitude, Vendor_PrimaryInfo.Longitude, Vendor_PrimaryInfo.ImageUrl,
Vendor_PrimaryInfo.ContactNo, Vendor_PrimaryInfo.Email,Vendor_PrimaryInfo.Vendor_ID
FROM Unit_Table INNER JOIN
Vendor_Base_Price ON Unit_Table.Unit_ID = Vendor_Base_Price.Unit_ID INNER JOIN
Vendor_PrimaryInfo ON Vendor_Base_Price.Vendor_ID = Vendor_PrimaryInfo.Vendor_ID INNER JOIN
Vendor_Registration ON Vendor_Base_Price.Vendor_ID = Vendor_Registration.Vendor_ID AND
Vendor_PrimaryInfo.Vendor_ID = Vendor_Registration.Vendor_ID INNER JOIN
Category_Table ON Vendor_Registration.Category_ID = Category_Table.Category_ID
LEFT JOIN
Vendor_Value_Table ON Vendor_Registration.Vendor_ID = Vendor_Value_Table.Vendor_ID LEFT JOIN
Feature_Table ON Vendor_Value_Table.Feature_ID = Feature_Table.Feature_ID
where Vendor_Registration.Category_ID=5 and Vendor_PrimaryInfo.City='City'
AND(
value_text in('Dhol Wala$Shahnai Wala')
or
(SELECT CASE WHEN ISNUMERIC(value_text) = 1 THEN CAST(value_text AS INT) ELSE -1 END) between 0 and 100
)
You can do this using AND/OR logic
value_text NOT LIKE '%[^0-9]%' and
(
value_text between 0 and 100
Or
value_text between 101 and 200
)
If you don't want to repeat the column name then frame the range in table valued constructor and join with your table
SELECT Row_number()
OVER (
ORDER BY Vendor_PrimaryInfo.Vendor_ID ASC )AS RowNumber,
Unit_Table.Unit_title,
Vendor_Base_Price.Base_Price,
Vendor_Base_Price.showprice,
Category_Table.Title,
Vendor_Registration.Business_Name,
Vendor_PrimaryInfo.Street_Address,
Vendor_PrimaryInfo.Locality,
Vendor_PrimaryInfo.Nearest_Landmark,
Vendor_PrimaryInfo.City,
Vendor_PrimaryInfo.State,
Vendor_PrimaryInfo.Country,
Vendor_PrimaryInfo.PostalCode,
Vendor_PrimaryInfo.Latitude,
Vendor_PrimaryInfo.Longitude,
Vendor_PrimaryInfo.ImageUrl,
Vendor_PrimaryInfo.ContactNo,
Vendor_PrimaryInfo.Email,
Vendor_PrimaryInfo.Vendor_ID
FROM Unit_Table
INNER JOIN Vendor_Base_Price
ON Unit_Table.Unit_ID = Vendor_Base_Price.Unit_ID
INNER JOIN Vendor_PrimaryInfo
ON Vendor_Base_Price.Vendor_ID = Vendor_PrimaryInfo.Vendor_ID
INNER JOIN Vendor_Registration
ON Vendor_Base_Price.Vendor_ID = Vendor_Registration.Vendor_ID
AND Vendor_PrimaryInfo.Vendor_ID = Vendor_Registration.Vendor_ID
INNER JOIN Category_Table
ON Vendor_Registration.Category_ID = Category_Table.Category_ID
LEFT JOIN Vendor_Value_Table
ON Vendor_Registration.Vendor_ID = Vendor_Value_Table.Vendor_ID
LEFT JOIN Feature_Table
ON Vendor_Value_Table.Feature_ID = Feature_Table.Feature_ID
JOIN (VALUES (0, 100),
(101, 200),
(201, 300)) tc (st, ed)
ON Try_cast(value_text AS INT) BETWEEN st AND ed
OR Try_cast(value_text AS VARCHAR(100)) = 'Dhol Wala$Shahnai Wala'
WHERE Vendor_Registration.Category_ID = 5
AND Vendor_PrimaryInfo.City = 'City'
Note : You have stored two different information's in a single column which causes lot of pain when you want to extract the data like this. Consider changing your table structure
Related
I am having a query in which number of execution is equal to the number of rows in the table.
Anyone knows any possible reason for that as a result the clustered index seek %age is very high.
I have update the Stats for the Tables.But still no benefit.
select #AdditionalBillToNumericValue = cast(ab_vnum as int) from #table1
inner join table4 on ab_btid = ay_bt and ab_type = 'SRVGRP'
if(#AdditionalBillToNumericValue is not null or #AdditionalBillToNumericValue <> '')
begin
select distinct
fs.sv_pkey
, fs.sv_id
, fs.sv_desc
,ISNULL( fs.sv_ehr,0) as sv_ehr
,ISNULL( fs.sv_emn,0) as sv_emn
,ISNULL( fs.sv_lhr,0) as sv_lhr
,ISNULL( fs.sv_lmn,0) as sv_lmn
,(case when #table1.fh_sv = fs.sv_id then 1 else 0 end) IsDefault
from table3 fr
inner join table4 ab on ab.ab_vnum = fr.sr_sgpkey
inner join table5 fs on fr.sr_svpkey = fs.sv_pkey or fs.sv_pkey = #ServiceWindowKey
inner join #table1 on ab.ab_btid = #table1.ay_bt
AND ab.ab_type = 'SRVGRP'
end
else
begin
select
fs.sv_pkey
, fs.sv_id
, fs.sv_desc
,ISNULL(sv_ehr,0) as sv_ehr
,ISNULL(sv_emn,0) as sv_emn
,ISNULL(sv_lhr,0) as sv_lhr
,ISNULL(sv_lmn,0) as sv_lmn
,(case when #table1.fh_sv = fs.sv_id then 1 else 0 end) IsDefault
from table3 fr
inner join table4 ab on ab.ab_vnum = fr.sr_sgpkey
inner join table5 fs on fr.sr_svpkey = fs.sv_pkey or fs.sv_pkey = #ServiceWindowKey
LEFT join #table1 with(nolock) on ab.ab_btid = #table1.ay_bt
where ab.ab_type = 'SRVGRP'
Please Verify where we are having the join with table5 it is getting the issue.
In Join Both are having int datatype.
I am trying to insert some values (CIID and AID) taken from another tables, but as not an expert, I couldn't manage to do it.
Insert query :
INSERT INTO INST_ACTIVE_ACTIONS act
(act.CIID, act.AID, act.STEPNUM, act.CREATEDATE)
VALUES
(CIID , ,0,GETDATE())
The CIID Query is :
SELECT C.CIID FROM INST_COURSE C
LEFT JOIN INST_ACTIVE_ACTIONS AA ON (AA.CIID = C.CIID)
LEFT JOIN INST_TASKS T ON (T.CIID = C.CIID)
LEFT JOIN SYS_SCH_ACTION SCH ON (SCH.CIID = C.CIID)
LEFT JOIN SYS_SUB_STACK SUB ON (SUB.RETURN_CIID=C.CIID)
WHERE C.COMPLETED IS NULL AND AA.AID IS NULL AND T.AID IS NULL AND SCH.AID IS NULL AND SUB.RETURN_AID IS NULL
AID Query is :
SELECT TOP 1 ca.AID
FROM INST_COMPLETE_ACTIONS CA
INNER JOIN TMPL_ACT_MASTER TAM ON CA.AID=TAM.AID
WHERE ca.CIID =c.CIID ORDER BY TSTAMP DESC
act.CIID = C.CIID = ca.CIID and CA.AID = act.AID
Edited :
the last query is
INSERT INTO INST_ACTIVE_ACTIONS (CIID,AID,stepnum,CREATEDATE
)
VALUES
(
(SELECT c.CIID
FROM INST_COURSE C
LEFT JOIN INST_ACTIVE_ACTIONS AA
ON (
aa.CIID = c.CIID)
LEFT JOIN INST_TASKS T
ON (
t.CIID = c.CIID)
LEFT JOIN SYS_SCH_ACTION SCH
ON (
sch.CIID = c.CIID)
LEFT JOIN sys_sub_stack SUB
ON (
sub.RETURN_CIID = c.CIID)
WHERE c.completed IS NULL
AND aa.AID IS NULL
AND t.AID IS NULL
AND sch.AID IS NULL
AND sub.return_AID IS NULL),
(
SELECT TOP 1
ca.AID
FROM INST_COMPLETE_ACTIONS CA
INNER JOIN tmpl_act_master TAM
ON ca.AID=tam.AID
ORDER BY tstamp DESC
),
0,
Getdate() )
but i get an error as
Msg 512, Level 16, State 1, Line 1 Subquery returned more than 1
value. This is not permitted when the subquery follows =, !=, <, <= ,
, >= or when the subquery is used as an expression. The statement has been terminated.
Just condensed though its real busy if you ask me.
INSERT INTO INST_ACTIVE_ACTIONS
(CIID, AID, STEPNUM, CREATEDATE)
--OUTPUT INSERTED.* --if you want to see what was inserted uncomment this
(SELECT C.CIID,
(SELECT TOP 1 CA.AID
FROM INST_COMPLETE_ACTIONS CA
INNER JOIN TMPL_ACT_MASTER TAM ON CA.AID=TAM.AID
WHERE CA.CIID =C.CIID ORDER BY TSTAMP DESC)
, 0,GETDATE())
FROM INST_COURSE C
LEFT JOIN INST_ACTIVE_ACTIONS AA ON (AA.CIID = C.CIID)
LEFT JOIN INST_TASKS T ON (T.CIID = C.CIID)
LEFT JOIN SYS_SCH_ACTION SCH ON (SCH.CIID = C.CIID)
LEFT JOIN SYS_SUB_STACK SUB ON (SUB.RETURN_CIID=C.CIID)
WHERE C.COMPLETED IS NULL AND AA.AID IS NULL AND T.AID IS NULL AND SCH.AID IS NULL AND SUB.RETURN_AID IS NULL)
Revised. tested, and working on my end. I still think if you have control on these tables you might look into the concept of Normalizing your database tables.
You can always perform something like this, and use as many sub queries as needed. Make sure that the types are matching on both sides.
Insert into table1 (value1, value2) Select val1,val2 from table2 ...
edit
INSERT INTO INST_ACTIVE_ACTIONS act (act.CIID, act.AID,
act.STEPNUM, act.CREATEDATE) Select
(SELECT C.CIID FROM INST_COURSE C LEFT JOIN INST_ACTIVE_ACTIONS AA ON (AA.CIID = C.CIID) LEFT JOIN INST_TASKS T ON
(T.CIID = C.CIID) LEFT JOIN SYS_SCH_ACTION SCH ON (SCH.CIID = C.CIID)
LEFT JOIN SYS_SUB_STACK SUB ON (SUB.RETURN_CIID=C.CIID) WHERE
C.COMPLETED IS NULL AND AA.AID IS NULL AND T.AID IS NULL AND SCH.AID
IS NULL AND SUB.RETURN_AID IS NULL), (SELECT TOP 1 ca.AID FROM
INST_COMPLETE_ACTIONS CA INNER JOIN TMPL_ACT_MASTER TAM ON
CA.AID=TAM.AID WHERE ca.CIID =c.CIID ORDER BY TSTAMP DESC act.CIID
= C.CIID = ca.CIID and CA.AID ),0,GETDATE()
I get a list of values from the select query. From the list, I am checking whether the items in the list is available in the table. If the values exist, I need to update the values else Insert the list into the Table.
With the list, I can insert the list of values in the table.
How to check and update the list in sql.
My Query :
WITH pq AS
(
SELECT A.[ProductId] ,A.[Quantity],A.[OrderId],D.[ProductName],E.[SpecialPrice],E.[SpecialPrice]*A.[Quantity] AS SPrice FROM [Table1] A
LEFT JOIN [Table2] B ON A.[OrderId] = B.[OrderId] INNER JOIN [Table3] D
ON A.[ProductId] = D.[ProductId] INNER JOIN [Table4] E
ON A.[ProductId] = E.[ProductId] WHERE B.[CustomerId] = 1
AND A.[OrderId] = 77
)
IF (EXISTS(SELECT [ProductId] FROM [Table5] WHERE [ProductId] = A.[ProductId]))
BEGIN
UPDATE [Table5]
SET [Quantity] = A.[Quantity]
WHERE B.[CustomerId] = 1 AND [ProductId] = A.[ProductId]
END
ELSE
BEGIN
INSERT INTO [Table5]
([ProductId],[ProductName],[Quantity],[Price],[TotalAmount])
SELECT
[ProductId],[ProductName],[Quantity],[SpecialPrice],SPrice
FROM pq;
END
Any suggestions will be greatly helpful.
EDIT : SELECT QUERY RESULT
ProductId Quantity
65 2
64 1
Assuming you're on SQL Server 2008 or above, the MERGE statement will solve your problem:
MERGE Table5 TRG
USING (
SELECT
A.ProductId,
A.Quantity,
A.OrderId,
D.ProductName,
E.SpecialPrice,
(E.SpecialPrice * A.Quantity) SPrice
FROM Table1 A
LEFT JOIN Table2 B ON A.OrderId = B.OrderId
INNER JOIN Table3 D ON A.ProductId = D.ProductId
INNER JOIN Table4 E ON A.ProductId = E.ProductId
WHERE
B.CustomerId = 1
AND A.OrderId = 77
) SRC
ON TRG.ProductID = SRC.ProductID
WHEN MATCHED THEN
UPDATE SET TRG.Quantity = SRC.Quantity
WHEN NOT MATCHED BY TARGET THEN
INSERT (
ProductId
, ProductName
, Quantity
, Price
, TotalAmount
)
VALUES (
SRC.ProductId
, SRC.ProductName
, SRC.Quantity
, SRC.SpecialPrice
, SRC.SPrice)
;
You can move the SELECT query out to a CTE for legibility like you did in your example.
I want to get data historical and the production. My stored procedure is as follows:
ALTER PROCEDURE [dbo].[pCaRptACInactivas](
#CodAsesor VARCHAR(15),
#CodOficina VARCHAR(4))
AS
SET NOCOUNT ON
DECLARE #CodArbolConta VARCHAR(25)
IF #CodOficina = '%'
SET #CodArbolConta = '%'
ELSE
SELECT #CodArbolConta = CodArbolConta + '%' FROM tClOficinas WHERE CodOficina LIKE #CodOficina
SELECT
tabACInactivas.CodOficina,
tabACInactivas.NomOficina,
tabACInactivas.NomAsesor,
MAX(tabACInactivas.CodPrestamo) CodPrestamo,
tabACInactivas.CodAsociacion,
tabACInactivas.NombreAsociacion,
MAX(tabACInactivas.Ciclo) AS Ciclo,
COUNT(DISTINCT tabACInactivas.CodUsuario) AS CantSocias,
MAX(tabACInactivas.FechaEstado) AS FechaCancelacion--,
FROM ( SELECT tClOficinas.CodOficina, tClOficinas.NomOficina, tCaClAsesores.CodAsesor, tCaClAsesores.NomAsesor, tCaPrestamos.CodPrestamo, tCaAsociacion.CodAsociacion, tCaAsociacion.NombreAsociacion, tCaPrestamos.Ciclo, tCaPrCliente.CodUsuario, tCaPrestamos.FechaEstado, tClParametros.FechaProceso FROM tCaPrestamos WITH(NOLOCK) INNER JOIN tCaProducto WITH(NOLOCK) ON tCaProducto.CodProducto = tCaPrestamos.CodProducto INNER JOIN tClOficinas WITH(NOLOCK) ON tClOficinas.CodOficina = tCaPrestamos.CodOficina INNER JOIN tCaAsociacion WITH(NOLOCK) ON tCaAsociacion.CodAsociacion = tCaPrestamos.CodAsociacion INNER JOIN tCaPrCliente WITH(NOLOCK) ON tCaPrCliente.CodPrestamo = tCaPrestamos.CodPrestamo INNER JOIN tClParametros WITH(NOLOCK) ON tClParametros.CodOficina = tClOficinas.CodOficina INNER JOIN tCaClAsesores ON tCaClAsesores.CodAsesor = tCaAsociacion.CodAsesor WHERE tCaPrestamos.Estado = 'CANCELADO' AND DATEDIFF(DAY, tCaPrestamos.FechaEstado, tClParametros.FechaProceso) > 30 AND NOT EXISTS(SELECT 1
FROM tCaPrestamos Pr
INNER JOIN tCaPrCliente PrCl ON PrCl.CodPrestamo = Pr.CodPrestamo
WHERE Pr.Estado NOT IN ('TRAMITE', 'APROBADO')
AND Pr.FechaDesembolso >= tCaPrestamos.FechaEstado
AND Pr.CodAsociacion = tCaPrestamos.CodAsociacion
) AND tCaProducto.Tecnologia = 3 AND tCaPrestamos.CodAsesor LIKE #CodAsesor AND tCaPrestamos.CodOficina IN (SELECT CodOficina FROM tClOficinas WHERE CodArbolConta LIKE #CodArbolConta)
UNION ALL
SELECT tClOficinas.CodOficina, tClOficinas.NomOficina, tCaClAsesores.CodAsesor, tCaClAsesores.NomAsesor, tCaHPrestamos.CodPrestamo, tCaAsociacion.CodAsociacion, tCaAsociacion.NombreAsociacion, tCaHPrestamos.Ciclo, tCaHPrCliente.CodUsuario, tCaHPrestamos.FechaEstado, tClParametros.FechaProceso FROM tCaHPrestamos WITH(NOLOCK) INNER JOIN tCaProducto WITH(NOLOCK) ON tCaProducto.CodProducto = tCaHPrestamos.CodProducto INNER JOIN tClOficinas WITH(NOLOCK) ON tClOficinas.CodOficina = tCaHPrestamos.CodOficina INNER JOIN tCaAsociacion WITH(NOLOCK) ON tCaAsociacion.CodAsociacion = tCaHPrestamos.CodAsociacion INNER JOIN tCaHPrCliente WITH(NOLOCK) ON tCaHPrCliente.CodPrestamo = tCaHPrestamos.CodPrestamo INNER JOIN tClParametros WITH(NOLOCK) ON tClParametros.CodOficina = tClOficinas.CodOficina INNER JOIN tCaClAsesores ON tCaClAsesores.CodAsesor = tCaAsociacion.CodAsesor WHERE tCaHPrestamos.Estado = 'CANCELADO' AND DATEDIFF(DAY, tCaHPrestamos.FechaEstado, tClParametros.FechaProceso) > 30 AND NOT EXISTS(SELECT 1
FROM tCaHPrestamos Pr
INNER JOIN tCaHPrCliente PrCl ON PrCl.CodPrestamo = Pr.CodPrestamo
WHERE Pr.Estado NOT IN ('TRAMITE', 'APROBADO')
AND Pr.FechaDesembolso >= tCaHPrestamos.FechaEstado
AND Pr.CodAsociacion = tCaHPrestamos.CodAsociacion
) AND tCaProducto.Tecnologia = 3 AND tCaHPrestamos.CodAsesor LIKE #CodAsesor AND tCaHPrestamos.CodOficina IN (SELECT CodOficina FROM tClOficinas WHERE CodArbolConta LIKE #CodArbolConta)
)tabACInactivas
GROUP BY tabACInactivas.CodAsociacion, tabACInactivas.NombreAsociacion, tabACInactivas.NomOficina, tabACInactivas.CodOficina, tabACInactivas.NomAsesor
I want the CantSocias column takes the most value of the Ciclo column, but not working
That stored procedure that you have posted up is way too large and blocky to even try to interpret and understand. So I will go off of your last sentence:
I want the CantSocias column takes the most value of the Ciclo column,
but not working
Basically if you want to set a specific column to that, you can do something like this:
update YourTable
set CantSocias =
(
select max(Ciclo)
from YourOtherTable
)
-- here is where you can put a conditional WHERE clause
You may need to create a sub query to get the most value of Ciclo and join back to your query.
An example of what I mean is here:
create table #Product
(
ID int,
ProductName varchar(20)
)
insert into #Product(ID, ProductName)
select 1,'ProductOne'
union
select 2,'ProductTwo'
create table #ProductSale
(
ProductID int,
Number int,
SalesRegion varchar(20)
)
insert into #ProductSale(ProductID,Number,SalesRegion)
select 1,1500,'North'
union
select 1, 1200, 'South'
union
select 2,2500,'North'
union
select 2, 3200, 'South'
--select product sales region with the most sales
select * from #Product p
select ProductId, Max(Number) as Bestsale from #ProductSale ps group by ProductID
--combining
select
p.ID,
p.ProductName,
tp.Bestsale,
ps.SalesRegion
from
#Product p
inner join
(select ProductId, Max(Number) as Bestsale from #ProductSale ps group by ProductID) as tp on p.ID = tp.ProductID
inner join
#ProductSale ps on p.ID = ps.ProductID and tp.Bestsale = ps.Number
I have a problem with ROW_NUMBER() , if i used it with DISTINCT in the following Query
I have 2 scenarios:
1- run this query direct : give me for example 400 record as a result
2- uncomment a line which start with [--Uncomment1--] : give me 700 record as a result
it duplicated some records not all the records
what I want is to solve this problem or to find any way to show a row counter beside each row, to make a [where rownumber between 1 and 30] --Uncomment2--
if I put the whole query in a table, and then filter it , it is work but it still so slow
waiting for any feedback and I will appreciate that
Thanks in advance
SELECT * FROM
(SELECT Distinct CRSTask.ID AS TaskID,
CRSTask.WFLTaskID,
--Uncomment1-- ROW_NUMBER() OVER (ORDER By CRSTask.CreateDate asc ) AS RowNum ,
CRSTask.WFLStatus AS Task_WFLStatus,
CRSTask.Name AS StepName,
CRSTask.ModifiedDate AS Task_ModifyDate,
CRSTask.SendingDate AS Task_SendingDate,
CRSTask.ReceiveDate AS Task_ReceiveDate,
CRSTask.CreateDate AS Task_CreateDate,
CRS_Task_Recipient_Vw.Task_CurrentSenderName,
CRS_Task_Recipient_Vw.Task_SenderName,
CRS_INFO.ID AS CRS_ID,
CRS_INFO.ReferenceNumber,
CRS_INFO.CRSBeneficiaries,
CRS_INFO.BarCodeNumber,
ISNULL(dbo.CRS_FNC_GetTaskReceiver(CRSTask.ID), '') + ' ' + ISNULL
(CRS_Organization.ArName, '')
AS OrgName,
CRS_Info.IncidentID,
COALESCE(CRS_Subject.ArSubject, 'غير مبين') AS ArSubject,
COALESCE(CRS_INFO.Subject, 'Blank Subject') AS CRS_Subject,
CRS_INFO.Mode,
CRS_Task_Recipient_Vw.ReceiverID,
CRS_Task_Recipient_Vw.ReceiverType,
CRS_Task_Recipient_Vw.CC,
Temp_Portal_Users_View.ID AS CRS_LockedByID,
Temp_Portal_Users_View.ArabicName AS CRS_LockedByName,
CRSDraft.ID AS DraftID,
CRSDraft.Type AS DraftType,
CASE
WHEN CRS_Folder = 1 THEN Task_SenderName
WHEN CRS_Folder = 2 THEN Task_SenderName
WHEN CRS_Folder = 3 THEN Task_CurrentSenderName
END AS SenderName,
CRS_Task_Folder_Vw.CRS_Folder,
CRS_INFO.Status,
CRS_INFO.CRS_Type,
CRS_Type.arName AS CRS_Type_Name
FROM CRS_Task_Folder_Vw
LEFT OUTER JOIN CRSTask
ON CRSTask.ID = CRS_Task_Folder_Vw.TaskID
LEFT OUTER JOIN CRS_INFO
ON CRS_INFO.ID = CRSTask.CRSID
LEFT OUTER JOIN CRS_Subject
ON COALESCE(
SUBSTRING(
CRS_INFO.Subject,
CHARINDEX('_', CRS_INFO.Subject) + 1,
LEN(CRS_INFO.Subject)
),
'Blank Subject'
) = CRS_Subject.ID
LEFT OUTER JOIN CRSInfoAttribute
ON CRS_INFO.ID = CRSInfoAttribute.ID
LEFT OUTER JOIN CRS_Organization
ON CRS_Organization.ID = CRSInfoAttribute.SourceID
LEFT OUTER JOIN CRS_Type
ON CRS_INFO.CRS_Type = CRS_Type.ID
LEFT OUTER JOIN CRS_Way
ON CRS_INFO.CRS_Send_Way = CRS_Way.ID
LEFT OUTER JOIN CRS_Priority
ON CRS_INFO.CRS_Priority_ID = CRS_Priority.ID
LEFT OUTER JOIN CRS_SecurityLevel
ON CRS_INFO.SecurityLevelID = CRS_SecurityLevel.ID
LEFT OUTER JOIN Portal_Users_View
ON Portal_Users_View.ID = CRS_INFO.CRS_Initiator
LEFT OUTER JOIN AD_DOC_TBL
ON CRS_INFO.DocumentID = AD_DOC_TBL.ID
LEFT OUTER JOIN CRSTask AS Temp_CRSTask
ON CRSTask.ParentTask = Temp_CRSTask.ID
LEFT OUTER JOIN Portal_Users_View AS Temp_Portal_Users_View
ON Temp_Portal_Users_View.ID = AD_DOC_TBL.Lock_User_ID
LEFT OUTER JOIN Portal_Users_View AS Temp1_Portal_Users_View
ON Temp1_Portal_Users_View.ID = CRS_INFO.ClosedBy
LEFT OUTER JOIN CRSDraft
ON CRSTask.ID = CRSDraft.TaskID
LEFT OUTER JOIN CRS_Task_Recipient_Vw
ON CRSTask.ID = CRS_Task_Recipient_Vw.TaskID
--LEFT OUTER JOIN CRSTaskReceiverUsers ON CRSTask.ID =
CRSTaskReceiverUsers.CRSTaskID AND CRS_Task_Recipient_Vw.ReceiverID = CRSTaskReceiverUsers.ReceiverID
LEFT OUTER JOIN CRSTaskReceiverUserProfile
ON CRSTask.ID = CRSTaskReceiverUserProfile.TaskID
WHERE Crs_Info.SUBJECT <> 'Blank Subject'
AND (CRS_INFO.Subject NOT LIKE '%null%')
AND CRS_Info.IsDeleted <> 1
/* AND CRSTask.WFLStatus <> 6
AND CRSTask.WFLStatus <> 8 */
AND (
(
CRS_Task_Recipient_Vw.ReceiverID IN (1, 29)
AND CRS_Task_Recipient_Vw.ReceiverType IN (1, 3, 4)
)
)
AND 1 = 1
)Codes
--Uncomment2-- WHERE Codes.RowNum BETWEEN 1 AND 30
ORDER BY
Codes.Task_CreateDate ASC
If the issue is that you have duplicate rows and DISTINCT is failing because the ordinal row number is making each row unique; try (with DISTINCT):
DENSE_RANK() OVER (ORDER By CRSTask.CreateDate asc)
You can also remove the DISTINCT and GROUP BY everything in the CTE.