I have View here
What I want to try is to make the v_total_project looks like that.
I have try this code
select o.id_project AS id_project,
sum((o.office_expense + m.misc_expense)) AS total_expense
from v_office_project o inner join v_misc_project m on o.id_project = m.id_project
group by o.id_project, m.id_project
I know my code will not print the v_misc_project.id_project. So I tried the Union to print both id_project, and got error different numbers of column.
select o.id_project AS id_project
from v_office_project o
union
select m.id_project as id_project,
sum((o.office_expense + m.misc_expense)) AS total_expense
from v_office_project o inner join v_misc_project m on o.id_project = m.id_project
group by id_project
So, is there another way to do that?
Thanks in advance,
SELECT
o.id_project AS id_project,
sum(
o.office_expense + ifnull(m.misc_expense, 0)
) AS total_expense
FROM
v_office_project o
LEFT JOIN v_misc_project m ON o.id_project = m.id_project
GROUP BY
id_project
UNION
SELECT
m.id_project AS id_project,
sum(
ifnull(o.office_expense, 0) + m.misc_expense
) AS total_expense
FROM
v_office_project o
RIGHT JOIN v_misc_project m ON o.id_project = m.id_project
GROUP BY
id_project
In the first select of your union you are missing the field for your expenses.
Also the join was missing, added it. Instead of INNER JOIN use a LEFT or RIGHT Join, otherwise you only get results for id_project's which are in both tables.
Added ifnull() in the calculation, because the LEFT / RIGHT joins can return null, which has to be treated as 0.
Related
I have this setence that select a Product NAME from my table Produto and select the quantity from other table named Contagens where the Produto.EANU = Contagens.PRODUTO but I also have some QTD that are related to EANP istead of EANU and I want to them be shown as ARM instead of QTD.
Is there any way that I can get those 2 sentences in one ?
SELECT Produto.NAME, IFNULL(Contagens.QTD ,0) as QTD FROM Produto
LEFT
OUTER JOIN Contagens ON Produto.EANU = Contagens.PRODUTO
ORDER BY NAME
SELECT Produto.NAME, IFNULL(Contagens.QTD ,0) as ARM FROM Produto
LEFT
OUTER JOIN Contagens ON Produto.EANP = Contagens.PRODUTO
ORDER BY NAME
Combine the conditions, and then use IF in the SELECT list to test which condition was matched.
SELECT p.Name, IF(p.EANU = c.PRODUTO, Contagens.QTD, 0) AS QTD, IF(p.EANP = c.PRODUTO, Contagents.QTD, 0) AS ARM
FROM Produto AS p
LEFT JOIN Contagens AS c ON c.PRODUTO IN (p.EANU, p.EANP)
ORDER BY p.Name
Just join them using aliases:
SELECT Produto.NAME,
IFNULL(EANU_Contagens.QTD ,0) as QTD,
IFNULL(EANP_Contagens.QTD ,0) as ARM
FROM Produto
LEFT OUTER JOIN Contagens AS EANU_Contagens ON Produto.EANU = EANU_Contagens.PRODUTO
LEFT OUTER JOIN Contagens AS EANP_Contagens ON Produto.EANP = EANP_Contagens.PRODUTO
ORDER BY NAME
SELECT e.qty * e.unit_cost * e.unit_no * (e.factor/100) AS Ext_price, c.cost_type, c.cost_dt, c.internal_cost_amt, c.client_cost_amt
FROM equipment e
LEFT JOIN CostAllocation c ON (c.object_row_id = e.row_id)
I now want to divide the Ext_price by the number of CostAllocation rows that match the equipment row. How would I do that?
SELECT e.qty*e.unit_cost*e.unit_no*e.factor/(100*x.cnt) AS Ext_price,
c.cost_type,
c.cost_dt,
c.internal_cost_amt,
c.client_cost_amt
FROM equipment e
LEFT JOIN CostAllocation c ON c.object_row_id = e.row_id
LEFT JOIN
(SELECT row_id,
count(*) cnt
FROM CostAllocation
GROUP BY row_id) x ON c.object_row_id = x.row_id
try below:
select g.*,g.Ext_price/(select count(*) from CostAllocation k where k.object_row_id=g.row_id) as youranswer
from
(
SELECT e.row_id,e.qty*e.unit_cost*e.unit_no*e.factor/100 AS Ext_price, c.cost_type, c.cost_dt, c.internal_cost_amt, c.client_cost_amt
FROM equipment e
LEFT JOIN CostAllocation c ON c.object_row_id = e.row_id)g
Since mysql does not support analytical functions it is probably easiest to add a subquery (untested):
SELECT Ext_price, cost_type, cost_dt, internal_cost_amt
, client_cost_amt
, Ext_price / ( nullif( (select count(1)
from CostAllocation c2
where c2.object_row_id = e.row_id), 0) )
FROM (
SELECT e.qty*e.unit_cost*e.unit_no*e.factor/100 AS Ext_price
, c.cost_type, c.cost_dt, c.internal_cost_amt, c.client_cost_amt
FROM equipment e
LEFT JOIN CostAllocation c
ON c.object_row_id = e.row_id
) as T
I added nullif to avoid division by zero.
I have query like this ::
SELECT account.AccountNumber, account.NAME, Sum(agro.price * agro.qty) AS Expr1
FROM ((account
INNER JOIN data ON account.AccountNumber = data.acno)
INNER JOIN agro ON agro.BillNo = data.BillNo)
WHERE data.db='true'
GROUP BY account.AccountNumber, account.NAME;
I want to deduct another groupby query output in to Sum(agro.price * agro.qty) this
the another group by query is SELECT Sum(rs),acno
FROM jma group by acno;
i want to deduct Sum(agro.price * agro.qty)-Sum(rs) how its work please help me solve this
If I am understanding you correctly the following query may work for you:
SELECT subQ.AccountNumber, subQ.NAME, (subQ.subSum - jmaSum.jSum) AS FinalSum
FROM
(
SELECT a.AccountNumber, a.NAME, Sum(ag.price * ag.qty) AS subSum
FROM (account AS a
INNER JOIN data AS d ON a.AccountNumber = d.acno)
INNER JOIN agro AS ag ON ag.BillNo = d.BillNo
WHERE d.db = 'true'
GROUP BY a.AccountNumber, a.NAME
) AS subQ
LEFT JOIN
(
SELECT Sum(j.rs) AS jSum, j.acno
FROM jma AS j
GROUP BY j.acno
) AS jmaSum ON subQ.AccountNumber = jmaSum.acno
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.
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;