Conditional DropDown List with SSRS - reporting-services

I created a SSRS report with a dropdown to parameterize data being reported. This works fine. I now need to add another parameter to filter the report a little further. For example, I have a location dropdown that shows area, country, region, etc. I need to add another dropdown that is dependant on the first dropdown. So if I select "country" in the 1st dropdown, I show the list of countries in the 2nd dropdown or if I select Region - I show list of Regions in the 2nd dropdown. Country, Area, Region data is stored in different tables. So basically my query needs to be smart enough to run the appropriate sql based on 1st dropdown selection.
Thanks so much for any assistance given.

One of the powerful features of Reporting Services is that everything is an expression, including the dataset's SQL statement.
Let's say your first parameter (the one that describes what to select) is called Location and it selects a list of locations such as country, region, etc. Perhaps you get that from a table which has a LocationId and a Description like so:
SELECT LocationId, Description FROM Locations
You hook up the Location parameter to this query to get your drop down list of location selectors for the Location parameter.
Now create a second parameter called Select where we want to select from a list of countries or regions. To keep it simple, I'm going to assume there are only two locations: Country with a LocationId of 1 and Region with a LocationId of 2.
Create a new dataset called Selections and manually add fields to it called Id and Description. Hook up your Select parameter to this dataset. Now for the SQL Statement for the Selections dataset, enter the following expression:
=IIF(Parameters!Location.Value = 1,
"SELECT CountryId AS Id, CountryName AS Description FROM Countries",
"SELECT RegionId AS Id, RegionName AS Description FROM Regions")
So, where the Location parameter is set to 1 (Country) you select from the Countries table otherwise you select from the Regions table. You alias the field names so you get consistently named fields for your dataset for use in the Select parameter query. Obviously, you can extend this to more selections as required.
You get the idea but this is a little fragile - whenever you want to add a new location type you have to go through all your reports and update the SQL statement for the Selections dataset. That's tedious and no one wants that job. What we want is an automated system where all reports get the new selections whenever they are added.
So let's add a column to the Locations table, called SQLStatement. For the Country row, this will have the value:
SELECT CountryId AS Id, CountryName AS Description FROM Countries
For the Region row, the SQLStatement field has the value:
SELECT RegionId AS Id, RegionName AS Description FROM Regions
Now the Locations table has the value of the SQLStatement in it for the Selections dataset. You can't use this directly (your dataset would just return the value of the SQL statement field, not execute it) but you want to have something that returns this string as the expression to use for the SQL statement for the Selections dataset. Custom code can be used to do this. So the expression to use for the Selections dataset will be something like this:
=Code.GetSQLStatement(Parameters!Location.Value)
And then you have custom code function like this (significant parts left out):
Public Function GetSQLStatement(LocationId As Integer) As String
Dim SQL As String
SQL = "SELECT SQLStatement FROM Locations WHERE LocationId = " + CStr(LocationId)
' Connect to database, get SQLStatement field
GetSQLStatement = <Field Value>
End Function
When you want to add another location selection, for example continents, all you have to do is add another row to the Locations table, say LocationId = 3, Description = Continent and SQLStatement = SELECT ContinentId AS Id, ContinentName AS Description FROM Continents and now every report you have that selects by location will be able to use Continents.

