Retrieve customers with more than one loan - sql

I'm struggling to write a SQL query that will identify customers who have taken out multiple loans in a short period of time (say 60 days).
MyTable
CREATE TABLE [dbo].[MyTable](
[Customerid] [numeric](18, 0) NOT NULL,
[Date Open] [date] NOT NULL,
[Date Closed] [date] NOT NULL,
[Rank] [varchar](50) NOT NULL
) ON [PRIMARY]
E.g. - Customerid 1 has taken multiple loans with date open say 12/01/2017, 12/02/2017, 13/04/2018 etc. I need to find the customers who have taken next loan (Date Open) within 60 days.

It looks like you can do this with a simple INNER JOIN. I'm not entirely sure what Rank indicates in your table, but something like below should work if they can't take out two loans on the same day.
SELECT DISTINCT yt1.Customerid
FROM yourtable yt1
INNER JOIN yourtable yt2 ON yt2.Customerid = yt1.Customerid AND
yt2.[Date Open] > yt1.[Date Open] AND
yt2.[Date Open] <= DATEADD(DAY, 60, yt1.[Date Open])

Related

Trying to find the syntax for multiplying DateDiff * number of rent days, then add to the amount due column

Write a query to assign values to the Amount_Due column in the Transaction row based on the Price of the Car and the number of days of the rent.
The amount due will be calculated as (Price * number of days),
Number of days can be calculated by using pickup and return dates.
I have found how to calculate the Number of days:
SELECT DATEDIFF(d, PickupDate, ReturnDate) AS 'Number of days', CAR.Price, Amount_Due
FROM [TRANSACTION]
INNER JOIN CAR ON CAR.CarID = [TRANSACTION].CarID
But I tried to assign the values back with this statement, but it doesn't seem to do anything:
SELECT DATEDIFF(d, PickupDate, ReturnDate) AS 'Number of days', CAR.Price, Amount_Due
FROM [TRANSACTION]
INNER JOIN CAR ON CAR.CarID = [TRANSACTION].CarID
WHERE Price * DATEDIFF(d, PickupDate, ReturnDate) = Amount_Due;
Table Schema:
CREATE TABLE [TRANSACTION] (
tID int PRIMARY KEY,
cID int REFERENCES CUSTOMER(cID),
CarID int REFERENCES CAR(CarID),
eID int REFERENCES EMPLOYEE(eID),
tDate date NOT NULL,
PickupDate date NOT NULL,
ReturnDate date NOT NULL
);
CREATE TABLE CAR (
CarID int PRIMARY KEY,
Make varchar(30) NOT NULL,
Model varchar(30) NOT NULL,
type varchar(50) NOT NULL,
year date NOT NULL,
Price Mmoney NOT NULL
);
Your query will return the Number of days but you have not done any Assignment. To do that you will need to write an update statement.
Before we go that step, make sure you have correctly calculated the Expected Amount_Due by including your calculation output and the input fields in a single select:
SELECT PickupDate,
ReturnDate,
DATEDIFF(d, PickupDate, ReturnDate) AS 'Number of days',
CAR.Price,
Car.Price * DATEDIFF(d, PickupDate, ReturnDate) as Amount_Due
FROM [TRANSACTION]
INNER JOIN CAR ON CAR.CarID = [TRANSACTION].CarID
If the amount due matches your expectation then the following UPDATE statement could be used
UPDATE [TRANSACTION]
SET Amount_Due = Car.Price * DATEDIFF(d, t.PickupDate, t.ReturnDate)
FROM [TRANSACTION] t
INNER JOIN CAR ON CAR.CarID = t.CarID
This solution is tested against MS SQL Server, your RDBMS might have slightly different syntax for UPDATE that has joins, in MySQL for instance the JOIN needs to be specified before SET
This query will update all records, in the real world you would limit this with some specific criteria, like where the Amount_Due IS NULL or there would be some sort of criteria on the transaction or the car where you would no longer want to update the transaction.

