How to use posted parameter in query in Pentaho CDE - pentaho

I made two CDE dashboards.
Dashboard(1) select ID and post it as parameter with URL to Dashboard(2).
Dashboard(2) show report using prpt component.
The Dashboard(2) has two parameters.They are ID(Custom Parameter) and Date(Simple Parameter).
ID is posted from Dashboard(1).
Date is selected by user using SelectComponent.
The Dashboard has SelectCompoment so that users can select Date.
Datasource is SelectDateQuery.
Date is calculated in sql over sqlJndi named SelectDateQuery.
Query is:
select Date from tablefoo
where ID= ${ID}
order by Date DESC
tablefoo is(I use MySQL Database):
|ID| Date|Score|
| 1|04-01| 90|
| 1|04-02| 100|
| 1|04-03| 80|
| 2|04-01| 100|
| 2|04-03| 70|
| 2|04-05| 60|
…
If ID=1 is posted from Dashboard(1),I wish Select Component shows like bellow.
|04-03|
|04-02|
|04-01|
But the SelectComponent is blank.
I checked MySQL log.
The query was:
select distinct Date from tablefoo
where ID= NULL
order by Date DESC
Parameter ID isn't used in the query.
What is wrong?
Thanks.

Related

How do I get all entries 5 minutes after a certain condition in SQL?

I am trying to solve the following problem using SQL:
I have a table (example shown below) with action items per user, the timestamp when the action happened and a unique identifier for each entry.
I want to find out what actions each user takes in the 5 minutes after a specific action occurs. For example, I want to see for all users with the action item "sit" what happens in the 5 minutes after that, so to see all entries starting with the "sit" action item.
I hope someone can help!!
Thank you!
table example
I started using ROW_NUMBER and then partition by users and order by time, but after that I dont know how to continue.
Your question is not entirely clear, however, in my understanding, it is easier to use a JOIN
create table log(UserName varchar(20),ActionTime datetime,ActionItem varchar(10),ActionId varchar(26));
insert into log values
('Anna' ,cast('2022-07-30 13:17:22' as datetime),'walk' ,'uid_1')
,('Peter' ,cast('2022-07-30 15:39:46' as datetime),'drive' ,'uid_2')
,('Sarah' ,cast('2022-07-30 09:07:53' as datetime),'stand' ,'uid_3')
,('Kurt' ,cast('2022-07-30 00:56:14' as datetime),'sit' ,'uid_4')
,('Deborah' ,cast('2022-07-30 15:26:02' as datetime),'lie' ,'uid_5')
,('Michelle',cast('2022-07-30 15:26:03' as datetime),'scratch','uid_6')
,('Sven' ,cast('2022-07-30 15:26:04' as datetime),'run' ,'uid_7')
,('Sarah' ,cast('2022-07-30 15:28:06' as datetime),'swim' ,'uid_8')
,('Peter' ,cast('2022-07-30 13:17:22' as datetime),'look' ,'uid_9')
;
select a.ActionId,a.UserName,a.ActionItem,a.ActionTime
,b.ActionTime,b.UserName,b.ActionItem,b.ActionId
from log a left join log b
on b.ActionId<>a.ActionId
and b.ActionTime>=a.ActionTime
and datediff(mi,a.ActionTime,b.ActionTime)<5
I guess this problem can not be solved with a single query. But you can use a series of queries.
In answer to your question I will use MySQL dialect of SQL. I believe it doesn't matter.
On first step let's assume that we are only interested in the last action "sit". In this case we can do such query:
SELECT * FROM user_actions WHERE ACTION_ITEM = "sit" ORDER BY TIMESTAMP DESC LIMIT 1;
So the result is
+------+---------------------+-------------+-------------------+
| USER | TIMESTAMP | ACTION_ITEM | UNIQUE_IDENTIFIER |
+------+---------------------+-------------+-------------------+
| Kurt | 2022-07-30 00:56:14 | sit | 4 |
+------+---------------------+-------------+-------------------+
Then save timestamp value in variable:
SELECT TIMESTAMP INTO #reason_ts FROM user_actions WHERE ACTION_ITEM = "sit" ORDER BY TIMESTAMP DESC LIMIT 1;
And now we need to get further actions in next 5 minutes (actually I took 12 hours because 5 minutes is not enough for your example). Let's do this:
SELECT csq.* FROM user_actions AS csq WHERE TIMESTAMP BETWEEN #reason_ts AND ADDTIME(#reason_ts, '12:00:00');
The result is:
+-------+---------------------+-------------+-------------------+
| USER | TIMESTAMP | ACTION_ITEM | UNIQUE_IDENTIFIER |
+-------+---------------------+-------------+-------------------+
| Sarah | 2022-07-30 09:07:53 | stand | 3 |
| Kurt | 2022-07-30 00:56:14 | sit | 4 |
+-------+---------------------+-------------+-------------------+
If you need all further action modify query:
SELECT csq.* FROM user_actions AS csq WHERE TIMESTAMP >= #reason_ts;
If you need not only last action "sit" it will be more difficult. I think you need to write some kind of script or sql function. But still it is doable.

