Hi Team
I have view and want to get 3 months limit, want to use single query in excel that will populate this so that when i refreshed the spreesheet i get current date of these months. Here is my below, logic.
Create View [dbo].[vw_Adr_Batch_Recipe_Log] AS
SELECT TOP (1000) [idx]
,[dt]
,[BatchNrA]
,[BatchNrB]
,[BatchNrC]
,[Recipe]
,[RecipeCode]
,[RecipeDescription]
,[InstructionNo]
,[Shift]
FROM [Adroit_Batch_Suite].[dbo].[Adr_Batch_Recipe_Log]
WHERE [dt] >= DATEADD(Month, -3, GetDate());
SELECT
idx,
dt,
BatchNrA,
BatchNrB,
BatchNrC,
Recipe,
RecipeCode,
RecipeDescription,
InstructionNo,
Shift
FROM dbo.vw_Adr_Batch_Recipe_Log
WHERE dt >= DATEADD(month, -3, GETDATE());
Prasad RaveendranPosted Jul 10, 2024, 1:32 AM
Your SQL code for creating the view is correct. The view will always return records from the past three months relative to the current date.
You can use Excel's "Get & Transform Data" feature (also known as Power Query) to pull data from SQL Server. Here's how you can set it up:
Open Excel and go to the Data tab.
Get Data: Select
Get Data->From Database->From SQL Server Database.Connect to SQL Server:
Query the View:
[dbo].[vw_Adr_Batch_Recipe_Log].LoadorLoad Toto specify how you want to load the data into Excel (as a table, PivotTable, etc.).Refresh Data:
Refresh Allor right-click on the table and selectRefresh.Here’s a sample SQL query that Excel will use to get the data from the view, which ensures that it always gets the records from the past three months:
By following these steps, your Excel spreadsheet will automatically fetch the latest data from the past three months each time you refresh it, ensuring that you always have the most up-to-date information.
Vishal JoshiPosted Jul 10, 2024, 9:36 AM
Hello,
Checkout the below SQL script to get the expected result.
Thanks
Vishal Joshi
Guest UserPosted Jul 10, 2024, 5:23 AM
Hi Team
I am now after following both steps mentioned here, this is my current error both sql server and excel sheet when using this query.
Msg 208, Level 16, State 1, Line 30
Invalid object name 'dbo.vw_Adr_Batch_Recipe_Log'.
Jignesh KumarPosted Jul 10, 2024, 4:41 AM
Hello,
You can create view with additional column RowNum and partiion by date