SQL, join workdate to pay rate by effective date

I have 2 tables, one with employee work dates and one with pay rates for each employee and when those rates took effect.
They look like this:
CREATE TABLE pay_rates
(
[EMP ID] [int] NOT NULL,
[RATE] [decimal](11, 6) NOT NULL,
[EFFECTIVE DATE] [datetime] NOT NULL
)
CREATE TABLE work_dates
(
[EMP ID] [int] NOT NULL,
[WORK DATE] [datetime] NOT NULL,
)
The pay_rates table will have many entries for each employee as their pay was adjusted over the years.
The work_dates table contains dates each employee worked.
Both of these tables contain other information, but I've simplified down to the relevant columns.
I would like to join the two such that I can see the what the pay rate would have been on each work date. More technically I want to join each record in work_dates to a record in pay_rates where with the largest EFFECTIVE DATE that is less than the WORK DATE and where the EMP ID matches.
Perhaps when you join pay_rates to work_dates, you could use not exists to require that no rate exists whose effective date is on or before the work date and is strictly after the effective date of the rate to which you're joining. Something like this:
declare #pay_rates table (
[EMP ID] [int] NOT NULL,
[RATE] [decimal](11, 6) NOT NULL,
[EFFECTIVE DATE] [datetime] NOT NULL
);
declare #work_dates table (
[EMP ID] [int] NOT NULL,
[WORK DATE] [datetime] NOT NULL
);
insert #pay_rates values
(1, 10.25, '20160615'),
(1, 10.65, '20170101'),
(1, 11.85, '20180101');
insert #work_dates values
(1, '20160701'),
(1, '20170101'),
(1, '20170701'),
(1, '20171231'),
(1, '20180102');
select
WorkDate.[EMP ID],
WorkDate.[WORK DATE],
Rate.RATE,
Rate.[EFFECTIVE DATE]
from
#work_dates WorkDate
inner join #pay_rates Rate on
WorkDate.[EMP ID] = Rate.[EMP ID] and
WorkDate.[WORK DATE] >= Rate.[EFFECTIVE DATE] and
not exists
(
select 1
from #pay_rates LaterRate
where
LaterRate.[EMP ID] = WorkDate.[EMP ID] and
LaterRate.[EFFECTIVE DATE] <= WorkDate.[WORK DATE] and
LaterRate.[EFFECTIVE DATE] > Rate.[EFFECTIVE DATE]
);
I have assumed here that you do not allow two pay rates to have taken effect on the same date for the same employee. In that case, the above query would give you two results for each work date that uses those rates, since it has no way to prioritize one over the other.
I think you need:
SELECT pr2.[emp id], pr2.rate
FROM pay_rates pr2
INNER JOIN
(SELECT wd.[emp id], MAX(pr.[effective date]) AS mx_date
FROM work_dates wd
LEFT JOIN pay_rates pr
ON (wd.[emp id] = pr.[emp id] AND wd.[work date] > pr.[effective date])
GROUP BY wd.[emp id]) sub
ON pr2.[emp id] = sub.[emp id] AND pr2.[effective date] = sub.mx_date
In the sub-query, select the largest effective day per emp id that is less than the employee's work date. Then join the result back to the pay rates table to pull in the rate.

SQL - counts for two days

