Pass order by clause from Java in Jasper Report - jasper-reports

I am using latest version of Jasper soft studio to design Jasper reports. I want to pass order by clause to jasper report based on certain criteria from Java. Can you please guide as how to append parameter(containing order by clause from Java) in the report query?
Query
with tmp as (
select d.gl_code, nvl(sum(d.debit),0)- nvl(sum(d.credit),0) as simple
from gl_forms g,gl_voucher_detail d,financial_year f
where g.voucher_no = d.voucher_no
and f.fid = $P{P_FID}
and g.voucher_date >= to_date('01-07-'+f.start_year,'dd-MM-yyyy') and g.voucher_date < $P{P_FROM}
and g.post=1
group by d.gl_code)
select g.voucher_no,
g.voucher_date,
g.voucher_type,
g.remarks remarks,
d.sr_no,
c.account_name,
d.debit,
d.credit,
d.narration narration,
c.account_code,
c.account_name || ' - ' || c.account_code Account,
(select name from company) name,
nvl((o.debit-o.credit),0) + nvl(tmp.simple,0) opening
from gl_voucher_detail d
join gl_forms g
on g.voucher_no = d.voucher_no
and g.post=1
and g.voucher_date between $P{P_FROM} and $P{P_TO}
left outer join opening_balances o
on d.gl_code = o.account_code
and o.fid = $P{P_FID}
right outer join chart_of_account c
on c.account_code = d.gl_code
left outer join tmp
on c.account_code = tmp.gl_code
where c.account_level=4
and c.active=1
and c.account_code = CASEWHEN($P{P_ACCOUNT}='%',c.account_code,$P{P_ACCOUNT})
Order by clause shall be one among these below two based on front fields selection:
order by c.account_name, g.voucher_date,g.voucher_no,d.sr_no
order by c.debit+c.credit desc

Related

Why Group By is not working as expected in PostgreSQL?

I m doing a query and it is showing me the output but not as expected.
Above image it is giving me two extra row (blue indicator) after doing group by which not should be exists in the output. Here is my query
SELECT som.customer_po,
pro.product_id,
pro.product_name,
som.mo_id,
ri.status
FROM schema_order_map som
JOIN product pro ON som.label_reference_id = pro.product_id
JOIN risk_information ri ON som.customer_po = ri.customer_po
WHERE ri.created_by = 18
AND ri.product_id = som.label_reference_id
GROUP BY som.customer_po,
pro.product_id,
product_name,
som.mo_id,
ri.status
I tried different way but it is giving me the same result.
It solves my problem somehow. but i don't know it is best practice or not
SELECT max(som.customer_po) AS customer_po,
max(pro.product_id) AS product_id,
max(pro.product_name) product_name,
max(som.mo_id) mo_id,
max(ri.status) AS risk_status
FROM schema_order_map som
JOIN product pro ON som.label_reference_id = pro.product_id
JOIN risk_information ri ON som.customer_po = ri.customer_po
WHERE ri.created_by = 18
AND ri.product_id = som.label_reference_id
GROUP BY som.customer_po,
pro.product_id

SQL Server Select one from multiple rows based on calculation

