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.
SQL
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 RaveendranRank #320 · 7.8K reputationPosted Jul 9, 2024, 9:32 PM EDT
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.
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());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:
SELECT idx, dt, BatchNrA, BatchNrB, BatchNrC, Recipe, RecipeCode, RecipeDescription, InstructionNo, Shift FROM dbo.vw_Adr_Batch_Recipe_LogBy 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.
Jignesh KumarRank #91 · 22.9K reputationPosted Jul 10, 2024, 12:41 AM EDT
Hello,
You can create view with additional column RowNum and partiion by date
-- Drop the view if it already exists and recreate view again IF OBJECT_ID('[dbo].[vw_Adr_Batch_Recipe_Log]', 'V') IS NOT NULL DROP VIEW [dbo].[vw_Adr_Batch_Recipe_Log]; GO CREATE VIEW [dbo].[vw_Adr_Batch_Recipe_Log] AS SELECT [idx], [dt], [BatchNrA], [BatchNrB], [BatchNrC], [Recipe], [RecipeCode], [RecipeDescription], [InstructionNo], [Shift] FROM ( SELECT [idx], [dt], [BatchNrA], [BatchNrB], [BatchNrC], [Recipe], [RecipeCode], [RecipeDescription], [InstructionNo], [Shift], ROW_NUMBER() OVER (ORDER BY [dt] DESC) AS RowNum FROM [Adroit_Batch_Suite].[dbo].[Adr_Batch_Recipe_Log] ) AS RowOrdered WHERE RowNum = DATEADD(Month, -3, GetDate()); -- create select query from the view to get last three months data SELECT idx, dt, BatchNrA, BatchNrB, BatchNrC, Recipe, RecipeCode, RecipeDescription, InstructionNo, Shift FROM dbo.vw_Adr_Batch_Recipe_Log WHERE dt >= DATEADD(month, -3, GETDATE());Guest UserRank #877 · 2.5K reputationPosted Jul 10, 2024, 1:23 AM EDT
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'.
Vishal JoshiRank #377 · 6.7K reputationPosted Jul 10, 2024, 5:36 AM EDT
Hello,
Checkout the below SQL script to get the expected result.
-- Drop the view if it already exists and recreate view again IF OBJECT_ID('[dbo].[vw_Adr_Batch_Recipe_Log]', 'V') IS NOT NULL DROP VIEW [dbo].[vw_Adr_Batch_Recipe_Log]; GO CREATE VIEW [dbo].[vw_Adr_Batch_Recipe_Log] AS SELECT [idx], [dt], [BatchNrA], [BatchNrB], [BatchNrC], [Recipe], [RecipeCode], [RecipeDescription], [InstructionNo], [Shift] FROM ( SELECT [idx], [dt], [BatchNrA], [BatchNrB], [BatchNrC], [Recipe], [RecipeCode], [RecipeDescription], [InstructionNo], [Shift], ROW_NUMBER() OVER (ORDER BY [dt] DESC) AS RowNum FROM [dbo].[Adr_Batch_Recipe_Log] ) AS RowOrdered WHERE RowNum = DATEADD(Month, -3, GetDate()); GO -- create select query from the view to get last three months data SELECT * FROM [dbo].[vw_Adr_Batch_Recipe_Log]Thanks
Vishal Joshi