I have a table like
CREATE TABLE [dbo].[Log](
[Id] [BIGINT] IDENTITY(1,1) NOT NULL,
[GuidId] [UNIQUEIDENTIFIER] NOT NULL,
[TableName] [VARCHAR](50) NULL,
[CreatedDate] [DATETIME] NULL,
[Operation] [VARCHAR](50) NULL,
[Status] [VARCHAR](50) NULL,
[UserId] [VARCHAR](50) NOT NULL)
Am trying to get query like
SELECT TOP 25 UserId, TableName, Operation, COUNT(1) Records
FROM dbo.Log
WHERE CreatedDate > GETDATE() - 1
AND Status='failed'
GROUP BY UserId, TableName,Operation
I need to add another column to have output of count that has GetDate() - 7 criteria too in the same select.
Share some thoughts
Something like this?
SELECT TOP 25
UserId
, TableName
, Operation
, SUM(CASE WHEN CreatedDate > GETDATE() - 1 THEN 1 ELSE 0 END) Records1Day
, COUNT(1) Records7Days
FROM dbo.Log
WHERE
CreatedDate > GETDATE() - 7
AND Status='failed'
GROUP BY
UserId
, TableName
, Operation
If I understand correctly, you want the most recent UserID fails that had a MAX(CreatedDate) > yesterday. The subquery dT, below does this using MAX(CreatedDate) for the user ID, and HAVING to filter. This is Ordered in descending faildate order. I subqueried because you added the condition that you want to know the date for 7 days prior to this, and the receiving query does this... using DATEADD to subtract 7 days from the [Most Recent UserID Failure] date.
SELECT dT.UserId
,dT.[Most Recent UserID Failure]
,DATEADD(DAY, -7, dT.[Most Recent UserID Failure]) AS [7 Days ago]
,(SELECT COUNT (*)
FROM #Log
WHERE UserId = dT.UserID
AND CreatedDate >= DATEADD(DAY, -7, CAST(dT.[Most Recent UserID Failure] as date))
AND CreatedDate <= dT.[Most Recent UserID Failure]
AND Status = 'failed'
) AS [Num Fails]
FROM (
SELECT TOP 25
UserID
,MAX(CreatedDate) AS [Most Recent UserID Failure]
FROM Log
WHERE Status = 'failed'
GROUP BY UserID
HAVING MAX(CreatedDate) > GETDATE() - 1
ORDER BY [Most Recent UserID Failure] DESC
) AS dT

How do I count overlapping vacation days?

