I have an SQL Server ALM project. I need a query that extracts all requirements with their full path, which can up to four levels including the requirement name.
Since I'm on SQL Server I'm unable to use the tools supplied by Oracle (which make this quite easy). The query must run from within ALM.
All I have so far is this:
SELECT distinct RQ_REQ_ID AS "Int Req ID",
REQ.RQ_REQ_NAME AS "Req ID",
REQ.RQ_REQ_REVIEWED as "Req Status",
REQ.RQ_REQ_STATUS AS "Req Coverage"
FROM REQ
WHERE RQ_TYPE_ID != 1
ORDER BY RQ_REQ_NAME
Can anyone please complete the statement so it would contain the full requirement path?
Thanks
Okay, I'm at work, its the holidays and I've finished up my projects, and yeah I've seen this as an oracle query and there's some simplicity but I worked in SQL for a bit. This is a total hack query and its written in oracle but the concept is the same.
SELECT distinct RQ_REQ_ID AS "Int Req ID",
r.RQ_REQ_NAME AS "Req ID",
r.RQ_REQ_REVIEWED as "Req Status",
r.RQ_REQ_STATUS AS "Req Coverage",
r.rq_req_path,
(select rq_req_name from req where rq_req_path = substr(r.rq_req_path, 1, 3))
|| '/' ||
(select rq_req_name from req where rq_req_path = substr(r.rq_req_path, 1, 6))
|| '/' ||
(select rq_req_name from req where rq_req_path = substr(r.rq_req_path, 1, 9))
FROM REQ r
WHERE RQ_TYPE_ID != 1
ORDER BY RQ_REQ_NAME
You'll want & instead of || and you may have to do some cast/convert operations on the return. The reason I thought of doing it this way is that crawling the rq_req_path and iterating down is kind of...frowned upon in SQL Server.
What I did was found the max length of RQ_REQ_PATH and just added n concatenations. Afterwards, it would be easy to strip out the extra /.
I'm totally positive there's a better, cleaner way of doing it but in case anyone else is looking at this, this should be a starting point and if its a one-off report, this works fine.
Related
I inherited a query that is used for extract information from Oracle Database and they want now to create a XML file using SSIS. So far I read several forum and blogs and I found a way to connect it but not sure how I should use this query. I found that Oracle use XMLElement to create the XML but I got
Unsupported Oracle data type USERDEFINED encountered. (System.Data.OracleClient)
when I tried to run the query. I will provide part of the original query, because it is quite large code
SELECT
--SCHED_STASK.SCHED_ID,
ACFT_INV.INV_NO_SDESC AS "Aircraft",
SCHED_STASK.BARCODE_SDESC AS "Barcode",
EVT_EVENT.ACTUAL_START_DT AS "Act_Start_Dt",
EVT_EVENT.EVENT_DT AS "Act_End_Dt",
TASK_TASK.TASK_ORIGINATOR_CD AS "Originator",
SCHED_STASK.TASK_CLASS_CD AS "Class",
SCHED_STASK.TASK_SUBCLASS_CD AS "SubClass",
EVT_EVENT.EVENT_STATUS_CD AS "Status",
CASE
WHEN TASK_TASK.TASK_CD is null THEN null
WHEN TASK_TASK.TASK_CD is not null THEN TASK_TASK.TASK_CD||' ('||TASK_TASK.TASK_NAME||')'
END AS "Task Defn",
ORG_HR.HR_CD AS "Employee number / ID",
SCHED_WORK_TYPE.WORK_TYPE_CD AS "Worktype",
EVT_STAGE.STAGE_DT AS "Signed Date"
......
and a huge bunch of left joins. then the where clause
So could someone guide me on how to solve this scenario I will more than happy, quite new using Oracle, I am more familiar with MS SQL + SSIS than Oracle + SSIS
If I understand you correctly you want xml output from oracle sql? try this!
with data as(SELECT
'Blériot' AS Aircraft,
1234 AS BARCODE,
DATE '2018-01-01' ACT_START_DT,
DATE '2018-02-01' ACT_END_DT,
'Louis' Originator,
'XI' Class,
'Civil tourer/trainer/military' SubClass,
'Obsolete but cool' Status,
CASE
WHEN 'Rebuild on mass' IS NULL THEN NULL
WHEN 'Rebuild on mass' IS NOT NULL THEN 'Rebuild on mass'||' ('||'Splinters in the sky'||')'
END "Task Defn",
666 AS "Employee number / ID",
'capentry and seamstressing' Worktype,
DATE '2018-02-01' "Signed Date"
from dual)
SELECT XMLELEMENT("Project",
(XMLELEMENT("Aircraft",AIRCRAFT)),
(XMLELEMENT("BARCODE",BARCODE)),
(XMLELEMENT("ACT_START_DT",ACT_START_DT)),
(XMLELEMENT("ACT_END_DT",ACT_END_DT)),
(XMLELEMENT("Originator",Originator)),
(XMLELEMENT("Class",Class)),
(XMLELEMENT("SubClass",SubClass)),
(XMLELEMENT("Status",Status)),
(XMLELEMENT("Task Defn","Task Defn")),
(XMLELEMENT("Employee number / ID","Employee number / ID")),
(XMLELEMENT("Worktype",Worktype)),
(XMLELEMENT("Signed Date","Signed Date"))
)
from data
the result beeing:
<Project>
<Aircraft>Blériot</Aircraft>
<BARCODE>1234</BARCODE>
<ACT_START_DT>2018-01-01</ACT_START_DT>
<ACT_END_DT>2018-02-01</ACT_END_DT>
<Originator>Louis</Originator>
<Class>XI</Class>
<SubClass>Civil tourer/trainer/military</SubClass>
<Status>Obsolete but cool</Status>
<Task Defn>Rebuild on mass (Splinters in the sky)</Task Defn>
<Employee number / ID>666</Employee number / ID>
<Worktype>capentry and seamstressing</Worktype>
<Signed Date>2018-02-01</Signed Date>
</Project>
For This particular case, I decided to use another ssis control that I downloaded, it seems that over here the settings used by the company didnt allow a good interaction between the oracle and the ms ssis.
We access an Oracle database through a web application. The web application is published, hosted and administered off-site. One component of the web application are dashboards, which use SQL to return data from the system on regular intervals that are then displayed and can trigger alarms should certain thresholds be reached.
The SQL has several parameters that are assigned in the dashboard setup: LOCATION, OWNER_DEPT, USING_DEPT and DAYS_AHEAD and appear to be working properly. Here is the query:
SELECT * FROM
(
SELECT DISTINCT
"MFIVE"."VIEW_WORK_REQ_OCC"."UNIT_NO" AS "UNIT NO", "MFIVE"."VIEW_WORK_REQ_OCC"."WORK_REQ_NO" AS "WORK REQ NO", "MFIVE"."VIEW_WORK_REQ_OCC"."JOB" AS "JOB",
CASE WHEN "MFIVE"."VIEW_WORK_REQ_OCC"."DUE_DATE" IS NULL THEN NULL ELSE ("MFIVE"."VIEW_WORK_REQ_OCC"."DUE_DATE") END AS "DUE DATE",
CASE WHEN "MFIVE"."VIEW_WORK_REQ_OCC"."FIRST_DATE" IS NULL THEN NULL ELSE ("MFIVE"."VIEW_WORK_REQ_OCC"."FIRST_DATE") END AS "FIRST DATE",
CASE WHEN "MFIVE"."VIEW_WORK_REQ_OCC"."LAST_DATE" IS NULL THEN NULL ELSE ("MFIVE"."VIEW_WORK_REQ_OCC"."LAST_DATE") END AS "LAST DATE"
FROM "MFIVE"."VIEW_WORK_REQ_OCC"
WHERE
("MFIVE"."VIEW_WORK_REQ_OCC"."MNT_PREVENTIVE_FL" = 'Y') AND
(UPPER("MFIVE"."VIEW_WORK_REQ_OCC"."LOCATION") LIKE UPPER(CONCAT(CONCAT('%', ':<LOCATION>'), '%'))) AND
(UPPER("MFIVE"."VIEW_WORK_REQ_OCC"."OWNER_DEPT") LIKE UPPER(CONCAT(CONCAT('%', ':<OWNER_DEPT>'), '%'))) AND
(UPPER("MFIVE"."VIEW_WORK_REQ_OCC"."USING_DEPT") LIKE UPPER(CONCAT(CONCAT('%', ':<USING_DEPT>'), '%'))) AND
("MFIVE"."VIEW_WORK_REQ_OCC"."DUE_DATE" >= SYSDATE + ':<DAYS_AHEAD>')
ORDER BY
"MFIVE"."VIEW_WORK_REQ_OCC"."UNIT_NO" ASC,
"MFIVE"."VIEW_WORK_REQ_OCC"."JOB" ASC,
CASE WHEN "MFIVE"."VIEW_WORK_REQ_OCC"."DUE_DATE" IS NULL THEN NULL ELSE ("MFIVE"."VIEW_WORK_REQ_OCC"."DUE_DATE") END AS "DUE DATE" ASC
)
The issue is with the statements involving "MFIVE"."VIEW_WORK_REQ_OCC"."DUE_DATE". We're attempting to return only values where DUE_DATE is greater than the SYSDATE plus the value in the DAYS_AHEAD parameter. Deleting these lines allows the query to execute properly; leaving them as-is returns the error ORA-00907: missing right parenthesis.
The majority of the SQL was lifted from the SQL as generated by an ad-hoc reporting component of the web application. The report runs properly, but we are not able to pass parameters through the ad-hoc reporting module. The modifications to the original were to add the bind variables.
I'm hoping someone can point me in the correct direction to remedy this issue. Any assistance is appreciated.
Thanks to Kaushik Nayak for helping me see the forest for the trees.
The correction only affects the ORDER BY clause:
...
ORDER BY
"MFIVE"."VIEW_WORK_REQ_OCC"."UNIT_NO" ASC,
"MFIVE"."VIEW_WORK_REQ_OCC"."JOB" ASC,
)
I have a following table:
EstimatedCurrentRevenue -- Revenue column value of yesterday
EstimatedPreviousRevenue --- Revenue column value of current day
crmId
OwnerId
PercentageChange.
I am querying two snapshots of the similarly structured data in Azure data lake and trying to query the percentage change in Revenue.
Following is my query i am trying to join on OpportunityId to get the difference between the revenue values:
#opportunityRevenueData = SELECT (((opty.EstimatedCurrentRevenue - optyPrevious.EstimatedPreviousRevenue)*100)/opty.EstimatedCurrentRevenue) AS PercentageRevenueChange, optyPrevious.EstimatedPreviousRevenue,
opty.EstimatedCurrentRevenue, opty.crmId, opty.OwnerId From #opportunityCurrentData AS opty JOIN #opportunityPreviousData AS optyPrevious on opty.OpportunityId == optyPrevious.OpportunityId;
But i get the following error:
E_CSC_USER_SYNTAXERROR: syntax error. Expected one of: AS EXCEPT FROM
GROUP HAVING INTERSECT OPTION ORDER OUTER UNION UNION WHERE ';' ')'
','
at token 'From', line 40
near the ###:
This expression is having the problem i know but not sure how to fix it.
(((opty.EstimatedCurrentRevenue - optyPrevious.EstimatedPreviousRevenue)*100)/opty.EstimatedCurrentRevenue)
Please help, i am completely new to U-sql
U-SQL is case-sensitive (as per here) with all SQL reserved words in UPPER CASE. So you should capitalise the FROM and ON keywords in your statement, like this:
#opportunityRevenueData =
SELECT (((opty.EstimatedCurrentRevenue - optyPrevious.EstimatedPreviousRevenue) * 100) / opty.EstimatedCurrentRevenue) AS PercentageRevenueChange,
optyPrevious.EstimatedPreviousRevenue,
opty.EstimatedCurrentRevenue,
opty.crmId,
opty.OwnerId
FROM #opportunityCurrentData AS opty
JOIN
#opportunityPreviousData AS optyPrevious
ON opty.OpportunityId == optyPrevious.OpportunityId;
Also, if you are completely new to U-SQL, you should consider working through some tutorials to establish the basics of the language, including case-sensitivity. Start at http://usql.io/.
This same crazy sounding error message can occur for (almost?) any USQL syntax error. The answer above was clearly correct for the provided code.
However since many folks will probably get to this page from a search for 'AS EXCEPT FROM GROUP HAVING INTERSECT OPTION ORDER OUTER UNION UNION WHERE', I'd say the best advice to handle these is look closely at the snippet of your code that the error message has marked with '###'.
For example I got to this page upon getting a syntax error for a long query and it turned out I didn't have a casing issue, but just a malformed query with parens around the wrong thing. Once I looked more closely at where in the snippet the ### symbol was, the error became clear.
Today I wrote this bit of sql:
SELECT COUNT(T0021_werk_naam)
FROM (SELECT Distinct T0021_werk_naam,T0021_jaar,T0021_kwartiel
FROM T0021_offertes
WHERE T0021_status_code = 'G' AND T0021_jaar = 2013 AND (T0021_kwartiel = 3))
This sql runs great when I run it locally in access, however, when I run it through the code that has been used for ages for this and most certainly definetly is not the problem, and send it to SQL Express it gives an error that says there's a problem near ')'
After stripping away all the brackets possible it becomes clear that it detects there's a problem with the last ')' but I don't see the problem.
Any Ideas?
You need to give an alias for the select in the parenthesis:
SELECT COUNT(T0021_werk_naam)
FROM (
SELECT Distinct T0021_werk_naam,
T0021_jaar,
T0021_kwartiel
FROM T0021_offertes
WHERE T0021_status_code = 'G'
AND T0021_jaar = 2013
AND (T0021_kwartiel = 3)
) T
notice the T in the end after the last parenthesis.
I'm working on repairing the test suite for a project of ours, which is being tested through Hibernate/DBUnit. There are several test cases which all throw a similar exception from Hibernate, which looks something like this:
java.sql.SQLException: Not in aggregate function or group by clause: org.hsqldb.Expression#109062e in statement [... blah ...]
Through my googling, I am suspicious that this is caused by our use of the aggregate function AVG(), as this is in the exception's message, and all of the queries that throw contain it. However, I discovered several links to people who were getting this error, and were able to fix it by either commenting out an "ORDER BY" or "GROUP BY" clause, or by including the other columns from the SELECT clause in the grouping. I understand why this would fix such an error message, but I'm not sure whether it applies to my situation, because I tried doing the same and it made no difference. Also, we have some test cases throwing exceptions which use ORDER/GROUP, but not all. For example:
ThingerVO myThinger = (ThingerVO)session.createQuery("SELECT new ThingerVO(" +
"r.id, " + "u.id, " + "u.alias, " + "s.id, " +
"s.name, " + "r.URL," + "AVG(v.rating), " +
"r.totalCount, " + "r.isPrivate, " + "a.id, " +
"a.name, " + "r.transactionId, " + "r.size, " +
"u.hasPicture " +
") FROM Thinger r LEFT OUTER JOIN r.votes as v, Table1S s " +
"JOIN s.Table2A AS a, User u " +
"WHERE r.userId = u.id AND " +
"s.id = r.Table1SId AND " +
"r.id = :thingId")
.setInteger("thingId", thingId)
.uniqueResult();
This query also causes the same exception to be thrown, even though it doesn't use an ORDER/GROUP clause. Also, I cut/pasted the generated HSQL code from Hibernate directly into the MySQL query browser, and it ran without problems. Also, it's worth pointing out that all of this code works fine on our production database, so I'm really confused as to why it throws here.
Some other potentially useful information -- we're using a flat XML database structure with some dummy test data for the test cases, and the MySQL dialect for hibernate. We're using dbunit 2.4.3/hibernate 3.2.6. I tried using the latest hibernate, version 3.3.1, but it behaved the same.
Any pointers or hints would be greatly appreciated.
If you use an aggregate function (e.g. AVG()) in the SELECT part of the SQL query along with other non-aggregate expressions, then you must have a GROUP BY clause which should list all the non-aggregate expressions.
I'm not familiar with java, but looking at the code, it looks like it's going to create and run a query something like this (not quite right, but close enough, I think):
SELECT r.id,
u.id,
u.alias,
s.id,
s.name,
r.URL,
AVG(v.rating),
r.totalCount,
r.isPrivate,
a.id,
a.name,
r.transactionId,
r.size,
u.hasPicture
FROM Thinger r
LEFT OUTER JOIN r.votes as v,
Table1S s
JOIN s.Table2A AS a, User u
WHERE r.userId = u.id
AND s.id = r.Table1SId
AND r.id = :thingId
... This has no GROUP BY, but does mix aggregate and non-aggregate expressions in the SELECT clause. The problem is that the SQL is badly formed.
The fix would be to add a GROUP BY to the end of the query.
I can't say why this is working in your production system, but I suspect that there is some subtle difference there. Perhaps something is adding the GROUP BY automatically?
Can you post a printout of the SQL it executes?
Also, ORDER BY does not work in hsqldb when the order-by field is not a String.
Unfortunately, this results in the Not in aggregate function or group by clause error message, which suggests a grouping problem, hence the confusion...
See: http://markmail.org/message/42vmifme4opz4jgl
In some systems (for example TALEND) query doesn't work if there are comment lines example:
SELECT r.id,
u.alias,
AVG(v.rating),
r.totalCount
FROM Thinger r
LEFT OUTER JOIN r.votes as v,
Table1S s
JOIN s.Table2A AS a, User u
WHERE r.userId = u.id
AND s.id = r.Table1SId
AND r.id = :thingId
--AND r.name is not null
GROUP BY r.id, u.alias, r.totalCount
Gives an error for MS SQL queries. Instead of comment line
--
use these symbols for commenting
/* AND r.name is not null */
Maybe it will help someone and save some time.