SELECT datecreated,
totcount
FROM (SELECT REPLACE(RIGHT(CONVERT(VARCHAR(9), tof.create_stamp, 6), 6), ' ', ' ') AS datecreated,
COUNT(DISTINCT tof.unique_id) AS totcount
FROM trn_open_files tof
JOIN trn_debtor tb
ON ( tof.unique_id = tb.unique_id
AND tb.debtor_seq_num = 1
AND tb.rec_status = 'R' )
JOIN trn_property tp
ON ( tof.unique_id = tp.unique_id )
LEFT OUTER JOIN trn_parties prty_cccs
ON ( prty_cccs.unique_id = tof.unique_id
AND prty_cccs.party_role = 'CCCS'
AND prty_cccs.rec_status = 'A' )
LEFT OUTER JOIN trn_parties prty_srv
ON ( prty_srv.unique_id = tof.unique_id
AND prty_srv.party_role = 'SRV'
AND prty_srv.rec_status = 'A' )
LEFT OUTER JOIN trn_parties prty_vend
ON ( prty_vend.unique_id = tof.unique_id
AND prty_vend.party_role = 'CLMO'
AND prty_vend.rec_status = 'A' )
LEFT OUTER JOIN trn_parties prty_ins
ON ( prty_ins.unique_id = tof.unique_id
AND prty_ins.party_role = 'INS'
AND prty_ins.rec_status = 'A' )
LEFT OUTER JOIN trn_parties prty_ss
ON ( prty_ss.unique_id = tof.unique_id
AND prty_ss.party_role = 'SS'
AND prty_ss.rec_status = 'A' )
LEFT OUTER JOIN trn_parties prty_realtor
ON ( prty_realtor.unique_id = tof.unique_id
AND prty_realtor.party_role = 'REALTOR'
AND prty_realtor.rec_status = 'A' )
LEFT OUTER JOIN mst_comp_info mci1
ON ( mci1.comp_id = prty_cccs.party_comp_id
AND mci1.comp_type = 'CCCS'
AND mci1.rec_status = 'A' )
LEFT OUTER JOIN mst_comp_info mci
ON ( mci.comp_id = prty_srv.party_comp_id
AND mci.comp_type = 'SRV'
AND mci.rec_status = 'A'
AND mci.pilot_flag = 'Y' )
LEFT OUTER JOIN mst_comp_info mci2
ON ( mci2.comp_id = prty_vend.party_comp_id
AND mci2.comp_type = 'VEND'
AND mci2.rec_status = 'A' )
LEFT OUTER JOIN mst_comp_info mci3
ON ( mci3.comp_id = prty_ins.party_comp_id
AND mci3.comp_type = 'INS'
AND mci3.rec_status = 'A' )
LEFT OUTER JOIN mst_comp_info mci4
ON ( mci4.comp_id = prty_ss.party_comp_id
AND mci4.comp_type = 'SS'
AND mci4.rec_status = 'A' )
LEFT OUTER JOIN mst_comp_info mci5
ON ( mci5.comp_id = prty_realtor.party_comp_id
AND mci4.comp_type = 'REALTOR'
AND mci4.rec_status = 'A' )
LEFT OUTER JOIN mst_status_codes i
ON ( i.status_code = tof.case_status
AND i.comp_id = tof.comp_id
AND ( i.party_role IS NULL
OR i.party_role = 'VEND' )
AND tof.file_type = i.file_type )
WHERE tof.cur_status = 'A'
AND tof.cms_flag = 'Y'
AND CONVERT(DATE, tof.create_stamp) >= CONVERT(DATE, '2010-06-06')
AND CONVERT(DATE, tof.create_stamp) <= CONVERT(DATE, '2011-10-01')
AND prty_cccs.party_comp_id = 10153
AND prty_cccs.party_comp_id = 10153
GROUP BY REPLACE(RIGHT(CONVERT(VARCHAR(9), tof.create_stamp, 6), 6), ' ', ' '),
prty_cccs.party_comp_id) q1
having this procedure how to show month in proper sequence.
now month is appear as
out put: DateCreated totcount
Sep 11 7
May 11 2
Jun 11 10
Jul 11 40
Aug 11 144
i want month in sequence like DateCreated totcount
May 11 2
Jun 11 10
Jul 11 40
Aug 11 144
Sep 11 7
Not tested but I think you should replace REPLACE(RIGHT(CONVERT(VARCHAR(9), tof.create_stamp, 6), 6), ' ', ' ') with dateadd(month, datediff(month, 0, tof.create_stamp), 0) and do the presentation thing in the outer query. And adding an order by to the outer query ORDER BY q1.datecreated.
SELECT REPLACE(RIGHT(CONVERT(VARCHAR(9), datecreated, 6), 6), ' ', ' '),
totcount
FROM (SELECT Dateadd(MONTH, Datediff(MONTH, 0, tof.create_stamp), 0) AS datecreated,
COUNT(DISTINCT tof.unique_id) AS totcount
FROM trn_open_files tof
JOIN trn_debtor tb
ON ( tof.unique_id = tb.unique_id
AND tb.debtor_seq_num = 1
AND tb.rec_status = 'R' )
JOIN trn_property tp
ON ( tof.unique_id = tp.unique_id )
LEFT OUTER JOIN trn_parties prty_cccs
ON ( prty_cccs.unique_id = tof.unique_id
AND prty_cccs.party_role = 'CCCS'
AND prty_cccs.rec_status = 'A' )
LEFT OUTER JOIN trn_parties prty_srv
ON ( prty_srv.unique_id = tof.unique_id
AND prty_srv.party_role = 'SRV'
AND prty_srv.rec_status = 'A' )
LEFT OUTER JOIN trn_parties prty_vend
ON ( prty_vend.unique_id = tof.unique_id
AND prty_vend.party_role = 'CLMO'
AND prty_vend.rec_status = 'A' )
LEFT OUTER JOIN trn_parties prty_ins
ON ( prty_ins.unique_id = tof.unique_id
AND prty_ins.party_role = 'INS'
AND prty_ins.rec_status = 'A' )
LEFT OUTER JOIN trn_parties prty_ss
ON ( prty_ss.unique_id = tof.unique_id
AND prty_ss.party_role = 'SS'
AND prty_ss.rec_status = 'A' )
LEFT OUTER JOIN trn_parties prty_realtor
ON ( prty_realtor.unique_id = tof.unique_id
AND prty_realtor.party_role = 'REALTOR'
AND prty_realtor.rec_status = 'A' )
LEFT OUTER JOIN mst_comp_info mci1
ON ( mci1.comp_id = prty_cccs.party_comp_id
AND mci1.comp_type = 'CCCS'
AND mci1.rec_status = 'A' )
LEFT OUTER JOIN mst_comp_info mci
ON ( mci.comp_id = prty_srv.party_comp_id
AND mci.comp_type = 'SRV'
AND mci.rec_status = 'A'
AND mci.pilot_flag = 'Y' )
LEFT OUTER JOIN mst_comp_info mci2
ON ( mci2.comp_id = prty_vend.party_comp_id
AND mci2.comp_type = 'VEND'
AND mci2.rec_status = 'A' )
LEFT OUTER JOIN mst_comp_info mci3
ON ( mci3.comp_id = prty_ins.party_comp_id
AND mci3.comp_type = 'INS'
AND mci3.rec_status = 'A' )
LEFT OUTER JOIN mst_comp_info mci4
ON ( mci4.comp_id = prty_ss.party_comp_id
AND mci4.comp_type = 'SS'
AND mci4.rec_status = 'A' )
LEFT OUTER JOIN mst_comp_info mci5
ON ( mci5.comp_id = prty_realtor.party_comp_id
AND mci4.comp_type = 'REALTOR'
AND mci4.rec_status = 'A' )
LEFT OUTER JOIN mst_status_codes i
ON ( i.status_code = tof.case_status
AND i.comp_id = tof.comp_id
AND ( i.party_role IS NULL
OR i.party_role = 'VEND' )
AND tof.file_type = i.file_type )
WHERE tof.cur_status = 'A'
AND tof.cms_flag = 'Y'
AND CONVERT(DATE, tof.create_stamp) >= CONVERT(DATE, '2010-06-06')
AND CONVERT(DATE, tof.create_stamp) <= CONVERT(DATE, '2011-10-01')
AND prty_cccs.party_comp_id = 10153
AND prty_cccs.party_comp_id = 10153
GROUP BY Dateadd(MONTH, Datediff(MONTH, 0, tof.create_stamp), 0),
prty_cccs.party_comp_id) q1
ORDER BY q1.datecreated
Related
I have a query that is giving me different results when i enclosed it in Parentheses, however when I run it without Parentheses its giving me different results. I want to apply Union in between so I have to use Parentheses as without union doesn't work.
The query is as follows:
SELECT Distinct
recurring_billing.id,
recurring_billing.kid_id,
recurring_billing.class_id,
recurring_billing.app_id,
recurring_billing.Region_ID,
CC.month_name,
CC.billing_year,
CASE
WHEN
CC.month_name = recurring_billing.billing_month
AND CC.billing_year = recurring_billing.billing_year
THEN
recurring_billing.billing_status
ELSE
'Pending'
END
AS billing_status, tblkids.kid_name, tblkids.kid_Lastname, tblkids.kid_EMail, tbl_app.app_CCExp AS cc_exp, tbl_app.app_CCName AS cc_name, tbl_app.app_CCNumber AS cc_number, tbl_app.app_CCType AS cc_type, tblclasses.cla_EndDate, tblclassdays.classday_day, CC.remainingclasses, tbl_app.cost_per_class, CC.remainingclasses * tbl_app.cost_per_class AS cost_amount,
CASE
WHEN
OP.override_amt IS NOT NULL
THEN
OP.override_amt
ELSE
CC.remainingclasses * tbl_app.cost_per_class
END
AS pmt_amount,
CASE
WHEN
OP.process_payment = False
THEN
OP.process_payment
ELSE
True
END
AS process_payment
FROM
recurring_billing
LEFT JOIN
tblkidsxclass
ON recurring_billing.kid_id = tblkidsxclass.kxc_kidid
AND recurring_billing.app_id = tblkidsxclass.kxc_appid
LEFT JOIN
tbl_app
ON recurring_billing.app_ID = tbl_app.app_ID
LEFT JOIN
tblkids
ON recurring_billing.kid_id = tblkids.kid_ID
LEFT JOIN
tblclasses
ON recurring_billing.class_id = tblclasses.cla_ID
LEFT JOIN
tblclassdays
ON tblclasses.cla_ID = tblclassdays.classday_classID
INNER JOIN
(
SELECT
MONTHNAME(classday_day) AS month_name,
YEAR(classday_day) AS billing_year,
cla_ID,
COUNT(classday_classid) AS remainingclasses,
c.cost_per_class AS cost_per_class,
COUNT(classday_classid) * cost_per_class AS TotalClassCost
FROM
tblclassdays
JOIN
(
SELECT
tblclasses.cla_ID,
tblclasses.cost_per_class,
tblclasses.cla_nextclass AS next1,
tblclasses_1.cla_nextclass AS next2
FROM
tblclasses
LEFT JOIN
tblclasses AS tblclasses_1
ON tblclasses.cla_nextclass = tblclasses_1.cla_ID
WHERE
tblclasses.cla_ID IN
(
SELECT DISTINCT
recurring_billing.class_id
FROM
recurring_billing
LEFT JOIN
tblkidsxclass
ON recurring_billing.kid_id = tblkidsxclass.kxc_kidid
AND recurring_billing.app_id = tblkidsxclass.kxc_appid -- LEFT JOIN tblkids ON recurring_billing.kid_id = tblkids.kid_ID
LEFT JOIN
tblclasses
ON recurring_billing.class_id = tblclasses.cla_ID -- LEFT JOIN tblclassdays ON tblclasses.cla_ID = tblclassdays.classday_classID
WHERE
ISNULL(tblkidsxclass.kxc_dropoutdate)
AND tblkidsxclass.pmt_option = 'Recurring'
AND tblclasses.cla_active = TRUE -- AND tblclasses.cla_EndDate >= NOW()
GROUP BY
recurring_billing.id
)
)
c
ON tblclassdays.classday_classid IN
(
c.cla_ID,
c.next1,
c.next2
)
WHERE
tblclassdays.classday_noclass = FALSE
AND MONTH(classday_day) = 11
AND YEAR(classday_day) = 2020
AND
(
CONCAT(CAST(classday_day AS DATE), ' ', CAST(classday_endtime AS TIME)) > CAST(CURDATE() AS DATETIME)
)
GROUP BY
cla_ID
)
CC
ON CC.cla_ID = tblclassdays.classday_classID
LEFT JOIN
override_payments OP
ON recurring_billing.app_id = OP.app_id
AND recurring_billing.kid_id = OP.kid_id
AND CC.month_name = OP.billing_month
AND CC.billing_year = OP.billing_year
WHERE
ISNULL(tblkidsxclass.kxc_dropoutdate)
AND tblkidsxclass.pmt_option = 'Recurring'
AND tblclasses.cla_active = TRUE
AND recurring_billing.kid_id NOT IN
(
SELECT
kid_id
from
recurring_billing
where
billing_month = 'November'
and billing_year = '2020'
)
-- AND tblclasses.cla_EndDate >= NOW()
GROUP BY
recurring_billing.id
ORDER BY
recurring_billing.id ASC
Same query if enclosed in Parentheses will give me different results.
Parentheses are not required for a UNION statement. The following 2 union statements both work and yield the same results.
SELECT 1 UNION SELECT 2;
(SELECT 1) UNION (SELECT 2);
Our website sometimes causes extreme server strain due to a complex MySQL query. The site actually goes down.
The webhoster warned that if we don't get this in order they will suspend our account.
Could someone give some pointers at which parts of this query eat the most resources?
Any suggestions on making this better?
SELECT SQL_CALC_FOUND_ROWS wp_s3mv0r_posts.ID
FROM wp_s3mv0r_posts
INNER JOIN wp_s3mv0r_term_relationships ON (wp_s3mv0r_posts.ID = wp_s3mv0r_term_relationships.object_id)
INNER JOIN wp_s3mv0r_postmeta ON ( wp_s3mv0r_posts.ID = wp_s3mv0r_postmeta.post_id )
INNER JOIN wp_s3mv0r_postmeta AS mt1 ON ( wp_s3mv0r_posts.ID = mt1.post_id )
INNER JOIN wp_s3mv0r_postmeta AS mt2 ON ( wp_s3mv0r_posts.ID = mt2.post_id )
INNER JOIN wp_s3mv0r_postmeta AS mt3 ON ( wp_s3mv0r_posts.ID = mt3.post_id )
INNER JOIN wp_s3mv0r_postmeta AS mt4 ON ( wp_s3mv0r_posts.ID = mt4.post_id )
INNER JOIN wp_s3mv0r_postmeta AS mt5 ON ( wp_s3mv0r_posts.ID = mt5.post_id )
INNER JOIN wp_s3mv0r_postmeta AS mt6 ON ( wp_s3mv0r_posts.ID = mt6.post_id )
INNER JOIN wp_s3mv0r_postmeta AS mt7 ON ( wp_s3mv0r_posts.ID = mt7.post_id )
INNER JOIN wp_s3mv0r_postmeta AS mt8 ON ( wp_s3mv0r_posts.ID = mt8.post_id )
INNER JOIN wp_s3mv0r_postmeta AS mt9 ON ( wp_s3mv0r_posts.ID = mt9.post_id )
INNER JOIN wp_s3mv0r_postmeta AS mt10 ON ( wp_s3mv0r_posts.ID = mt10.post_id )
INNER JOIN wp_s3mv0r_postmeta AS mt11 ON ( wp_s3mv0r_posts.ID = mt11.post_id )
INNER JOIN wp_s3mv0r_postmeta AS mt12 ON ( wp_s3mv0r_posts.ID = mt12.post_id )
WHERE 1=1 AND (
wp_s3mv0r_term_relationships.term_taxonomy_id IN (11,24,25)
) AND (
( wp_s3mv0r_postmeta.meta_key = 'acf_house_minprice' AND CAST(wp_s3mv0r_postmeta.meta_value AS SIGNED) BETWEEN '274990' AND '599990' )
AND
( mt1.meta_key = 'acf_house_minlotwidth' AND CAST(mt1.meta_value AS SIGNED) BETWEEN '16' AND '16' )
AND
( mt2.meta_key = 'acf_location_area' AND CAST(mt2.meta_value AS CHAR) IN ('South of river') )
AND
( mt3.meta_key = 'acf_house_bedroom' AND CAST(mt3.meta_value AS CHAR) = '4' )
AND
( mt4.meta_key = 'acf_house_studyroom' AND CAST(mt4.meta_value AS SIGNED) > '0' )
AND
( mt5.meta_key = 'acf_house_theaterroom' AND CAST(mt5.meta_value AS SIGNED) > '0' )
AND
( mt6.meta_key = 'acf_house_alfresco' AND CAST(mt6.meta_value AS SIGNED) > '0' )
AND
( mt7.meta_key = 'acf_house_activityroom' AND CAST(mt7.meta_value AS SIGNED) > '0' )
AND
( mt8.meta_key = 'acf_house_doublegarage' AND CAST(mt8.meta_value AS SIGNED) > '0' )
AND
( mt9.meta_key = 'acf_house_reargarage' AND CAST(mt9.meta_value AS SIGNED) > '0' )
AND
( mt10.meta_key = 'acf_house_islbeninkitchen' AND CAST(mt10.meta_value AS SIGNED) > '0' )
AND
( mt11.meta_key = 'acf_house_frontmasterbedroom' AND CAST(mt11.meta_value AS SIGNED) > '0' )
AND
( mt12.meta_key = 'acf_house_rearmaster' AND CAST(mt12.meta_value AS SIGNED) > '0' )
) AND wp_s3mv0r_posts.post_type = 'house' AND (wp_s3mv0r_posts.post_status = 'publish')
GROUP BY wp_s3mv0r_posts.ID
ORDER BY wp_s3mv0r_posts.post_date DESC
LIMIT 0, 10
How about this:
SELECT p.ID, count(*) AS 'PostCount'
FROM wp_s3mv0r_posts p INNER JOIN wp_s3mv0r_term_relationships r ON (p.ID = r.object_id)
INNER JOIN wp_s3mv0r_postmeta pm ON (p.ID = pm.post_id)
WHERE
r.term_taxonomy_id IN (11,24,25) AND p.post_type = 'house' AND p.post_status = 'publish' AND (
(pm.meta_key = 'acf_house_minprice' AND pm.meta_value BETWEEN '274990' AND '599990') OR
(pm.meta_key = 'acf_house_minlotwidth' AND pm.meta_value BETWEEN '16' AND '16') OR
(pm.meta_key = 'acf_location_area' AND pm.meta_value = 'South of river') OR
(pm.meta_key = 'acf_house_bedroom' AND pm.meta_value = '4') OR
(pm.meta_key IN ('acf_house_studyroom', 'acf_house_theaterroom', 'acf_house_alfresco', 'acf_house_activityroom',
'acf_house_doublegarage', 'acf_house_reargarage', 'acf_house_islbeninkitchen', 'acf_house_frontmasterbedroom',
'acf_house_rearmaster') AND pm.meta_value > '0'))
GROUP BY p.ID
ORDER BY p.post_date DESC
LIMIT 0, 10
Hello I have a query where I am getting counts of records that are group by month (see example result below). What I would like SQL to do is have an additional Total row at the bottom which will be a sum of all the values in the "AccountID" column. I have highlighted the "Total" row where I would this addition to my current result set. Appreciate any help!!
Below is my current query I am using to gather the data:
SELECT Distinct
Month = MONTH(pv.VisitStartDateTime)
,AccountID_Count = count(pv.AccountID)
FROM hpavisit pv With ( NOLOCK )
left outer join HPat pt With ( NOLOCK )
ON pv.Pat_OID = pt.ObjectID
and pt.isdeleted = 0
Left Outer Join HCareUnit HCU With ( NOLOCK )
on HCU.ObjectID = pv.PatLocation_oid
Left Outer Join HCareUnit HCU1 With ( NOLOCK )
on HCU1.ObjectID = pv.UnitContacted_oid
WHERE pv.PatLocation_oid <> 0 and pv.PatLocation_oid is not null
and pv.IsDeleted = 0
and pv.VisitStartDateTime > '08/31/2014 23:23:59'
and pv.VisitStartDateTime < '03/01/2015'
GROUP BY MONTH(pv.VisitStartDateTime)
On SQL2008 you can add sumary row
SELECT Distinct
Month = MONTH(pv.VisitStartDateTime)
,AccountID_Count = count(pv.AccountID)
FROM hpavisit pv With ( NOLOCK )
left outer join HPat pt With ( NOLOCK )
ON pv.Pat_OID = pt.ObjectID
and pt.isdeleted = 0
Left Outer Join HCareUnit HCU With ( NOLOCK )
on HCU.ObjectID = pv.PatLocation_oid
Left Outer Join HCareUnit HCU1 With ( NOLOCK )
on HCU1.ObjectID = pv.UnitContacted_oid
WHERE pv.PatLocation_oid <> 0 and pv.PatLocation_oid is not null
and pv.IsDeleted = 0
and pv.VisitStartDateTime > '08/31/2014 23:23:59'
and pv.VisitStartDateTime < '03/01/2015'
GROUP BY MONTH(pv.VisitStartDateTime)
Union ALL
SELECT Distinct
'TOTAL',
,AccountID_Count = count(pv.AccountID)
FROM hpavisit pv With ( NOLOCK )
left outer join HPat pt With ( NOLOCK )
ON pv.Pat_OID = pt.ObjectID
and pt.isdeleted = 0
Left Outer Join HCareUnit HCU With ( NOLOCK )
on HCU.ObjectID = pv.PatLocation_oid
Left Outer Join HCareUnit HCU1 With ( NOLOCK )
on HCU1.ObjectID = pv.UnitContacted_oid
WHERE pv.PatLocation_oid <> 0 and pv.PatLocation_oid is not null
and pv.IsDeleted = 0
and pv.VisitStartDateTime > '08/31/2014 23:23:59'
and pv.VisitStartDateTime < '03/01/2015'
On SQL20008R2 you can try:
SELECT Distinct
Month = MONTH(pv.VisitStartDateTime)
,AccountID_Count = count(pv.AccountID)
FROM hpavisit pv With ( NOLOCK )
left outer join HPat pt With ( NOLOCK )
ON pv.Pat_OID = pt.ObjectID
and pt.isdeleted = 0
Left Outer Join HCareUnit HCU With ( NOLOCK )
on HCU.ObjectID = pv.PatLocation_oid
Left Outer Join HCareUnit HCU1 With ( NOLOCK )
on HCU1.ObjectID = pv.UnitContacted_oid
WHERE pv.PatLocation_oid <> 0 and pv.PatLocation_oid is not null
and pv.IsDeleted = 0
and pv.VisitStartDateTime > '08/31/2014 23:23:59'
and pv.VisitStartDateTime < '03/01/2015'
GROUP GROUP BY GROUPING SETS(
(MONTH(pv.VisitStartDateTime),
())
I've a query like this which is working fine but I want to create a pure CI query from this. I'm new in CI so can anyone help me to figure out how this can be done or suggest me a reference website or link from where i can get help. Thanks
SELECT
user_des,
IFNULL((sent),0) as sent,
IFNULL((viewed),0) as viewed,
CONCAT(IFNULL(fname_usr,user_des),' ',lname_usr,user_des) as fullname,
IFNULL(sum(duration),0) as totalviewtime,
IFNULL((totalviews),0) as views,
IFNULL(sum(sharedcount),0) as shares,
IFNULL(sum(shared),0) as shared
FROM dem_senddemo
INNER JOIN
(SELECT * FROM dem_demos WHERE id_dem IN (746,943,748) AND customer_dem = '1') as demosfiltered
ON demo_des = id_dem
LEFT JOIN
(
(
Select senddemo_wis, sum(duration) as duration, max(datereceived_wis) as datereceived_wis
FROM
(
select senddemo_wis, invited_wis, sum(IFNULL(duration_fea,0) * IFNULL(percentviewed_wis,0)) as duration, max(datereceived_wis) as datereceived_wis
FROM (sta_views
LEFT JOIN sta_featureswisitavisits as s ON visit_fvi = id_vis
LEFT JOIN
(
(SELECT shortvideovid_fea as videoid_fea, shortvideoduration_fea as duration_fea
FROM dem_features
where shortvideoduration_fea > 0)
UNION
(SELECT longvideovid_fea as videoid_fea, longvideoduration_fea as duration_fea
FROM dem_features
where longvideoduration_fea > 0)
) as x
ON videoid_fea = showedvideo_fvi)
LEFT JOIN
sta_wistia as w ON invited_fvi = invited_wis AND s.showedvideo_fvi = w.mediaid_wis AND wistia_vis = email_wis
WHERE countvisit_wis <> 'N'
GROUP BY invited_wis
) as durfea
GROUP BY senddemo_wis
) UNION
(
SELECT senddemo_wis,sum(IFNULL(mainvideoduration_dem ,0) * IFNULL(percentviewed_wis,0)) as duration, max(datereceived_wis) as datereceived_wis
FROM sta_wistia as w
INNER JOIN
(select *, customer_dem as cuss FROM dem_demos) as demcorp ON demo_wis = id_dem AND mainmessagingvideovid_dem = w.mediaid_wis
LEFT JOIN sta_views ON email_wis = wistia_vis
WHERE customer_dem = 1 AND senddemo_wis > 0 AND countvisit_wis <> 'N'
GROUP BY senddemo_wis
)
) as c ON id_des = senddemo_wis
LEFT JOIN
(
SELECT user_des as user2_des, count(id_vis) as totalviews
FROM sta_views
LEFT JOIN dem_invited ON id_invited = invited_vis
LEFT JOIN dem_senddemo ON id_des = id_senddemo
WHERE NOT ISNULL(id_senddemo) AND countvisit_wis != 'N' AND createdon_des BETWEEN '2015-05-31 00:00:00' AND '2015-06-29 23:59:59' AND demo_des IN (746,943,748)
GROUP BY user_des
) as z ON user_des = user2_des
LEFT JOIN
(
SELECT user_des as user3_des, count(id_des) as sent
FROM dem_senddemo
LEFT JOIN dem_invited ON id_senddemo = id_des
WHERE createdon_des BETWEEN '2015-05-31 00:00:00' AND '2015-06-29 23:59:59' AND demo_des IN (746,943,748) AND first_invited = 'YES'
GROUP BY user_des
) as za ON user_des = user3_des
LEFT JOIN
(
SELECT user_des as user4_des, count(id_vis) as viewed
FROM sta_views LEFT JOIN dem_invited ON id_invited = invited_vis
LEFT JOIN dem_senddemo ON id_des = id_senddemo
WHERE NOT ISNULL(id_senddemo) AND countvisit_wis != 'N' AND createdon_des BETWEEN '2015-05-31 00:00:00' AND '2015-06-29 23:59:59' AND state = 'VIEWED'
AND first_invited = 'YES' AND demo_des IN (746,943,748)
GROUP BY user_des
) as zb ON user_des = user4_des
LEFT JOIN
(
SELECT id_senddemo as iddemdemos2, count(id_senddemo) as shared, allshares as sharedcount
FROM
(
SELECT id_senddemo, count(id_senddemo) as allshares
FROM dem_invited
LEFT JOIN dem_senddemo on id_senddemo = id_des
WHERE first_invited <> 'YES' AND demo_des IN (746,943,748)
GROUP BY id_senddemo
) as x
GROUP BY id_senddemo
) as zc ON iddemdemos2 = id_des
LEFT JOIN
sec_users ON user_des = id_usr
WHERE customer_dem = 1 AND createdon_des BETWEEN '2015-05-31 00:00:00' AND '2015-06-29 23:59:59'
GROUP BY user_des
I have table karton00 which holds thousands of records. This makes the queries slow. I would like to use indexes, but even though I set them, they are not being used.
Here is my table. and here is the result of using EXPLAIN.
This is my query:
SELECT '' checkbox,
k00_eszkozid,
k00_eszkoznev,
k01_gyariszam,
k01_leltszam,
prsn_name,
k00_status,
tstikon,
k00_startflow,
tstname,
k00_eszkozid,
k01_gyariszam,
k01_leltszam
FROM karton00
LEFT JOIN karton01
ON k00_eszkozid = k01_eszkozid
AND k01_status = 'A'
LEFT JOIN karton02
ON k00_eszkozid = k02_eszkozid
AND k02_status = 'A'
LEFT JOIN karton04
ON k00_eszkozid = k04_eszkozid
AND k04_status = 'A'
LEFT JOIN karton05
ON k00_eszkozid = k05_eszkozid
AND k05_status = 'A'
LEFT JOIN karton06
ON k00_eszkozid = k06_eszkozid
AND k06_status = 'A'
LEFT JOIN karton08
ON k00_eszkozid = k08_eszkozid
AND k08_status = 'A'
LEFT JOIN karton09
ON k00_eszkozid = k09_eszkozid
AND k09_status = 'A'
LEFT JOIN telephely et
ON k06_telephelyid = et.telepid
LEFT JOIN ktghely00 ek
ON k02_ktghid = ek.ktghid
LEFT JOIN person00 us
ON k06_userid = prsn_id
LEFT JOIN ktghely00 fk
ON prsn_ktgh = fk.ktghid
LEFT JOIN telephely ft
ON prsn_telep = ft.telepid
LEFT JOIN tamstatus
ON k02_tamstatusid = tstid
LEFT JOIN szakleltar
ON k02_szakleltarid = szleltid
LEFT JOIN tamszerv
ON k02_tamszervid = tszid
LEFT JOIN ktghely01
ON k02_vgazdaid = vgid
AND vgstatus = 'A'
LEFT JOIN ktghely00 vk
ON vgktghid = vk.ktghid
LEFT JOIN szallitok m
ON k05_mincegid = m.szallid
LEFT JOIN szallitok s
ON k04_szallitoid = s.szallid
LEFT JOIN dctnry00 ym
ON k05_minosit = ym.dctnryname
AND ym.dctnrygrp = 'YESNO'
LEFT JOIN dctnry00 yd
ON k05_check = yd.dctnryname
AND yd.dctnrygrp = 'YESNO'
WHERE k00_status IN ( 'A', 'H' )
AND ( k02_vgazdaid IN ( 7 ) )
AND NOT EXISTS (SELECT *
FROM eszkozkizar
WHERE ez_flid = 0
AND ez_emintaid = k00_eszkozmintaid
AND k00_uzembedatum < ( Date_add(Curdate(),
INTERVAL ez_honap month) )
AND k00_uzembedatum != ''
AND k02_vgazdaid NOT IN ( 7 ))
AND NOT EXISTS(SELECT *
FROM eszkozkizar
WHERE ez_flid = 0
AND ez_emintaid = k00_eszkozmintaid
AND k02_tamszervid = ez_tamszerv
AND k02_tamstatusid = ez_tamsts
AND k00_uzembedatum < ( Date_add(Curdate(),
INTERVAL ez_honap month) )
AND k00_uzembedatum != ''
AND k02_vgazdaid NOT IN ( 7 ))
ORDER BY k00_eszkoznev ASC;
Also you can use USE INDEX (index_list) statement: http://dev.mysql.com/doc/refman/5.5/en/index-hints.html