I am using UNION ALL, then I want ORDER BY globaly - mysql

Can I order by flt.flt_Date_Start(Globaly union) If its there any solution please help me
Here I try to achieve order by Date Globally Please Help me
Can I order by flt.flt_Date_Start(Globaly union) If its there any solution please help me
Here I try to achieve order by Date Globally Please Help me
Can I order by flt.flt_Date_Start(Globaly union) If its there any solution please help me
Here I try to achieve order by Date Globally Please Help me
(Select
flt.flt_Id as fltId,
flttyp.flttyp_Name As FlightType,
off.off_Name as offName,
fltrte.fltrte_P1_Plt_Per_Id_Fk As pilot1Id,
fltrte.fltrte_P2_Plt_Per_Id_Fk As pilot2Id,
per1.per_Name As Pilot1Name,
per2.per_Name As Pilot2Name,
per1.per_Name As GrpPilotName,
flt.flt_Date_Start as dtStrt,
flt.flt_Date_End as dtEnd,
air.air_Id as airId,
flt.flt_Trip_Rpt_No as rptNo,
flttyp.flttyp_Id as flttypId,
flt.flt_Eng_Tim As EngT,
flt.flt_Flt_Tim As FlyT,
flt.flt_Wait_Tim As WaitT,
(select sum(pltfltdtytim.pltfltdtytim_Tot_Dty_Tim) from pltfltdtytim where pltfltdtytim.pltfltdtytim_Flt_Id_Fk = flt.flt_Id and pltfltdtytim.pltfltdtytim_Per_Id_Fk=per1.per_Id) As DutyT,
flt.flt_Route as fltRout
From
fltrte left Join
flt On fltrte.fltrte_Flt_Id_Fk = flt.flt_Id left Join
off On flt.flt_Off_Id_Fk = off.off_Id left Join
air On flt.flt_Air_Id_Fk = air.air_Id left Join
per per1 On fltrte.fltrte_P1_Plt_Per_Id_Fk = per1.per_Id left Join
per per2 On fltrte.fltrte_P2_Plt_Per_Id_Fk = per2.per_Id left join
flttyp On flt.flt_Flttyp_Id_Fk = flttyp.flttyp_Id
where
flt.flt_Status <> 4 and per1.per_Name is not null and per1.per_Name !=''
Group By
off.off_Name,flt.flt_Date_Start,
flt.flt_Trip_Rpt_No, flt.flt_Id, flttyp.flttyp_Name, flt.flt_Route,
flt.flt_Status
Order By
flt.flt_Date_Start)
union all
(Select
flt.flt_Id as fltId,
flttyp.flttyp_Name As FlightType,
off.off_Name as offName,
fltrte.fltrte_P1_Plt_Per_Id_Fk As pilot1Id,
fltrte.fltrte_P2_Plt_Per_Id_Fk As pilot2Id,
per1.per_Name As Pilot1Name,
per2.per_Name As Pilot2Name,
per2.per_Name As GrpPilotName,
flt.flt_Date_Start as dtStrt,
flt.flt_Date_End as dtEnd,
air.air_Id as airId,
flt.flt_Trip_Rpt_No as rptNo,
flttyp.flttyp_Id as flttypId,
flt.flt_Eng_Tim As EngT,
flt.flt_Flt_Tim As FlyT,
flt.flt_Wait_Tim As WaitT,
(select sum(pltfltdtytim.pltfltdtytim_Tot_Dty_Tim) from pltfltdtytim where pltfltdtytim.pltfltdtytim_Flt_Id_Fk = flt.flt_Id and pltfltdtytim.pltfltdtytim_Per_Id_Fk=per2.per_Id) As DutyT,
flt.flt_Route as fltRout
From
fltrte left Join
flt On fltrte.fltrte_Flt_Id_Fk = flt.flt_Id left Join
off On flt.flt_Off_Id_Fk = off.off_Id left Join
air On flt.flt_Air_Id_Fk = air.air_Id left Join
per per1 On fltrte.fltrte_P1_Plt_Per_Id_Fk = per1.per_Id left Join
per per2 On fltrte.fltrte_P2_Plt_Per_Id_Fk = per2.per_Id left join
flttyp On flt.flt_Flttyp_Id_Fk = flttyp.flttyp_Id
where
flt.flt_Status <> 4 and per2.per_Name is not null and per2.per_Name !=''
Group By
off.off_Name,flt.flt_Date_Start,
flt.flt_Trip_Rpt_No, flt.flt_Id, flttyp.flttyp_Name, flt.flt_Route,
flt.flt_Status
Order By
flt.flt_Date_Start)

