We have a report that pulls data from an Oracle 10g database.
We are using Coldfusion 9 to display the report.
The report contains 3 CLOB columns that is killing the report processing time.
Tested this by removing the three CLOB columns and report displays in 2 - 3 seconds.
Is there anything that can be done to increase the processing time of the report?
I understand why its running slow due to how large the CLOB fields are.
Just looking to improve the processing time.
Just to give an idea of rows typical report returns 200-300 row so very small amount of rows.
many thanks
Matthew
UPDATE - We have tried using dbms_lob.substr() and return max 4000 character but this does not improve the processing time.
here's the query:
SELECT
R.NC_REQUEST_ID REQUEST_ID,
R.TC_REQUEST_NAME REQUEST_NAME,
R.TC_REQUEST_NAME_2 REQUEST_NAME_2,
R.TC_INITIATOR INITIATOR,
R.NC_EXPENSE EXPENSE,
S.NC_FORM_STEP_ID FORM_STEP_ID,
S.NC_FORM_ID FORM_ID,
S.NC_STEP_ID STEP_ID,
S.TC_STEP_CODE STEP_CODE,
S.TC_STEP_NAME STEP_NAME,
TO_CHAR(R.DC_SUBMIT_DATE,'DD-Mon-YYYY') SUBMIT_DATE,
TO_CHAR(R.DC_ORIGINAL_DATE,'DD-Mon-YYYY') ORIGINAL_DATE,
TO_CHAR(R.DC_REVISION_DATE,'DD-Mon-YYYY') REVISION_DATE,
R.NC_BUS_GROUP_ID_FK BUS_GROUP_ID,
R.NC_BUS_UNIT_ID_FK BUS_UNIT_ID,
R.NC_BUS_SUB_ID_FK BUS_SUB_ID,
R.TC_ACT_NUMBER ACT_NUMBER,
R.TC_REQUEST_NAME REQUEST_NAME,
R.NC_YEAR YEAR_NUM,
R.NC_ACT_END_NUMBER ACT_END_NUMBER,
R.DC_START_DATE START_DATE,
R.DC_END_DATE END_DATE,
R.NC_ACT_TYPE_ID_FK ACT_TYPE_ID_FK,
TO_CHAR(R.DC_APPROVAL_DATE,'DD-Mon-YYYY') APPROVAL_DATE,
CC_ATTACHED_EXHIBIT ATTACHED_EXHIBIT,
CC_ROUTED ROUTED,
R.IC_RESTRICTED RESTRICTED,
ROI.NC_PI_OFF PI_OFF,
ROI.NC_PI_CA COST_AVOIDANCE_PI,
(SELECT COUNT(NC_REQUEST_ID_FK) FROM TBXS31_REMOVE_SECTION WHERE NC_REQUEST_ID_FK = R.NC_REQUEST_ID AND NC_TEMPLATE_ID_FK IN(346,1455,1456,1458,1459,1460,1464.1641) GROUP BY NC_REQUEST_ID_FK) AS COUNT_OF_REMOVE_SECTIONS,
<cfoutput query="variables.qryEnergyListItems">
(SELECT NVL(ED.NC_EXISTINGUSAGE - ED.NC_PROPOSEDUSAGE,0) FROM TBYB28_SI_ENERGY_DATA ED,TBXP73_LIST_ITEMS LI WHERE LI.NC_VALUE_ID = ED.NC_ENERGYFORM(+) AND LI.NC_VALUE_ID = #variables.qryEnergyListItems.NC_VALUE_ID# AND ED.NC_REQUEST_ID_FK = R.NC_REQUEST_ID AND LI.IC_ACTIVE = 'T' AND LI.NC_VALUE_ID <> 808) AS "#variables.qryEnergyListItems.TC_MED_DESC#",
</cfoutput>
(SELECT TC_MED_DESC FROM TBXP73_LIST_ITEMS WHERE NC_VALUE_ID = SD.NC_BUSINESSUNIT) AS TC_BUS_UNIT,
(SELECT TC_MED_DESC FROM TBXP73_LIST_ITEMS WHERE NC_VALUE_ID = SD.NC_BUSINESSSUBUNIT) AS TC_BUS_SUB_UNIT,
(SELECT TC_MED_DESC FROM TBXP73_LIST_ITEMS WHERE NC_VALUE_ID = SD.NC_SITE) AS TC_BUS_SITE,
(SELECT NC_PROPOSEDGHG - NC_EXISTINGGHG FROM TBYB27_SUSTAINABILITY_DATA WHERE NC_REQUEST_ID_FK = R.NC_REQUEST_ID) AS ANNUAL_IMPACT_C02E,
(SELECT DECODE(TBYB14_SI_BASELINE.NC_BASE_CO2E , 0, NULL, ((TBYB27_SUSTAINABILITY_DATA.NC_PROPOSEDGHG - TBYB27_SUSTAINABILITY_DATA.NC_EXISTINGGHG) / TBYB14_SI_BASELINE.NC_BASE_CO2E) * 100) FROM TBYB14_SI_BASELINE,TBYB27_SUSTAINABILITY_DATA WHERE TBYB27_SUSTAINABILITY_DATA.NC_REQUEST_ID_FK = R.NC_REQUEST_ID AND TBYB27_SUSTAINABILITY_DATA.NC_SITE = TBYB14_SI_BASELINE.NC_SITE_ID_FK(+) AND TBYB27_SUSTAINABILITY_DATA.NC_BASELINEYEAR = TBYB14_SI_BASELINE.NC_YEAR(+) ) AS PCT_IMPACT_CO2E,
(SELECT NC_PROPOSEDWASTE - NC_EXISTINGWASTE FROM TBYB27_SUSTAINABILITY_DATA WHERE NC_REQUEST_ID_FK = R.NC_REQUEST_ID) AS ANNUAL_IMPACT_WASTE,
(SELECT DECODE(TBYB14_SI_BASELINE.NC_BASE_WASTE, 0, NULL, ((TBYB27_SUSTAINABILITY_DATA.NC_PROPOSEDWASTE - TBYB27_SUSTAINABILITY_DATA.NC_EXISTINGWASTE) / TBYB14_SI_BASELINE.NC_BASE_WASTE) * 100) FROM TBYB14_SI_BASELINE,TBYB27_SUSTAINABILITY_DATA WHERE TBYB27_SUSTAINABILITY_DATA.NC_REQUEST_ID_FK = R.NC_REQUEST_ID AND TBYB27_SUSTAINABILITY_DATA.NC_SITE = TBYB14_SI_BASELINE.NC_SITE_ID_FK(+) AND TBYB27_SUSTAINABILITY_DATA.NC_BASELINEYEAR = TBYB14_SI_BASELINE.NC_YEAR(+) ) AS PCT_IMPACT_WASTE,
(SELECT NC_PROPOSEDWATER - NC_EXISTINGWATER FROM TBYB27_SUSTAINABILITY_DATA WHERE NC_REQUEST_ID_FK = R.NC_REQUEST_ID) AS ANNUAL_IMPACT_WATER,
(SELECT DECODE(TBYB14_SI_BASELINE.NC_BASE_WATER, 0, NULL, ((TBYB27_SUSTAINABILITY_DATA.NC_PROPOSEDWATER - TBYB27_SUSTAINABILITY_DATA.NC_EXISTINGWATER) / TBYB14_SI_BASELINE.NC_BASE_WATER) * 100) FROM TBYB14_SI_BASELINE,TBYB27_SUSTAINABILITY_DATA WHERE TBYB27_SUSTAINABILITY_DATA.NC_REQUEST_ID_FK = R.NC_REQUEST_ID AND TBYB27_SUSTAINABILITY_DATA.NC_SITE = TBYB14_SI_BASELINE.NC_SITE_ID_FK(+) AND TBYB27_SUSTAINABILITY_DATA.NC_BASELINEYEAR = TBYB14_SI_BASELINE.NC_YEAR(+) ) AS PCT_IMPACT_WATER,
(SELECT TBXS32_TEXT_SECTIONS.TC_TEXT FROM TBXS32_TEXT_SECTIONS WHERE TBXS32_TEXT_SECTIONS.NC_REQUEST_ID_FK = R.NC_REQUEST_ID AND TBXS32_TEXT_SECTIONS.NC_TEMPLATE_ID_FK = 570 AND NC_SUBSECTION_ID = 0) AS PROPOSAL_TEXT,
(SELECT TBXS32_TEXT_SECTIONS.TC_TEXT FROM TBXS32_TEXT_SECTIONS WHERE TBXS32_TEXT_SECTIONS.NC_REQUEST_ID_FK = R.NC_REQUEST_ID AND TBXS32_TEXT_SECTIONS.NC_TEMPLATE_ID_FK = 1456 AND NC_SUBSECTION_ID = 0) AS COMMENTS_EXPLANATIONS,
(SELECT TBXS32_TEXT_SECTIONS.TC_TEXT FROM TBXS32_TEXT_SECTIONS WHERE TBXS32_TEXT_SECTIONS.NC_REQUEST_ID_FK = R.NC_REQUEST_ID AND TBXS32_TEXT_SECTIONS.NC_TEMPLATE_ID_FK = 1458 AND NC_SUBSECTION_ID = 0) AS COMMENTS_ON_ADD_SI_IMPACTS,
<!--- Get Capital --->
(
SELECT
SUM(NVL((NC_QUANTITY * COST) * (1 + NC_TAX_RATE),0))
FROM
(
SELECT
E.*,
NVL(E.MC_COST,0) * FROM_CURR.NC_EXCHANGE_RATE / TO_CURR.NC_EXCHANGE_RATE COST,
E.MC_MAINT_COST * FROM_CURR.NC_EXCHANGE_RATE / TO_CURR.NC_EXCHANGE_RATE MAINT_COST
FROM
TBXS23_EXPENDITURES E,
TBXS41_EXCHANGE_RATE_CIT FROM_CURR,
TBXS41_EXCHANGE_RATE_CIT TO_CURR,
TBXP78_REQUESTS R,
V_TBXS13_STEPS SS
WHERE
R.NC_ACT_TYPE_ID_FK != 40
AND
R.NC_FORM_STEP_ID_FK = SS.NC_FORM_STEP_ID
AND
E.NC_REQUEST_ID_FK = R.NC_REQUEST_ID
AND
TO_CURR.NC_CURRENCY_ID = <cfqueryparam value="#session.objUser.getCurrencyPreference()#" cfsqltype="cf_sql_numeric">
AND
E.NC_CURRENCY_ID_FK = FROM_CURR.NC_CURRENCY_ID
AND
E.NC_EXPENDITURE_TYPE_ID_FK = 83
AND
R.DC_APPROVAL_DATE
BETWEEN
TO_CURR.DC_VALID_FROM
AND
TO_CURR.DC_VALID_TO
AND
R.DC_APPROVAL_DATE
BETWEEN
FROM_CURR.DC_VALID_FROM
AND
FROM_CURR.DC_VALID_TO
) WHERE NC_REQUEST_ID_FK = R.NC_REQUEST_ID
)AS CAPITAL,
<!--- Get Capital --->
<!--- Project Expense One Time --->
(
SELECT
SUM(NVL((NC_QUANTITY * COST) * (1 + NC_TAX_RATE),0))
FROM
(
SELECT
E.*,
NVL(E.MC_COST,0) * FROM_CURR.NC_EXCHANGE_RATE / TO_CURR.NC_EXCHANGE_RATE COST,
E.MC_MAINT_COST * FROM_CURR.NC_EXCHANGE_RATE / TO_CURR.NC_EXCHANGE_RATE MAINT_COST
FROM
TBXS23_EXPENDITURES E,
TBXS41_EXCHANGE_RATE_CIT FROM_CURR,
TBXS41_EXCHANGE_RATE_CIT TO_CURR,
TBXP78_REQUESTS R,
V_TBXS13_STEPS SS
WHERE
R.NC_ACT_TYPE_ID_FK != 40
AND
R.NC_FORM_STEP_ID_FK = SS.NC_FORM_STEP_ID
AND
E.NC_REQUEST_ID_FK = R.NC_REQUEST_ID
AND
TO_CURR.NC_CURRENCY_ID = <cfqueryparam value="#session.objUser.getCurrencyPreference()#" cfsqltype="cf_sql_numeric">
AND
E.NC_EXPENDITURE_TYPE_ID_FK = 84
AND
E.NC_CURRENCY_ID_FK = FROM_CURR.NC_CURRENCY_ID
AND
R.DC_APPROVAL_DATE
BETWEEN
TO_CURR.DC_VALID_FROM
AND
TO_CURR.DC_VALID_TO
AND
R.DC_APPROVAL_DATE
BETWEEN
FROM_CURR.DC_VALID_FROM
AND
FROM_CURR.DC_VALID_TO
) WHERE NC_REQUEST_ID_FK = R.NC_REQUEST_ID
)AS PROJECT_EXPENSE
<!--- Project Expense One Time --->
FROM
TBXP78_REQUESTS R,
V_TBXS13_STEPS S,
TBXS39_ROI ROI,
TBYB27_SUSTAINABILITY_DATA SD
WHERE
R.NC_FORM_STEP_ID_FK = S.NC_FORM_STEP_ID
AND
R.NC_REQUEST_ID = ROI.NC_REQUEST_ID_FK(+)
AND
R.NC_REQUEST_ID = SD.NC_REQUEST_ID_FK(+)
AND
R.NC_ACT_TYPE_ID_FK != 40
These are the CLOB fields:
(SELECT TBXS32_TEXT_SECTIONS.TC_TEXT FROM TBXS32_TEXT_SECTIONS WHERE TBXS32_TEXT_SECTIONS.NC_REQUEST_ID_FK = R.NC_REQUEST_ID AND TBXS32_TEXT_SECTIONS.NC_TEMPLATE_ID_FK = 570 AND NC_SUBSECTION_ID = 0) AS PROPOSAL_TEXT,
(SELECT TBXS32_TEXT_SECTIONS.TC_TEXT FROM TBXS32_TEXT_SECTIONS WHERE TBXS32_TEXT_SECTIONS.NC_REQUEST_ID_FK = R.NC_REQUEST_ID AND TBXS32_TEXT_SECTIONS.NC_TEMPLATE_ID_FK = 1456 AND NC_SUBSECTION_ID = 0) AS COMMENTS_EXPLANATIONS,
(SELECT TBXS32_TEXT_SECTIONS.TC_TEXT FROM TBXS32_TEXT_SECTIONS WHERE TBXS32_TEXT_SECTIONS.NC_REQUEST_ID_FK = R.NC_REQUEST_ID AND TBXS32_TEXT_SECTIONS.NC_TEMPLATE_ID_FK = 1458 AND NC_SUBSECTION_ID = 0) AS COMMENTS_ON_ADD_SI_IMPACTS,
Related
I have pretty big MySQL query for performance optimization I'm adding subqueries inside a join statement. With raw SQL all working fine. Here is the query:
SELECT
campaigns.id,
campaigns.name,
CONCAT(users.id, ' ', users.email) AS usersData,
CONCAT(campaigns.cpm, ' ', currencies.currency_code) AS cpm,
CONCAT(campaign_budgets.total_spend, ' ', currencies.currency_code) AS total_spend,
creatives.impressionsCount,
creatives.bidsCount,
creatives.winsAmount,
creatives.winsPercentage,
creatives.creativeIds
FROM campaigns
INNER JOIN users ON campaigns.user_id = users.id
INNER JOIN campaign_budgets ON campaigns.id = campaign_budgets.campaign_id
INNER JOIN currencies ON campaigns.currency_type_id = currencies.id
LEFT JOIN (
SELECT
GROUP_CONCAT(creatives.id) as creativeIds,
creatives.campaign_id,
creatives.user_id,
impressions.impressionsCount,
bids.bidsCount,
bids.winsAmount,
bids.winsPercentage
from creatives
LEFT JOIN (
SELECT
count(impressions.id) as impressionsCount,
impressions.user_id,
impressions.creative_id
from impressions
GROUP BY impressions.user_id
) as impressions ON creatives.user_id = impressions.user_id
LEFT JOIN (
SELECT
count(bids.id) as bidsCount,
SUM(CASE WHEN bids.status = 'won' THEN 1 ELSE 0 END) AS winsAmount,
SUM(CASE WHEN bids.status = 'won' THEN 1 ELSE 0 END) / COUNT(bids.id) * 100 AS winsPercentage,
bids.user_id,
bids.creative_id
from bids
GROUP BY bids.user_id
) as bids ON creatives.user_id = bids.user_id
GROUP BY creatives.campaign_id
) as creatives ON campaigns.id = creatives.campaign_id
GROUP BY campaigns.id
and I need convert it to Doctrine DQL somehow if is it possible. I've faced an issue when adding a subquery to join statement. Here is my code:
$columns = [
'campaign.id',
'campaign.name',
'CONCAT(owner.id,\' \', owner.email) as ownerEmail',
'CONCAT(campaign.cpm,\' \', currency.currencyCode) as cpm',
'CONCAT(budget.totalSpend,\' \', currency.currencyCode) as totalSpend',
'COUNT(imp.id) as impressionsCount',
'COUNT(bid.id) as totalBidsCount',
'SUM(case when bid.status = \'won\' then 1 else 0 end) as winsAmount',
'SUM(case when bid.status = \'won\' then 1 else 0 end)/COUNT(bid.id)*100 as winsPercentage',
];
$bids = $this->_em->getRepository(Bid::class)
->createQueryBuilder('bids')
->select([
'count(bids.id) as bidsCount',
'SUM(CASE WHEN bids.status = \'won\' THEN 1 ELSE 0 END) AS winsAmount',
'SUM(CASE WHEN bids.status = \'won\' THEN 1 ELSE 0 END) / COUNT(bids.id) * 100 AS winsPercentage',
'bids.userId',
'bids.creativeId'
])->getDQL();
$impressions = $this->_em->getRepository(Impression::class)
->createQueryBuilder('imp')
->select([
'count(imp.id) as impressionsCount',
'imp.userId',
'imp.creativeId'
])->getDQL();
$creative = $this->_em->getRepository(Creative::class) ->createQueryBuilder('cr')->select('cr.id')
->select([
'GROUP_CONCAT(cr.id) as creativeIds',
'cr.campaignId',
'cr.userId',
'impressions.impressionsCount',
'bids.bidsCount',
'bids.winsAmount',
'bids.winsPercentage'
])
->leftJoin(Impression::class, sprintf('(%s) as imp', $impressions), Expr\Join::WITH, 'imp.id = cr.userId')
->leftJoin(Bid::class, sprintf('(%s) as bid', $bids), Expr\Join::WITH, 'bids.id = cr.userId')
->getDQL();
$query = $this->createQueryBuilder('campaign')
->select($columns);
$query
->join('campaign.user', 'owner')
->join('campaign.campaignBudget', 'budget')
->join('campaign.currencyType', 'currency')
->leftJoin(Creative::class, sprintf('(%s) as creative', $creative), Expr\Join::WITH, 'campaign.id = cr.campaignId');
$query->groupBy('campaign.id');
$query->setMaxResults($limit);
$query->setFirstResult($offset);
return $query->getQuery()->useQueryCache(true)->getResult();
I'm getting the error [Syntax Error] line 0, col 626: Error: Expected Doctrine\ORM\Query\Lexer::T_IDENTIFIER, got '(', issue is in adding subquery to join statement.
I would appreciate any help!!
Okay I found a solution using DBAL instead of DQL using this as a reference
I am very new at triggers - actually this will be my first one.
What I am trying to achieve is the following:
When I create a new Sales Opportunity, the Quote no field needs to spit out a number starting from this Q1500/09/2017 so Q is the prefix, 1500 is the first quote number, 09 the month and 2017 the year.
Can someone assist me in getting this right - do i need a SP and a table in order for my trigger to work correctly?
Quote_No is a table field so I'm guessing it should look something like this:
CREATE TABLE [dbo].[Quote_Pref_No](
[Yr] [varchar](4) NULL,
[Mth] [varchar](2) NULL,
[Quote_Pref] [varchar](10) NULL,
[Quote_No] [int] NULL
) ON [PRIMARY]
Then the SP:
Create Proc [dbo].[New_Quote_Num] #ClientID varchar(24),#ContactNum int, #Year varchar(4), #mth varchar(2)
As
Begin
Declare #Pref varchar(5),
#Num int,
#QuoteNo varchar(255)
Select #QuoteNo = B.Description
From dbo.AMGR_User_Fields_Tbl A inner join dbo.AMGR_User_Field_Defs_Tbl B
on A.Type_Id = B.Type_Id and A.Code_Id = B.Code_Id and A.Type_Id = 117
Where A.Client_Id = #ClientId and A.Contact_Number = #ContactNum
Select #Pref = Quote_Pref, #Num = Quote_No + 1
From dbo.Quote_Pref_No
Where Yr = #Year and Mth = #Mth
Set #QuoteNum = 'Q/'+Right(#Year,2)+'/'+Right(#mth,2)+'/'+#Pref+case when Len(#Num) <= 3 then Right('000000'+Convert(Varchar(55),#Num),3) else CONVERT(Varchar(55),#Num) end
Insert into dbo.O_QuoteNo(Client_Id,Contact_Number,Type_Id,Code_Id,O_QuoteNo)
Values(#ClientId,#ContactNum,13,0,#QuoteNum)
Update dbo.Quote_Pref_No
Set Quote_No = #Num
Where Yr = #Year and mth = #mth
End
Grant execute on dbo.New_Quote_Num to CRMGroup
GO
I just dont know how to generate the month and year together.
Trigger code:
Create Trigger [dbo].[QuoteNew]
on [dbo].[AMGR_User_Fields_Tbl] after insert
As
Declare #ClientId varchar(24),
#ContactNum int,
#TypeId int,
#Year varchar(4),
#quoteYear varchar(4),
#mth varchar(2)
Select #ClientId = Client_Id,
#ContactNum = Contact_Number,
#TypeId = TYPE_ID,
#Year = CONVERT(varchar(4),Datepart(yyyy,DateCol)) from inserted
#mth = convert(varchar(2), datepart(mm,Datecol)) from inserted
Select #quoteYear = Yr from dbo.quote_Pref_No where Yr = #Year
select #mth = mth from dbo.quote_Pref_No where mth = #mth
If #TypeID = 117 and #QuoteYear is null or #quoteYear = ''
Begin
Execute dbo.New_Year_Certs #Year
Execute dbo.New_quote_Num #ClientId,#ContactNum,#Year
End
If #TypeID = 117 and #quoteYear = #Year
Begin
Execute dbo.New_Cert_Num #ClientId,#ContactNum,#Year
End
GO
Do I need to create a separate SP for month or can I do both month and year in one SP?
In database I have partitioning table by column 'status' for better performance. My database administrator ask me about put in query value for that column directly in sql (not bind by parameter).
I can change binding by set hint QueryHints.BIND_PARAMETERS on false, but then all parameters are inside sql.
Can I set not bind only on 'status' parameter ?
Example result when BIND_PARAMETERS = true
SELECT t0.* FROM S_JOBS_ORG_UNIT_CFG t0
WHERE ((((t0.ORG_ID = ?) AND (t0.SCHEDULER_NEXT_ACTIVATION < SYSDATE)) AND (t0.ACTIVE = ?))
AND NOT EXISTS (SELECT ? FROM S_JOBS t1 WHERE (((t1.ORDER_ID = t0.ORDER_ID) AND (t1.ORG_ID = t0.ORG_ID)) AND NOT ((t1.STATUS = ?)))) )
bind => [472100, Y, 1, E]
and result when BIND_PARAMETERS = false
SELECT t0.* FROM S_JOBS_ORG_UNIT_CFG t0
WHERE ((((t0.ORG_ID = 472100) AND (t0.SCHEDULER_NEXT_ACTIVATION < SYSDATE)) AND (t0.ACTIVE = Y))
AND NOT EXISTS (SELECT 1 FROM S_JOBS t1 WHERE (((t1.ORDER_ID = t0.ORDER_ID) AND (t1.ORG_ID = t0.ORG_ID)) AND NOT ((t1.STATUS = E)))) )
Code:
Query jobOrgUnitCfgQuery = entityManager.createQuery(
"SELECT c FROM JobOrgUnitCfg c WHERE c.orgId = :orgId and c.schedulerNextActivation < current_timestamp and c.active = :active and " +
" not exists (SELECT j FROM Job j WHERE j.orderId = c.orderId and j.orgId = c.orgId and j.status <> 'E')");
jobOrgUnitCfgQuery.setParameter("orgId", orgId);
jobOrgUnitCfgQuery.setParameter("active", Boolean.TRUE);
return jobOrgUnitCfgQuery.getResultList();
I think your best bet is just to programmatically build your query like you're doing with a hard coded status and escape the other paramaters manually to avoid SQL Injection.
My Requirement is to write a sql query to get the sub-region wise (fault)events count that occurred for the managedobjects. My database is postgres 8.4. Let me explain using the table structure.
My tables in django:
Managedobject:
class Managedobject(models.Model):
name = models.CharField(max_length=200, unique=True)
iscontainer = models.BooleanField(default=False,)
parentkey = models.ForeignKey('self', null=True)
Event Table:
class Event(models.Model):
Name = models.CharField(verbose_name=_('Name'))
foid = models.ForeignKey(Managedobject)
Managedobject Records:
NOC
Chennai
MO_1
MO_2
MO_3
Mumbai
MO_4
MO_5
MO_6
Delhi
Bangalore
IP
Calcutta
Cochin
Events Records:
event1 MO_1
event2 MO_2
event3 MO_3
event4 MO_5
event5 MO_6
Now I need to get the events count for all the sub-regions. For example,
for NOC region:
Chennai - 3
Mumbai - 2
Delhi - 0
Bangalore - 0
So far I am able to get the result in two different queries.
Get the subregions.
select id from managedobject where iscontainer = True and parentkey = 3489
For each of the region (using for loop), get the count as follows:
SELECT count(*)
from event ev
WHERE ev.foid
IN (
WITH RECURSIVE q AS (
SELECT h
FROM managedobject h
WHERE parentkey = 3489
UNION ALL
SELECT hi
FROM q
JOIN managedobject hi
ON hi.parentkey = (q.h).id
)
SELECT (q.h).id FROM q
)
Please help to combine the queries to make it a single query and for getting the top 5 regions. Since the query is difficult in django, I am going for a raw sql query.
I got the query:
WITH RECURSIVE q AS (
SELECT h,
1 AS level,
id AS ckey,
displayname as dname
FROM managedobject h
WHERE parentkey = 3489
and logicalnode=True
UNION ALL
SELECT hi,
q.level + 1 AS level,
ckey,
dname
FROM q
JOIN managedobject hi ON hi.parentkey = (q.h).id
)
SELECT count(ckey) as ccount,
ckey,
dname
FROM q
JOIN event as ev on ev.foid_id = (q.h).id
GROUP BY ckey, dname
ORDER BY ccount DESC
LIMIT 5
I've got the below MySQL query, it's causing an error, the error is below too.
SELECT DISTINCT s.id as id, s.auctioneer as auctioneer, s.advertType as advertType, s.saleType as saleType, an.name as auctioneerName, st.entryCopy as saleTypeName, at.entryCopy as advertTypeName, s.heading AS heading, sl.city AS city, sd.id AS sdId, sd.startDate AS startDate
FROM sales s LEFT JOIN saleloc sl ON sl.saleId = s.id LEFT JOIN saledates sd ON sd.saleLoc = sl.id,
auctioneers an,
lookupcopy st,
lookupcopy at
#replace(findWhere,"''","'","all")# AND
s.id = sd.saleId AND sl.saleId = s.id
AND an.id = s.auctioneer
AND st.id = s.saleType
AND at.id = s.advertType
GROUP BY id
ORDER BY startDate, auctioneerName, city
Error from database
SELECT DISTINCT s.id as id, s.auctioneer as auctioneer, s.advertType as advertType, s.saleType as saleType, an.name as auctioneerName, st.entryCopy as saleTypeName, at.entryCopy as advertTypeName, s.heading AS heading, sl.city AS city, sd.id AS sdId, sd.startDate AS startDate
FROM sales s
LEFT JOIN saleloc sl ON sl.saleId = s.id
LEFT JOIN saledates sd ON sd.saleLoc = sl.id, auctioneers an, lookupcopy st, lookupcopy at
'WHERE s.advertType > 0
AND s.saleType > 0
AND sl.region = "2" '
AND s.id = sd.saleId
AND sl.saleId = s.id
AND an.id = s.auctioneer
AND st.id = s.saleType
AND at.id = s.advertType
GROUP BY id
ORDER BY startDate, auctioneerName, city
I didn't write this code and I'm not sure why the #Replace()# is being used, can anyone see how to fix the syntax error it's causing?
Before the query code, do a replace as follows:
<cfset findWhere = Replace(findWhere, "''", "'", "ALL")#
<cfif Left(findWhere, 1) EQ "'">
<cfset findWhere = Right(findWhere, Len(findWhere) - 1)>
</cfif>
<cfif Right(findWhere, 1) EQ "'">
<cfset findWhere = Left(findWhere, Len(findWhere) - 1)>
</cfif>
<cfquery name="qry" datasource="mysql">
SELECT DISTINCT s.id as id, s.auctioneer as auctioneer, s.advertType as advertType, s.saleType as saleType, an.name as auctioneerName, st.entryCopy as saleTypeName, at.entryCopy as advertTypeName, s.heading AS heading, sl.city AS city, sd.id AS sdId, sd.startDate AS startDate
FROM sales s
LEFT JOIN saleloc sl ON sl.saleId = s.id
LEFT JOIN saledates sd ON sd.saleLoc = sl.id,
auctioneers an,
lookupcopy st,
lookupcopy at
#findWhere# AND
s.id = sd.saleId AND sl.saleId = s.id
AND an.id = s.auctioneer
AND st.id = s.saleType
AND at.id = s.advertType
GROUP BY id
ORDER BY startDate, auctioneerName, city
</cfquery>
The value stored in findWhere includes single-quotes at the beginning and end of the string.
On another note: Unless you created findWhere without any direct user input value, then you need to secure it.
Better to do:
...
WHERE 1= 1
<cfif listFind( 'foo' , findWhere )>
foo= 2
<cfelseif listFind( 'bar' , findWhere )>
bar= 209
</cfif>
...
Just to clarify, I don't believe you can do a distinct and a group by statement in the same query.
They both do the same kind of thing, but for different reasons.