Link to home
Start Free TrialLog in
Avatar of HelpdeskJBC
HelpdeskJBCFlag for Austria

asked on

MS SQL need to insert calculated record

Our Problem:
We have a sql tabel on our server were we need to calculate ('l'+'h'/2)='m' and add to the existing table those m values. (See: "Our Stored Proceedure")
L= Low value
H= High value
M= Mid value
In the table there are besides 'l' and 'h' as well 'u' and 'c' records
With the stored proceedure we couldn't loop through all these records in the table.

Please help us with that one.

Our Tabel Design:
EvalDate,      smalldatetime,      Unchecked
IndexName,      varchar(150),      Unchecked
IndexLH,      char(1),      Unchecked
PriceFixing,      float,      Unchecked
PriceCurrency,      varchar(3),      Unchecked
Unit,      varchar(10),      Unchecked

Our Stored Proceedure:
/****** Object:  StoredProcedure [dbo].[proc_Import_IndexFixings_Create_M] ******/
USE [QuoteBase]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


ALTER procedure [dbo].[proc_Import_IndexFixings_Create_M]
as
insert into dbo.IndexFixings_BAK (evaldate, indexname, indexlh, pricefixing, pricecurrency, unit)
select h.Evaldate, h.indexname, 'M', (l.pricefixing + h.pricefixing) / 2, h.pricecurrency, h.unit from
      dbo.IndexFixings_BAK L
      inner join dbo.IndexFixings_BAK H on l.indexname = h.indexname and l.evaldate = h.evaldate
where l.indexlh = 'L' and h.indexlh = 'H'
/******  ******/
ASKER CERTIFIED SOLUTION
Avatar of tring_tring
tring_tring
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Scott Pletcher
Scott Pletcher
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of HelpdeskJBC

ASKER

@Scott:

The result of that query is:
---
Msg 515, Level 16, State 2, Line 1
Cannot insert the value NULL into column 'PriceCurrency', table 'QuoteBase.dbo.IndexFixings_BAK'; column does not allow nulls. INSERT fails.
The statement has been terminated.
Warning: Null value is eliminated by an aggregate or other SET operation.
---
The only difference of L and H are in IndexLH and Pricefixings, the other rows with different values then L and H needs to be skipped.

@tring
It's not a query but a stored proceedure. What I need is a query which goes through all those records in IndexFixings_BAK and uses this proceedure. I tried it but it didn't do anything.
Hmm, looks like you have some pricecurrency values that are NULL.  You will need to substitute the desired value for them in the query:

insert into dbo.IndexFixings_BAK (evaldate, indexname, indexlh, pricefixing, pricecurrency, unit)
select evaldate, indexname, 'M',
    sum(case when indexlh in ('H', 'L') then pricefixing else 0 end) / 2,
    max(case when indexlh = 'H' then COALESCE(pricecurrency, '?') end), --??
    max(case when indexlh = 'H' then unit end) --??
from dbo.IndexFixings_BAK
--where evaldate >= dateadd(day, datediff(day, 0, getdate()), 0)
group by evaldate, indexname