How to use the distinct and count function to get distinct users and number of messages sent

There is this table I'm currently trying to make a query on. the data looks like as follows:
--------------------------------
| Message_Time | User | Message|
--------------------------------
|y-m-d H:M:S |User1 | msg-body
|y-m-d H:M:S |User1 | msg-body
|y-m-d H:M:S |User1 | msg-body
|y-m-d H:M:S |User2 | msg-body
|y-m-d H:M:S |User2 | msg-body
I'm trying to select the users and count the amount of messages sent by each individual user
I've tried to select from this db and do a distinct on the User column and count on the Message column but i keep getting an error. I'm sure I'm not approaching it the correct way.
I tried the following query:
SELECT DISTINCT(USER) AS [User_ID], COUNT(Message) AS [Messages_Sent]
FROM [dbo].[Table]
This is my desired output:
--------------------------
| User_ID | Messages_Sent|
--------------------------
|User1 | 3 |
|User2 | 2 |
But unfortunately this is the error that occurs:
Operand data type ntext is invalid for count operator.
Any help would be great, thanks.
Use GROUP BY and COUNT(*):
SELECT USER AS [User_ID], COUNT(*) AS [Messages_Sent]
FROM [dbo].[Table]
GROUP BY USER;
Presumably, Message is never NULL, so this does the same thing.
By the way, replace text with nvarchar(max) or varchar(max). text is deprecated. As the documentation states:
IMPORTANT! ntext, text, and image data types will be removed in a future version of SQL Server. Avoid using these data types in new development work, and plan to modify applications that currently use them. Use nvarchar(max), varchar(max), and varbinary(max) instead.
Fix your data type, and then your query will work fine:
ALTER TABLE dbo.[Table] ALTER COLUMN Message nvarchar(MAX);
As mentioned in the comments ntext has been deprecated for years, it's past time to stop using it and use the correct one.
use group by for aggreagated
SELECT USER AS [User_ID], COUNT(*) AS [Messages_Sent]
FROM [dbo].[Table]
GROUP BY USER
this return not null rows
the aggreation function always return distinct result
SELECT USER AS [User_ID], COUNT(message) AS [Messages_Sent]
FROM [dbo].[Table]
GROUP BY USER
this return the count where column value is not null

How to fix: cannot retrieve all fields MongoDB collection with Apache Drill SQL expression query