You only need to do a subselect:
SELECT ... dtStrt ...
FROM (
(SELECT ... as dtStrt, ...
FROM ... JOIN... WHERE...)
union all
(SELECT ... as dtStrt, ...
FROM ... JOIN... WHERE...)
) as val
ORDER BY dtStrt

Related

How can i Get Better Time Result on This Mysql Query

i am Using This Mysql Query and is working Good, But i need go get a Better Time result. How can i do it?
SELECT TblExistencias.id as ID, TblExistencias.codigo as Codigo,
TblPartes.detalle as Detalle,TblPartes.neto1 as PrecioActual,
TblExistencias.Condicion_Producto as Condicion,TblCategorias.categoria as Categoria,
TblSubcategorias.subcategoria as Subcategoria, TblExistencias.costo as Costo,
TblExistencias.serial as Serial, TblExistencias.vendido as Vendido,
TblConceptosFacturas.ventaonline as VentaOnline, TblRemitos.nroremitocompleto as Remito,
TblFacturas.nrofacturacompleto as Factura, TblFacturas.fecha as FechaVenta,
TblConceptosFacturas.ventaTotUn as Venta,TblConceptosFacturas.comisionmlunit as Comision,
TblFacturas.costoenvio as Envio, if(TblExistencias.vendido =1,
TblConceptosFacturas.ventaTotUn - TblExistencias.Costo - TblConceptosFacturas.comisionmlunit - TblFacturas.costoenvio,0) as Ganancia,
TblProveedores.razonsocial as Proveedor, TblFacturasCompras.nrofacturacompleto as Compra,
TblFacturasCompras.fecha as FechaCompra, TblClientes.razonsocial as Cliente
from TblExistencias
left join TblPartes on TblExistencias.codigo = TblPartes.codigo1
left join TblRemitos on TblExistencias.id_RemitoVenta = TblRemitos.id
left join TblFacturasCompras on TblExistencias.id_factura = TblFacturasCompras.id
left join TblClientes on TblRemitos.id_cliente = TblClientes.id
left join TblFacturas on TblRemitos.id_factura = TblFacturas.id
left join TblConceptosFacturas on TblFacturas.id=TblConceptosFacturas.id_factura and TblConceptosFacturas.codigoproducto = TblExistencias.codigo
left join TblCategorias on TblCategorias.id = TblPartes.id_categoria
left join TblSubcategorias on TblPartes.id_subcategoria = TblSubcategorias.id
left join TblProveedores on TblFacturasCompras.id_proveedor = TblProveedores.id
order by comision desc
The solution is create an index on each relational columns, like:
TblFacturasCompras.id_proveedor = TblProveedores.id
Problem was in order clause, Query passed from 87 Secs to 4 Secs. Thanks for help.

MYSQL UNION JOIN SUM(amount) output two result instead of one

