I have this RAWSQL query
with
group as (
select * from groups where id = ?
),
attributes as (
select JSON_ARRAYAGG(
JSON_OBJECT('id', a.id,'name', a.name ))
from attributes a
join groups g on g.id = ?
join attribute_group ag on ag.group_id = g.id
and ag.attribute_id = a.id
),
templates as (
select JSON_ARRAYAGG(
JSON_OBJECT('id', t.id,'name', t.name))
from templates t
join groups g on g.id = ?
join group_template gt on gt.group_id = g.id
and gt.template_id = t.id
)
select *,
(select cast(count(*) as char) from attribute_group where group_id = ? ) groups_count,
(select * from groups) groups,
(select cast(count(*) as char) from group_template where group_id = ? ) templates_count,
from group
I have this error
Query 1 ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'group as (
The documentation advises to not use reserved words like group and groups. They are reserved characters and should only be used for things like the GROUP BY statement:
GROUP (R)
GROUPING (R); added in 8.0.1 (reserved)
GROUPS (R); added in 8.0.2 (reserved)
Outside of that, I noticed a few syntax errors:
If you have a table named GROUP, which it looks like you do, you
can escape the reserved word by using back ticks `` or double quotes "" (I.e. `group`, "group").
Remove the comma , after templates_count,.
Replace ? with a valid id column value (I used 1 below).
Like so:
with
`group` as (
select * from `groups` where id = 1
),
attributes as (
select JSON_ARRAYAGG(
JSON_OBJECT('id', a.id,'name', a.name ))
from attributes a
join `groups` g on g.id = 1
join attribute_group ag on ag.group_id = g.id
and ag.attribute_id = a.id
),
templates as (
select JSON_ARRAYAGG(
JSON_OBJECT('id', t.id,'name', t.name))
from templates t
join `groups` g on g.id = 1
join group_template gt on gt.group_id = g.id
and gt.template_id = t.id
)
select *,
(select cast(count(*) as char) from attribute_group where group_id = 1 ) groups_count,
(select * from `groups`) `groups`,
(select cast(count(*) as char) from group_template where group_id = 1 ) templates_count
from `group`
Related
My code looks like this:
...
LEFT JOIN
(
SELECT *
FROM REI_COUNTRY_CURRENCY T_CC
WHERE T_CC.AS_FROM_DATE =
(
SELECT MAX(T2_CC.AS_FROM_DATE)
FROM REI_COUNTRY_CURRENCY T2_CC
WHERE T2_CC.COUNTRY_ID = T_CC.COUNTRY_ID
) T
) CC
ON C.COUNTRY_ID = CC.COUNTRY_ID
...
I can't see why it would say "missing right parenthesis".
All parentheses are paired.
Aliases are not necessary/allowed on sub-queries:
LEFT JOIN
(
SELECT *
FROM REI_COUNTRY_CURRENCY T_CC
WHERE T_CC.AS_FROM_DATE =
(
SELECT MAX(T2_CC.AS_FROM_DATE)
FROM REI_COUNTRY_CURRENCY T2_CC
WHERE T2_CC.COUNTRY_ID = T_CC.COUNTRY_ID
) -- Remove the T from this line
) CC
ON C.COUNTRY_ID = CC.COUNTRY_ID
Or, to get rid of the correlated sub-query:
LEFT JOIN
(
SELECT *
FROM (
SELECT T_CC.*,
RANK() OVER (
PARTITION BY Country_ID
ORDER BY AS_FROM_DATE DESC
) AS rn
FROM REI_COUNTRY_CURRENCY T_CC
)
WHERE rn = 1
) CC
ON C.COUNTRY_ID = CC.COUNTRY_ID
Need alternative solution for below SQL Query ,TableMasterID NOT IN take so much time, If i remove AND TableMasterID NOT IN
(SELECT DISTINCT c.TableMasterID
FROM ComFinalDataBS as C
WHERE C.ComFileID IN
(SELECT Number FROM fn_SplitInt(#ComFileID,','))) text from below query then getting result in 20 seconds otherwise result getting arround 4 minus.
SELECT A.SubTitleId,TableMasterID from SubTitle as A JOIN ComTableMaster as B ON a.SubTitle = b.TblName AND TableMasterID NOT IN (SELECT DISTINCT c.TableMasterID
FROM ComFinalDataBS as C
WHERE C.ComFileID IN
(SELECT Number FROM fn_SplitInt(#ComFileID,','))) AND B.TableMasterID IN
(SELECT DISTINCT d.TableMasterID
FROM ComData as D
WHERE D.ComFileID IN
(SELECT Number FROM fn_SplitInt(#ComFileID,','))) ORDER BY A.MainTitleID
SELECT A.SubTitleId ,
TableMasterID
FROM SubTitle AS A
JOIN ComTableMaster AS B ON A.SubTitle = B.TblName
WHERE NOT EXISTS ( SELECT 1
FROM ComFinalDataBS C
WHERE TableMasterID = C.TableMasterID
AND C.ComFileID IN (
SELECT Number
FROM MEFCampus..fn_SplitInt(#ComFileID, ',') ) )
AND NOT EXISTS ( SELECT 1
FROM ComData D
WHERE TableMasterID = D.TableMasterID
AND D.ComFileID IN (
SELECT Number
FROM MEFCampus..fn_SplitInt(#ComFileID,
',') ) )
AND B.IsDeleted = 0
ORDER BY MainTitleID
Have you tried storing the result from the fn_split_string() into an indexed temp-table first? It should help the Query Optimizer a lot.
SELECT DISTINCT Number
INTO #ComFileID
FROM dbo.fn_SplitInt(#ComFileID,',')
CREATE UNIQUE CLUSTERED INDEX uq0_ComFileID ON #ComFileID (Number) WITH (FILLFACTOR = 100)
SELECT A.SubTitleId,TableMasterID
FROM SubTitle as A
JOIN ComTableMaster as B
ON a.SubTitle = b.TblName
/*
AND B.TableMasterID NOT IN (SELECT DISTINCT c.TableMasterID
FROM ComFinalDataBS as C
JOIN #ComFileID CFI
ON CFI.Number = C.ComFileID )
*/
AND NOT EXISTS ( SELECT *
FROM ComFinalDataBS as C
JOIN #ComFileID CFI
ON CFI.Number = C.ComFileID
WHERE c.TableMasterID = B.TableMasterID )
/*
AND B.TableMasterID IN (SELECT DISTINCT d.TableMasterID
FROM ComData as D
JOIN #ComFileID CFI
ON CFI.Number = D.ComFileID
*/
AND EXISTS ( SELECT *
FROM ComData as D
JOIN #ComFileID CFI
ON CFI.Number = D.ComFileID
WHERE D.TableMasterID = B.TableMasterID )
ORDER BY A.MainTitleID
We have a pattern we use all the time and this is usually pretty straightforward.
sortOrder IN VARCHAR2 := 'Title'
query VARCHAR2(32767) := q'[
SELECT
Columns
FROM tables
ORDER BY {sortOrder}
]';
query := REPLACE(query, '{sortOrder}', sortOrder);
But for this string it is not working:
WITH appl_List
AS
(
SELECT DISTINCT
appls.admin_phs_ORG_code || TO_CHAR(appls.serial_num, 'FM000000') AS core_proj_number,
appls.Appl_ID
FROM TABLE(:portfolioTable) appls
),
g1SupportingProjCount AS
(
SELECT
gen1grants.silverchair_id,
COUNT(DISTINCT al.Appl_ID) AS ApplCount
FROM
appl_List al
JOIN cg_cited_reference_gen1_grant gen1grants
ON al.core_proj_number = gen1grants.ic_serial_num
JOIN cg_cited_reference_gen1 gen1refs
ON gen1grants.silverchair_id = gen1refs.silverchair_id
GROUP BY gen1grants.Silverchair_id
),
g1SupportedPubCount AS
(
SELECT
gen1grants.silverchair_id,
COUNT(DISTINCT gen1refs.gen1_wos_uid) AS PubCount
FROM
appl_List al
JOIN cg_cited_reference_gen1_grant gen1grants
ON al.core_proj_number = gen1grants.ic_serial_num
JOIN cg_cited_reference_gen1 gen1refs
ON gen1grants.silverchair_id = gen1refs.silverchair_id
GROUP BY gen1grants.Silverchair_id
),
g2SupportingProjCount AS
(
SELECT
gen2grants.silverchair_id,
COUNT(DISTINCT al.Appl_ID) AS ApplCount
FROM
appl_List al
JOIN cg_cited_reference_gen2_grant gen2grants
ON al.core_proj_number = gen2grants.ic_serial_num
JOIN cg_cited_reference_gen2 gen2refs
ON gen2grants.silverchair_id = gen2refs.silverchair_id
GROUP BY gen2grants.Silverchair_id
),
g2SupportedPubCount AS
(
SELECT
gen2grants.silverchair_id,
COUNT(DISTINCT gen2refs.gen2_wos_uid) AS PubCount
FROM
appl_List al
JOIN cg_cited_reference_gen2_grant gen2grants
ON al.core_proj_number = gen2grants.ic_serial_num
JOIN cg_cited_reference_gen2 gen2refs
ON gen2grants.silverchair_id = gen2refs.silverchair_id
GROUP BY gen2grants.Silverchair_id
),
portfolio_cg_ids AS
(
SELECT DISTINCT md.silverchair_id
FROM
(
SELECT silverchair_id
FROM cg_cited_reference_gen1_grant gen1Grants
JOIN Appl_List appls
ON appls.core_proj_number = gen1Grants.ic_serial_num
UNION
SELECT silverchair_id
FROM cg_cited_reference_gen2_grant gen2Grants
JOIN Appl_List appls
ON appls.core_proj_number = gen2Grants.ic_serial_num
) grant_sc_ids
JOIN cg_metadata md
ON grant_sc_ids.silverchair_id = md.silverchair_id
)
SELECT
silverchairId,
TITLE,
PMID,
PMCID,
publication_year as year,
referenceCount1Gen,
supportingProjectCount1Gen,
supportedPublicationCount1Gen,
referenceCount2Gen,
supportingProjectCount2Gen,
supportedPublicationCount2Gen,
COUNT(1) OVER() as TotalCount
FROM
(
SELECT
md.SILVERCHAIR_ID silverchairId,
md.TITLE,
md.PMID,
md.PMCID ,
md.publication_year as year,
g1RefCounts.referenceCount1Gen as referenceCount1Gen,
g1SupportingProjCount.ApplCount as supportingProjectCount1Gen,
g1SupportedPubCount.PubCount as supportedPublicationCount1Gen,
g2RefCounts.referenceCount2Gen as referenceCount2Gen,
g2SupportingProjCount.ApplCount as supportingProjectCount2Gen,
g2SupportedPubCount.PubCount as supportedPublicationCount2Gen,
--COUNT(1) OVER() as TotalCount
FROM cg_metadata md
-- BEGIN datascope to current portfolio
JOIN portfolio_cg_ids
ON portfolio_cg_ids.silverchair_id = md.silverchair_id
-- END datascope to current portfolio
LEFT JOIN g1SupportingProjCount
ON g1SupportingProjCount.Silverchair_id = md.silverchair_id
LEFT JOIN g2SupportingProjCount
ON g2SupportingProjCount.Silverchair_id = md.silverchair_id
LEFT JOIN g1SupportedPubCount
ON g1SupportedPubCount.Silverchair_id = md.silverchair_id
LEFT JOIN g2SupportedPubCount
ON g2SupportedPubCount.Silverchair_id = md.silverchair_id
OUTER APPLY
(
Select Count(*) as referenceCount1Gen
FROM cg_cited_reference_gen1 g1Refs
WHERE g1Refs.silverchair_id = md.silverchair_id
) g1RefCounts
OUTER APPLY
(
Select Count(*) as referenceCount2Gen
FROM cg_cited_reference_gen2 g2Refs
WHERE g2Refs.silverchair_id = md.silverchair_id
) g2RefCounts
) results
ORDER BY {sortOrder}
Are there cases where some kind of special char in the string can keep this from working?
I'm kind of perplexed. I've been using this pattern for like 3 years and I've never had this not work.
What could be breaking this?
The query has 4000+ characters.
The text is probably being truncated somewhere down the line.
I have a table with structure as below:
'Type' column has three type of values that is 1,2 and 3.
Each Type has certain Rule (mentioned in RULE column)
Same rule can be associated with multiple Type records.
Now I want to get only those records as output which has higher value.
For example:
'Bench Type' is a rule associated with Type 1,2 and 3 so we will pick Rule with Value with Type 3, that is Single.
'Complexity' is with Type 1 & 2 so we will pick rule Value 5.
If a Rule is present in just one Type then it will be picked as it is.
I tried following query but it is working with Type 1 & 2.
select *
FROM CX_ROSTER_RULES_VIEW v
INNER JOIN
(
select RULE, MAX("Type") AS MAX_TYPE
FROM CX_ROSTER_RULES_VIEW
GROUP BY RULE
) v1
ON v.RULE = v1.RULE
AND v."Type" = v1.MAX_TYPE
WHERE ( branch = 'Civil' AND category = 'C.M. (Civil)' AND sub_category = 'Pauper' )
OR ( branch = 'Civil' AND category = 'C.M. (Civil)' AND sub_category IS NULL )
OR ( branch = 'Civil' AND category IS NULL AND sub_category IS NULL )
Use the ROW_NUMBER() analytic function then you will not need to do a self-join:
SELECT *
FROM (
SELECT v.*,
ROW_NUMBER() OVER ( PARTITION BY Rule ORDER BY "Type" DESC ) AS rn
FROM CX_ROSTER_RULES_VIEW v
WHERE branch = 'Civil'
AND ( category IS NULL
OR ( category = 'C.M. (Civil)'
AND ( sub_category IS NULL OR sub_category = 'Pauper' )
)
)
)
WHERE rn = 1;
i would do this.
(select rule, max(type) as type from yourtable
group by rule) --this will give you the rule with the highest priority
with that do this
select m.* from CX_ROSTER_RULES_VIEW as m inner join (select rule, max(type) as type from CX_ROSTER_RULES_VIEW
group by rule) p on m.rule = p.rule and m.type = p.type
don't sql on this machine, if the above don't work try this. I will be able to test tomorrow if you don't have it complete.
select m.* from (select rule, max(type) as type from CX_ROSTER_RULES_VIEW
group by rule) as p left join CX_ROSTER_RULES_VIEW as m on p.rule = m.rule and p.type = m.type
it's pretty much the same, just want to see if a left join will do anything different.
Try this:
select v.* from
CX_ROSTER_RULES_VIEW v join
(select rule, max(type) as maxType from CX_ROSTER_RULES_VIEW group by rule) M
on v.Rule = M.Rule and v.type = M.maxType
is it possible to use the table alias in an ON-Statement?
SQL Statement:
SELECT XMLELEMENT("row", XMLATTRIBUTES(rownum as "order"),
(
SELECT
XMLAGG(XMLELEMENT("attribute",XMLATTRIBUTES(z as "identifier") ) )
FROM b
LEFT JOIN c
ON c.ID = b.cID
AND c.example = table_alias.example
)
)
FROM
(
SELECT example FROM x ORDER BY y
) table_alias
I'm getting the error that table_alias.example is an invalid identifier.
If I move the c.example = table_alias.example into a WHERE Statement it works, but of course I'll get the wrong result.
Anyone have an idea?
TY
frgtv10
try this
SELECT XMLELEMENT("row", XMLATTRIBUTES(rownum as "order"),
( SELECT XMLAGG(XMLELEMENT("attribute"),XMLATTRIBUTES(z as "identifier") ) )
FROM b, c, x
where c.ID = b.cID(+) AND c.example = x.example
)
) from dual