I'm trying to retrieve all(*) columns from a MongoDB object with Apache Drill expression SQL:
`_id`.`$oid`
Background: I'm using Apache Drill to query MongoDB collections. By default, Drill retrieves the ObjectId values in a different format than the stored in the database. For example:
Mongo: ObjectId(“59f2c3eba83a576fe07c735c”)
Drill query result: [B#3149a…]
In order to get the data in String format (59f2c3eba83a576fe07c735c) from the object, I changed the Drill config "store.mongo.bson.record.reader" to "false".
ALTER SESSION SET store.mongo.bson.record.reader = false
Drill query result after config set to false:
select * from calc;
+--------------------------------------+---------+
| _id | name |
+--------------------------------------+---------+
| {"$oid":"5cb0e161f0849231dfe16d99"} | thiago |
+--------------------------------------+---------+
Running a query by _id:
select `o`.`_id`.`$oid` , `o`.`name` from mongo.od_teste.calc o where `o`.`_id`.`$oid`='5cb0e161f0849231dfe16d99';
Result:
+---------------------------+---------+
| EXPR$0 | name |
+---------------------------+---------+
| 5cb0e161f0849231dfe16d99 | thiago |
+---------------------------+---------+
For an object with a few columns like the one above (_id, name) it's ok to specify all the columns in the select query by id. However, in my production database, the objects have a "hundred" of columns.
If I try to query all (*) columns from the collection, this is the result:
select `o`.* from mongo.od_teste.calc o where `o`.`_id`.`$oid`='5cb0e161f0849231dfe16d99';
or
select * from mongo.od_teste.calc o where `o`.`_id`.`$oid`='5cb0e161f0849231dfe16d99';
+-----+
| ** |
+-----+
+-----+
No rows selected (6.112 seconds)
Expected result: Retrieve all columns from a MongoDB collection instead of declaring all of them on the SQL query.
I have no suggestions here, because it is a bug in Mongo Storage Plugin.
I have created Jira ticket for it, please take a look and feel free to add any related info there: DRILL-7176

How to join records from multiple object tables to a master table with a single query?

So I have a data model which is set up with a table that contains NAME, ID, and CONDITION columns for a series of objects (each object has a unique id number). The rest of the attributes for these objects are contained in columns of several respective tables based on the object type (there are some different attributes associated with each type). All the type-specific tables have an ID column so that the objects can be matched to the master list.
I want to write an sql query that will return information about objects of several different types based on the CONDITION tied to their unique ID.
Here is a simplified example of what I am working with:
object_master_list
| ID | NAME | CONDITION |
-------------------------
|1234| obj1| true|
|0000| obj2| false|
|1236| obj3| true|
|0001| obj4| false|
|5832| obj5| true|
|6698| obj6| false|
|6699| obj7| false|
obj_type_one
| ID | NAME | HEIGHT |
-------------------------
|1234| obj1| o1height|
|0000| obj2| o2height|
|5832| obj5| o5height|
|6699| obj7| o7height|
obj_type_two
| ID | NAME | WEIGHT |
-------------------------
|1236| obj3| o3height|
|0001| obj4| o4height|
|6698| obj6| o6height|
As you can see, there is no correlation between NAME and type or ID and type.
I am currently working in iReport, and I have been using the query designer and editing it manually as necessary.
Right now an example query would look like:
SELECT
object_master_list."NAME" AS NAME,
obj_type_one."HEIGHT" AS HEIGHT,
obj_type_two."WEIGHT" AS WEIGHT
FROM
object_master_list INNER JOIN obj_type_one ON object_master_list."ID" =
obj_type_one."ID"
INNER JOIN obj_type_two ON obj_type_two."ID" = object_master_list."ID"
WHERE
object_master_list."CONDITION" = 'true'
My data is returning no results. From the research I have done on sql joins, I believe this is happening:
Where circle "A" represents my master list.
iReport stores and utilizes the values returned from a query row by row, with a field for each column. So ideally I should end up with this:
$F{NAME} which will receive the following values in succession ("obj1", "obj3", "obj5")
$F{HEIGHT} with value series (o1hieght, null, o5height)
$F{HEIGHT} with value series (null, o3weight, null)
The table representation I suppose would look like this:
| NAME | HEIGHT | WEIGHT |
------------------------------
| obj1| o1height| null|
| obj3| null| o3weight|
| obj5| o5height| null|
My question is how do I accomplish this?
I ran in to this on a smaller scale before, so I am aware that I could use subreports or create multiple data sets, but frankly I have a lot of object types and I would rather not if I could help it. I am also not allowed to add a TYPE column to the master list.
Thanks in advance for any replies.
You can use left join in the following way :
select o1.name, o2.height, o3.weight
from object_master_list o1 left join obj_type_one o2 on o1.id = o2.id
left join obj_type_two o3 on o1.id = o3.id
where o1.condition = 'true'
SQL Fiddle

How would I compare two fields in an SQL SELECT statement to then select other fields based on this result?

I have a table which contains a list of products for a company. They input data about how much stock they have and also the level at which they want to be reminded that they need to order new stock.
For example:
+-------+-------+----------------+
|column1|column2|column1<=column2|
+-------+-------+----------------+
|value1 |value1 | true |
|value2 |value3 | false |
|value4 |value4 | true |
+-------+-------+----------------+
I want to list all the true results in a form which the user is then able to navigate through. What would be the best way to go about doing this?
How about
SELECT * FROM mytable WHERE column1<=column2 ?
Using SQL I would suggest a statement like this:
SELECT * FROM table WHERE column1 <= column2