I have this below:
SELECT a.* FROM ( SELECT
asset_liability_income_expenditure_tbl.a_l_code,
SUM(mainaccount_a_2017.amount), mainaccount_a_2017.dr_cr_action
FROM `mainaccount_a_2017` LEFT JOIN chart_of_account
ON (
chart_of_account.joint_account_numbers =
mainaccount_a_2017.joint_account_number
)
LEFT JOIN asset_liability_income_expenditure_tbl
ON (
asset_liability_income_expenditure_tbl.a_l_code =
chart_of_account.account_type
)
WHERE asset_liability_income_expenditure_tbl.a_l_code = 'FA'
AND mainaccount_a_2017.dr_cr_action = 'DR' UNION
SELECT asset_liability_income_expenditure_tbl.a_l_code,
SUM(mainaccount_b_2017.amount),
mainaccount_b_2017.dr_cr_action
FROM `mainaccount_b_2017`
LEFT JOIN chart_of_account ON (
chart_of_account.joint_account_numbers =
mainaccount_b_2017.joint_account_number
)
LEFT JOIN asset_liability_income_expenditure_tbl ON (
asset_liability_income_expenditure_tbl.a_l_code =
chart_of_account.account_type
)
WHERE asset_liability_income_expenditure_tbl.a_l_code = 'FA'
AND mainaccount_b_2017.dr_cr_action = 'DR'
) AS a
it works fine, but displays either one empty row at the top and the sum below or vis-a-vis. I tried LIMIT 1, but the problem is when the SUM(amount) outputs in row 2, i cannot fetch and if I don't apply any limit, it only fetches result whose SUM(amount) outputs in row 1. I don't know what am missing. Please kindly assist. Thanks.
I figured it out. I had to flip something. Results below:
SELECT SUM(a) FROM
(
SELECT SUM(mainaccount_a_2017.amount)
AS a FROM `mainaccount_a_2017`
LEFT JOIN chart_of_account ON (chart_of_account.joint_account_numbers = mainaccount_a_2017.joint_account_number)
LEFT JOIN asset_liability_income_expenditure_tbl ON (asset_liability_income_expenditure_tbl.a_l_code = chart_of_account.account_type)
WHERE asset_liability_income_expenditure_tbl.a_l_code = 'FA'
AND mainaccount_a_2017.dr_cr_action = 'DR'
UNION ALL
SELECT SUM(mainaccount_b_2017.amount)
AS a FROM `mainaccount_b_2017`
LEFT JOIN chart_of_account ON (chart_of_account.joint_account_numbers = mainaccount_b_2017.joint_account_number)
LEFT JOIN asset_liability_income_expenditure_tbl ON (asset_liability_income_expenditure_tbl.a_l_code = chart_of_account.account_type)
WHERE asset_liability_income_expenditure_tbl.a_l_code = 'FA' AND mainaccount_b_2017.dr_cr_action = 'DR'
) a

How to write a select statement inside another select in SQL

Can anyone tell me what is wrong with this query?
it gives a syntax error near the 2nd select
SELECT b.mc_boxes_idmc_boxes,
t.idtitles,
t.title,
t.languages_idlanguages,
MAX(h.idtitle_history),
MAX(h.edition)
(SELECT h.preview, h.file WHERE h.idtitle_history = MAX(h.idtitle_history))
FROM mc_boxes_has_titles b
LEFT JOIN titles t ON b.titles_idtitles = t.idtitles
LEFT JOIN title_history h ON h.titles_idtitles = t.idtitles
WHERE b.mc_boxes_idmc_boxes = 12
AND h.edition IS NOT NULL
GROUP BY b.mc_boxes_idmc_boxes, idtitles
ORDER BY b.sortorder;
looks like you are missing a comma after MAX(h.edition)
SELECT b.mc_boxes_idmc_boxes,
t.idtitles,
t.title,
t.languages_idlanguages,
MAX(h.idtitle_history),
MAX(h.edition),
(SELECT h.preview, h.file WHERE h.idtitle_history = MAX(h.idtitle_history))
FROM mc_boxes_has_titles b
LEFT JOIN titles t ON b.titles_idtitles = t.idtitles
LEFT JOIN title_history h ON h.titles_idtitles = t.idtitles
WHERE b.mc_boxes_idmc_boxes = 12
AND h.edition IS NOT NULL
GROUP BY b.mc_boxes_idmc_boxes, idtitles
ORDER BY b.sortorder;
besides the comma, you are selecting two fields in your subquery
SELECT b.mc_boxes_idmc_boxes,
t.idtitles,
t.title,
t.languages_idlanguages,
MAX(h.idtitle_history),
MAX(h.edition),
(SELECT preview FROM title_history WHERE idtitle_history = MAX(h.idtitle_history)),
(SELECT [file] FROM title_history WHERE idtitle_history = MAX(h.idtitle_history))
FROM mc_boxes_has_titles b
LEFT JOIN titles t ON b.titles_idtitles = t.idtitles
LEFT JOIN title_history h ON h.titles_idtitles = t.idtitles
WHERE b.mc_boxes_idmc_boxes = 12
AND h.edition IS NOT NULL
GROUP BY b.mc_boxes_idmc_boxes, idtitles
ORDER BY b.sortorder;
Adding to bluefeet's answer, you may want to check for reserved words. "File" for example is a reserved word in sql server.
Alias it as a virtual table. Change something like
SELECT b.mc_boxes_idmc_boxes,
t.idtitles,
t.title,
t.languages_idlanguages,
MAX(h.idtitle_history),
MAX(h.edition)
(SELECT h.preview, h.file WHERE h.idtitle_history = MAX(h.idtitle_history))
FROM mc_boxes_has_titles b
LEFT JOIN titles t ON b.titles_idtitles = t.idtitles
LEFT JOIN title_history h ON h.titles_idtitles = t.idtitles
WHERE b.mc_boxes_idmc_boxes = 12
AND h.edition IS NOT NULL
GROUP BY b.mc_boxes_idmc_boxes, idtitles
ORDER BY b.sortorder) as virtual_column_alias;
Check this out. Hope this will work.

