Filling a column with Case When Statement in reference to another column - ms-access

I am fairly new to SQL language and I am trying to do some work for Uni in Access. I have three columns: Bindugen, with values shown below, and then blank Columns Code and 33/SDGL:
Bindungen: 33,811, / 811,SDGL,33,/ 33,812,SDGL, / 812,SDGL,/ 811, (etc.)
My main column is Bindungen and I want to fill the others in relation to that one. Basically in the column 33/SDGL I tried to do the Update Case When Statement shown below. It obviously didn't work.
SELECT
FROM Codes;
UPDATE (Codes)
SET 33/SDGL ( CASE
WHEN Bindungen = "*," THEN Allein
WHEN Bindungen = "33,*," Or "*,33," THEN 33
WHEN Bindungen = "SDGL,*," Or "*,SDGL," THEN SDGL
ELSE Both
END);
My goal for the 33/SDGL column is that when Bindungen is: "," = Allein / ",33," Or "33,," = 33 / ",SDGL," Or "SDGL,*," = SDGL
and for the rest = BOTH
Could someone help me write this SQL code?
Also for the Code column, I would like it to find from Bindungen which code is there (eg: 811 or 812). Can someone point me in the right direction?

MS Access does not use the CASE statement. You need to write a SWITCH statement instead, like this:
SWITCH(Bindungen = "*,", Allein, Bindungen = "33,*,", 33, Bindungen = "*,33,", 33,
Bindungen = "SDGL,*,", SDGL, Bindungen = "*,SDGL,", SDGL, BOTH)
Also note that in this situation, Allein, SDGL, and BOTH must ALL be fields that you are selecting from. If you just want the text instead of a field value, wrap those words in "double quotes".

Related

SSRS - IIF error in Report Builder

I am having an issue with a divide by zero error that I have half way worked through.
Basically I need (EstimatedValuePlanned - EAC) / EstimatedValuePlanned
I have the following, however I am still getting #Error on some:
= IIF (Fields!EstimatedValuePlanned.Value = 0 , 0,
Fields!EstimatedValuePlanned.Value - Fields!EAC.Value)
/
SUM(Fields!EstimatedValuePlanned.Value)
I have changed the code around several times but I still either get Error or NaN
Thank you
IIF will always evaluate both parts of the function, so when SUM(Fields!EstimatedValuePlanned.Value) is zero you will get the error even though that's not what will be returned.
Try using a SWITCH statement. SWITCH stops once an expression returns True.
Something like this
= SWITCH(
SUM(Fields!EstimatedValuePlanned.Value) = 0 , 0,
True, (Fields!EstimatedValuePlanned.Value - Fields!EAC.Value) / SUM(Fields!EstimatedValuePlanned.Value)
)
The True expression simply acts like an else
UPDATE WITH SAMPLE
I created a new report added a dataset and set the dataset query to be the following (just to generate some dummy data).
DECLARE #t TABLE (EVP float, EAC float)
INSERT INTO #t VALUES
(1,2),
(2,3),
(5,4),
(0,2),
(1,0),
(0,0)
SELECT * FROM #t
I then added a table, set the first to columns to be EVP and EAC respectively and set the 3rd column to an expression as follows.
=SWITCH (
Fields!EVP.Value = 0, 0
, True, (Fields!EVP.Value - Fields!EAC.Value) / Fields!EVP.Value
)
The report design looks like this.
And when it's run this is the result....
Try replicating the above steps and see if you can get it to work like this first then review difference to your report.
I got it to work with:
=IIF (Fields!EstimatedValuePlanned.Value = 0 , 0,
Fields!EstimatedValuePlanned.Value - Fields!EAC.Value)
/
IIF(Fields!EstimatedValuePlanned.Value =
0,1,Fields!EstimatedValuePlanned.Value)
Thank you both

Access 2013 Calculation with If Statement

Maybe someone can help me. What I am trying to do is to create a calculation with an If Condition. I'm doing my current calculations in the field within my DesignerView.
=Summe([Menge/Liter])
=Summe([Menge/Liter]*[Preis/Liter])
Here i need to have that it only calculates the tables that have status = 1
Is there a way how to do this?
Use IIf Function:
=IIF(status = 1, Summe([Menge/Liter]*[Preis/Liter]), Summe([Menge/Liter] ))
Or is it:
=IIF(status = 1, Summe([Menge/Liter]*[Preis/Liter]), "")

SSRS If Column X value = "X" then display corresponding column Y value

I am using SSRS 2008 and have a dataset that looks like this: (simplified)
RESULTGUID Question ANSWER
1234 How Old 15
1234 How New 13
1234 How Big 700
1234 How Small 100
etc etc
I'm looking for an IIF statement like:
=iif(fields!Question.value = "How Old",Fields!Answer.value,"N/A")
However, this doesn't work apart from the first row, so if first(fields! etc then works fine, but not for the rows lower down the data-set.
Can anyone offer a solution to this please ?
If you are doing it all in SSRS it could be one big nested Iif statement:
=Iif(fields!Question.value = "How Old",Fields!Answer.value,Iif(fields!Question.value = "How New",Fields!Answer.value,
Iif(fields!Question.value = "How Big",Fields!Answer.value,Iif(fields!Question.value = "How Small",Fields!Answer.value,"N/A"))))
or you could do a SWITCH statement:
=SWITCH(
fields!Question.value = "How Old", Fields!Answer.value
fields!Question.value = "How New", Fields!Answer.value
fields!Question.value = "How Big", Fields!Answer.value
fields!Question.value = "How Small", Fields!Answer.value
)

dynamic Format at ssrs

this one is my report
This is my result (but iddescripcion is not visible)
my question is.
how do i get when iddescripcion=1,3,5,8 or 10 then valor is going to have this format 10,000 (not decimals)
when iddescripcion=2,4,6, or 9 then valor is going to have this format 1,000.0 or 3,000.1( one decimal)
and when iddescripcion=7,11 then valor is going to have this format $2,389,012 (not decimalsa and $ before)
i believe the format i need add it at here but i do not have any idea how to do it..
You can apply condition formatting using a Switch or IIf statement in the Text Box Expression value.
I put a simple example together using the following expression:
=Switch(Fields!iddescripcion.Value = 1 or Fields!iddescripcion.Value = 2, Format(Fields!valor.Value, "N0")
, Fields!iddescripcion.Value = 3 or Fields!iddescripcion.Value = 4, Format(Fields!valor.Value, "N1")
, Fields!iddescripcion.Value = 5 or Fields!iddescripcion.Value = 6, Format(Fields!valor.Value, "$#,#"))
Basically just applying Format to valor based on iddescripcion for each row. This works for some simple data:
Hopefully you can adapt this for your example.

Correlate 2 columns in SQL

SELECT ica.CORP_ID, ica.CORP_IDB, ica.ITEM_ID, ica.ITEM_IDB,
ica.EXP_ACCT_NO, ica.SUB_ACCT_NO, ica.PAT_CHRG_NO, ica.PAT_CHRG_PRICE,
ica.TAX_JUR_ID, ica.TAX_JUR_IDB, ITEM_PROFILE.COMDTY_NAME
FROM ITEM_CORP_ACCT ica
,ITEM_PROFILE
WHERE (ica.CORP_ID = 1000)
AND (ica.CORP_IDB = 4051)
AND (ica.ITEM_ID = 1000)
AND (ica.ITEM_IDB = 4051)
AND ica.EXP_ACCT_NO = ITEM_PROFILE.EXP_ACCT_NO
I'm trying basically say since the exp account code is '801500' then the Name should return "Miscellaneous Medic...".
It seems as if what you are showing is not possible. Have you edited the data in the editor??? You are joining using ica.EXP_ACCT_NO = ITEM_PROFILE.EXP_ACCT_NO . Therefore, every entry with EXP_ACCT_NO = 801500, should also have the same COMDTY_NAME.
However, it could be the case that your IDs are not actually numbers and that they are strings with whitespace (801500__ vs 801500 ). But since you are not performing a left-outer join, it would also mean you have an entry in ITEM_PROFILE with the same whitespace.
You also need to properly normalize your table data (unless this is a view) but it still means you have erroneous data.
Try to perform the same query, but using the TRIM function to remove whitespace: https://stackoverflow.com/a/6858168/1688441 .
Example:
SELECT ica.CORP_ID, ica.CORP_IDB, ica.ITEM_ID, ica.ITEM_IDB,
ica.EXP_ACCT_NO, ica.SUB_ACCT_NO, ica.PAT_CHRG_NO, ica.PAT_CHRG_PRICE,
ica.TAX_JUR_ID, ica.TAX_JUR_IDB, ITEM_PROFILE.COMDTY_NAME
FROM ITEM_CORP_ACCT ica
,ITEM_PROFILE
WHERE (ica.CORP_ID = 1000)
AND (ica.CORP_IDB = 4051)
AND (ica.ITEM_ID = 1000)
AND (ica.ITEM_IDB = 4051)
AND trim(ica.EXP_ACCT_NO) = trim(ITEM_PROFILE.EXP_ACCT_NO);