How can we use iReport parameter in sql query column - sql

In an iReport I'm using parameter in SQL query, but the parameter field is not showing in the data set. I have a fee structure table which 12 months columns for amount, I'm trying to get the desire month by passing the parameter as column, but the parameter column is not showing in the data field. Here is my query, I'm passing FeeStructure.$P!{Months} as a parameter column.
SELECT
StudentInformation.ID, ClassS.Class, Parents.Parents,
FeeCatagory.FeeCatagory, FeeStructure.$P!{Months}
FROM
StudentInformation
INNER JOIN
ClassS ON StudentInformation.[C/Class] = ClassS.Id
INNER JOIN
Parents ON StudentInformation.ParentID = Parents.ID
INNER JOIN
FeeStructure ON ClassS.Id = FeeStructure.ClassID AND Parents.ID = FeeStructure.ParentID
INNER JOIN
FeeCatagory ON FeeStructure.CatagoryID = FeeCatagory.ID
WHERE
Class = $P{ClassID}
AND Parents = $P{Parent}
AND StudentInformation.ID = $P{StudentID}
ORDER BY
FeeCatagory

plz check with this.
$P!{Months} for this parameter give any month name as default value
and also use aliasing
FeeStructure.$P!{Months} as month
instead of
FeeStructure.$P!{Months}

Related

Oracle SQL query that deals with inner Joins and values

SELECT sc.TAAC_SHARE_CLASS_ID,
SCS.SHARE_CLASS_SID,
SCS.REPORTING_DT,
SCS.SHARE_CLASS_SNAPSHOT_SID,
SCS.DIST_UNMOD_30_DAY_YIELD_PCT,
SCS.DER_DIST_12_MO_YIELD_PCT,
SCS.DER_SEC_30_DAY_YIELD_PCT AS SCS_DER_SEC_30_DAY_YIELD_PCT,
SCS.DER_SEC_RESTATED_YIELD_PCT AS SCS_DER_SEC_RESTATED_YIELD_PCT
FROM SHARE_CLASS sc
INNER JOIN PORTFOLIO P ON (P.PORTFOLIO_SID=SC.PORTFOLIO_SID)
INNER JOIN SHARE_CLASS_SNAPSHOT SCS ON
(SCS.SHARE_CLASS_SID=sc.SHARE_CLASS_SID)
WHERE SCS.REPORTING_DT = '24-JUL-17' AND P.PORTFOLIO_ID = 638;
I ran this query and got the following output : image
Here, instead of getting separate rows for the same TAAC_SHARE_CLASS_ID, I want to merge the outputs of same TAAC_SHARE_CLASS_ID.
For example, the first row with TAAC_SHARE_CLASS_ID = 000648 should have values for all the 4 columns :
SCS.DIST_UNMOD_30_DAY_YIELD_PCT,
SCS.DER_DIST_12_MO_YIELD_PCT,
SCS.DER_SEC_30_DAY_YIELD_PCT,
SCS.DER_SEC_RESTATED_YIELD_PCT.
Hence the first row should have values for those columns as 2.96,3.2972596, 7541.085263433, 7550.
The last 4 rows of my output are not really required, as we have now merged those data into first 4 rows correspondingly.
How can I alter this query to achieve the same? Please help.
I suggest you group your results by TAAC_SHARE_CLASS_ID column, and MAX() the remaining columns, something like this:
SELECT sc.TAAC_SHARE_CLASS_ID,
max(SCS.SHARE_CLASS_SID) as SHARE_CLASS_SID,
max(SCS.REPORTING_DT) as REPORTING_DT,
max(SCS.SHARE_CLASS_SNAPSHOT_SID) as SHARE_CLASS_SNAPSHOT_SID,
max(SCS.DIST_UNMOD_30_DAY_YIELD_PCT) as DIST_UNMOD_30_DAY_YIELD_PCT,
max(SCS.DER_DIST_12_MO_YIELD_PCT) as DER_DIST_12_MO_YIELD_PCT,
max(SCS.DER_SEC_30_DAY_YIELD_PCT) AS SCS_DER_SEC_30_DAY_YIELD_PCT,
max(SCS.DER_SEC_RESTATED_YIELD_PCT) AS SCS_DER_SEC_RESTATED_YIELD_PCT
FROM SHARE_CLASS sc
INNER JOIN PORTFOLIO P ON (P.PORTFOLIO_SID=SC.PORTFOLIO_SID)
INNER JOIN SHARE_CLASS_SNAPSHOT SCS ON (SCS.SHARE_CLASS_SID=sc.SHARE_CLASS_SID)
WHERE SCS.REPORTING_DT = '24-JUL-17' AND P.PORTFOLIO_ID = 638
GROUP BY sc.TAAC_SHARE_CLASS_ID;