First dataset add this SQL
Select ContryName, CountryID From Country
Assuming the name the above dataset's parameter is #country add the following SQL on the second dataset
Select RegionName, RegionID From Region
Where CountryID IN( #country)

It is very simple. Let’s assume that the data of the table “ONE” as follows:
**Location_Type** **Location**
Country India
Country Sri Lanka
Country China
Country Japan
City Bangalore
City Hyderabad
City Delhi
Ex:- Query for the Report parameter (#Location_Type1):
Select Distinct Location_Type from One
Query for the second Report Parameter:
Select Location from ONE where Location_Type = #Location_Type1.
Please let me know if it is not clear.

Related

Get data from table by County in a single row

I want to get data from table in a single row for single county. Right now as shown in images it shows Different rows for Single county as Kalamazoo. It should show single with both records in single row.
I am using following query
SELECT County, Virus, SumOfPositiveTests
FROM StatewiseData
WHERE State = 'MICHIGAN'
I want the results to be shown as following
County Virus SUMOFPOSITIVETESTS
---------------------------------------------
Kalamazoo H3N2,H3N8 3
Total sum of both H3N2 and H3N8
use MYSQL GROUP_CONCAT
The following MySQL statement will return a list of comma(,) separated 'Virus's for each group of 'County' from the StatewiseData table
SELECT County,GROUP_CONCAT(Virus),sum(SumOfPositiveTests) FROM StatewiseData where State='MICHIGAN' group by County
Try this if you are using mysql:
SELECT country, GROUP_CONCAT(virus) _virus, SUM(SumofPositiveTests) as TotalPostitiveTest
FROM StatewiseData
WHERE Country = 'Kalamzoo'
GROUP BY Country
Please mind that this will not work with most databases, as they need you to to specify only the grouping field and aggregate functions of other fields in your query.
Here is the source
Try this. this would work on IBM DB2 and MySQL. what DB you are using
SELECT DISTINCT Country, Virus,SumOfPositiveTests FROM Customers where State='MICHIGAN' group by Country;
why are you using state in where condition? (please provide list of columns do you have in a column if i missed something)
if you are only selecting the country then it should be as simple as:
SELECT County,Virus,SumOfPositiveTests FROM StatewiseData where Country='Kalamzoo'
if you want to select summary or totals of single country then you you should use sum like:
SELECT country, SUM(SumofPositiveTests) as TotalPostitiveTest
FROM StatewiseData WHERE Country='Kalamzoo' GROUP BY Country

Getting values from form as query criteria

I have a library database. There are some kind of data (books, magazines and etc.).
I have a form on base of report of these data. In this example I'm trying to add in combobox just one value - Name of the book. I'm doing it by such a query in row source of the combobox:
SELECT [Forms]![books_list]![name] AS id;
books_list - the name of the form and name is the text box called name.
It returns only the first name of the book in all comboboxes - 123.
I need to show only these name values in different comboboxex, which has reference by the row.
To make it clearer - in first combobox - 123, second - Прошлое, third - May be next time.
As I understand, you want to take the value from the text box name, and use a query to select the same value in the combo box?
SELECT <insert your column name here>
FROM <insert your table name here>
GROUP BY <insert your column name here>
HAVING ((<insert column name here>)=[Forms]![books_list]![name] AS id);

SQL Server Report Filter Expressions

So I have two datasets called Employee and EmployeeData. Employee is the main dataset that is used by the report and I want to add a filter to it. The filter parameter is created as EmployeeID. Employee has columns Name, Department, Job, etc. EmployeeData has columns Name, EmployeeID, Address.
I'm trying to filter using EmployeeID on Employee so I need to do some sort of join with EmployeeData using Name as the key. This would be easy in SQL but since I'm new to SSRS, I don't know what the syntax of the expression would look like here.
Thanks
Create another parameter #employeename and set it's default value to the employee name from the employeename dataset, then use that parameter to filter the Employee dataset based on the like operator and the name field.
SSRS is very sensitive to parameter order, so you may need to delete and recreate them in the correct order.

MSACCESS. Forms- Dropdown to update foreign key

Tables
Company- CompanyID, CountryID , Company Name,
Country-CountryID, Country Name
I am creating a form to edit the company information. I am having problems updating Company's CountryID. I want a dropdown to displays every country which will modify the company's foreign key.
I followed the tutorial here. http://www.techonthenet.com/access/comboboxes/bind_index.php. It works with inner but it doesn't display all the countries. But when I switched SQL inner to left join, the form fails.
My sql-
SELECT DISTINCT table_country.Country, table_company.Country_id
FROM table_country LEFT JOIN table_company ON table_country.Country_id = table_company.Country_id
ORDER BY table_country.Country;
![enter image description here][2]
The the RowSource property of the combobox should only contain a lookup list. It should not be retrieving records from the company table.
The RowSource property should be:
SELECT Country_ID, Country FROM table_country ORDER BY Country
Set Column Count to 2
Set Bound Column to 1
Set Column Width to 0 as you don't want to display the country_id only country.

Microsoft Access question

I have a combo box which has 3 pieces of information
COMBO BOX 210: Materials ID, Name of Product, Cost, Description
After I update the combo box the cost figure is stored in another field in the table, so far so good, so in selecting "Apples" the cost "$1" stores.
Now, what I want to is have a different field where the description of the apple (the text) is stored yet I only update the combo box once, in other words after updating the combo box Field 1 stores the price and Field 2 the description of the apple.
I will offer you a different example which I hope is similar to what you're asking.
My form includes a combo named cboNames. This is the query for its row source:
SELECT f.id, f.fname, f.date_added
FROM food_test AS f
ORDER BY f.fname, f.date_added;
In the combo's After Update event I can access the values of those 3 columns in the selected combo row by referring to the Column index. Notice the column index numbering starts with zero.
Private Sub cboNames_AfterUpdate()
Debug.Print Me.cboNames.Column(0)
Debug.Print Me.cboNames.Column(1)
Debug.Print Me.cboNames.Column(2)
End Sub
So if I wanted to put the date_added value into another data control, I could add this to the combo's After Update event.
Me.SomeOtherControl = Me.cboNames.Colummn(2)
However I wouldn't actually store both id and date_added in a row of another table. In my food table, each id is associated with a unique combination of fname and date_added. So I would store only the id, and use a SELECT with a JOIN to the food table to look up the associated fname and date_added values whenever I needed them.