MYSQL UNION and ORDER BY not working

I have a mysql query which is as follows
(SELECT order_product.op_id,
order_product.ocat_id,
order_product.op_partnunber,
order_product.op_name,
order_product.op_upc,
order_product.op_desc,
order_stockavailable.osa_id,
order_stockavailable.of_id,
order_stockavailable.osa_stocka,
order_category.ocat_name
FROM
order_product
LEFT JOIN order_category
ON order_product.ocat_id = order_category.ocat_id
LEFT JOIN order_stockavailable
ON order_product.op_id = order_stockavailable.op_id)
UNION
(SELECT order_product.op_id,
order_product.ocat_id,
order_product.op_partnunber,
order_product.op_name,
order_product.op_upc,
order_product.op_desc,
order_stockavailable_attributes.id,
order_stockavailable_attributes.of_id,
order_stockavailable_attributes.opap_stock,
order_category.ocat_name
FROM order_product
LEFT JOIN order_category
ON order_product.ocat_id = order_category.ocat_id
LEFT JOIN order_stockavailable
ON order_product.op_id = order_stockavailable.op_id
LEFT JOIN order_stockavailable_attributes
ON order_product.op_id = order_stockavailable_attributes.op_id)
ORDER BY order_product.op_name
The query is givng error, T
Table 'order_product' from one of the SELECTs cannot be used in global ORDER clause
I checked the MYSQL manual, but am not getting any clue, any help would be really great.
SELECT *
FROM (
SELECT order_product.op_id,
order_product.ocat_id,
order_product.op_partnunber,
order_product.op_name,
order_product.op_upc,
order_product.op_desc,
order_stockavailable.osa_id,
order_stockavailable.of_id,
order_stockavailable.osa_stocka,
order_category.ocat_name
FROM
order_product
LEFT JOIN order_category
ON order_product.ocat_id = order_category.ocat_id
LEFT JOIN order_stockavailable
ON order_product.op_id = order_stockavailable.op_id
UNION
SELECT order_product.op_id,
order_product.ocat_id,
order_product.op_partnunber,
order_product.op_name,
order_product.op_upc,
order_product.op_desc,
order_stockavailable_attributes.id,
order_stockavailable_attributes.of_id,
order_stockavailable_attributes.opap_stock,
order_category.ocat_name
FROM order_product
LEFT JOIN order_category
ON order_product.ocat_id = order_category.ocat_id
LEFT JOIN order_stockavailable
ON order_product.op_id = order_stockavailable.op_id
LEFT JOIN order_stockavailable_attributes
ON order_product.op_id = order_stockavailable_attributes.op_id
) t
ORDER BY op_name
Btw: there is no need to put the individual SELECTs of a UNION into brackets.
Try the following syntax?
SELECT
x, y
FROM
(
SELECT x, y FROM z
UNION
SELECT a, b FROM c
)
ORDER BY
x, y
Try
order by op_name
instead of
ORDER BY order_product.op_name
SELECT *
FROM (SELECT order_product.op_id,
...
FROM
... )
UNION
(SELECT order_product.op_id,
...
FROM order_product
... ) U
ORDER BY op_name;