I'm trying to find the greatest number of days (and the reviewer's badge number) it took one of several reviewers to approve a particular document in a workflow. For example, I have a table that holds several workflow approval steps (submitter, manager, controller, QA), along with their badge numbers, and date they approved. The table is called "Workflow" and has those four workflow steps mentioned above as records in the table, and the main table Design that has a one-to-many relationship with Workflow.
I'm trying to determine how many days for the longest review step (number of days), and the badge number of the reviewer for that step (who is holding up the approval workflow, basically). I've been trying to set independent variables to be used later, but not sure how to also set the badge number and I'm confused. I have tried CASE, IIF, and COALESCE but am not having any luck because I don't want the first true value returned and then stop, I want it to continue to evaluate all the steps. Here is an example of my SQL:
declare #managerTime int = 0
declare #controllerTime int = 0
declare #qaTime int = 0
SET #managerTime = (SELECT DATEDIFF(day, manager.BadgeDate, submitter.BadgeDate)
from Design d
left outer join Workflow submitter on (d.DCRId = submitter.DCRId and submitter.RoleName = 'Submitter')
left outer join Workflow manager on (d.DCRId = manager.DCRId and manager.RoleName = 'System Manager')
SET #controllerTime = (SELECT DATEDIFF(day, controller.BadgeDate, manager.BadgeDate)
from Design d
left outer join Workflow manager on (d.DCRId = manager.DCRId and manager.RoleName = 'System Manager')
left outer join Workflow controller on (d.DCRId = controller.DCRId and controller.RoleName = 'DCR Controller')
This is how I would do it:
Create table WorkflowDefinition with flow definiton:
Source Destination Description
Submitter System Manager Submitter -> System Manager
System Manager DCR Controller System Manager -> DCR Controller
DCR Controller QA DCR Controller -> QA
Now we use this table to join workflow elements and calculate greatest number of days:
SET #MaxTime = (SELECT MAX(DATEDIFF(day, source.BadgeDate, destination.BadgeDate))
from Design d
inner join Workflow source on d.DCRId = source.DCRId
inner join WorkflowDefinition flow on source.RoleName = flow.source
inner join Workflow destination on d.DCRId = destination.DCRId
and destination.RoleName = flow.destination
)
When we have this value we can select all workflow elements which took this exact number of days to complete:
Select
destination.BadgeNumber
from Design d
inner join Workflow source on d.DCRId = source.DCRId
inner join WorkflowDefinition flow on source.RoleName = flow.source
inner join Workflow destination on d.DCRId = destination.DCRId
and destination.RoleName = flow.destination
where DATEDIFF(day, source.BadgeDate, destination.BadgeDate) = #MaxTime
If you want to know max value of days for every type of step separately then we can do something like this: Calculate max value of days per step type and put this into temp table:
SELECT
MAX(DATEDIFF(day, source.BadgeDate, destination.BadgeDate)) as maxDays,
flow.Description as StepDescription
into #tmp
from Design d
inner join Workflow source on d.DCRId = source.DCRId
inner join WorkflowDefinition flow on source.RoleName = flow.source
inner join Workflow destination on d.DCRId = destination.DCRId
and destination.RoleName = flow.destination
group by flow.Description
And now use this table to select steps matching max number of days and step description:
Select
destination.BadgeNumber
from Design d
inner join Workflow source on d.DCRId = source.DCRId
inner join WorkflowDefinition flow on source.RoleName = flow.source
inner join Workflow destination on d.DCRId = destination.DCRId
and destination.RoleName = flow.destination
inner join #tmp on DATEDIFF(day, source.BadgeDate, destination.BadgeDate) = maxDays and StepDescription = flow.Description
I ended up tackling this from the "many" table (Workflow), instead of the Design table. I used the following SQL which got me the results I was after. Thank you all for trying to make sense of my ramblings.
select
w.DesignId,
w.RoleName,
w.BadgeNumber,
w.BadgeDate,
DATEDIFF(day,
(select x.BadgeDate from Workflow x
where x.BadgeDate is not null
and x.DesignId = w.DesignId
and x.StepOrder = w.StepOrder - 1),
(select b.BadgeDate from Workflow b
where b.BadgeDate is not null
and b.DesignId = w.DesignId
and b.StepOrder = w.StepOrder))
as StepDuration,
w.StepOrder,
TotalDuration = DATEDIFF(day,
(select y.BadgeDate from Workflow y
where y.RoleName = 'Submitter'
and y.DesignId = w.DesignId),
(select v.BadgeDate from Workflow v
where v.RoleName = 'Approver'
and v.DesignId = w.DesignId)),
d.VersionNumber,
d.Title
from Workflow w
inner join Design d on d.DesignId = w.DesignId

Symfony DQL Postgresql group by isnt working

I have just switched from mysql to postgresql and my working dql queries stopped working.
Here is my dql
SELECT p
FROM AppBundle:Photo p
JOIN AppBundle:Like l WITH p = l.photo
WHERE p.isModerated = :isModerated
AND p.isActive = :isActive
AND p.category IN(:categories)
AND p.creationDate <= :begin
AND p.creationDate >= :end
GROUP BY p.id
ORDER BY l.creationDate DESC
Getting the next error
SQLSTATE[42803]: Grouping error: 7 ERROR: column "l1_.creation_date" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: ... p0_.creation_date >= $4 GROUP BY p0_.id ORDER BY l1_.creati...
As i can understand, it says that group by column should be in SELECT. I dont it to be there. I need to select just p (AppBundle:Photo) and nothing more. What should be edited in dql to get it working properly?
Thank you.
I just replaced ORDER BY l.creationDate DESC to ORDER BY p.creationDate DESC
It will work for me. As i understood, ORDER BY column should be SELECTed

How to create variables in derived table using different conditions