SQL - select data based on one specific input value or all if no input value

I am trying to retrieve data using an input variable that is a Customer code. If a user enters the customer code, the query retrieves that customers data, but if the user leaves the customer code blank, I want to retrieve all customers data. Below is the code where I can retrieve data based on the customer code input in '3_customer' but I can't figure out how to do this IF-THEN-ELSE type of query I need to get data for all customers if the input variable is left blank.
Thanks,
Don
SELECT
open_item.order_id order_id,
convert(varchar(10),orders.ordered_date,101) order_date,
orders.revenue_code_id,
orders.operations_user,
open_item.customer_id cust_no,
customer.name cust_name,
convert(varchar(10),open_item.gl_date,101) adjust_date,
open_item.amount amount,
open_item.record_type type,
open_item.ar_reason_code_id,
ar_reason_code.descr reason
FROM
open_item
LEFT OUTER JOIN
customer ON customer.id = open_item.customer_id
LEFT OUTER JOIN
ar_reason_code ON open_item.ar_reason_code_id = ar_reason_code.id
LEFT OUTER JOIN
orders ON orders.id = open_item.order_id
WHERE
open_item.gl_date >= $1_sdate$ AND
open_item.gl_date <= $2_edate$ AND
open_item.source = 'C' AND
open_item.record_type = 'C' AND
open_item.customer_id = $3_customer$
An alternative approach is to:
open_item.customer_id = coalesce($3_customer$, open_item.customer_ID)
assuming $3_customer$ will be NULL
so $3_customer$ must not be null so then use a case
open_item.customer_id = case when $3_customer$ = ''
then open_item.customer_ID else $3_customer$
coalesce takes the first non-null value in a series in essence this will always return TRUE when the customer parameter is null. It does this because it compares the same values thus always equaling; otherwise this will filter for the specific $3_customer$ provided
You need to slightly change the WHERE clause for the customer condition from this:
and open_item.customer_id = $3_customer$
to something like this: (The below code comes with the assumption that $3_customer$ is getting inserted into the query string, and will be set to NULL if empty)
and ($3_customer$ IS NULL OR open_item.customer_id = $3_customer$)

Select a field called "return" in postgreSQL

I'm having a problem with a query in postgres, the table cgporders_items has a field called return, I cannot get actual result of that field with this query, it returns me al ceros.
SELECT "Cgporder".id AS "Cgporder__id"
,"Sale".preorder_number AS "Sale__preorder_number"
,"Contact".id AS "Contact__id"
,"Contact".NAME AS "Contact__name"
,"Ptype".NAME AS "Ptype__name"
,(
SELECT code
FROM products
WHERE id = "CgporderItem".parent_id
) AS "Product__parent_code"
,"Product".id AS "Product__id"
,"Product".code AS "Product__code"
,"Product".NAME AS "Product__name"
,"CgporderItem".quantity AS "CgporderItem__quantity"
,"CgporderItem".return AS "CgporderItem__return"
,"CgporderItem".cep_id AS "CgporderItem__cep"
FROM cgporders AS "Cgporder"
INNER JOIN contacts AS "Contact" ON ("Contact".id = "Cgporder".contact_id)
INNER JOIN cgporders_items AS "CgporderItem" ON ("Cgporder".id = "CgporderItem".cgporder_id)
INNER JOIN products AS "Product" ON ("Product".id = "CgporderItem".product_id)
INNER JOIN ptypes AS "Ptype" ON ("Ptype".id = "Product".ptype_id)
LEFT JOIN cgporders_sales AS "CgporderSale" ON ("Cgporder".id = "CgporderSale".cgporder_id)
LEFT JOIN sales AS "Sale" ON ("Sale".id = "CgporderSale".sale_id)
WHERE "CgporderItem".parent_id != 0
AND "Cgporder"."issue_date" >= '2015-11-27'
AND "Cgporder"."issue_date" <= '2015-11-27'
AND "Cgporder"."status" = 'confirmed'
ORDER BY "Ptype".NAME
,"Product"."code";
There are actually a lots of rows that matches the select condition, but it return cero on "CgporderItem".return AS "CgporderItem__return"
If I make a simple query like select "return" from cgporders_items it works. But in this query it does not work.
Can you help me please?
"return" is a reserved word in SQL, but not in Postgres. See the list here. The following code works find in Postgres (SQL Fiddle is here):
create table dum (return int);
select dum.return from dum;
Your problem is something else. If I had to guess, the where clause is too restrictive (the condition on dates is a bit suspect).

