BIRT reporting - use the same parameter multiply times in a sql query - eclipse

I have this where clause:
WHERE p.ROLE = 'doctor'
AND((p6.PA_Name='Event_Day_From' AND p6.PA_Value>= SUBSTRING('01.01.2012', 1, 2))
AND (p7.PA_Name='Event_Month_From' AND p7.PA_Value>=SUBSTRING('01.01.2012', 4, 2))
AND (p8.PA_Name='Event_Year_From' AND P8.PA_Value>=SUBSTRING('01.01.2012', 7, 4)))
AND ((p9.PA_Name='Event_Day_To' AND P9.PA_Value<=SUBSTRING('30.12.2012', 1, 2))
AND (p10.PA_Name='Event_Month_To' AND P10.PA_Value<=SUBSTRING('30.12.2012', 4, 2))
AND (p11.PA_Name='Event_Year_To' AND P11.PA_Value<=SUBSTRING('30.12.2012', 7, 4)))
after the above I have an union all and at the end I have another WHERE clause exacly the same as the above.
In BIRT a parameter can be passed and you have to insert ? in the SQL query where you need it to be passed. As you can see I have a start date and an end date wich will be passed trough an user input. Now my problem is that I have no idea how to pass the SDate parameter to all the start dates and the EndDate parameter to all the end dates
Is there a solution for my problem?

I solved this problem by creating table with a single row from the parameters, and attaching it to the report query like so:
,(select ? SDate, ? EDate) params
WHERE p.ROLE = 'doctor'
AND((p6.PA_Name='Event_Day_From' AND p6.PA_Value>= SUBSTRING(params.SDate, 1, 2))
AND (p7.PA_Name='Event_Month_From' AND p7.PA_Value>=SUBSTRING(params.SDate, 4, 2))
AND (p8.PA_Name='Event_Year_From' AND P8.PA_Value>=SUBSTRING(params.SDate, 7, 4)))
AND ((p9.PA_Name='Event_Day_To' AND P9.PA_Value<=SUBSTRING(params.EDate, 1, 2))
AND (p10.PA_Name='Event_Month_To' AND P10.PA_Value<=SUBSTRING(params.EDate, 4, 2))
AND (p11.PA_Name='Event_Year_To' AND P11.PA_Value<=SUBSTRING(params.EDate, 7, 4)))
Hope this helps.
**I use this method on a PostgreSQL database currently.

[EDIT] BIRT uses plain JDBC not it's not supporting "named parameters", so you can't write:
... p6.PA_Value>= SUBSTRING(:start, 1, 2) ...
This will give an error.
Other options are to build the query using Property Binding or JavaScript and replace parts of it before it is executed. See this example report
Some databases also support the WITH syntax to assign an alias to a ? parameter. This blog post explains how to use this.

Related

How to format date in SSRS?

In SSRS report query is generating date as a column in the format of :
Sales ID20200331 ID20200430 ID20200531
To remove the ID i used following expression:
=Right( Fields!ID20210331.Value, len(Fields!ID20210331.Value) - 2)
This gives me 84, instead of removing ID.
How can I remove ID and format date as 2020 Mar etc.
Thanks
If your fields values are "ID20200430" etc then in SSRS you can use something like this..
=DateSerial(
MID(Fields!IDDate.Value, 3, 4),
MID(Fields!IDDate.Value, 7, 2),
RIGHT(Fields!IDDate.Value, 2)
)
However It appears that it's your column [names] that represent dates is this correct?
If this is true, then you would have to UNPIVOT the columns in SQL then convert the resulting values into a real date format.
Here' some sample data to show how to do this.
DECLARE #t TABLE (Sales varchar(10), ID20200331 int, ID20200430 int, ID20200531 int)
INSERT INTO #t VALUES
('A', 1,2,3),
('B', 4,5,6),
('C', 7,8,9)
SELECT
Sales, IdDate, SomeNumber
, MyDate = DATEFROMPARTS(SUBSTRING(IdDate, 3, 4), SUBSTRING(IdDate, 7, 2), SUBSTRING(IdDate, 9, 2))
FROM #t
UNPIVOT(
SomeNumber FOR IdDate IN ([ID20200331],[ID20200430],[ID20200531])
) unpvt
Which gives us this including the myDate column which is the correct date type
You could then use this in a matrix control in SSRS to get the data back into a pivoted view

How to Order By a column but array come first