I want to generate a table and all the variables I need are from a derived temporary table. Now, I need to calculate the time differences under different conditions, and I need to deal with 2 questions:
1. how to create variables in derived table.
2. how to split a variable into 2 variables using different conditions.
Please note that the sql statement I provide below is the simplified statement and please pay attention to the comments which will help you understand the question.
Thanks in advance for any tips.
Here is the SQL statement:
Select id, name, offline_time, Process_time from
## in the derived table, use the difference of createon in table a and b to calculte the offline_time and Process_time ##
## when {a.id = st.id AND a.activityid = 5008 and a.sessiontype = 7} a.creaton = a.creaton1 ##
## when {b.activityid = 5011} b.creaton = b.creaton1 ##
(select
(UNIX_TIMESTAMP(MAX(a.createdon)) - UNIX_TIMESTAMP(MAX(b.createdon))) as 'Offline_Time',
(UNIX_TIMESTAMP(MAX(a.createdon1)) - UNIX_TIMESTAMP(MAX(b.createdon1))) as 'Process_Time',
q.id,
q.name,
a.sessiontype
FROM tv_sessiontimer st
LEFT JOIN sessionactivity_log a ON (a.id = st.id AND a.activityid in (5004,5008) and a.sessiontype IN (3,4,5,6,7))
LEFT JOIN sessionactivity_log b ON (a.ssessionId = b.sessionId AND a.id = b.id AND b.activityid in (5003,5011))
INNER JOIN tv_offline_request q ON q.id = a.sessionid
LEFT JOIN tv_subject sub ON (sub.id = q.subjectid AND sub.SortKey > 10800 AND sub.sortkey < 30200)
)

How to determine the size of a Full-Text Index on SQL Server 2008 R2?

I have a SQL 2008 R2 database with some tables on it having some of those tables a Full-Text Index defined. I'd like to know how to determine the size of the index of a specific table, in order to control and predict it's growth.
Is there a way of doing this?
The catalog view sys.fulltext_index_fragments keeps track of the size of each fragment, regardless of catalog, so you can take the SUM this way. This assumes the limitation of one full-text index per table is going to remain the case. The following query will get you the size of each full-text index in the database, again regardless of catalog, but you could use the WHERE clause if you only care about a specific table.
SELECT
[table] = OBJECT_SCHEMA_NAME(table_id) + '.' + OBJECT_NAME(table_id),
size_in_KB = CONVERT(DECIMAL(12,2), SUM(data_size/1024.0))
FROM sys.fulltext_index_fragments
-- WHERE table_id = OBJECT_ID('dbo.specific_table_name')
GROUP BY table_id;
Also note that if the count of fragments is high you might consider a reorganize.
If you are after a specific Catalogue
Use SSMS
- Clik on [Database] and expand the objects
- Click on [Storage]
- Right Click on {Specific Catalogue}
- Choose Propertie and click.
IN General TAB.. You will find the Catalogue Size = 'nn'
I use something similar to this (which will also calculate the size of XML-indexes, ... if present)
SELECT S.name,
SO.name,
SIT.internal_type_desc,
rows = CASE WHEN GROUPING(SIT.internal_type_desc) = 0 THEN SUM(SP.rows)
END,
TotalSpaceGB = SUM(SAU.total_pages) * 8 / 1048576.0,
UsedSpaceGB = SUM(SAU.used_pages) * 8 / 1048576.0,
UnusedSpaceGB = SUM(SAU.total_pages - SAU.used_pages) * 8 / 1048576.0,
TotalSpaceKB = SUM(SAU.total_pages) * 8,
UsedSpaceKB = SUM(SAU.used_pages) * 8,
UnusedSpaceKB = SUM(SAU.total_pages - SAU.used_pages) * 8
FROM sys.objects SO
INNER JOIN sys.schemas S ON S.schema_id = SO.schema_id
INNER JOIN sys.internal_tables SIT ON SIT.parent_object_id = SO.object_id
INNER JOIN sys.partitions SP ON SP.object_id = SIT.object_id
INNER JOIN sys.allocation_units SAU ON (SAU.type IN (1, 3)
AND SAU.container_id = SP.hobt_id)
OR (SAU.type = 2
AND SAU.container_id = SP.partition_id)
WHERE S.name = 'schema'
--AND SO.name IN ('TableName')
GROUP BY GROUPING SETS(
(S.name,
SO.name,
SIT.internal_type_desc),
(S.name, SO.name), (S.name), ())
ORDER BY S.name,
SO.name,
SIT.internal_type_desc;
This will generally give numbers higher than sys.fulltext_index_fragments, but when combined with the sys.partitions of the table, it will add up to the numbers returned from EXEC sys.sp_spaceused #objname = N'schema.TableName';.
Tested with SQL Server 2016, but documentation says it should be present since 2008.