SQL Server, Efficient way to query multiple tables for a specific day of data - sql

We have started saving the daily change data for a bunch of tables. We first put in all of the original data and than the change data with start and end dates for each table record (So not all records are saved each day just changed records). So to pull the data for a specific date I have to look at the beginning and end dates (end date might be null) and pull the MAX begin date to get the right records.
SELECT *
FROM dbo.DIM_EX_NAME_MASTER AS DEXNM
INNER JOIN
(SELECT APPID, MAX(DW_RECORD_START) AS StartDate
FROM dbo.DIM_EX_NAME_MASTER
WHERE (DW_RECORD_START < '4/4/2020') AND (DW_RECORD_END > '4/4/2020')
OR (DW_RECORD_START < '4/4/2020') AND (DW_RECORD_END IS NULL)
GROUP BY APPID) AS INNER_DEXNM ON DEXNM.APPID = INNER_DEXNM.APPID AND DEXNM.DW_RECORD_START = INNER_DEXNM.StartDate
So that's not so bad for one table but we want to build a report with a query that pulls from 25 tables with subqueries where the user selects the date to pull for.
That's going to be some really messy sql. Ideally I would like to create a view for each table and pass in the date as a parameter but SQL server doesn't allow for parameterized views.
Anyone have any ideas on how I can build multi-table date based queries without adding all of this extra sql per table?
Thanks for any help you can give!

SQL server doesn't allow for parameterized views.
No, but SQL Server does support user-defined table-valued functions.
This is pretty much exactly what you are asking for -- they can accept a date parameter and return the results as a table.

Related

Design SQL table to get data from different SQL query at regular intervals

I ma trying to capture cpu usage of my current SQL server over a time period and came across a query from here
Please guide me how can i use the above query to insert the results in permanent table over collected period of time without overwriting timestamp values or duplicating entries?
Thanks
You could use a scheduled job to execute the query and insert data into your table
https://learn.microsoft.com/en-us/sql/ssms/agent/schedule-a-job?view=sql-server-ver15
If you use the syntax
INSERT INTO dbo.sysUseLig
SELECT --the query
Having previously created your table dbo.sysUseLig with columns with the correct data types you will have what you need.
There is a timestamp based column in the select.

Fetching repeated data from database without repetition

Guys I have a table in database like this.
open to see
I need to fetch data such that i have only variety and number of grafts each date summed up without repetition.
I get this instead: result
I think, this is the query you are looking for
select sum(variety), sum(number_grafts), date
from <table_name>
group by date

How does work AS OF clause in system-versioned temporal tables?

You need to query the customer table to return the state of the table before these changes were made - which query should you use?
Correct answer is: this query uses as of sub-clause to return the state of the customer table before changes were made.
SELECT *
FROM dbo.customer
FOR SYSTEM_TIME AS OF '2017-10-01 00:00:00'
ORDER BY customerID;
Question: How does, in this case, AS OF clause work? How this can query history table for '2017-10-01 00:00:00'?
You are working with system-versioned temporal tables. First introduced at ANSI SQL 2011 and then at SQL SERVER 2016.
This tables have two control columns, sysStartDate and sysEndDate, which specifies the period for which a record is valid (when a record is still valid end-date is set to 9999-12-31).
This clause, FOR SYSTEM_TIME AS OF <date_time> will query data that was valid for a specified date time. That means, it will filter out data with sysStartDate bigger than <date_time> and sysEndDate smaller than <date_time>, i.e., you are querying data that was valid at <date_time>.

SQL Server 2005 simple query to get all dates between two dates

I know there are a lot of solutions to this but I am looking for a simple query to get all the dates between two dates.
I cannot declare variables.
As per the comment above, it's just guesswork without your table structures and further detail. Also, are you using a 3NF database or star schema structures, etc. Is this a transaction system or a data warehouse?
As a general answer, I would recommend creating a Calendar table, that way you can create multiple columns for Working Day, Weekend Day, Business Day, etc. and add a date key value, starting at 1 and incrementing each day.
Your query then is a very simple sub-select or join to the table to do something like
SELECT date FROM Calendar WHERE date BETWEEN <x> AND <y>
How to create a Calender table for 100 years in Sql
There are other options like creating the calendar table using iterations (eg, as a CTE table) and linking to that.
SQL - Create a temp table or CTE of first day of the month and month names

How to retrieve rows from database by passing month in where condition

How to retrieve rows from a SQL Server database by passing month in where condition, as I have used datetime datatype for the date column.
Is there any function is SQL Server, where I can send month and will get the row accordingly?
I would create an indexed computed column based on the month in the date column. This way queries will scale as you'll have a specific column indexed for querying against.
Using Month(yourdatecolumn) = somemonth is going to table scan (as in read every value to determine the month) at query execution time so wouldn't be a good idea for large volumes of data.
See this article for a commentary on why using functions in your where clause is bad.
SELECT * FROM table
WHERE MONTH(datecolumn) = 1
this will return all records for January