SSRS Sql query with when clause and input parameter

I'm trying to get a SQL query working in SSRS.
This query is executed when I execute my report in SSRS. It is a select which simply populates my dataset.
I want to have the Date at the moment of the report execution on first execution and then I want the user to be able to input a different date if needed thus the need for the parameter.
Here it is :
Select contrat from contrats ctr,
INNER JOIN structure rs ON rs.id = ctr.id
INNER JOIN structure_members rsm ON rsm.id_structure = rs.id_structure
where
(((Date(ctr.date) = CASE WHEN ? IS NULL THEN Date(Now) END) or
(Date(rs.endDate) = CASE WHEN ? IS NULL THEN Date(Now) END)
GROUP BY crt.contrat
Don't mind any syntax error, I just simplified my query to get to the point : is this possible?
Can I do a case when end like this?
`CASE WHEN ? IS NULL THEN Date(Now)`
The ? is a date that I input such as 2014-08-04. The value could be null if I wanted to. That's why I'm trying to check if it is null then complete the query using Date(now) instead.
Thanks for your help.
Try this:
Select contrat from contrats ctr
INNER JOIN structure rs ON rs.id = ctr.id
INNER JOIN structure_members rsm ON rsm.id_structure = rs.id_structure
where
Date(ctr.date) = ISNULL(?,Today())
or
Date(rs.enddate) = ISNULL(?,Today())
GROUP BY crt.contrat
The CASE statement can be replaced by using ISNULL to get a default value for null input and using it in the comparison. If parameter is non-null, you compare the parameter value, else you compare today's date.

SQL Reporting count of parameter in a column

I am working in SSRS 3.0 with a SQL table including the following fields:
ApptID BookedBy ConfirmedBy CancelledBy
I also have a parameter setup to select which users to filter by (matches data in the BookedBy, ConfirmedBy and CancelledBy columns) called #Scheduler (which is a multi vale parameter/array).
I need to get a count for booked, confirmed and scheduled for how many times any value in the Scheduler parameter shows up in that column.
Basically:
COUNT(BookedBy IN (#Scheduler)) AS BookedCount
Can anyone help me out with the syntax for doing this?
Try this
SELECT Count(BookedBy = #Scheduler) as [BookedCount],
Count(ConfirmedBy = #Scheduler) as [ConfirmedCount],
Count(CancelledBy = #Scheduler) as [CancelledCount]
FROM tablename
WHERE BookedBy = #Scheduler OR
ConfirmedBy = #Scheduler OR
CancelledBy = #Scheduler
NB - Not tested might contain typos
If your input is a list separated by commas you can convert that to a table. See a reference like this:
http://www.projectdmx.com/tsql/sqlarrays.aspx
For this use case I'd recommend one of the solutions that saves the result in a CTE (since you only need to convert your input once and this will be fastest)
Then you could use that table (called sTable with column name) like this:
SELECT Count(Bo.Name) as [BookedCount],
Count(Co.Name) as [ConfirmedCount],
Count(Ca.Name) as [CancelledCount]
FROM tablename
LEFT JOIN sTable Bo ON BookedBy = Bo.name
LEFT JOIN sTable Co ON ConfirmedBy = Co.name
LEFT JOIN sTable Ca ON CancelledBy = Ca.name
I guess this will work but it does not seem as nice as the others:
SELECT (SELECT COUNT(*) FROM table WHERE BookedBy in (#Scheduler)) AS [BookedCount],
(SELECT COUNT(*) FROM table WHERE ConfirmedBy in (#Scheduler)) as [ConfirmedCount],
(SELECT COUNT(*) FROM table WHERE CancelledBy in (#Scheduler)) as [CancelledCount]