complicated nested if statement - ms-access

I am hoping that someone can modify my existing query and provide me the SQL for me to copy and paste based on my 2 conditions listed below. Thank you very much in advance, Nathaniel
condition#1. compare the values of the following 3 fields and output the lowest value of the 3 fields (exlcuding null values):
"lowest list price"
"lowest price sold"
"lining_price_exception"
example: if "lowest list price" = 3 and "lowest price sold" = 7 and "lining_price_exception" =null, then the output should be 3.
conditon#2. if the value in the field "overide_price" is not null, then the field "overide_price" should replace the output from from condition #1. if the value in "overide_price" is null, then output the value from condition#1.
SELECT q.Part_ID, q.[lowest list price], q.[lowest price sold],
q.lining_price_exception, q.overide_price
FROM qry_to_determine_wmx_price_01 AS q;

This should be complicated enough for you. Here is tested SQL no need to define any VBA methods which meets both criteria:
Select q.Part_ID,
q.[lowest list price],
q.[lowest price sold],
q.lining_price_exception,
q.overide_price,
NZ(q.overide_price,
IIf(q.[lowest list price]<q.[lowest price sold] OR ISNULL(q.[lowest price sold]),
IIf(q.[lowest list price]<q.lining_price_exception or ISNULL(q.lining_price_exception),
NZ(q.[lowest list price],"No valid Price listed"),
q.lining_price_exception
),
IIf(q.[lowest price sold]<q.lining_price_exception or ISNULL(q.lining_price_exception),
NZ(q.[lowest price sold],"No valid Price listed"),
q.lining_price_exception
)
)
) as Actual_Low_Price
from qry_to_determine_wmx_price_01 as q;

If you will be running your query from within an Access session, you can use a custom VBA function to make this easier.
Testing the MinValue() function in the Immediate window ...
? MinValue(4, Null, 2)
2
? MinValue(Null, Null, 2)
2
? MinValue(Null, Null, Null)
Null
So you could tackle your condition#1 in a query like this ...
SELECT
MinValue(
q.[lowest list price],
q.[lowest price sold],
q.lining_price_exception
) AS lowest_value
qry_to_determine_wmx_price_01 AS q;
As for your conditon#2, I think the Nz() function would be useful.
Nz(q.overide_price, MinValue(q.[lowest list price], q.[lowest price sold],
q.lining_price_exception))
Basically that Nz() expression says "return overide_price if it's not Null, otherwise return the value from the MinValue() expression".
This is the function I mentioned.
Public Function MinValue(ParamArray pValues() As Variant) As Variant
Dim i As Long
Dim iUbound As Long
Dim varMin As Variant
iUbound = UBound(pValues)
varMin = Null
For i = 0 To iUbound
If Not IsNull(pValues(i)) Then
If Not IsNull(varMin) Then
If pValues(i) < varMin Then
varMin = pValues(i)
End If
Else
varMin = pValues(i)
End If
End If
Next
MinValue = varMin
End Function

Related

Insert multiple value parameters into table

