Rewriting SQL query to simplify logic - sql

I'm quite a newbie in optimizing queries.. it would be a first for me to handle other persons query and optimize it to improve performance. Can you kindly give me advice on which part of the query could I simplify in order to improve its performance....
CREATE FUNCTION SALES
(#SUPPLIERCODE VARCHAR(15),
#BATCHID VARCHAR(50))
RETURNS
#STOCKDETAILS TABLE([ID] CHAR(1),
[BATCH RECEIVEDATE] DATETIME,
SUPPLIERCODE VARCHAR(15),
[NOW - RECEVEDATE] INT,
[DUEDATE] VARCHAR(50)
)
AS
BEGIN
DECLARE #RECEIVEDATE DATETIME,
#SUPPLIERCODE1 VARCHAR(15)
SELECT TOP 1
#RECEIVEDATE = O.ReceivedDate,
#SUPPLIERCODE1 = A.SUPPLIERCODE
FROM
TRANSACT.dbo.FIELDS A WITH(NOLOCK)
INNER JOIN
TRANSACT.dbo.DELIV O WITH(NOLOCK) ON O.BATCHID = A.BATCHID
DECLARE #ID1 TABLE(SUPPLIERCODE VARCHAR(50))
INSERT INTO #ID1
SELECT P.SUPPLIERCODE
FROM
(SELECT
[SUPPLIERCODE] = SUPPLIERCODE,
[TOTAL] = ISNULL(SUM(ITEMPRICE + (ITEMPRICE * .12)), 0)
FROM TRANSACT.dbo.ProviderDiscount WITH(NOLOCK)
WHERE ACQUIREDDATE <> '1900-01-01 00:00:00.000'
AND SUPPLIERCODE = #SUPPLIERCODE1
GROUP BY SUPPLIERCODE) P
WHERE P.TOTAL <> 0
DECLARE #ID TABLE ([BATCH RECEIVEDATE] DATETIME,
SUPPLIERCODE VARCHAR(15),
ACQUIREDDATE DATETIME,
Coverage VARCHAR(20),
CoverageItem VARCHAR(10),
[NOW - RECEVEDATE] INT,
DiscTerm1 INT, DiscTerm2 INT,
DiscTerm3 INT, DiscTerm4 INT,
DiscTerm5 INT,
[NEW ACQUIREDDATE] VARCHAR(50)
)
INSERT INTO #ID
SELECT DISTINCT
[BATCH RECEIVEDATE] = #RECEIVEDATE,
B.SUPPLIERCODE,
B.ACQUIREDDATE,
B.Coverage,
B.CoverageItem,
[NOW - RECEVEDATE] = DATEDIFF(DAY,#RECEIVEDATE,GETDATE()),
B.DiscTerm1, B.DiscTerm2, B.DiscTerm3,
B.DiscTerm4, B.DiscTerm5,
[NEW ACQUIREDDATE] = TRANSACT.dbo.fxnGetIDNewACQUIREDDATE(B.DiscTerm1, B.DiscTerm2, B.DiscTerm3, B.DiscTerm4, B.DiscTerm5, #RECEIVEDATE)
FROM
TRANSACT.dbo.ProviderDiscount B WITH(NOLOCK)
INNER JOIN
(SELECT
[ACQUIREDDATE] = MAX(ACQUIREDDATE),
[REOD] = MAX(REOD)
FROM
TRANSACT.dbo.ProviderDiscount B2 WITH(NOLOCK)
INNER JOIN
#ID1 B1 ON B1.SUPPLIERCODE = B2.SUPPLIERCODE
WHERE
B2.Coverage = #CLAIMTYPE
AND B2.ACQUIREDDATE < #RECEIVEDATE) B3 ON B3.REOD = B.REOD
INSERT INTO #STOCKDETAILS
SELECT DISTINCT
[ID] = 'Y',
[BATCH RECEIVEDATE],
SUPPLIERCODE,
[NOW - RECEVEDATE],
[DUEDATE] = MIN([NEW ACQUIREDDATE])
FROM
#ID
WHERE
ISNULL([NEW ACQUIREDDATE],'NONE') <> 'NONE'
GROUP BY
[BATCH RECEIVEDATE], SUPPLIERCODE, [NOW - RECEVEDATE]
RETURN
END

Well, here's one thing you can do.
DECLARE #ID1 TABLE(SUPPLIERCODE VARCHAR(50))
INSERT INTO #ID1
SELECT P.SUPPLIERCODE
FROM
(
SELECT
[SUPPLIERCODE] = SUPPLIERCODE,
[TOTAL] = ISNULL(SUM(ITEMPRICE+(ITEMPRICE*.12)),0)
FROM TRANSACT.dbo.ProviderDiscount WITH(NOLOCK)
WHERE ACQUIREDDATE <> '1900-01-01 00:00:00.000'
AND SUPPLIERCODE = #SUPPLIERCODE1
GROUP BY SUPPLIERCODE
) P
WHERE P.TOTAL <> 0
Can be rewritten stripped of lots of extra. Maybe even more than I do here:
DECLARE #ID1 TABLE(SUPPLIERCODE VARCHAR(50))
INSERT INTO #ID1
SELECT SUPPLIERCODE
FROM TRANSACT.dbo.ProviderDiscount WITH(NOLOCK)
WHERE ACQUIREDDATE <> '1900-01-01 00:00:00.000'
AND SUPPLIERCODE = #SUPPLIERCODE1
GROUP BY SUPPLIERCODE
HAVING ISNULL(SUM(ITEMPRICE),0) <> 0
This almost looks like test question for refactoring SQL.

Related

Refresh issue with Stored Procedure in MSSQL

I have one SP for mobile application chat app (used with API) it has 25 filters and 10 tables join and return posts for user, it works good for 2 days but after 2 days it stop working, then i need to refresh it again with alter query or something then it start working for next few days.
If filter stop for any particular user than why others have issue with SP.
USE [DatabaseName]GOSET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOALTER PROC [dbo].[uspGetShoutouts] (
#UserId UNIQUEIDENTIFIER,
#anotherUserid UNIQUEIDENTIFIER,
#Interest NVARCHAR(100),
#Gender NVARCHAR(100),
#AgeFrom INT,
#AgeTo INT,
#StartLatitude NUMERIC(20,15),
#StartLongitude NUMERIC(20,15),
#Radius FLOAT,
#Location BIT = NULL,
#IsTraveler BIT,
#IsLocal BIT,
#HomeTownCity VARCHAR(100),
#UserName NVARCHAR(200),
#FirstName NVARCHAR(200),
#LastName NVARCHAR(200),
#CreatedDate DATETIME,
#IsMyShoutout BIT,
#ShoutoutTypeId INT=0,
#PostTypeId INT=0,
#oldMoment BIT,
#SortExpression CHAR(1) = 1,
#CurrentPage INT = 1,
#PageSize INT = 10,
#IsFriend BIT=0,
#IsPrivate BIT,
#GroupId NVARCHAR(200),
#TotalRows INT OUTPUT
)
AS
BEGIN
DECLARE #UserList TABLE(
UserId uniqueidentifier,
FirstName NVARCHAR(100),
LastName NVARCHAR(100),
QuickBloxId NVARCHAR(100),
SocialId NVARCHAR(100),
Gender char(1),
Email NVARCHAR(100),
IsProfileImageSync bit,
IsFriend bit,
IsRequestSent bit,
RequestSender NVARCHAR(100),
IsBlocked bit,
FriendBlocked bit,
Age int,
HomeTownCity NVARCHAR(100),
ImageName NVARCHAR(200),
Distance numeric(20,6),
DistanceTemp numeric(20,6),
LastActiveDateTime datetime,
ShoutoutGuid uniqueidentifier,
ShoutoutId bigint not null,
InterestId int,
InterestName NVARCHAR(100),
ShoutoutImageName NVARCHAR(200),
Description NVARCHAR(max),
CreatedOn DateTime,
IsImageSync bit,
DisplayUpdatedOn NVARCHAR(50),
ShoutoutCity NVARCHAR(500),
ShoutoutCountry NVARCHAR(500),
AdministrativeAreaLevel1 NVARCHAR(500),
PlaceId NVARCHAR(500),
FormattedAaddress NVARCHAR(500),
Url NVARCHAR(500),
TotalLike int,
TotalComment int,
Selflike bit,
ShoutoutTypeId int,
ShoutoutType NVARCHAR(200),
PostTypeId int,
PostTypeName NVARCHAR(200),
oldMoment bit,
UserLatitude numeric(20,6),
UserLongitude numeric(20,6),
ShoutoutLatitude numeric(20,6),
ShoutoutLongitude numeric(20,6),
DistanceShoutoutMiles numeric(20,6)
)
SET NOCOUNT ON;
DECLARE #StartLatitude1 numeric(20,2) = #StartLatitude
DECLARE #StartLongitude1 numeric(20,2) =#StartLongitude;
;with
UserList as (
select
distinct
USRSHTOUT.ShoutoutId,
USRSHTOUT.ShoutoutGuid,
U.UserId,
U.FirstName,
U.LastName,
U.QuickbloxId,
convert(int,round(datediff(hour,dob,getdate())/8766.0,0)) as Age,
U.SocialId,
U.Gender,
U.Email,
isnull(U.IsProfileImageSync,0) IsProfileImageSync,
U.HomeTownCity,
U.ImageName,
isnull(fr.IsFriend,0) as IsFriend,
isnull(fr.IsRequestSent,0) as IsRequestSent,
isnull(fr.RequestSender,cast(cast(0 as binary) as uniqueidentifier)) as RequestSender,
isnull(fr.IsBlocked,0) as IsBlocked,
isnull(fr.FriendBlocked,0) as FriendBlocked,
[dbo].[fnCalcDistanceKM](cast(#StartLatitude1 as float),cast(#StartLongitude1 as float),cast (isnull(USRSHTOUT.Latitude,0) as float) ,cast(isnull(USRSHTOUT.Longitude,0) as float)) as Distance,
(geography::Point(coalesce(USRSHTOUT.Latitude,0), coalesce(USRSHTOUT.Longitude,0), 4326).STDistance(geography::Point(#StartLatitude1, #StartLongitude1, 4326))/1000) as DistanceTemp,
ULAL.UpdatedOn as LastActiveDateTime,
0 as InterestId,
'' as InterestName,
isnull(USRSHTOUT.ImageName,'') 'ShoutoutImageName',
isnull(USRSHTOUT.Description,'') Description,USRSHTOUT.CreatedOn,IsImageSync,
isnull(format(UGL.UpdatedOn,'dd-MMM, yyyy HH:mm tt'),'') 'DisplayUpdatedOn',
isnull(ADC.locality,'')[ShoutoutCity],
isnull(ADC.country,'')[ShoutoutCountry],
isnull(ADC.administrative_area_level_1,'') [AdministrativeAreaLevel1],
isnull(ADC.place_id,'') [PlaceId],
isnull(ADC.formatted_address,'') FormattedAaddress,
isnull(ADC.url,'') Url,
isnull(lt.TotalLike, 0) as TotalLike,
isnull(ct.TotalComment, 0) as TotalComment,
isnull(lt.SelfLike, 0) as SelfLike,
SHT.ShoutoutTypeId,
isnull(SHT.Name,'') ShoutoutType,
UPT.PostTypeId,
isnull(UPT.Name,'') PostTypeName,
USRSHTOUT.oldMoment,
UGL.Latitude 'UserLatitude',
UGL.Longitude 'UserLongitude',
USRSHTOUT.Latitude 'ShoutoutLatitude',
USRSHTOUT.Longitude 'ShoutoutLongitude',
DistanceShoutoutMiles
from
Users U
inner join UserShoutouts USRSHTOUT on U.UserId=USRSHTOUT.UserId
left join UserLastActivityLog ULAL on ULAL.UserId=U.UserId
left join UserGeoLocation UGL on UGL.UserId=U.UserId
left join AddressShoutoutMapping ASMP on ASMP.ShoutoutId=USRSHTOUT.ShoutoutId
left join AddressComponents ADC on ADC.AddressComponentId=ASMP.AddressComponentId
left join [ShoutoutType] SHT on SHT.ShoutoutTypeId=isnull(USRSHTOUT.ShoutoutTypeId,0)
LEFT JOIN [UserPostType] UPT ON UPT.PostTypeId = ISNULL(USRSHTOUT.PostTypeId,0)
cross apply (select cos(radians(#StartLatitude1)) * cos(radians(USRSHTOUT.Latitude)) * cos(radians(USRSHTOUT.Longitude) - radians(#StartLongitude1)) + sin(radians(#StartLatitude1)) * sin(radians(USRSHTOUT.Latitude))) T(ACosInput)
cross apply (select ((3959 * acos(case when abs(ACosInput) > 1 then sign(ACosInput)*1 else ACosInput end)))) T2(DistanceShoutoutMiles)
left join (
select ItemId,
count(1) TotalLike,
cast(sum(case when UserId = #anotherUserid then 1 else 0 end) as bit) as SelfLike
from dbo.LikeDetails
where Liked=1 and LikeSourceId=1
group by ItemId) as lt on USRSHTOUT.ShoutoutId = lt.ItemId
left join (
select ParentId,
count(1) as TotalComment
from Comments
where CommentSourceId=1 and Comments.IsDeleted=0
group by ParentId) as ct on USRSHTOUT.ShoutoutId = ct.ParentId
left join dbo.tvf_GetFriendsByUserId (#UserId) fr on u.UserId = fr.ThisFriendId
left join UserShoutoutsPrivate USP on USP.ShoutoutId = USRSHTOUT.ShoutoutId
left join UserInterest UI on UI.InterestId = USP.GroupId
left join Interest INTRST on INTRST.Id = UI.InterestId
where
USRSHTOUT.IsDeleted=0
and (((#oldMoment = 0) and USRSHTOUT.oldMoment = #oldMoment)
or ((#oldMoment = 1) and USRSHTOUT.oldMoment = 1 or USRSHTOUT.oldMoment = 0) )
and U.IsEmailVerified=1 and isnull(U.IsDeactivated,0)=0
and (#ShoutoutTypeId=0 or USRSHTOUT.ShoutoutTypeId=#ShoutoutTypeId)
and (#IsMyShoutout=0 or (#IsMyShoutout=1 and USRSHTOUT.UserId = #UserId))
and ((#UserName is not null and #UserName='all'
OR ((U.FirstName + ' ' + U.LastName) = #UserName)OR (U.FirstName like LTRIM(RTRIM(#UserName))+'%') OR (U.LastName like LTRIM(RTRIM(#UserName))+'%'))
OR ( #FirstName is not null AND #LastName is null and
((U.FirstName = #FirstName ) OR (U.FirstName like LTRIM(RTRIM(#FirstName))+'%')))
OR ( #LastName is not null AND #FirstName is null and
((U.LastName = #LastName ) OR (U.LastName like LTRIM(RTRIM(#LastName))+'%')))
OR ((( #FirstName is not null AND #LastName is not null AND
((U.FirstName + ' ' + U.LastName) = #FirstName + ' ' + #LastName) OR (U.FirstName like LTRIM(RTRIM(#FirstName))+'%')) and (U.LastName like LTRIM(RTRIM(#LastName))+'%'))))
and((#Interest <>'' AND #IsPrivate = 1 AND (exists (select * from dbo.UserInterest where (UserId = U.UserId and InterestId in (select * from dbo.Split(#Interest,','))))
or #Interest ='')
or(#IsPrivate = 1
and (UI.Interestid in (select items from dbo.Split(#GroupId,',')) and INTRST.IsPrivate = 1 and INTRST.IsDelete = 0 and USRSHTOUT.IsDeleted=0 and
USP.ShoutoutId = USRSHTOUT.ShoutoutId and UI.IsActive = 1 and USP.GroupId in (select items from dbo.Split(#GroupId,','))))
)
OR(#Interest <>'' AND #IsPrivate = 0 AND (exists (select * from dbo.UserInterest where (UserId = U.UserId and InterestId in (select * from dbo.Split(#Interest,','))))
or #Interest ='')
or(#IsPrivate = 1
and (UI.Interestid in (select items from dbo.Split(#GroupId,',')) and INTRST.IsPrivate = 1 and INTRST.IsDelete = 0 and USRSHTOUT.IsDeleted=0 and
USP.ShoutoutId = USRSHTOUT.ShoutoutId and UI.IsActive = 1 and USP.GroupId in (select items from dbo.Split(#GroupId,','))))
)
OR(#Interest ='' AND #IsPrivate = 0 AND (exists (select * from dbo.UserInterest where (UserId = U.UserId and InterestId in (select * from dbo.Split(#Interest,','))))
or #Interest =''))
)
and (#PostTypeId=0 or USRSHTOUT.PostTypeId=#PostTypeId)
)
insert into #UserList
select
UserId,
FirstName,
LastName,
QuickBloxId,
SocialId,
Gender,
Email,
IsProfileImageSync,
IsFriend,
IsRequestSent,
RequestSender,
IsBlocked,
FriendBlocked,
Age,
HomeTownCity,
ImageName,
Distance,
DistanceTemp,
LastActiveDateTime,
ShoutoutGuid,
ShoutoutId,
InterestId,
InterestName,
ShoutoutImageName,
Description,
CreatedOn,
IsImageSync,
DisplayUpdatedOn,
ShoutoutCity,
ShoutoutCountry,
AdministrativeAreaLevel1,
PlaceId,
FormattedAaddress,
Url,
u.TotalLike,
u.TotalComment,
u.Selflike,
ShoutoutTypeId,
ShoutoutType,
PostTypeId,
PostTypeName,
oldMoment,
UserLatitude,
UserLongitude,
ShoutoutLatitude,
ShoutoutLongitude,
DistanceShoutoutMiles
from UserList u
where
(#Gender ='' or ((Gender in(select * from dbo.Split(#Gender,','))) or (#Gender like '%3%' and Gender=0)))
and(
((#AgeFrom = 18) AND ((#AgeTo= 65 and Age >= 15 )
or (Age >= 15 and Age <= #AgeTo)))
OR
((#AgeTo= 65 and Age >= #AgeFrom )
or (Age >= #AgeFrom and Age <= #AgeTo))
)
and IsBlocked <> 1
and FriendBlocked <> 1
and (#Location=0 or ((DistanceShoutoutMiles<= #Radius) or (#IsLocal=1 and HomeTownCity=#HomeTownCity)))
and (#IsFriend=0 or (#IsFriend=1 and IsFriend=1))
and (#Location=0 or (
((#IsTraveler=1 and #IsLocal=1) or (#IsTraveler=0 and #IsLocal=0)) or
((#IsTraveler=1 and #IsLocal=0 and lower(HomeTownCity) != lower(#HomeTownCity))
or (#IsTraveler=0 and #IsLocal=1 and lower(HomeTownCity) = lower(#HomeTownCity)))))OPTION(RECOMPILE)
set #TotalRows = ##rowcount
IF(#CurrentPage=0)
begin
select * from #UserList order by Distance asc OPTION(OPTIMIZE FOR UNKNOWN)
end
ELSE
BEGIN
declare #OffSetSize as bigint
set #OffSetSize = ((#CurrentPage * #PageSize) - #PageSize)
IF (#SortExpression = '1')
BEGIN
SELECT *
FROM #UserList
WHERE CreatedOn<#CreatedDate
ORDER BY CreatedOn DESC
OFFSET #OffSetSize ROWS
FETCH NEXT #PageSize ROWS ONLY
OPTION(OPTIMIZE FOR UNKNOWN)
END
ELSE IF (#SortExpression = '2')
BEGIN
SELECT *
FROM #UserList
WHERE CreatedOn>#CreatedDate
ORDER BY CreatedOn DESC
OFFSET #OffSetSize ROWS
FETCH NEXT #PageSize ROWS ONLY
OPTION(OPTIMIZE FOR UNKNOWN)
END
ELSE IF (#SortExpression = '3')
BEGIN
SELECT *
FROM #UserList
ORDER BY Distance ASC, CreatedOn DESC
OFFSET #OffSetSize ROWS
FETCH NEXT #PageSize ROWS ONLY
OPTION(OPTIMIZE FOR UNKNOWN)
END
ELSE IF (#SortExpression = '4')
BEGIN
SELECT *
FROM #UserList
ORDER BY Distance ASC
OFFSET #OffSetSize ROWS
FETCH NEXT #PageSize ROWS ONLY
OPTION(OPTIMIZE FOR UNKNOWN)
END
ELSE
BEGIN
SELECT *
FROM #UserList
ORDER BY CreatedOn DESC
OFFSET #OffSetSize ROWS
FETCH NEXT #PageSize ROWS ONLY
OPTION(OPTIMIZE FOR UNKNOWN)
END
END
ENDGO

Need to Add Another While in the below query

I've got the below code from one of the experts here in this forum (Which is working fine). So what I'm trying to do is to amend this query further to get the dates the same way but for all tasks. There is a table called "TASK" in the database and each task_id has a clndr_id and proj_id. The desired output would be (proj_id, task_id, clndr_id, date_Value), so I think we need to add another WHILE to get all dates for all tasks in the TASK table. I hope #CurseStacker or anyone else can help :)..thanks.
ALTER FUNCTION [dbo].[GetProjectDates]
(
#project_name varchar(50)
)
RETURNS #temp_tb TABLE([proj_id] int, [clndr_id] int , [date_value] date)
AS
BEGIN
-- Add the SELECT statement with parameter references here
DECLARE #project_id int
DECLARE #clndr_id int
DECLARE #walker int = 0
DECLARE #holder varchar(MAX)
DECLARE #date date
DECLARE #data varchar(MAX)
SELECT #project_id = [p].[proj_id]
,#clndr_id = [p].[clndr_id]
,#holder = [c].[clndr_data]
FROM [PMDB].[dbo].[PROJECT] AS [p]
INNER JOIN [PMDB].[dbo].[CALENDAR] AS [c] ON [p].[clndr_id] = [c].[clndr_id]
WHERE [p].[proj_short_name] = #project_name
WHILE #walker <> LEN(#holder) + 1
BEGIN
IF SUBSTRING(#holder, #walker, 2) = 'd|'
BEGIN
SET #data = SUBSTRING(#holder, #walker, 10)
IF SUBSTRING(#data, LEN(#data) - 2, 3) = ')()'
BEGIN
SET #date = DATEADD(D, CAST(SUBSTRING(#data, 3, 5) AS int) -2, '01/01/1900')
INSERT INTO #temp_tb VALUES (#project_id, #clndr_id, #date)
END
END
SET #walker = #walker + 1
END
RETURN
END
GO
So "TASK" table includes (proj_id, task_id, clndr_id) and from "Calendar" table we can get the clndr_data by linking to proj_id in task table (The above code gets the dates in clndr_data between 'd|' and ')()' for only one clndr_id, and it works fine' Now I need to do the same but for several calendars;
proj_id task_id clndr_id clndr_data
4917 310449 7143 (0||CalendarData()(.....
4917 310450 7144 (0||CalendarData()(.....
4917 310451 7149 (0||CalendarData()(.....
Desired Outcome (Assuming clndr_id 7143 has only 2 dates between 'd|' and ')()', clndr_id 7144 has 3 dates, and cldnr_id 7149 has two dates) JUST AN ASSUMPTION
proj_id task_id clndr_id date
4917 310449 7143 2018-09-24
4917 310449 7143 2018-09-25
4917 310450 7144 2018-09-26
4917 310450 7144 2018-10-01
4917 310450 7144 2018-10-02
4917 310451 7149 2018-10-03
4917 310451 7149 2018-10-04
try something like below:
ALTER FUNCTION [dbo].[GetProjectDates]
(
#project_name varchar(50)
)
RETURNS #temp_tb TABLE([proj_id] int, [task_id] int, [clndr_id] int , [date_value] date)
AS
BEGIN
-- Add the SELECT statement with parameter references here
DECLARE #project_id int
DECLARE #task_id int
DECLARE #clndr_id int
DECLARE #walker int = 0
DECLARE #holder varchar(MAX) = ''
DECLARE #date date
DECLARE #data varchar(MAX)
declare #tab table (project_id int, task_id int, clndr_id int, holder varchar(max))
insert into #tab
SELECT [T].[proj_id] as project_id
,[T].[task_id] as task_id
,[c].[clndr_id] as clndr_id
,[c].[clndr_data] as holder
FROM [PMDB].[dbo].[TASK] as [T]
INNER JOIN [PMDB].[dbo].[CALENDAR] AS [c] ON [T].[clndr_id] = [c].[clndr_id] and [T].[Proj_id] = [c].[proj_id]
WHERE [T].[proj_id] = (select proj_id FROM [PMDB].[dbo].[PROJECT] where [proj_short_name] = #project_name)
declare #cur_task int, #cur_clndr int
set #cur_task = (select top 1 task_id from #tab)
set #cur_clndr = (select top 1 clndr_id from #tab where task_id = #task_id)
set #holder = (select top 1 holder from #tab where task_id = #task_id)
while ((select count(1) from #tab) > 0)
begin
print 'Current loop running for Task_Id' + convert(varchar(10), #cur_task)
WHILE #walker <> LEN(#holder) + 1
BEGIN
IF SUBSTRING(#holder, #walker, 2) = 'd|'
BEGIN
SET #data = SUBSTRING(#holder, #walker, 10)
IF SUBSTRING(#data, LEN(#data) - 2, 3) = ')()'
BEGIN
SET #date = DATEADD(D, CAST(SUBSTRING(#data, 3, 5) AS int) -2, '01/01/1900')
INSERT INTO #temp_tb VALUES (#project_id, #task_id, #clndr_id, #date)
END
END
SET #walker = #walker + 1
END
delete #tab where task_id = #cur_task
set #cur_task = (select top 1 task_id from #tab)
set #cur_clndr = (select top 1 clndr_id from #tab where task_id = #task_id)
set #holder = (select top 1 holder from #tab where task_id = #task_id)
set #walker = 0
end
RETURN
END

MOST RECENT VALUE (CAN ANYONE DO THIS)

ive been told by my collegue to do this
"Perhaps what you can do is run a second table between #Basic and #Product where you only update the new retail column with the top 1 based on valid from desc for each currency code from #Basic?"
Create Table #Basic(
ProductCode Int,
ContractNo Int,
ProductDescription Varchar (max),
CGNo Nvarchar(MAX),
SCGNo Nvarchar(MAX),
EmpNo Int,
CurrencyCode NVARCHAR(MAX),
Retail decimal (38, 2),
ValidFrom datetime,
Active int,
ValidForCountry int
)
Insert Into #Basic
SELECT
c.ProductCode
,ContractNo
,p.Description
,p.CGNo
,p.SCGNo
,p.EmpNo
,r.CurrencyCode
,r.Retail
,r.ValidFrom
,p.active
,c.Validforcountry
FROM CONTRACT c
LEFT JOIN
(SELECT Distinct
ProductCode,
CurrencyCode,
Retail,
ValidFrom
From DIVRETAIL) as r on c.ProductCode = r.ProductCode
LEFT JOIN product p on c.ProductCode = p.ProductCode
WHERE ContractNo = 144546
And r.ValidFrom <= #enddate
SELECT Distinct
ProductCode,
CurrencyCode,
Retail,
ValidFrom
From DIVRETAIL
update #Basic
set Retail =(
select top 1 retail From DIVRETAIL
create table #basicretail
(ProductCode int
,ContractNo int
,Description varchar(MAX)
,CGNo int
,SCGNo int
,EmpNo int
,CurrencyCode varchar(10)
,Retail int
,ValidFrom datetime
,active int
,Validforcountry int
)
CREATE TABLE #Product
(
ProductCode Int,
ContractNo Int,
ProductDescription Varchar (max),
CGNo Nvarchar(MAX),
SCGNo Nvarchar(MAX),
EmpNo Int,
CurrencyCode NVARCHAR(MAX),
Retail decimal (38, 2),
ValidFrom datetime,
Active int,
ValidForCountry int,
Cost decimal (38,3)
)
INSERT INTO #Product
SELECT
ProductCode
,ContractNo
,ProductDescription
,CGNo
,SCGNo
,EmpNo
,CurrencyCode
,Retail
,ValidFrom
,Active
,ValidForCountry
,NULL
FROM #basic
UPDATE #Product
SET #Product.Cost =
(CASE WHEN #Product.CurrencyCode = 'EUR' THEN
(SELECT TOP 1
Case when c.IncoTermtype IN (1,9) then b.DDP/p.Packsize
when c.IncoTermtype IN (2,12) then b.DAT/p.Packsize
when c.IncoTermtype = 10 then (b.FOB+b.Freight)/p.Packsize
when c.IncoTermtype = 13 then (b.ExWorks+Freight)/p.Packsize else 0 End as Casecost
FROM braketvalue b
LEFT JOIN CONTRACT c on b.ContractNo = c.ContractNo
LEFT JOIN PRODUCT p on c.ProductCode = p.ProductCode
WHERE ValidFrom <= #enddate
AND b.DivNo like '8__'
AND #Product.CurrencyCode = 'EUR'
AND #Product.ContractNo = b.ContractNo)
ELSE
(SELECT TOP 1
Case when c.IncoTermtype IN (1,9) then b.DDP/p.Packsize
when c.IncoTermtype IN (2,12) then b.DAT/p.Packsize
when c.IncoTermtype = 10 then (b.FOB+b.Freight)/p.Packsize
when c.IncoTermtype = 13 then (b.ExWorks+Freight)/p.Packsize else 0 End as Casecost
FROM braketvalue b
LEFT JOIN CONTRACT c on b.ContractNo = c.ContractNo
LEFT JOIN PRODUCT p on c.ProductCode = p.ProductCode
WHERE ValidFrom <= #enddate
AND b.DivNo like '7__'
AND #Product.CurrencyCode = '£'
AND #Product.ContractNo = b.ContractNo)
END
)
I think you meant to say >= instead like
WHERE
r.ValidFrom >= '2015-01-07'
As well, move the conditions from WHERE clause to JOIN ON clause
From DIVTAIL) as r on c.ProductCode = r.ProductCode
AND r.ValidFrom >= '2015-01-07'
LEFT JOIN product p on c.ProductCode = p.ProductCode
AND p.CGNo = '01' AND p.SCGNo = '01' AND p.ProductCode = 3317
If you don't want an exact date you might have to do something like a date range that is calculated based on the date passed in. So your JOIN would be like LEFT JOIN ON date between BeginningDate and EndDate

suddenly query executing slowly

I have very typical situation where I'm doing wrong. I have query which have executed fast initially but later on it taking loads of time to execute ..
My query :
Declare
#fromdate varchar(20) = '01/01/2014',
#todate varchar(20)= '27/05/2015',
#SERVICE_ID CHAR(5) = '123'
DECLARE #FDATE DATETIME ,
#TDATE DATETIME
SET #FDATE = (CONVERT(DATETIME,#fromdate,103))
SET #TDATE = (CONVERT(DATETIME,#todate,103))
IF OBJECT_ID('tempdb..#RUID') IS NOT NULL
DROP TABLE #RUID
CREATE TABLE #RUID(
OFFICEID INT,
OFFICE_TITTLE INT,
MAIN_OFFICE_TITTLE VARCHAR(50),
RLB_NAME VARCHAR(20),
DIST_NAME INT,
district_description VARCHAR(30))
CREATE CLUSTERED INDEX IDX_C_RUID_ID ON #RUID(OFFICEID)
CREATE NONCLUSTERED INDEX IDX_RUID_Name ON #RUID(OFFICE_TITTLE,DIST_NAME)INCLUDE(district_description)
INSERT INTO #RUID
SELECT OFFICEID,
OFFICE_TITTLE,
MAIN_OFFICE_TITTLE,
RLB_NAME,
DIST_NAME,
D.district_description
FROM APSDC..DISTRICT D
INNER JOIN cdma..Unified_RUID_WARD_MSTR I WITH(NOLOCK)
ON D.CDMA_DistrictID = I.DIST_NAME
WHERE RLB_NAME in(3) AND I.STATEID ='01'
select C.MAIN_OFFICE_TITTLE AS 'OFFICE_TITTLE',C.officeid, C.DIST_NAME AS DistrictName, C.district_description,
ISNULL(count(I.ApplicationNumber),0) 'Total_Trans',
isnull(sum(case when Data_Available='Y' AND DataTampered = 'N' then 1 else 0 end),0) 'CategoryA'
from #RUID c with(nolock)
LEFT JOIN Unified_BirthDeathAppDetails I WITH(NOLOCK) ON
(C.OFFICE_TITTLE=I.RUID AND C.DIST_NAME=I.DistrictName)
AND I.Service_Type= '01' AND
(DATEADD(DD,0,DATEDIFF(DD,0,I.Created_Date))) BETWEEN #FDATE AND #TDATE
AND NOT EXISTS(select application_number from reversal_details WITH(NOLOCK) WHERE ApplicationNumber <> i.ApplicationNumber AND service_id='123' )
group by C.MAIN_OFFICE_TITTLE,C.officeid, C.DIST_NAME,C.district_description
order by C.district_description ,C.MAIN_OFFICE_TITTLE
I have tried with #temp table and table variable but it is not even showing any result set. But the same query executed in 2 secs now it is taking lot of time. I have tried UPDATE Statstics on this tables and I have checked with locking also. What I need to do I have followed every other peformance optimized techinique.
Try:
Declare
#FDate Date = '01/01/2014',
#TDate Date= '27/05/2015',
#SERVICE_ID CHAR(5) = '123'
;WITH RUID
AS
(
SELECT OFFICEID,
OFFICE_TITTLE,
MAIN_OFFICE_TITTLE,
RLB_NAME,
DIST_NAME,
D.district_description
FROM APSDC..DISTRICT D
INNER JOIN cdma..Unified_RUID_WARD_MSTR I
ON D.CDMA_DistrictID = I.DIST_NAME
WHERE RLB_NAME in(3) AND I.STATEID ='01'
),
AppDetails
AS
(
SELECT ApplicationNumber,
CASE WHEN Data_Available='Y' AND DataTampered = 'N'
THEN 1
ELSE 0
END CategoryA
FROM Unified_BirthDeathAppDetails I
WHERE I.CreateDate >= #FDate AND I.CreatedDate < #TDate AND
NOT EXISTS
( select application_number
FROM reversal_details
WHERE I.Service_Type= '01' AND
ApplicationNumber <> i.ApplicationNumber AND
service_id= #Service_iD
)
)
SELECT C.MAIN_OFFICE_TITTLE AS OFFICE_TITTLE
,C.officeid, C.DIST_NAME AS DistrictName
, C.district_description
,ISNULL(count(I.ApplicationNumber),0) Total_Trans
,isnull(sum(CategoryA),0) CategoryA
FROM RUID c
LEFT JOIN AppDetails I
ON C.OFFICE_TITTLE=I.RUID AND C.DIST_NAME=I.DistrictName
GROUP BY C.MAIN_OFFICE_TITTLE
,C.officeid
,C.DIST_NAME
,C.district_description
ORDER BU C.district_description
,C.MAIN_OFFICE_TITTLE
Make sure you have decent indexes on RLB_name, StateId, CDMA_DistrictId, Dist_Name, CreatedDate etc.

Comparing Sum from 2 different Columns

I'm trying to compare the sum of 2 different columns and I'm getting an error saying that I must declare #Base. Then I have tried to do something like #Base AS B, the error will disappear. But I'm not retrieving any data.
Can anyone help me with if I have made a typo or my INNER JOIN is wrong?
Declare #Base table(PickupDate smalldatetime, DeliveryDate smalldatetime, PickupAdrID int, PickupCustID varchar(10), DeliveryType char, DeliveryAdrID int, DeliveryCustID varchar(10), DeliveryAlias varchar (30), Volumen float, Weight float) Insert #Base(PickupDate,DeliveryDate, PickupAdrID, PickupCustID, DeliveryType, DeliveryAdrID, DeliveryCustID, DeliveryAlias, Volumen,Weight)
SELECT PickupDate,DeliveryDate, PickupAdrID, PickupCustID, DeliveryType, DeliveryAdrID, DeliveryCustID, DeliveryAlias, Volumen, Weight
FROM Sending
INNER JOIN Address_ViewI ON Sending.PickupAdrID = Address_ViewI.AdrID
INNER JOIN Address_ViewI AS Address_View_DE ON Sending.DeliveryAdrID = Address_View_DE.AdrID
WHERE (Address_ViewI.CountryUK = #puC AND Address_View_DE.CountryUK = #deC) AND (Sending.PickupDate >= #start) AND (Sending.PickupDate < #end) AND ((PickUpCustID Like 'TMHSE' OR DeliveryCustID like 'TMHSE' ) )
OR (Address_ViewI.CountryUK = #puC AND Address_View_DE.CountryUK = #deC) AND (Sending.PickupDate >= #start) AND (Sending.PickupDate < #end) AND ((PickUpCustID Like 'SomeName' OR DeliveryCustID like 'SomeName' ) )
SELECT totals.DeliveryAdrID, totals.PickupDate,
(CASE WHEN weightTOTAL <= volumenTOTAL THEN volumenTOTAL
WHEN weightTOTAL >= volumenTOTAL THEN weightTOTAL ELSE weightTOTAL END) AS InvoiceWeight
FROM #Base INNER JOIN
(SELECT DeliveryAdrID, CONVERT(CHAR(10),PickupDate,110) AS PickupDate,
CEILING(SUM(CASE Weight When 0 Then #zeroKiloVal ELSE Weight END)) AS WeightTOTAL,
CEILING(SUM(CASE Volumen WHEN 0 THEN (#zeroVoluVal * #zeroVoluFac) ELSE Volumen END)) AS volumenTOTAL,
COUNT(DeliveryAdrID)AS Packages
FROM #Base GROUP BY CONVERT(CHAR(10),PickupDate,110), DeliveryAdrID ) AS totals
ON #Base.DeliveryAdrID = totals.DeliveryAdrID AND CONVERT(CHAR(10),#Base.PickupDate,110) = totals.PickupDate
The full code is listed here http://pastie.org/8238866
And the error I'm getting
It worked for me when I placed an alias on the reference to #Base
Declare #zeroKiloVal float = 10
Declare #zeroVoluVal float = 10
Declare #zeroVoluFac float = 200
Declare #puC varchar = 'Sweden'
Declare #deC varchar = 'Sweden'
Declare #start smalldatetime = '2013-04-21'
Declare #end smalldatetime = '2013-05-01'
DECLARE #Base TABLE (SendingID INT, Barcode VARCHAR(50), PickupType CHAR, PickupDate SMALLDATETIME, DeliveryDate SMALLDATETIME, PickupAdrID INT, PickupCustID VARCHAR(10), DeliveryType CHAR, DeliveryAdrID INT, DeliveryCustID VARCHAR(10), DeliveryAlias VARCHAR (30), Volumen FLOAT, [Weight] FLOAT)
INSERT INTO #Base(SendingID, Barcode, PickupType, PickupDate,DeliveryDate, PickupAdrID, PickupCustID, DeliveryType, DeliveryAdrID, DeliveryCustID, DeliveryAlias, Volumen,[Weight])
SELECT SendingID = 1, Barcode= 1, PickupType= 1, PickupDate= 1,DeliveryDate= 1, PickupAdrID= 1, PickupCustID= 1, DeliveryType= 1, DeliveryAdrID= 1, DeliveryCustID= 1, DeliveryAlias= 1, Volumen= 1, [Weight] = 1
-- Replacing below code with stubbed data for testing.
-- SELECT SendingID, Barcode, PickupType, PickupDate,DeliveryDate, PickupAdrID, PickupCustID, DeliveryType, DeliveryAdrID, DeliveryCustID, DeliveryAlias, Volumen, Weight
-- FROM Sending
-- INNER JOIN Address_ViewI ON Sending.PickupAdrID = Address_ViewI.AdrID
-- INNER JOIN Address_ViewI AS Address_View_DE ON Sending.DeliveryAdrID = Address_View_DE.AdrID
-- WHERE (Address_ViewI.CountryUK = #puC AND Address_View_DE.CountryUK = #deC) AND (Sending.PickupDate >= #start) AND (Sending.PickupDate < #end) AND ((PickUpCustID Like 'TMHSE' OR DeliveryCustID like 'TMHSE' ) )
-- OR (Address_ViewI.CountryUK = #puC AND Address_View_DE.CountryUK = #deC) AND (Sending.PickupDate >= #start) AND (Sending.PickupDate < #end) AND ((PickUpCustID Like 'TMHSE' OR DeliveryCustID like 'TMHSE' ) )
SELECT totals.DeliveryAdrID
, totals.PickupDate
, InvoiceWeight =
(
CASE WHEN weightTOTAL <= volumenTOTAL THEN volumenTOTAL
WHEN weightTOTAL >= volumenTOTAL THEN weightTOTAL ELSE weightTOTAL END
)
FROM #Base AS B -- <<Added alias here>>
INNER JOIN
(
SELECT DeliveryAdrID
, PickupDate = CONVERT(CHAR(10),PickupDate,110)
, WeightTOTAL = CEILING(SUM(CASE [Weight] WHEN 0 THEN #zeroKiloVal ELSE [Weight] END))
, volumenTOTAL = CEILING(SUM(CASE Volumen WHEN 0 THEN (#zeroVoluVal * #zeroVoluFac) ELSE Volumen END))
, Packages = COUNT(DeliveryAdrID)
FROM #Base
GROUP BY CONVERT(CHAR(10),PickupDate,110), DeliveryAdrID
) AS totals ON B.DeliveryAdrID = totals.DeliveryAdrID
AND CONVERT(CHAR(10),B.PickupDate,110) = totals.PickupDate
Maybe you need to use some wildcards in the constants being compared with the Like operator, such as:
PickUpCustID Like '%TMHSE%' OR DeliveryCustID like '%TMHSE%'
Otherwise, I think you're just doing the same as
PickUpCustID = 'TMHSE' OR DeliveryCustID = 'TMHSE'
or
'TMHSE' in (PickUpCustID, DeliveryCustID)
I figured out the error it seems that my declared variables was missing something:
Declare #puC varchar = 'Sweden'
Declare #deC varchar = 'Sweden'
I changed it to
Declare #puC varchar (50) = 'Sweden'
Declare #deC varchar (50) = 'Sweden'
Thanks for your time guys