SSRS Dropdown for column items - sql

I've built a SSRS report using a SQL query for the Dataset1. I'm trying to build a parameter that gives the users a dropdown list. I ended up creating a Dataset2 to get distinct values for the parameter Label and then use Dataset1 for the actual value (allowing for multiple values).
EDITING PER Request:
Dataset1 query:
SELECT vu_SOPWork_HistoryUnion.Type
,vu_SOPWork_HistoryUnion.SOPTYPE
,vu_SOPWork_HistoryUnion.SOPNUMBE
,vu_SOPWork_HistoryUnion.Date_Document
,vu_SOPWork_HistoryUnion.ExtendedPrice
,IV00101.ITEMNMBR
,IV00101.USCATVLS_2 AS Family
,IV00101.USCATVLS_3 AS Product
,vu_SOPWork_HistoryUnion.VoidStatus
,RM00101.CUSTCLAS
,GL00100.MNACSGMT
,vu_SOPWork_HistoryUnion.BillTo_CustNum
,vu_SOPWork_HistoryUnion.BillTo_CustName
,vu_SOPWork_HistoryUnion.sales_territory
,vu_SOPWork_HistoryUnion.ITEMDESC
FROM (
(
test.dbo.vu_SOPWork_HistoryUnion vu_SOPWork_HistoryUnion INNER JOIN test.dbo.IV00101 IV00101 ON vu_SOPWork_HistoryUnion.ITEMNMBR = IV00101.ITEMNMBR
) INNER JOIN test.dbo.RM00101 RM00101 ON vu_SOPWork_HistoryUnion.BillTo_CustNum = RM00101.CUSTNMBR
)
INNER JOIN test.dbo.GL00100 GL00100 ON IV00101.IVSLSIDX = GL00100.ACTINDX
Dataset2 Query:
SELECT DISTINCT IV00101.USCATVLS_2 AS FamilyNames
FROM test.dbo.IV00101
I would like to use the Dataset2 "FamilyNames" as the label options against Dataset1 "Family" values. Dataset2 gets the distinct values that I want to put in the drop down for the user to choose and then receive all rows that has that value in Dataset1 "Family". I'm an expert on not giving enough information. Hope this helps you help me. Thanks.