I have a client who wants to drop a list of values into a parameter in Reporting Services, compare it to a table in her database and return the full list of values with a yes or no in the next field indicating whether the value is found in the database table. For example if her list is Duck, Horse, Chicken and only Duck exists in the table she wants the result to look like this:
Duck Yes
Horse No
Chicken No
She doesn't want to return only those values that match so a simple WHERE Animal IN (#ReportParameter1) isn't going to do it.
I can make this work for a single value parameter like this:
DECLARE #FarmAnimals AS TABLE (Animal varchar(50))
INSERT INTO #FarmAnimals VALUES (#ReportParameter1)
SELECT Animal
,'In Barnyard’ = CASE WHEN EXISTS
(SELECT *
FROM tblBarnyard
WHERE BarnyardAnimal = Animal)
THEN 'Yes'
ELSE 'No'
END
FROM #FarmAnimals
But is it possible to loop through a multiple value parameter of unspecified length and create an INSERT INTO statement for each individual value? If this isn't possible I'm happy to tell her it can't be done but I can't think of a time I've found that something was impossible in SQL.
There's no need to do it in SQL, you can just write a custom code function to iterate through the selected parameter values to get the result that you want.
On the Report menu, access Report Properties... and select the Code tab. Insert the following code:
Function IsFieldSelected (fieldValue as String, ByVal parameter As Parameter) As String
Dim Result As String
Result = "No"
If parameter.IsMultiValue Then
For i As integer = 0 To parameter.Count-1
If (fieldValue = parameter.Value(i)) Then
Result = "Yes"
End If
Next
Else
If (fieldValue = parameter.Value) Then
Result = "Yes"
End If
End If
Return Result
End Function
then use this expression in the cell that you want the "Yes/No" to access the result:
=Code.IsFieldSelected(Fields!MyField.Value, Parameters!MyParameter)
Note that you are passing the parameter object here, not the Value property (so don't put .Value on the end). We access the Value property of the parameter object in the custom code function and compare it to the field value passed.
This will work for both single- and multi-value parameters.
You can do this using the STRING_SPLIT function in SQL Server.
--Placeholder table
DECLARE #ExistingValuesInTable TABLE (Val VARCHAR(255)) ;
INSERT INTO #ExistingValuesInTable (Val) VALUES ('Duck') ;
--
DECLARE #UserInput VARCHAR(255) = 'Duck, Horse, Chicken' ;
SELECT ss.value,
CASE WHEN evit.Val IS NULL THEN 'No' ELSE 'Yes' END AS AlreadyExists
FROM STRING_SPLIT(#UserInput, ',') AS ss
LEFT OUTER JOIN #ExistingValuesInTable AS evit ON ss.value = evit.Val ;

create a set expression for a cell in a table report

I have a report which creates a spreadsheet. one cell could have data from one of three fields depending on a fourth field status or field two being blank..
I was thinking of writing as CASE statement but that does not work in ssrs.
The IIF statement works great for 2 values but how do you write for three values?
In common language it would be "If secondary value ="Yes", use secondary name, If account field value is Null, use Contact field value, otherwise use account field value"
Thanks
You can use nested IIF statements but they quickly get messy. You should use SWITCH which acts a bit like a case statement.
A simple example would be
= SWITCH(
Fields!myFirstField.Value = <10 , "Small",
Fields!myFirstField.Value = <30 , "Medium",
Fields!myFirstField.Value = <80 , "Large",
True , "HUGE",
)
As switch stops at the first true expression there is not need to check ranges of values if you get them in the correct order.
The final "True" expression just acts like an ELSE
If you need to check multiple criteria per condition then you can do that too like this..
= SWITCH(
Fields!myFirstField.Value = <10 AND Fields!mySecondField.Value = 1 , "Small Cat",
Fields!myFirstField.Value = <10 AND Fields!mySecondField.Value = 2 , "Small Dog",
Fields!myFirstField.Value = <30 , "Medium Animal",
Fields!myFirstField.Value = <80 , "Large Animal",
True , "HUGE Animal",
)

How can I get both values from a multi-dimensional array in a multi-select ssrs report parameter?

I have the following SQL query in an SSRS dataset:
SELECT c.Id
, c.LastName + ', ' + c.FirstName AS CustomerName
, r.PurchaseDate
FROM tblCustomer c
JOIN Receipt r ON r.CustomerId = c.Id
WHERE StoreId = #storeId
I also have three parameters for the report: start date, end date, and customer. The workflow is: select a start date, select an end date, then the above dataset is filtered to only show the customer names in the multi-select dropdown parameter that have a receipt date within the start and end dates. The problem is, when customer has multiple receipts within the date range, the customer shows up more than once in the dropdown parameter. I copied the VB code that filters out the duplicates:
Public Shared Function RemoveDuplicates(parameter As Parameter) As String()
Dim items As Object() = parameter.Value
System.Array.Sort(items)
Dim k As Integer = 0
For i As Integer = 0 To items.Length - 1
If i > 0 AndAlso items(i).Equals(items(i - 1)) Then
Continue For
End If
items(k) = items(i)
k += 1
Next
Dim unique As [String]() = New [String](k - 1) {}
System.Array.Copy(items, 0, unique, 0, k)
Return unique
End Function
which works great, except that it only shows the Customer ID in the dropdown.
How do I get the multi-select dropdown to have the CustomerName as the label and the Customer Id as the value?
You can get around this by doing a cascading parameters.
I don't see where the start_date and end_date parameters are being used..
You can create another dataset, lets call it customers.
Your customer dateset query will be:
SELECT DISTINCT
c.Id
, c.LastName + ', ' + c.FirstName AS CustomerName
FROM tblCustomer c
JOIN Receipt r ON r.CustomerId = c.Id
WHERE StoreId = #storeId
-- and Receipt.somedate between #start_date and #end_date
Set your customer parameter to source it's data from this query. You will only ever have customers from the above select..
Go to the customer parameter.. available value -> customer data set
Set the values in there - value field will be ID and Label field will be name
Of course, your main dataset need to have #customerID filter along with #start_date and #end_date

Mysql assign a single column value to a declared value using Select case

I am stucked with following issue,
I ve delcared a varibale (DOUBLE) in a stored procedure, and i need to assign a value (item price) from a table (item) to this declared variable. however, i need to get this value from a select query which uses case inside, the item price could be in 2 columns based on a logic i have to find the correct item price. Please help me solve this as when i execute it gives me a error,
Here how is layered,
DECLARE no_more_users INT DEFAULT 0;
DECLARE user_id INT DEFAULT 0;
DECLARE cart_id INT DEFAULT 0;
DECLARE cart_item_id INT DEFAULT 0;
DECLARE user_gift_id INT DEFAULT 0;
DECLARE itemPrice DOUBLE DEFAULT 0.0;
SELECT
CASE
WHEN sale_price=0 OR sale_price IS NULL THEN (price - ( price * discount ))
ELSE sale_price
END
INTO itemPrice
FROM item WHERE item_id = p_item_id ;
DECLARE checked_in_users CURSOR FOR
SELECT DISTINCT ul.user_id
FROM user_location ul
LEFT JOIN location_section ls ON ul.location_section_id = ls.location_section_id
INNER JOIN user u ON ul.user_id = u.user_id
INNER JOIN user_profile up ON u.user_id = up.user_id
INNER JOIN location_event le ON ul.location_event_id = le.location_event_id
WHERE ul.location_id = p_location_id AND ul.location_event_id = p_event_id
AND ul.checked_out_on IS NULL AND (ul.checked_in_on BETWEEN le.start_time AND le.end_time )
AND u.status = 1 ;
DECLARE CONTINUE HANDLER FOR NOT FOUND
SET no_more_users = 1;
OPEN checked_in_users;
FETCH checked_in_users INTO user_id;
read_loop: LOOP
some more code...
please note, sale_price, price, and discount are coulmns of item table, The logic is if sale_price is null or value is 0, then i actual sale price should be obtained from price coulmn. Finally what i need is asign corect item price in to previously declared varibale.
Any help will be highly appriciated.
There are two foms to the CASE statement, one with expressions and one with values. You are mixing them up.
with values:
CASE variable
WHEN value_1 THEN foo
WHEN value_2 THEN bar
END
with expressions:
CASE
WHEN expression_1 THEN foo
WHEN expression_2 THEN bar
END
Try
CASE
WHEN sale_price=0 OR sale_price IS NULL THEN (price - ( price * discount ))
ELSE sale_price
END
Also, you do not need the "AS correct_price" when you are using an INTO
Your select query has some problems.
The version of CASE you are are using - ie CASE var WHEN val1 THEN - doesn't work when trying to match a NILL, because in SQL null is not equal to null.
Also, you can't combine values with conditions as you hAve.
Instead, use the version if CASS that simply conditions. There are other suntactic problems. Converting your broken query then gives:
SELECT
CASE
WHEN ifnull(sale_price, 0) = 0 THEN price - (price * discount)
ELSE sale_price
END
INTO itemPrice
FROM item
WHERE item_id = p_item_id;
Note: if your table has s column called itemPrice, you must choose another name from your variable. Mysql gets confused :/

Using SSRS expression as a variable

I am trying to use a expression within an expression in SSRS and it's getting really complicated. Is there a way around this?
For example if I have a number of sales and number of contacts I can create the conversion rate by dividing the sales into the contacts ie
=fields!sales.value/fields!contacts.value
What I want to do next is use this to put the sales people into various bands so that those with a conversion rate in thresholds are displayed in bandings.
=iif(fields!sales.value/fields!contacts.value<0.5,"poor","Ok")
This is fine for 2 bandings (ok and poor), but gets more complicated for the 5 bandings I want.
I must be able to define the first variable and then use that within the iif statement?
How do I do this in SSRS 2008?
Thanks in advance.
Better to use Switch which returns the first true condition
=Switch(
fields!sales.value/fields!contacts.value < value1, "very poor",
fields!sales.value/fields!contacts.value < value2, "poor",
...
fields!sales.value/fields!contacts.value < value(n-2), "good",
fields!sales.value/fields!contacts.value < value(n-1), "very good",
True, "excellent"
)
where value1 < value2 < ... < valuen.
The final True is dummy like the use of default in an SQL Case expression
An alternative is using custom vb code you can create a function
Public Function GetRating( ByVal rating As Decimal ) As String
Select Case rating
Case Is < value1
Return "poor"
...
Case Is < valuen
return "very good"
Case Else
Return "excellent"
End Function
Then in the rows use the expression
= Code.GetRating( fields!sales.value/fields!contacts.value )