SQL Query Problem

I've been at this for a bit now. Basically, I'm needing to add a derived column to count the hits to a weblog entry in the database. The problem is, the hits are being totaled and shown on only on the first record. Any Ideas? I've emboldened the parts of the query I'm talking about. The query is below:
SELECT DISTINCT(t.entry_id),
exp_categories.rank,
**exp_hits.hits,**
t.entry_id,
t.weblog_id,
t.forum_topic_id,
t.author_id,
t.ip_address,
t.title,
t.url_title,
t.status,
t.dst_enabled,
t.view_count_one,
t.view_count_two,
t.view_count_three,
t.view_count_four,
t.allow_comments,
t.comment_expiration_date,
t.allow_trackbacks,
t.sticky,
t.entry_date,
t.year,
t.month,
t.day,
t.entry_date,
t.edit_date,
t.expiration_date,
t.recent_comment_date,
t.comment_total,
t.trackback_total,
t.sent_trackbacks,
t.recent_trackback_date,
t.site_id as entry_site_id,
w.blog_title,
w.blog_name,
w.search_results_url,
w.search_excerpt,
w.blog_url,
w.comment_url,
w.tb_return_url,
w.comment_moderate,
w.weblog_html_formatting,
w.weblog_allow_img_urls,
w.weblog_auto_link_urls,
w.enable_trackbacks,
w.trackback_use_url_title,
w.trackback_field,
w.trackback_use_captcha,
w.trackback_system_enabled,
m.username,
m.email,
m.url,
m.screen_name,
m.location,
m.occupation,
m.interests,
m.aol_im,
m.yahoo_im,
m.msn_im,
m.icq,
m.signature,
m.sig_img_filename,
m.sig_img_width,
m.sig_img_height,
m.avatar_filename,
m.avatar_width,
m.avatar_height,
m.photo_filename,
m.photo_width,
m.photo_height,
m.group_id,
m.member_id,
m.bday_d,
m.bday_m,
m.bday_y,
m.bio,
md.*,
wd.*
FROM exp_weblog_titles AS t
LEFT JOIN exp_weblogs AS w ON t.weblog_id = w.weblog_id
LEFT JOIN exp_weblog_data AS wd ON t.entry_id = wd.entry_id
LEFT JOIN exp_members AS m ON m.member_id = t.author_id
LEFT JOIN exp_member_data AS md ON md.member_id = m.member_id
LEFT JOIN exp_category_posts ON wd.entry_id = exp_category_posts.entry_id
**LEFT JOIN
(
SELECT COUNT(*) AS hits, exp_hits.entry_id FROM exp_hits ORDER BY exp_hits.entry_id
) exp_hits ON t.entry_id = exp_hits.entry_id**
LEFT JOIN
(
SELECT exp_categories.cat_id, cat_name as rank
FROM exp_categories
WHERE exp_categories.group_id = '9'
) exp_categories ON exp_categories.cat_id = exp_category_posts.cat_id
WHERE t.entry_id IN (2,3,4) ORDER BY exp_categories.rank DESC, **exp_hits.hits DESC**, entry_date desc
Try changeing the subselect to
SELECT COUNT(*) AS hits,
exp_hits.entry_id
FROM exp_hits
GROUP BY exp_hits.entry_id
Out of curiosity, is your hits functionality something that can't be accomplished with the view_count_one/two/three/four fields already present in the database and supported by ExpressionEngine template tags?