May 24, 2022 at 5:18 pm
Hello friends,
I am looking at a query where the designs are as follows:
Table A
Name, DateOfBirth
Sam, 05/17/1988
Michael, 02/26/1982
Samantha, 01/01/1993
Frank, 02/11/1984
I have another table showing some numbers datewise (except the table does not contain weekends and holidays)
Date, Count
01/01/2001, 23
02/01/2001, 35
03/01/2001, 42
04/01/2001, 5
05/01/2001, 16
08/01/2001, 8
Now I simply looking to create a query with Name, DateOfBirth, Date, Count
(Simple BUT for the birthdates which fall on Saturday/Sunday or holidays, the next available date should pick up with other details)
Example: 02/11/1984 is Saturday so the details should be like:
Name, DateOfBirth, Date, Count
Frank, 02/11/1984, 02/13/1984, 12
Please help me. Seems I am overthinking maybe 😞
May 24, 2022 at 5:26 pm
Didn't have usable data to test with, so the code is untested:
SELECT A.Name, A.DateOfBirth, ot.Count
FROM TableA A
CROSS APPLY (
SELECT TOP (1) *
FROM otherTable ot
WHERE ot.Date >= A.DateOfBirth
ORDER BY ot.Date
) AS ot
SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".
May 26, 2022 at 7:38 pm
This was removed by the editor as SPAM
May 29, 2022 at 12:40 am
Thanks a lot Scott. That helped. There's one more query which is not showing any issues but i doubt in future when the table size increases, it will start giving issue. Query is something like this:
select a.Col1, a.Col35, b.Col32, c.Col18,
case when a.col5 = 'abc' and a.ColDate6 is not null then datediff(day,a.ColDate5,a.ColDate6) when a.Col5 = 'abc' and a.ColDate6 is null then 0 when a.Col5 <> 'def' then datediff(day,(select top 1 x.ColDateStart from TableTimeTracker x where a.Col10 = x.Col10), getdate()) when a.Col5 = 'def' then datediff(day,(select top 1 x.ColDateStart from TableTimeTracker x where a.Col11 = x.Col11), getdate()) else datediff(day,(select top 1 x.ColDateStart from TableTimeTracker x where a.Col10 = x.Col10), getdate()) end as TimeTrackerDays
from TableA a inner join TableB b on a.col1 = b.col1 inner join TableC c on a.Col1 = c.Col1
In this query, there is a case statement in the "select" section which i think can create issue when table starts bulking up. I am trying to see how this query can be simplified. This is a sample query. Actual one contains many joins and many columns in select section including the case statement.
June 6, 2022 at 1:37 pm
This was removed by the editor as SPAM
June 15, 2022 at 7:55 am
This was removed by the editor as SPAM
June 17, 2022 at 11:12 am
Thanks for the information that you share with us. I will try to figure it out for more.
June 18, 2022 at 9:15 pm
I think that the easiest way would be to add a column to table A with an "OffsetDate" column built in. That will also me nicely indexable and easy to code for.
--Jeff Moden
Change is inevitable... Change for the better is not.
June 18, 2022 at 9:17 pm
Thanks for the information that you share with us. I will try to figure it out for more.
Hi and welcome aboard.
Be advised that your post looks and smells like a precursor to spam. If you're a spammer, please go away. We're watching.
If your not a spammer then, like I said, welcome aboard. You'll find some interesting stuff here.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply