Need Help regarding this IIF statement - ms-access

I entered this iif statement and it says expression you entered is to complex can someone give me advice on how to approach this. Do you think i should split the formula.
IIf([MarkUpI]=100 And [Stock/NonStock1]="Stock",[QTY1]+[UnitPrice1]*1.22,
IIf([MarkUpI]=101 And [Stock/NonStock1]="Stock",[QTY1]+[UnitPrice1]*1.22,
IIf([MarkUpI]=200 And [Stock/NonStock1]="Stock",[QTY1]+[UnitPrice1]*1.22,
IIf([MarkUpI]=201 And [Stock/NonStock1]="Stock",[QTY1]+[UnitPrice1]*1.22,
IIf([MarkUpI]=300 And [Stock/NonStock1]="Stock",[QTY1]+[UnitPrice1]*1,
IIf([MarkUpI]=400 And [Stock/NonStock1]="Stock",[QTY1]+[UnitPrice1]*1.05,
IIf([MarkUpI]=500 And [Stock/NonStock1]="Stock",[QTY1]+[UnitPrice1]*1.03,
IIf([MarkUpI]=600 And [Stock/NonStock1]="Stock",[QTY1]+[UnitPrice1]*22,
IIf([MarkUpI]=100 And [Stock/NonStock1]="Non-Stock",[QTY1]+[UnitPrice1]*1.22,
IIf([MarkUpI]=101 And [Stock/NonStock1]="Non-Stock",[QTY1]+[UnitPrice1]*1.05,
IIf([MarkUpI]=200 And [Stock/NonStock1]=Non-Stock,[QTY1]+[UnitPrice1]*1.22,
IIf([MarkUpI]=201 And [Stock/NonStock1]=Non-Stock,[QTY1]+[UnitPrice1]*1.05,
IIf([MarkUpI]=300 And [Stock/NonStock1]=Non-Stock,[QTY1]+[UnitPrice1]*1,
IIf([MarkUpI]=400 And [Stock/NonStock1]=Non-Stock,[QTY1]+[UnitPrice1]*1.05,
IIf([MarkUpI]=500 And [Stock/NonStock1]=Non-Stock,[QTY1]+[UnitPrice1]*1.03,
IIf([MarkUpI]=600 And [Stock/NonStock1]=Non-Stock,[QTY1]+[UnitPrice1]*22,0))))))))))))))))

Consider using Switch() function instead of embedded iifs.
You could also leave those 5 cases where the factor is 1.22 as a "Else" case
It seems you could also create a table with the different [MarkUpI] and [Stock/NonStock1] values and JOIN that table to get what you need ?

It appears Access has a limit to the amount of nested IIFs, but in your case you can divide it into 2 main IIFs (for Stock and Non-Stock), with the others nested inside these, and remove the common calculation as follows:
CalcResult: [QTY1]+[UnitPrice1] *
IIf([Stock/NonStock1]="Stock",
IIf([MarkUpI]=100,1.22,
IIf([MarkUpI]=101,1.22,
IIf([MarkUpI]=200,1.22,
IIf([MarkUpI]=201,1.22,
IIf([MarkUpI]=300,1,
IIf([MarkUpI]=400,1.05,
IIf([MarkUpI]=500,1.03,
IIf([MarkUpI]=600,22,0)))))))),
IIf([Stock/NonStock1]="Non-Stock",
IIf([MarkUpI]=100,1.22,
IIf([MarkUpI]=101,1.05,
IIf([MarkUpI]=200,1.22,
IIf([MarkUpI]=201,1.05,
IIf([MarkUpI]=300,1,
IIf([MarkUpI]=400,1.05,
IIf([MarkUpI]=500,1.03,
IIf([MarkUpI]=600,22,0))))))))))
NOTE: I'm not saying this is the best way to do it (I also agree that a lookup table would be better) but this will get you around your problem.

If you really want to do it all in a query, then use the Switch function like this:
[QTY1]+[UnitPrice1] *
IIf([Stock/NonStock1]="Stock",
Switch([MarkUpI]=100,1.22,[MarkUpI]=101,1.22,[MarkUpI]=200,1.22,[MarkUpI]=201,1.22,[MarkUpI]=300,1,[MarkUpI]=400,1.05,[MarkUpI]=500,1.03,[MarkUpI]=600,22,0),
Switch([MarkUpI]=100,1.22,[MarkUpI]=101,1.05,[MarkUpI]=200,1.22,[MarkUpI]=201,1.05,[MarkUpI]=300,1,[MarkUpI]=400,1.05,[MarkUpI]=500,1.03,[MarkUpI]=600,22,0))
But you really want to use a lookup table as the others have strongly suggested. It would be a LOT easier to change the values later and would look like this:
Stocked MarkupI Amt
Stock 100 1.22
Stock 101 1.22
Stock 200 1.22
Stock 201 1.22
Stock 300 1
Stock 400 1.05
Stock 500 1.03
Stock 600 22
Non-Stock 100 1.22
Non-Stock 101 1.05
Non-Stock 200 1.22
Non-Stock 201 1.05
Non-Stock 300 1
Non-Stock 400 1.05
Non-Stock 500 1.03
Non-Stock 600 22

Related

MySQL Percentage Rank without the function

I am forced to create a percent rank on a number of columns in a database table and I am really struggling with this. Usually I would use the PERCENT_RANK function but I am forced to use conventional query as our MySQL version doesn't offer that function.
I have a table that contains the columns:
UID Total-Orders-Placed Last-Order-Date-Diff
12884 8 351
10985 11 106
30613 3 43
30820 2 134
23421 9 76
I would like to add 2 ranking columns as below:
UID Total-Orders-Placed Last-Order-Date-Diff rec_rank freq_rank
12884 8 351 0.34 0.86
10985 11 106 0.64 0.91
30613 3 43 0.85 0.59
30820 2 134 0.57 0.40
23421 9 76 0.77 0.88
In reality I have thousands of rows and additional columns but that's the gist. I have been able to do it perfectly in Excel but I am really struggling to convert into Query/Views in our MySQL Database so our data can be viewed realtime.
I have tried PERCENT_RANK() but as I mention above this function isn't available to us.
I have tried the queries discussed here without too much success yet: http://code.openark.org/blog/mysql/sql-ranking-without-self-join
Any help writing the code or to give me a better understanding of it would really help.

SQL Query for loans

Question:
Suppose the following dbo.LoanStatements table was given to you. All the records of the table are shown below. Each record shows when the statement was sent.
LoanID StatementDate OriginalLoanToValue
-------------------------------------------
1 03/15/2005 0.80
1 04/15/2005 0.90
1 05/15/2005 0.90
1 06/15/2005 0.90
2 08/15/2017 66.7
2 09/15/2017 66.7
2 10/15/2017 0.50
3 10/15/2017 0.66
4 10/15/2017 0.55
Write a SELECT statement (or multiple statements) that returns the
following result set
LoanID OriginalLoanToValue
1 0.90
2 0.50
3 0.66
4 0.55
Attempted solution -
SELECT LoanID, OriginalLoanToValue
FROM LoanStatements
WHERE StatementDate IN (
SELECT MAX(StatementDate)
FROM LoanStatements
GROUP BY LoanID
);
How many statements were sent in 2005? In 2017? Please show the SELECT statement (or multiple statements).
Attempted solution:
2005:
SELECT LoanID,StatementDate,OriginalLoanToValue
FROM LoanStatements
WHERE year(StatementDate) = 2005;
2017:
SELECT LoanID,StatementDate,OriginalLoanToValue
FROM LoanStatements
WHERE year(StatementDate) = 2017;
I do not have any way of checking whether this code is correct this is just what I could come up with little SQL I knew, I would appreciate it if someone could clarify if this is correct or not. Thanks!
Your query will go correct if your data type is "DATE","DATETIME","TIMESTAMP","YEAR" for "StatementDate" column.

Access Calculated Field

I am having difficulty trying to make a calculated field that I need. So here is what I am trying to do:
I have a query that combines the information based on three tables. The most important fields that for the application are as follows:
Family Income Age Patient
15,000 18 Yes
28,000 25 No
30,000 1 Yes
From here I want to make a calculated field that gives the correct program the patient was enrolled in. based on these fields ie:
Program Minimum Income Maximum Income Minimum Age Maximum Age Patient
Children's 0 20,000 1 19 Yes
Adult 0 12,000 19 65 No
Non Patient 0 20,000 1 19 No
Adult 2 12,000 50,000 19 65 No
Etc.
to create:
Family Income Age Patient Program
15,000 18 Yes Children's
28,000 25 No Adult 2
30,000 1 Yes Children's 2
I know I can use IIf to hard code it in to the field, but then it will be really difficult for other people to update the information as the guidelines change. Is it possible to have the information stored in a table? and use the information on the table form etc, or will I need to use IIf
Any Ideas? is it possible to dynamically create the IIf in SQL using VBA while pulling the information from the table?
EDIT:::
Thank you for your response and for formatting my tables, I still have no idea how you changed it, but it looks amazing!
I tried to add the SQL you added down below, but I was not able to make it work. I'm not sure if I made a mistake so I included the SQL of my Query. The query currently returns 0 values, so I think I messed something up. (The real Query is embarassing...I'm sorry for that). Unfortunately, I have done everything in my power to avoid SQL, and now I am paying the price.
SELECT qry_CombinedIndividual.qry_PrimaryApplicant.[Application Date],
qry_CombinedIndividual.qry_PrimaryApplicant.[Eligibility Rep],
qry_CombinedIndividual.qry_PrimaryApplicant.Name,
qry_CombinedIndividual.qry_PrimaryApplicant.Clinic,
qry_CombinedIndividual.qry_PrimaryApplicant.Outreach,
qry_CombinedIndividual.qry_PrimaryApplicant.[Content Type ID],
qry_CombinedIndividual.qry_PrimaryApplicant.[Application Status],
qry_CombinedIndividual.qry_PrimaryApplicant.Renewal,
qry_CombinedIndividual.qry_Enrolled.EthnicityEnr,
qry_CombinedIndividual.qry_Enrolled.GenderEnr, qry_CombinedIndividual.AgeAtApp,
qry_CombinedIndividual.[Percent FPL], tbl_ChildrensMedical.MinPercentFPL,
tbl_ChildrensMedical.MaxPercentFPL, tbl_ChildrensMedical.MinAge,
tbl_ChildrensMedical.MaxAge, tbl_ChildrensMedical.Program
FROM qry_CombinedIndividual
INNER JOIN tbl_ChildrensMedical ON qry_CombinedIndividual.qry_Enrolled.Patient = tbl_ChildrensMedical.Patient
WHERE (((qry_CombinedIndividual.AgeAtApp)>=[tbl_ChildrensMedical].[MinAge]
And (qry_CombinedIndividual.AgeAtApp)<[tbl_ChildrensMedical].[MinAge])
AND ((qry_CombinedIndividual.[Percent FPL])>=[tbl_ChildrensMedical].[MinPercentFPL]
And (qry_CombinedIndividual.[Percent FPL])<[tbl_ChildrensMedical].[MaxPercentFPL]));
Also there are many different programs. Here is the real Children's Table (eventually I would like to add adults if possible)
*Note the actual table uses FPL (which takes family size into account, but is used the same as income). I am again at a total loss as to how you formated the table.
Program Patient MinPercentFPL MaxPercentFPL MinAge MaxAge
SCHIP (No Premium) No 0 210 1 19
SCHIP (Tier 1) No 210 260 1 19
SCHIP (Tier 2) No 260 312 1 19
Newborn No 0 300 0 1
Newborn (Patient) Yes 0 300 0 1
Children's Medical Yes 0 200 1 19
CHIP (20 Premium) Yes 200 250 1 19
CHIP (30 Premium) Yes 250 300 1 19
Do I have the correct implementation for the table I have? Or should I be changing something. I can also send more information/sample data if that would help.
Thank you again!
I just created some tables with your sample data and used the following SQL. Your 3rd 'patient' doesn't match any of the ranges (Age 1, Income $30K)
SELECT tblPatient.PatName, tblPatient.FamInc, tblPatient.Age, tblPatient.Patient,
tblPatientRange.Program, tblPatientRange.MinInc, tblPatientRange.MaxInc, tblPatientRange.MinAge,
tblPatientRange.MaxAge, tblPatientRange.Patient
FROM tblPatient INNER JOIN tblPatientRange ON tblPatient.Patient = tblPatientRange.Patient
WHERE (((tblPatient.FamInc)>=[tblPatientRange]![MinInc] And (tblPatient.FamInc)<=[tblPatientRange]![MaxInc])
AND ((tblPatient.Age)>=[tblPatientRange]![MinAge] And (tblPatient.Age)<=[tblPatientRange]![MaxAge]));

MySQL Trouble with a query

I have a table that for example it contain 1000 records. The query that I'm trying to do, is for get some like this:
substring_part_name number_of_warehose number_of_parts
156 1 50
156 2 140
156 3 300
180 3 130
120 1 80
120 2 300
And so obtain the 1000 records.
The trouble is this, the part_name is something like this: x_156, b_156, d_156, h_120, f_120 and so on. Every part has its corresponding warehouse.
The first column i get it on this way: distinct(substring(part_name,3)) as substring_part_name, I only want the last part of the name, How i can obtain that result??
My query is this:
select distinct(substring(part_name, 3)) as substring_part_name, count(#the number of parts by ware_house), ware_house from ware_houses
group by substring_part_name;
Use a negative integer for the SUBSTRING (-3) to get the last three characters.
select
distinct(substring(part_name, -3)) as substring_part_name,
number_of_warehouse,
number_of_parts
from table
You can also use RIGHT:
distinct(right(part_name, 3)) as substring_part_name

Removing redundant values in SSRS report group

I am developing an SSRS report with the following dataset. There is a filter for 'Period'. It is a multi-select filter. Data is grouped by 'Account' field. I need to display Total Expense for each group (which was easy). I also need to display 'Budget' on the same group level. The problem is the budget data is redundant - see below.
Say for the first group (Account=100 AND Period=201301), Sum([Budget]) would generate 200, which is not true. I can use the Average function which helps if user selects only one Period from the filter. If they select multiple values (e.g. 201301,201302) then the average will be (100+100+150+150)/4=125, which would be wrong because it has to be 100+150=250. I don't want to average among all rows in the returned dataset.
ID Account Period Expense Budget
1 100 201301 20 100
2 100 201301 30 100
3 100 201302 10 150
4 100 201302 40 150
5 200 ...................
So, how do I write an expression to make this happen?
A dirty workaound would be to eliminate redundant values in the Budget column so I can safely use Sum([Budget]) w/o worrying about duplication. The updated dataset would look like this:
ID Account Period Expense Budget
1 100 201301 20 100
2 100 201301 30 NULL
3 100 201302 10 150
4 100 201302 40 NULL
5 200 ...................
Please advice for either approach. Thank you.
The most elegant way is to use the FIRST() aggregate function.
=FIRST(Fields!Budget.Value, "MyAccountGroupName")
There are some situations where this won't work. Then you need to move the logic to your query as you describe or you can get fancy with embedded code in your report.
I would follow your "dirty workaround" approach. You might possibly be able to achieve the result just inside SSRS with some fancy calculations, but it will be totally obscure.