SQL Temp Table Cost 75% Insert - sql

I have been looking for ways to reduce this Table Insert cost down from 75%. The one way that I was thinking we be to create the temp table before the insert but that didnt help is there any other way to improve insert into Temp Table.
#StartDate date = null,
#EndDate date = null
IF #StartDate IS NULL AND #EndDate IS NULL
BEGIN
SET #StartDate = DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) + -12, 0)
SET #EndDate = DATEADD(MILLISECOND, -3, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0))
END
SELECT CustomerId, LogDate, StartDate = #StartDate, EndDate = #EndDate
INTO #Temp1
FROM Log l
where l.LogDate >= #StartDate and l.LogDate < dateadd(day, 1, #EndDate)
select
l.*,
a.StateId,
a.CountryId
into #Temp
from #Temp1 l
JOIN [Address] a ON a.CustomerId = l.CustomerId

Related

DateDiff In months between two different types date formats

I have two date formats, EndDate in YYYYMMDD format and another Monthfirstdate in YYYY-MM-DD FORMAT. I need to get the difference between these two dates.
SET NOCOUNT ON
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
DECLARE #Date DATE = dbo.value('DATE')
SELECT
E.EmployeeID
,E.StartDateKey
,E.EndDateKey
,E.FromDateKey
,E.ToDateKey
INTO #Employee
FROM
dbo.Employee E
---clean--
DECLARE #StartDate DATE = (SELECT DATEADD(YY, DATEDIFF(YY, 0, #Date) - 1, 0))
DECLARE #EndDate DATE = DATEADD(S, -1, DATEADD(MM, DATEDIFF(MM, 0, #Date) + 3, 0))
SELECT
E.EmployeeID
,CAST(CONVERT(VARCHAR(10), E.StartDateKey, 112) AS INT) AS _EmployeeStartDateKey
,CAST(CONVERT(VARCHAR(10), E.EndDateKey, 112) AS INT) AS _EmployeeFinishDateKey
,CAST(CONVERT(VARCHAR, D.MonthFirstDay, 112) AS INT) AS _DateKey
,???? AS [Tenure]
FROM #Employee AS E
INNER JOIN dbo.Date D ON
D.DateKey BETWEEN E.FromDateKey AND E.ToDateKey
WHERE
D.DateKey BETWEEN #tartDate AND #EndDate
AND D.DayOfMonth = 1
You can try below query in SQL Server:
DECLARE
#firstDate DATETIME = '2022-12-01',
#dateinStringFormat VARCHAR(20)= '20121201',
#endDate DATETIME;
--Converting date string to Datetime format(YYYY-MM-DD)
SELECT #endDate = CONVERT(CHAR(10), CONVERT(datetime, #dateinStringFormat), 120);
--Now, Calculating the date difference in Month
SELECT DATEDIFF(MONTH, #endDate, #firstDate) AS DateDiff;

Debit credit query but with same dates

I am facing issue in getting balance from debit credit record. But in my table dates are same but debit credit is different.
declare #StartDate Datetime = '2018-11-03';
declare #EndDate Datetime = '2018-11-03';
Select
y.Description,
y.DateTime,
y.Credit,
y.Debit,
SUM(ISNULL(x.Debit, 0) - ISNULL(x.Credit, 0)) AS Balance
FROM Accounts x
INNER JOIN Accounts y ON x.DateTime <= y.DateTime
WHERE
CONVERT(date, y.DateTime) >= CONVERT(date, #StartDate)
AND
CONVERT(date, y.DateTime) <= CONVERT(date, #EndDate)
GROUP BY
y.DateTime,
y.Credit,
y.Debit,
y.Description
Order By
y.DateTime
And my result is not what i want.
you can achieve this by using window function
select
DateTime,
Credit,
Debit,
(select sum(Debit - Credit) from Account a where a.Datetime < t.DateTime) +
Sum(Debit - Credit) Over (Order by DateTime) balance
from Account t
If I'm reading your question and comments correctly this is what you are looking for:
declare #StartDate Datetime = '2017-11-03';
declare #EndDate Datetime = '2019-11-03';
Select DateTime, Credit, Debit
, -- Get the prior Balance
(select sum(ISNULL(Debit, 0) - ISNULL(Credit, 0))
from Account p
where CONVERT(date, p.DateTime) < CONVERT(date, #StartDate))
-- and add it to the running total
+ sum(ISNULL(t.Debit, 0) - ISNULL(t.Credit, 0))
over (order by DateTime, id) Balance
from Account T
where CONVERT(date, DateTime) >= CONVERT(date, #StartDate)
and CONVERT(date, DateTime) <= CONVERT(date, #EndDate)
order by DateTime, ID
USE [YourDataBase]
GO
/****** Object: StoredProcedure [dbo].[Balance_Sheet] Script Date: 5/16/2019 3:13:53 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[Balance_Sheet]
#StartDate datetime,
#EndDate datetime
AS
BEGIN
Select
x.DateTime,
x.Comments,
x.Credit,
x.Debit,
SUM(coalesce(y.Credit, 0) - coalesce(y.Debit, 0)) AS Balance
FROM Balance x
INNER JOIN Balance y
ON y.DateTime <= x.DateTime
where x.DateTime >=dateadd(day,datediff(day,0,#StartDate),0) and x.DateTime<dateadd(day,datediff(day,0,#EndDate)+1,0)
GROUP BY
x.DateTime,
x.Credit,
x.Debit,
x.Comments
END

Monthly wise count using date calculation

I have the table named company with columns company name, create date, etc.
I want to get the count of companies created for this month.
I have the stored procedure for weekly wise count
CREATE PROCEDURE [dbo].[pr_NewCmpCount]
#StartDate DATETIME = NULL --'20130508'
, #EndDate DATETIME = NULL --'20130515'
AS
BEGIN
DECLARE #DateDiff INT
, #MainDate DATETIME
IF #StartDate IS NULL AND #EndDate IS NULL
BEGIN
IF (DATEPART(dw,GETDATE()) > 3)
SET #DateDiff = 0
ELSE
SET #DateDiff = 6
SET #MainDate = DATEADD (MINUTE, -30, DATEADD (HOUR, -5, DATEADD(wk, DATEDIFF(wk,#DateDiff,GETDATE()), 0)))
SELECT #StartDate = DATEADD (MINUTE, -30, DATEADD (HOUR, -5, DATEADD(wk, DATEDIFF(wk,0,#MainDate), -5)))
SELECT #EndDate = DATEADD (MINUTE, -30, DATEADD (HOUR, -5, DATEADD(wk, DATEDIFF(wk,0,#MainDate), 2)))
SELECT CONVERT (VARCHAR, Count(*)) AS [Count]
, CONVERT (DATE, DATEADD (DD, 1, #StartDate)) AS [StartDate]
, CONVERT (DATE, #EndDate) AS [EndDate]
FROM
Company WITH(NOLOCK)
WHERE CreateDate >= #StartDate
AND CreateDate < #EndDate
END
ELSE
IF ((CONVERT (DATE, #StartDate) < CONVERT (DATE, #EndDate)) AND (CONVERT (DATE, #EndDate) < CONVERT (DATE, GETDATE())))
BEGIN
SET #StartDate = CONVERT (DATE, #StartDate)
SET #EndDate = CONVERT (DATE, #EndDate)
SELECT #StartDate = DATEADD (MINUTE, -30, DATEADD (HOUR, -5, #StartDate))
SELECT #EndDate = DATEADD (MINUTE, 30, DATEADD (HOUR, 18, #EndDate))
SELECT CONVERT (VARCHAR, Count(*)) AS [Count]
, CONVERT (DATE, DATEADD (DD, 1, #StartDate)) AS [StartDate]
, CONVERT (DATE, #EndDate) AS [EndDate]
FROM
Company WITH(NOLOCK)
WHERE CreateDate >= #StartDate
AND CreateDate < #EndDate
END
ELSE
SELECT 'Please Run After Tuesday of Every Week or Given the Valid Date' [Count]
, CONVERT (DATE, #StartDate) AS [StartDate]
, CONVERT (DATE, #EndDate) AS [EndDate]
END
Output for this stored procedure is
Count Startdate EndDate
10 2016-03-13 2016-03-22
Expected output
Count Startdate EndDate
20 2016-02-01 2016-02-29
Where do I need to modify in my stored procedure to get monthly wise count?
Something like this:
DECLARE #StartDt Date
DECLARE #EndDt date
SELECT #StartDt,#EndDt,Count(*) AS TotalCompanies FROM company
WHERE [create date] BETWEEN #StartDt AND #EndDt
I skipped the stored procedure formatting for simplicity.

Find the Same day of Previous Year Given by Current Year Date in SQL Server

I am working with SQL Server, The scenario is to find out the Same Day's Date of Previous Year as of Today's Day.
Suppose 2014-03-06 is Today Date and Day is Thursday I want to Find the Same day in Previous lies in the same week .which is 2013-03-07
can any body help?
HERE is what i Have Written:
DECLARE #DateFrom AS DATETIME
DECLARE #DateTo AS DATETIME
SET #DateFrom = '2014-01-01'
SET #DateTo = '2014-02-10'
DECLARE #Count AS INT
SET #Count = DATEDIFF(DAY, #DateFrom, #DateTo)
CREATE TABLE #current_year /*This Year*/
(
[Date] DATETIME ,
WeekNum INT,
[Day] VARCHAR(20),
Data INT
)
CREATE TABLE #last_year /*This Year -1*/
(
[Date] DATETIME ,
WeekNum INT,
[Day] VARCHAR(20),
Data INT
)
WHILE ( #Count > 0 )
BEGIN
INSERT INTO #current_year
VALUES ( CONVERT(VARCHAR(10), #DateFrom, 101),
DATEPART(week,#DateFrom),
DATENAME(weekday, #DateFrom),#Count)
INSERT INTO #last_year
VALUES ( CONVERT(VARCHAR(10), DATEADD(YEAR, -1, #DateFrom), 101),
DATEPART(week,DATEADD(YEAR,1,#DateFrom)),
DATENAME(weekday, DATEADD(YEAR, -1, #DateFrom)),#Count)
SET #DateFrom = DATEADD(day, 1, #DateFrom)
SET #Count = #Count - 1
END
SELECT * from #current_year
SELECT * from #last_year
SELECT CONVERT(varchar(10),#current_year.[Date],111) AS CYDate,
--ISNULL(CONVERT(varchar(10),#last_year.[Date],111) ,/*CONVERT(varchar(10),DateAdd(dd, 1, DATEADD(yy, -1, #current_year.Date)),111)*/) AS LYDate
--CONVERT(varchar(10),#last_year.[Date],111) AS LYDate
Coalesce(CONVERT(varchar(10),#last_year.[Date],111) ,DateAdd(dd, 1, DATEADD(yy, -1, #current_year.Date))) AS LYDate,
#current_year.Data AS CD,
#last_year.Data AS LD
FROM #current_year
--LEFT JOIN #last_year ON #last_year.WeekNum = #current_year.WeekNum
-- AND #last_year.[Day] = #current_year.[Day]
Left JOIN #last_year ON #last_year.WeekNum = DatePart(wk, GETDATE())
DROP TABLE #current_year
DROP TABLE #last_year
Here is the Output:
Here is the output after adding your solution, now in left join it excludes (NULL) data of previous year
Basically you need to find difference in days between same dates in this and previous years, then understand "day difference" by mod 7, and sum it with date in previous year:
DECLARE #now DATETIME
SET #now = '2014-03-06'
SELECT CAST (DATEADD(YEAR, -1, #now) + (CAST (#now as INT) - CAST (DATEADD(YEAR, -1, #now) AS INT)) % 7 AS DATE)
Returns
2013-03-07
Try
DECLARE #now Date
SET #now = '2014-06-03'
SELECT DATEADD(week, -52, #now)
SELECT DateName(dw, DATEADD(yy, -1, GETDATE()))
gives Wednesday
SELECT DateName(dw, DateAdd(dd, 1, DATEADD(yy, -1, GETDATE())))
gives Thursday
edit:
SELECT DateAdd(dd, 1, DATEADD(yy, -1, GETDATE()))
gives '2013-03-07 17:30:16.590'
you need to cast the date as per you requirement..
update:
change this part with,
Left JOIN #last_year ON #last_year.WeekNum = DatePart(wk, GETDATE())
in your case:
Left JOIN #last_year ON DatePart(wk, #last_year.[Date]) = DatePart(wk, #current_year.[Date])
update 2:
Left JOIN #last_year ON (MONTH(#last_year.[Date])=MONTH(#current_year.[Date]) and Day(#last_year.[Date])=Day(#current_year.[Date]))
Output:
or
output:
Left JOIN #last_year ON (#last_year.WeekNum = #current_year.WeekNum and #last_year.[Day] = #current_year.[Day])
choose which ever is your required output.
DECLARE #Date DATE = '2014-03-06'
SELECT DATEADD(WEEK,-52,#Date)
Retrun value :
2013-03-07

Returns date range by quarter?

I am looking for a query that can returns a series of date range that is one quarter long.
For example, if the input is 2/1/2013 and 3/31/2014, then the output would look like:
start end
2/1/2013 4/30/2013
5/1/2013 7/31/2013
8/1/2013 10/31/2013
11/1/2013 1/31/2014
2/1/2014 3/31/2014
Notice that the last quarter is only 2 months long. Thanks for help in advance.
Just want to add that this is what I did after I did a bit of googling. I was thinking of some more efficient way but I think this is sufficient for my purpose. The first part is to populate a date table, the second part to calculate the quarter range.
DECLARE #StartDate SMALLDATETIME
DECLARE #EndDate SMALLDATETIME
SET #StartDate = '1/1/2011'
SET #EndDate = '12/31/2011'
-- creates a date table, not needed if there is one already
DECLARE #date TABLE ( [date] SMALLDATETIME )
DECLARE #offset INT
SET #offset = 0
WHILE ( #offset < DATEDIFF(dd, #StartDate, DATEADD(dd, 1, #EndDate)) )
BEGIN
INSERT INTO #date ( [date] )
VALUES ( DATEADD(dd, #offset, #StartDate) )
SELECT #offset = #offset + 1
END ;
WITH dateCTE
AS ( SELECT ROW_NUMBER() OVER ( ORDER BY [date] ASC ) AS qID ,
[date] AS qStart ,
CASE WHEN DATEADD(dd, -1, DATEADD(q, 1, [date])) > #EndDate
THEN #EndDate
ELSE DATEADD(dd, -1, DATEADD(q, 1, [date]))
END AS qEnd
FROM #date
WHERE [date] = #StartDate
OR ( DATEDIFF(mm, #StartDate, [date]) % 3 = 0
AND DATEPART(dd, [date]) = DATEPART(dd,
#StartDate)
)
)