I have a Timesheet table in SQL Server which stores personnel roles
CREATE TABLE [dbo].[TimeSheetTable](
[pk_TimeSheetID] [bigint] IDENTITY(1,1) NOT NULL,
[fk_PersonelID] [int] NOT NULL,
[StartDate] [datetime] NOT NULL,
[EndDate] [datetime] NOT NULL,
[fk_RoleID] [int] NOT NULL,
[Comments] [nvarchar](2000) NULL,
[dateCreated] [datetime] NOT NULL,
[CreatedBy] [int] NULL,
Sometimes, an employee can take a vacation, with overlapping months, starting from 28-Oct-2012 to 2-Nov-2012. With the above example, the employee had 4 days in October and 3 days in November.
My problem is writing a query that counts the number of days of vacation taken for each. If the enddate exceeds the month, it shouldn't count that.
SELECT TOP 1000
sum(DATEDIFF(day,startdate,EndDate))
FROM [db_ASCO_Personel].[dbo].[TimeSheetTable]
where fk_RoleID=51 /* how do I count leave days that over lap between two months*/
Row #17 has a 1 day in nov and 2days in dec( total 3 days vacation). how do I count the number of vacation days taking by each employee for 1 month alone?
TimeSheetID StartDate EndDate RoleID Comments dateCreated CreatedBy
15 August-6-2012 August-7-2012 51 03:10.6 NULL
16 August-1-2012 August-2-2012 51 03:31.5 NULL
17 November-29-2012 December-3-2012 51 51:15.5 NULL
You need to know the start and end of the month. Something like this:
with const as (
select cast('2012-09-01' as date) as MonthStart, cast('2012-09-30' as date) as MonthEnd
)
select sum(DATEDIFF(day,
(case when startdate < const.MonthStart then const.MonthStart else startdate end),
(case when enddate > const.MonthEnd then const.MontEnd else ndDate end)
)
FROM [db_ASCO_Personel].[dbo].[TimeSheetTable] tst cross join
const
where fk_RoleID=51 and
startdate <= const.MonthEnd and
enddate >= const.MonthStart
I would just grab the data and calculate with c# and put into a new table (or do whatever needs to be done with the values)
but if you can be sure that vacation time will never be more than an entire month (ex start is jan and end is never later than feb) then you could do some if else
SELECT TOP 1000 CASE
WHEN month(startDate) = month(enddate)
THEN sum(DATEDIFF(day,startdate,EndDate)) as daymonth1, 0 as daysmonth2
ELSE sum(DATEDIFF(day,startdate,EndDate)) - day(enddate) as daymonth1,day(enddate) as daysmonth2
END
FROM [db_ASCO_Personel].[dbo].[TimeSheetTable]
where fk_RoleID=51
check that syntax as I dont have ssms on this comp to fully test
Edit: to powaga: Unless I'm stupid he did (just not clearly) he said he wants to return 4 and 3 instead of 7 ... he want the resulting number of days to be per month instead of number of days

SQL calculate number of days of residency by month, by user, by location

I'm working on a query for a rehab organization where tenants (client/patients) live in a building when they first arrive, as they progress in their treatment they move to another building and as they near the end of treatment they are in a third building.
For funding purposes we need to know how many nights a tenant spent in each building in each month.
I can use DateDiff to get the total number of nights, but how do I get the total for each client in each month in each building?
For example, John Smith is in Building A 9/12-11/3; moves to Building B 11/3-15; moves to Building C on and is still there: 11/15 - today
What query returns a result that show the number of nights he spent in:
Building A in Septmeber, October and November.
Buidling B in November
Building C in November
Two tables hold the client's name, building name and move-in date and move-out date
CREATE TABLE [dbo].[clients](
[ID] [nvarchar](50) NULL,
[First_Name] [nvarchar](100) NULL,
[Last_Name] [nvarchar](100) NULL
) ON [PRIMARY]
--populate w/ two records
insert into clients (ID,First_name, Last_name)
values ('A2938', 'John', 'Smith')
insert into clients (ID,First_name, Last_name)
values ('A1398', 'Mary', 'Jones')
CREATE TABLE [dbo].[Buildings](
[ID_U] [nvarchar](50) NULL,
[Move_in_Date_Building_A] [datetime] NULL,
[Move_out_Date_Building_A] [datetime] NULL,
[Move_in_Date_Building_B] [datetime] NULL,
[Move_out_Date_Building_B] [datetime] NULL,
[Move_in_Date_Building_C] [datetime] NULL,
[Move_out_Date_Building_C] [datetime] NULL,
[Building_A] [nvarchar](50) NULL,
[Building_B] [nvarchar](50) NULL,
[Building_C] [nvarchar](50) NULL
) ON [PRIMARY]
-- Populate the tables with two records
insert into buildings (ID_U,Move_in_Date_Building_A,Move_out_Date_Building_A, Move_in_Date_Building_B,
Move_out_Date_Building_B, Move_in_Date_Building_C, Building_A, Building_B, Building_C)
VALUES ('A2938','2010-9-12', '2010-11-3','2010-11-3','2010-11-15', '2010-11-15', 'Kalgan', 'Rufus','Waylon')
insert into buildings (ID_U,Move_in_Date_Building_A,Building_A)
VALUES ('A1398','2010-10-6', 'Kalgan')
Thanks for your help.
I'd use a properly normalized database schema, your Buildings table is not useful like this. After splitting it up I believe that getting your answer will be pretty easy.
Edit (and updated): Here's a CTE which will take this strange table structure and split it into a more normalized form, displaying the user id, building name, move in and move out dates. By grouping on the ones you want (and using DATEPART() etc.) you should be able to get the data you need with that.
WITH User_Stays AS (
SELECT
ID_U,
Building_A Building,
Move_in_Date_Building_A Move_In,
COALESCE(Move_out_Date_Building_A, CASE WHEN ((Move_in_Date_Building_B IS NULL) OR (Move_in_Date_Building_C<Move_in_Date_Building_B)) AND (Move_in_Date_Building_C>Move_in_Date_Building_A) THEN Move_in_Date_Building_C WHEN Move_in_Date_Building_B>=Move_in_Date_Building_A THEN Move_in_Date_Building_B END, GETDATE()) Move_Out
FROM dbo.Buildings
WHERE Move_in_Date_Building_A IS NOT NULL
UNION ALL
SELECT
ID_U,
Building_B,
Move_in_Date_Building_B,
COALESCE(Move_out_Date_Building_B, CASE WHEN ((Move_in_Date_Building_A IS NULL) OR (Move_in_Date_Building_C<Move_in_Date_Building_A)) AND (Move_in_Date_Building_C>Move_in_Date_Building_B) THEN Move_in_Date_Building_C WHEN Move_in_Date_Building_A>=Move_in_Date_Building_B THEN Move_in_Date_Building_A END, GETDATE())
FROM dbo.Buildings
WHERE Move_in_Date_Building_B IS NOT NULL
UNION ALL
SELECT
ID_U,
Building_C,
Move_in_Date_Building_C,
COALESCE(Move_out_Date_Building_C, CASE WHEN ((Move_in_Date_Building_B IS NULL) OR (Move_in_Date_Building_A<Move_in_Date_Building_B)) AND (Move_in_Date_Building_A>Move_in_Date_Building_C) THEN Move_in_Date_Building_A WHEN Move_in_Date_Building_B>=Move_in_Date_Building_C THEN Move_in_Date_Building_B END, GETDATE())
FROM dbo.Buildings
WHERE Move_in_Date_Building_C IS NOT NULL
)
SELECT *
FROM User_Stays
ORDER BY ID_U, Move_In
This query run on your sample data produces he following output:
ID_U Building Move_In Move_Out
-------- ----------- ----------------------- -----------------------
A1398 Kalgan 2010-10-06 00:00:00.000 2010-11-23 18:35:59.050
A2938 Kalgan 2010-09-12 00:00:00.000 2010-11-03 00:00:00.000
A2938 Rufus 2010-11-03 00:00:00.000 2010-11-15 00:00:00.000
A2938 Waylon 2010-11-15 00:00:00.000 2010-11-23 18:35:59.050
(4 row(s) affected)
As you can see, from here on it will be much easier to isolate the days per patient or building, and also to find the records for specific months and calculate the correct stay duration in that case. Note that the CTE displays the current date for patients which are still in a building.
Edit (again): In order to get all months including their start and end dates for all relevant years, you can use a CTE like this:
WITH User_Stays AS (
[...see above...]
)
,
Months AS (
SELECT m.IX,
y.[Year], dateadd(month,(12*y.[Year])-22801+m.ix,0) StartDate, dateadd(second, -1, dateadd(month,(12*y.[Year])-22800+m.ix,0)) EndDate
FROM (
SELECT 1 IX UNION ALL
SELECT 2 UNION ALL
SELECT 3 UNION ALL
SELECT 4 UNION ALL
SELECT 5 UNION ALL
SELECT 6 UNION ALL
SELECT 7 UNION ALL
SELECT 8 UNION ALL
SELECT 9 UNION ALL
SELECT 10 UNION ALL
SELECT 11 UNION ALL
SELECT 12
)
m
CROSS JOIN (
SELECT Datepart(YEAR, us.Move_In) [Year]
FROM User_Stays us UNION
SELECT Datepart(YEAR, us.Move_Out)
FROM User_Stays us
)
y
)
SELECT *
FROM months;
So since we now have a tabular representation of all date ranges which can be of interest, we simply join this together:
WITH User_Stays AS ([...]),
Months AS ([...])
SELECT m.[Year],
DATENAME(MONTH, m.StartDate) [Month],
us.ID_U,
us.Building,
DATEDIFF(DAY, CASE WHEN us.Move_In>m.StartDate THEN us.Move_In ELSE m.StartDate END, CASE WHEN us.Move_Out<m.EndDate THEN us.Move_Out ELSE DATEADD(DAY, -1, m.EndDate) END) Days
FROM Months m
JOIN User_Stays us ON (us.Move_In < m.EndDate) AND (us.Move_Out >= m.StartDate)
ORDER BY m.[Year],
us.ID_U,
m.Ix,
us.Move_In
Which finally produces this output:
Year Month ID_U Building Days
----------- ------------ -------- ---------- -----------
2010 October A1398 Kalgan 25
2010 November A1398 Kalgan 22
2010 September A2938 Kalgan 18
2010 October A2938 Kalgan 30
2010 November A2938 Kalgan 2
2010 November A2938 Rufus 12
2010 November A2938 Waylon 8
-- set the dates for which month you want
Declare #startDate datetime
declare #endDate datetime
set #StartDate = '09/01/2010'
set #EndDate = '09/30/2010'
select
-- determine if the stay occurred during this month
Case When #StartDate <= Move_out_Date_Building_A and #EndDate >= Move_in_Date_Building_A
Then
(DateDiff(d, #StartDate , #enddate+1)
)
-- drop the days off the front
- (Case When #StartDate < Move_in_Date_Building_A
Then datediff(d, #StartDate, Move_in_Date_Building_A)
Else 0
End)
--drop the days of the end
- (Case When #EndDate > Move_out_Date_Building_A
Then datediff(d, #EndDate, Move_out_Date_Building_A)
Else 0
End)
Else 0
End AS Building_A_Days_Stayed
from Clients c
inner join Buildings b
on c.id = b.id_u
Try using a date table. For example, you could create one like so:
CREATE TABLE Dates
(
[date] datetime,
[year] smallint,
[month] tinyint,
[day] tinyint
)
INSERT INTO Dates(date)
SELECT dateadd(yy, 100, cast(row_number() over(order by s1.object_id) as datetime))
FROM sys.objects s1
CROSS JOIN sys.objects s2
UPDATE Dates
SET [year] = year(date),
[month] = month(date),
[day] = day(date)
Just modify the initial Dates population to meet your needs (on my test instance, the above yielded dates from 2000-01-02 to 2015-10-26). With a dates table, the query is pretty straight forward, something like this:
select c.First_name, c.Last_name,
b.Building_A BuildingName, dA.year, dA.month, count(distinct dA.day) daysInBuilding
from clients c
join Buildings b on c.ID = b.ID_U
left join Dates dA on dA.date between b.Move_in_Date_Building_A and isnull(b.Move_out_Date_Building_A, getDate())
group by c.First_name, c.Last_name,
b.Building_A, dA.year, dA.month
UNION
select c.First_name, c.Last_name,
b.Building_B, dB.year, dB.month, count(distinct dB.day)
from clients c
join Buildings b on c.ID = b.ID_U
left join Dates dB on dB.date between b.Move_in_Date_Building_B and isnull(b.Move_out_Date_Building_B, getDate())
group by c.First_name, c.Last_name,
b.Building_B, dB.year, dB.month
UNION
select c.First_name, c.Last_name,
b.Building_C, dC.year, dC.month, count(distinct dC.day)
from clients c
join Buildings b on c.ID = b.ID_U
left join Dates dC on dC.date between b.Move_in_Date_Building_C and isnull(b.Move_out_Date_Building_C, getDate())
group by c.First_name, c.Last_name,
b.Building_C, dC.year, dC.month
If you can't restructure the Building table you can create a query that will normalize it for you and allow for easier calculations:
SELECT "A" as Building, BuidlingA as Name, Move_in_Date_Building_A as MoveInDate,
Move_out_Date_Building_A As MoveOutDate
UNION
SELECT "B", BuidlingB, Move_in_Date_Building_B, Move_out_Date_Building_B
UNION
SELECT "C", BuidlingC, Move_in_Date_Building_C, Move_out_Date_Building_C