(Sorry for my bad English)
I have an array of users ids as below:
[5, 9, 3, 22, 16]
Obviously the values are dynamic.
Now, I need to SELECT all users but users with above ids come first.
What I've tried so far?
This query gives me exact answer :
SELECT * FROM users WHERE id IN (5, 9, 3, 22, 16)
UNION ALL
SELECT * FROM users WHERE id NOT IN (5, 9, 3, 22, 16);
But is there any better way?
P.S:
I'm using PostgreSQL 10.
Try this:
select *
from users
order by id not in (5, 9, 3, 22, 16), id
As stated in the documentation, an expression used in the ORDER BY clause
can be an arbitrary expression formed from input-column values.
In particular, you can use a Boolean expression, as values of this type are sortable. Note that false < true.
You can use CASE statements on your ORDER BY, I'll give you an example:
select *
from users
order by
CASE WHEN id=5 THEN 1
WHEN id=9 THEN 2
WHEN id=3 THEN 3
WHEN id=22 THEN 4
WHEN id=16 THEN 5
END, id
With CASE you can tell postgres the "priority" or value you want for each id if you know them beforehand. After that I added "id" so the rest of the rows gets ordered properly.

sqllite3 bind to text swift project

I am having a problem accessing sqlite3 database in an xcode swift project. Specifically when binding host parameters in a prepared sql statement to a text (string) value the bind always binds all ? rather than just one. So the results are not what is expected. (NOTE: I am just using the builtin sqlite3 lib with bridging header file specified, not any 3rd party wrappers)
For example: my test database table has a column named str1 whose values are years such as "2010", "2011", etc (not Integers, but Strings). If I use the SQL statement:
SELECT * FROM table1 WHERE str1 BETWEEN '2011' AND '2012'
it gives me all the rows that I expected to get. But if I use the statement:
SELECT * FROM table1 WHERE str1 BETWEEN ? AND ?
and bind the prepared statement with:
sqlite3_bind_text(statement, 1, "2011", -1, nil)
sqlite3_bind_text(statement, 2, "2012", -1, nil)
it returns only the rows for "2012". The prepare was done with:
sqlite3_prepare_v2(db, sql, -1, &statement, tail)
So, the bind is somewhat working, but it seems to always replace ALL ? with the value rather than just one. (It works no differently if I use unique parameter names such as ?1 and ?2 rather than just ? ?)
So, my question is: how to bind sqlite3 host parameter to Swift string value?
Looking at the source code of GRDB.swift (https://github.com/groue/GRDB.swift/blob/v2.9.0/GRDB/Core/Statement.swift#L179 and https://github.com/groue/GRDB.swift/blob/v2.9.0/GRDB/Core/Database.swift#L14) I think the following code will work:
let SQLITE_TRANSIENT = unsafeBitCast(OpaquePointer(bitPattern: -1), to: sqlite3_destructor_type.self)
sqlite3_bind_text(statement, index, string, -1, SQLITE_TRANSIENT)

OrientDB - Concatenate two strings and aggregate them

Normally, if I want to concatenate two strings and apply an aggregate I follow this syntax:
Sql Server
SELECT substring(t.field, 1, 3) + substring(t.field, 5,7), count(*)
FROM myTable t
GROUP BY substring(t.field, 1, 3) + substring(t.field, 5,7)
Oracle
SELECT CONCAT(substring(t.field, 1, 3), substring(t.field, 5,7)), count(*)
FROM myTable t
GROUP BY CONCAT(substring(t.field, 1, 3), substring(t.field, 5,7))
In OrientDB, is it possible concatenate two strings and get an aggregate of them?
Try this, the parser and the query executor of OrientDb can be some time a litle choosy expecially with the group by option, this would work with the 'Strict mode' disabled (studio -> log in -> db -> second tab on the bottom)
SELECT id, $goofy , count(*) as cont from myTable
LET $sub = id.subString(4),
$goofy = id.subString(1,3).append($sub)
group by $goofy
note:
i've splitted the code in 2 variable couse the parser have some problem parsing the function inside the .append
I'm pretty sure that in the next releases of Orientdb this kind of issue will be fixed (they alredy have a new parser in development)

Hashbytes MD5 syntax

Why do the hashed results below differ? Shouldn't they be the same?
SELECT [teststring]
, SUBSTRING(master.dbo.fn_varbintohexstr(HashBytes('MD5', [teststring])), 3, 32)
, SUBSTRING(master.dbo.fn_varbintohexstr(HashBytes('MD5', 'HelloWorld')), 3, 32)
FROM [test]
WHERE [teststring]='HelloWorld'
HelloWorld 87434a4b7918d288dc1c1e0ca7544e77 68e109f0f40ca72a15e05cc22786f8e6
I can reproduce this, it would appear that your column is storing an NVARCHAR rather than a VARCHAR (which is what you have specified your string as in your query), these are stored differently and will therefore give different results.
If you run the following query you should see that they are the same when using the same datatype (NVARCHAR):
SELECT [teststring]
, SUBSTRING(master.dbo.fn_varbintohexstr(HashBytes('MD5', [teststring])), 3, 32)
, SUBSTRING(master.dbo.fn_varbintohexstr(HashBytes('MD5', N'HelloWorld')), 3, 32)
FROM [test]
WHERE [teststring]='HelloWorld'