---step 1 ----===Create a SQL Table with below SQL Query
USE [abc]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[IC_Raw_In](
[I_Date] [varchar](50) NULL,
[I_O_P] [money] NULL,
[I_O_H] [money] NULL,
[I_O_L] [money] NULL,
[I_C_O] [money] NULL,
[I_Serial] [numeric](18, 0) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
---step 2 --===Insert the Data into IC_Raw_In Table as a bulk... Since i get the data every time bulk i must use here a bulk data
BULK
INSERT dbo.IC_Raw_In
FROM 'C:\ABC\InputData.txt'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = ''
)
GO
---step 3 ---====Create a SQL View for AMPS12_C
USE [abc]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[AMPS12_C] AS
WITH RankedPrices
AS
(SELECT i_serial , I_C_O, ROW_NUMBER() OVER (ORDER BY i_serial) AS rn
FROM IC_Raw_In)
SELECT a.i_serial, AVG(b.I_C_O) AS AMPS12_C
FROM RankedPrices AS a LEFT JOIN
RankedPrices AS b ON b.rn BETWEEN a.rn-11 AND a.rn
GROUP BY a.i_serial
GO
---step 4 ---=== Create a view as v_AMP_C for easy output view
create view v_AMP_C as
SELECT dbo.IC_Raw_In.I_Date, dbo.IC_Raw_In.I_O_P, dbo.IC_Raw_In.I_O_H, dbo.IC_Raw_In.I_O_L, dbo.IC_Raw_In.I_C_O, dbo.AMPS12_C.AMPS12_C,
dbo.IC_Raw_In.I_Serial
FROM dbo.IC_Raw_In INNER JOIN
dbo.AMPS12_C ON dbo.IC_Raw_In.I_Serial = dbo.AMPS12_C.i_serial
---step 5 pending (I am looking for help here)
Now I want write a SQL Query to get results in new Column called C12WR for the below quetion.
I want to exclude(Use NULL) the first 11 rows in C12WR Column, and in 12th row of C12WR Column "use a static value which is in “AMPS12_C” . This value will change every time i import the data to my table so the will change dynamically every time. And in the AMPS12_C Column it should calculate the below formula after 13th row to end of the table.
After 13th Row in C12WR Column = (the value of above row (that is currunt row number -1) from C12WR *11 + Current row value from I_C_O Column) /12
You can create dynamic SQL by storing a SQL query in a string (varchar or nvarchar) and then running exec() on it.
declare @sql as nvarchar(max) declare @id as int = 1
set @sql = 'select * from table where id =' + id
exec(@sql)
You would just have to dynamically determine the column names.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.