Creating View with parameter [SQL,Pentaho Report Designer] - sql

I have a SQL Query that accepts parameters for filtering report
(${p_year, ${p_month}, ${p_comp}) in Pentaho Report Designer.
[In Pentaho Report Designer before using view]
SELECT INSERT_DATE,EXT,DURATION_S
FROM TABLE1
WHERE CONVERT(VARCHAR(6),INSERT_DATE,112) = CONCAT(${p_year}, ${p_month})
AND DIRECTION = 'OUT'
AND EXT IN (
SELECT DISTINCT TABLE2.EXT
FROM TABLE2
WHERE ((COMP = ${p_comp} OR 'ALL' = ${p_comp}))
)
I am trying to create the view to include that query in SQL Server. However, I am not sure this is a correct view or not.
[In SQL Server]
CREATE VIEW CALLING AS
SELECT INSERT_DATE,EXT,DURATION_S
FROM TABLE1
WHERE DIRECTION = 'OUT' AND EXT IN (
SELECT DISTINCT TABLE2.EXT
FROM TABLE2
)
After that in Report Designer, I change the code to
select * from CALLING
However, I don't know how to add parameter (${p_year}, ${p_month}, ${p_comp}) into the new code. Any idea please advice.

Your current view seems to correct for me but use JOIN inside the VIEW
CREATE VIEW Calling
AS
SELECT
t1.INSERT_DATE, t2.EXT, t1.DURATION_S, t2.COMP
FROM table1 t1
INNER JOIN table2 t2 ON t2.EXT = t1.EXT
WHERE t1.DIRECTION = 'OUT'
And, you would require to call the view with WHEREclause
SELECT * FROM Calling
WHERE INSERT_DATE = #parameter AND COMP = #parameter
Or, you would also go with stored procedure where you could pass multiple parameters

Related

SQL Select all if parameter is empty

I'm looking for a solution that allow me select all records in a
column if the parameter is empty. (see where clause section).
The empty parameters are in the Tempdb and I can't create Store Procedures.
SELECT
DB_NAME(),
ST.ID,
ST.DESC,
SP.DESC,
TF.STARTDATE,
TF.TERMDate
FROM STANDA ST --1st DB -can't create procedures
LEFT JOIN TABLEFEE TF ON TF.TABLEFEE = ST.ID --1st DB -can't create procedures
LEFT JOIN SPECIAL SP ON SP.SPECIALC = TF.SPECIALC --1st DB -can't create procedures
-- EACH FILTER SHOULD SELECT ALL IF PARAMETER IS EMPTY IN THE BELOW SELECT STATEMENT ENCLOSED WITH
((SELECT...))
WHERE ST.DESC IN
((SELECT ME_Desc FROM ##ME_DATA WHERE ME_ID = ##Counter)) --##ME_DATA 2nd DB (temp)
AND SP.DESC IN
((SELECT ME_Special FROM ##ME_DATA WHERE ME_ID = ##Counter))
AND TF.STARTDATE IN
((SELECT ME_STARTDate FROM ##ME_Data WHERE ME_ID = ##Counter))
AND TF.TERMDATE IN
((SELECT ME_TERMDate FROM ##ME_Data WHERE ME_ID = ##Counter))
You can do something like this
(SELECT ME_Desc FROM ##ME_DATA WHERE ME_ID = ##Counter OR ##Counter IS NULL)
Although where you use "IS NULL" may depend on exactly what the data type of ##Counter is ... you say "empty", but potentially it could be a numeric value that will either be zero or a valid number - therefore OR ##Counter = 0 might be more applicable. Also, you haven't specified exactly which database platform you are using, so you should be able to adapt the syntax as required for your situation, if necessary.
Alternatively:
WHERE (
##Counter IS NULL
OR
(ST.DESC IN
((SELECT ME_Desc FROM ##ME_DATA WHERE ME_ID = ##Counter)))
)

"Object not found" error when using multiple table expressions in WITH...AS inside of CREATE VIEW

I am trying to create a view based on complex query in HSQLDB (version 2.5.1).
The query looks like this (simplified for clarity), also includes DDL for the tables:
DROP VIEW TEST_VIEW IF EXISTS;
DROP TABLE TEST_1 IF EXISTS;
CREATE TABLE TEST_1 (
contentid VARCHAR(10),
contenttype VARCHAR(10),
downloaddate TIMESTAMP
);
DROP TABLE TEST_2 IF EXISTS;
CREATE TABLE TEST_2 (
dbid INTEGER,
contentid VARCHAR(10),
version VARCHAR(10)
);
CREATE VIEW TEST_VIEW AS
WITH a AS (
SELECT CONTENTID, count(*) AS amount
FROM TEST_2
GROUP BY CONTENTID
),
b AS (
SELECT CONTENTID, amount
FROM a
)
SELECT b.CONTENTID, b.amount, i.DOWNLOADDATE
FROM b /* error here */
JOIN TEST_1 i ON i.CONTENTID = b.CONTENTID
ORDER BY b.CONTENTID;
However, it fails with the following error:
[42501][-5501] user lacks privilege or object not found: JOIN in statement [CREATE VIEW TEST_VIEW AS......
The same query runs fine when used as a SELECT (without CREATE VIEW...AS).
Also, the view is created successfully if there is only one table expression in WITH...AS statement, like below:
CREATE VIEW TEST_VIEW AS
WITH a AS (
SELECT CONTENTID, count(*) AS amount
FROM TEST_2
GROUP BY CONTENTID
)
SELECT a.CONTENTID, a.amount, i.DOWNLOADDATE
FROM a
JOIN TEST_1 i ON i.CONTENTID = a.CONTENTID
ORDER BY a.CONTENTID;
It looks like in the first statement the DB engine tries to parse "JOIN" as a table alias for table "b".
Is there a syntax error I have not noticed, or does HSQLDB not support multiple table expressions in WITH...AS inside of CREATE VIEW?
Edit: Updated example query to include table DDL for completeness.
HSQLDB supports creating this type of view.
As you haven't provided table definitions, I tried with a similar query with the test tables that are generated by DatabaseManager and it was successful. Please report the tables.
CREATE VIEW REPORT_LINKED_IDS AS
WITH a AS (
SELECT PRODUCTID, count(*) AS amount
FROM ITEM
GROUP BY PRODUCTID
),
b AS (
SELECT PRODUCTID, amount
FROM a
)
SELECT b.PRODUCTID, b.amount, i.NAME, i.PRICE
FROM b
JOIN PRODUCT i ON i.ID = b.PRODUCTID
ORDER BY b.PRODUCTID;
Thanks to a suggestion by #fredt, I have confirmed that the issue is with trying to use this query in IntelliJ IDEA (2020.1). The query worked fine and the view was created successfully in the same DB when another tool was used (DbVisualizer in my case). Furthermore, after having created the view in DB, IntelliJ IDEA throws an exception on the same word "JOIN" when trying to connect to this DB - the error is similar to this: The specified database user/password combination is rejected: org.hsqldb.HsqlException: unexpected token: NOT. Similarly to a comment in the above question, I have recovered from the error by manually editing the .script file.
There are at least 2 possible options to resolve the issue:
1st solution: Refactor SQL query to only have one table in WITH clause. In my case I just moved the first table to a select expression in FROM clause, like below:
CREATE VIEW TEST_VIEW AS
WITH b AS (
SELECT CONTENTID, amount
FROM (
SELECT CONTENTID, count(*) AS amount
FROM TEST_2
GROUP BY CONTENTID
)
)
SELECT b.CONTENTID, b.amount, i.DOWNLOADDATE
FROM b
JOIN TEST_1 i ON i.CONTENTID = b.CONTENTID
ORDER BY b.CONTENTID;
2nd solution: Use a different tool to work with the DB, or have the issue fixed in IDEA.

Valid Query, Failed View

I have had an issue with some DB2 SQL and could really use some help.
In short, people want me to create a view. I wrote a query that works and returns what we wanted... but when I wrap it with a 'CREATE VIEW' statement, the view throws errors when I query it with anything.
The query is (names changed obviously):
SELECT DISTINCT "QTable"."Add" "Q_Add", "QTable"."Approved" "Q_Approved", "QTable"."Link" "Q_Link"
FROM ((
SELECT * FROM db.schema.VTable)
"QTable" LEFT OUTER JOIN (SELECT * FROM db.schema.ETable)
"QStat" on "QTable"."Status" = "QStat"."ETable")
Each time I run this I get 10 record back. Awesome, that's what I want. When I wrap it as a View, which I do by entering:
CREATE VIEW TestSchema.TestTable AS
SELECT * FROM ( *query I just wrote above*)
It runs (very quickly) but then I run a basic SELECT * FROM viewname I always get the same error of:
SQL0206N "QTable.Status" is not valid in the context where it is used. SQLSTATE=42703
I have been stuck for a while now. I am assuming I am creating the view wrong. Any ideas? Tips?
Edit: DB2 11.1
I would get rid of just about ll the brackets and quotes. I suspect they are not necessary, unless DB2 is very non-standard sql. I've left the quotes in although I would take all of those out as well unless they are reserved words.
There is no reason to do something like from (select * from table)
The query should just be:
SELECT DISTINCT "QTable"."Add" "Q_Add", "QTable"."Approved" "Q_Approved", "QTable"."Link" "Q_Link"
FROM db.schema.VTable QTable
LEFT OUTER JOIN db.schema.ETable "QStat" on "QTable"."Status" = "QStat"."ETable"
Your view then should also not be create view as select * from (...) it should just be:
create view myView as
SELECT DISTINCT "QTable"."Add" "Q_Add", "QTable"."Approved" "Q_Approved", "QTable"."Link" "Q_Link"
FROM db.schema.VTable QTable
LEFT OUTER JOIN db.schema.ETable "QStat" on "QTable"."Status" = "QStat"."ETable"
You may have a different problem. I just tried it in DB2 10.5 and it works well:
create table vtable (
"Add" int,
"Approved" int,
"Link" varchar(20),
"Status" int
);
create table etable (
"ETable" int
);
create view my_view1 as
select
*
from
(
SELECT
DISTINCT "QTable"."Add" "Q_Add",
"QTable"."Approved" "Q_Approved",
"QTable"."Link" "Q_Link"
FROM
(
( SELECT * FROM VTable) "QTable"
LEFT OUTER JOIN
(
SELECT
*
FROM ETable
)
"QStat" on "QTable"."Status" = "QStat"."ETable"
)
)
Anyway, you have excessive parenthesis. Remove the ones you don't need.
And QTable need to be in double quotes always, as in "QTable".

Update largest date, matching two fields

tables
Hi, I'm looking to update the last column in a blank table. Picture shows input and desired output. Trying to pick the largest date where workorder and state match.
I've tried a couple different codes:
UPDATE mytable
SET mytable.orderstartdate = MAX(table2.earliestdate)
FROM mytable as table2
WHERE (mytable.workorder = table2.workorder AND
mytable.state = table2.state)
;
"Syntax Error (missing operator) in query expression 'MAX(table2.earliestdate) FROM mytable as table2'."
UPDATE mytable
SET mytable.orderstartdate = (
SELECT max(earliestdate)
FROM mytable as table2
WHERE (mytable.workorder = table2.workorder AND
mytable.state = table2.state)
)
;
"Operation must use an updateable query"
Edit - click tables link for image.
Write PL/SQL Code.
First, select DISTINCT WorkOrder and State and capture in variables.
Now, Iterate the list and Write a query to get max date i.e. max(date) using work_order and State in where clause. Capture the
date.
Now, In the same loop write update query setting max(date) and workorder and State in where clause.
UPDATE table A
SET A.orderstartDate = (SELECT max(earliestdate)
FROM table B
WHERE A.WorkOrder = B.WorkOrder
GROUP BY WorkOrder)
not sure if access supports correlated subqueries but if it does...
and if not...
UPDATE table A
INNER JOIN (SELECT WorkOrder, max(OrderStartDate) MOSD
FROM Table B
GROUP BY WorkOrder) C
ON A.WorkOrder = C.workOrder
SET A.OrderStartDate = C.MOSD
Check database open mode, it may be locked for editing, or you may have no permission to to file.

Pass SSRS parameter to SQL query

I've built an SSRS report that is supposed to look at data from one of several tables with similar names - i.e., table001, table010, table011, etc. When I was building the report, I was only including three of the tables, out of more than a dozen. Everything worked fine until I added all the rest of the tables to the query, using multiple SELECT statements UNIONed together; it was trying to sort through so much data that it was taking almost half an hour to render the report. That's not going to work.
Is there a way to pass an SSRS parameter to the SQL query, specifying which table to access?
Based on your comments, you could do the following
DECLARE #Parameter NVARCHAR(15) = 'Foo'
SELECT CASE
WHEN #Parameter IN ('Foo', 'Bar')
THEN (
SELECT *
FROM table001
)
WHEN #Parameter IN ('Foobar')
THEN (
SELECT *
FROM table002
)
ELSE
(
SELECT *
FROM table003
)
END
Alternately
SELECT CASE #Parameter
WHEN 'Foo'
THEN (
SELECT *
FROM table001
)
WHEN 'Bar'
THEN (
SELECT *
FROM table002
)
WHEN 'Foobar'
THEN (
SELECT *
FROM table003
)
ELSE
(
SELECT *
FROM #table004
)
END
Have you followed the MSDN tutorial? This is good too: http://sql-bi-dev.blogspot.com/2010/07/report-parameters-in-ssrs-2008.html
Please share what you have tried and where you are having trouble. Essentially, you define a parameter in the report and include it in your query. SSRS provides the parameter value (or it is received through user input), and then the final query is passed off to the database.