OK, do the following..
Create DataSet1 with the query as you have it now but append the following to the end of the query
WHERE USCATVLS_2 IN(#Family)
When you do this, the #Family parameter will be created automatically in your report, we'll get back to this later.
Create another dataset called Dataset2 (or a more sensible name like 'families' in this case) with the following simple query.
SELECT DISTINCT USCATVLS_2 FROM test.dbo.IV00101 ORDER BY USCATVLS_2
Edit the #Family parameter:
Set it to multi-value
Change the available values to be a query
Select your 2nd dataset as the datasource
Choose USCATVLS_2 as both the labels and values.
Create your report as normal with the tablix/matrix based on Dataset1.
Notes:
A few other things you might want to consider, but not required.
If a list of family names with some kind of ID is available in your database then I suggest you use that for your parameter list, you would also then have to edit the query in dataset1 to match. Remember that the parameter will contain whatever is in the column of the query that you chose as the value column in the dataset that populates the parameter list
Consider using aliases for table names in your queries rather than the full table names all the time, it makes the code more concise and easier to read.
Name your datasets according to what they contain or do, e.g. I would call Dataset2 'Families' or something similar as that is what it contains. When your reports get more complex it will make them easier to understand.

Related

Creating a calculated field table based on data in separate tables

It is straight forward to create a calculated field in a table that uses data IN the table... due to the fact that the expression builder is straight forward to use. However, it appears to me that the expression builder for the calculated field only works with data IN the table;
i.e: expression builder in table MYTABLE works with fields FIELD1.MYTABLE, FIELD2.MYTABLE etc.
Inventory Problem
My problem is that I have two 'count' fields that result from my queries that apply to INPUTQUERY and OUTPUTQUERY (gives me a count of all input data added and a count of all output data added) and now I want to subtract the two to get a stock.
I can't link the table that was created from my query because it wont be able to continually update do the relationship itself, and thus i'm stuck either using the expression builder/SQL.
First question:
Is it possible to have the expression builder reference data from other tables?
i.e expressionbuilder for:
MAINTABLE CALCULATEDFIELD.MAINTABLE = INPUTSUM.INPUTTABLE - OUTPUTSUM.OUTPUTTABLE
(which gives a difference of the two)?
Second question:
if the above isn't possible, can I do this through an SQL code ?
i.e
SELECT(data from INPUTSUM)
FROM(INPUTTABLE)
-
SELECT(data from OUTPUTSUM)
FROM(OUTPUTTABLE)
Try this:
SELECT SUM(T.INPUTSUM) - SUM(T.OUTPUTSUM) AS RESULTSUM
FROM
(
SELECT INPUTSUM, 0 AS OUTPUTSUM
FROM INPUTTABLE
UNION
SELECT 0 AS INPUTSUM, OUTPUTSUM
FROM OUTPUTTABLE
) AS T

Dynamic SQL queries as parameter

I need a Report where a user has to choose 2 parameters. The first parameter contains the years (2017, 2016...), and the second one contains the ID process. Depending on the process that the user chooses, the SQL statement will be one or another. The parameter year is part of the WHERE clause of the SQL contained in the second parameter.
So I have this report with 2 parameters (param_year, Indicador). Query parameter is done using a table datasource, where the IDs column contains the SQL sentences and the Values column contains the text the user must select.
So what I'm doing next is to set ${Indicador} as the SQL statement in the JDBC connection that I have done to the Database. This is reporting me an SQL error
"Failed at query: ${Indicador}.
Any suggestions will be appreciated. Thanks in advance.
Another option is to create multiple datasources in your Master/sub report, then select appropriate datasource using PRD expression on Master/sub Report -> Attributes -> query -> name attribute.
More detailed explanation:
Create a query (I mean a query as a PRD object, which uses the PRD datasource) for every SQL string you need and move the SQL strings from the parameter table into Report Designer queries definitions.
Replace the SQL strings in your parameter table with names of corresponding queries, e.g:
Use the value of your parameter (which should be equal to the PRD query name) as value for Master/sub Report -> Attributes -> query -> name attribute:
You need Pentaho Data Integration to do this kind of dynamic query
If the table structure (output columns) for both queries is the same, you could put them together into one big SQL statement with UNION ALL and put in each query "WHERE ${Indicador} = ValueToRunThisQuery".
The optimizer should be smart enough to know the not-selected subquery is going to return zero rows and not even run it. You can supply a few null columns if one query has fewer columns, but the data types have to be the same for filled columns.
If the output table structure is different between the two queries they should be in different data sources, or even reports.
SELECT ID, BLA, BLA, BLA, ONLY_IN_A
FROM TABLE A
WHERE ${Indicador} = "S010"
UNION ALL
SELECT ID, BLA, BLA, BLA, NULL
FROM TABLE B
WHERE ${Indicador} = "S020"

SSRS: When Multi-value Parameter = Select ALL remove filter in Script

I have a report that has multiple multi-value parameter. What I wanted to do is if the parameter is = Select All I'll remove that parameter to my SQL Script.
Example is I have a Product group and Product Name parameter and what I want is if the user selects all the product group my script will be like:
SELECT * FROM TABLE WHERE PRODUCT_NAME IN (#ProductName)
While if the user did not select all Product Group, my script will be like:
SELECT * FROM TABLE WHERE PRODUCT_NAME IN (#ProductName)
AND PRODUCT_GROUP IN (#ProductGroup)
I want to know how Can I detect when multi-value parameter is = Select All. I think it will really help the loading time of the tool if I just remove the filter on my script.
As far as I know, you cannot detect when "Select All" has been checked by the user.
To remove the filter completely when the user selects all the choices in a multi-valued parameter you would have to employ logic in your stored procedure that checks to see if all the choices were passed to it in the parameter, and if so, don't use the parameter at all in the main query.
You can add your own ALL value to the parameter. In the dataset query check if the user have selected 'ALL' if so don't use the parameter.
Something like this:
IF ('ALL' IN #ProductGroup)
BEGIN
SELECT * FROM TABLE WHERE PRODUCT_NAME IN (#ProductName)
END
ELSE
SELECT * FROM TABLE WHERE PRODUCT_NAME IN (#ProductName)
AND PRODUCT_GROUP IN (#ProductGroup)
It is not tested but should work
If your parameter is based on a dataset, you can compare the numbers of elements selected in the parameter vs. the number of items in the dataset for the parameter.
=IIF(Parameters!AREA.Count = CountRows("Areas"), "ALL", "Some")

Automatically generated SQL code throws type mismatch

My problem is that I would like to make an append query in Ms-Access 2010. I tried to realize it in query designer, but it throws an error:
Type mismatch in expression
See the generated code below:
INSERT INTO Yield ( ProcessName, Sor, Lot,
ProcessCode, Outgoing, DefectReason, DefectQty, ModifyQty )
SELECT Process.[ProcessName], Sor.[Sor], Qty.[Lot], Qty.[ProcessCode],
Qty.[Outgoing Date], Qty.[Defect Reason], Qty.[Defect Qty], Qty.[Modify_Qty]
FROM (Sor INNER JOIN ProcessCode ON Sor.[SorID] = ProcessCode.[SorID])
INNER JOIN (Process INNER JOIN Qty ON Process.[ProcessID] = Qty.[ProcessID])
ON ProcessCode.[ProcessID] = Process.[ProcessID];
The tables and the attributes are all existing. The ID numbers are indexes, the Quantities are numerical, the 'ProcessName', 'Sor', 'Lot', 'ProcessCode', 'DefectReason' attributes are strings.
What could be the problem?
Thanks in advance.
Looks ok. The best advice is divide it in smaller pieces.
http://importblogkit.com/2015/05/how-do-you-eat-an-elephant/ .
Try this:
Remove the insert part. Just try the select to make sure the join are working properly. If this fail the problem is on the join fields
Then, Put the insert again, but instead of putting table fields from the SELECT use default values. '' for strings and 0 for numeric and put the right alias for column name. That way you make sure your data is bringing the right data type. If this fail then one of the field isnt really a string or a number. Like gustav suggest probably a DATE
If that work then try to put one table field each time until you find the one causing the problem. Maybe one field doesnt support null or is receiving a bigger value than supported.
The problem was that the Yield table did not have the listed attributes. I thought that if some of the listed output attributes are not included in the output table, Access automatically creates the missing new attributes. I was wrong. The output table has to contain the attributes (rows), new attributes cannot be inserted into it this way.

Inserting a new column into SQL

I have these queries:
SELECT *
FROM dbo.GRAUD_ProjectsByCostCategory
select right(CostCategoryId,14) as CostBreak
from dbo.GRAUD_ProjectsByCostCategory
They work well in that they give me the correct data, but I would like to know how to combine the new column CostBreak into the table of results rather than as a separate query result.
An example of the results I get are as below:
Where I want them in the same table
The data is coming from the same table so you should be able to just add that value to your initial query. You do not even have to perform a join to get it:
SELECT name,
description,
project,
CostCategoryId,
right(CostCategoryId,14) as CostBreak
FROM dbo.GRAUD_ProjectsByCostCategory