i want to sum a total value of eligible_2018 with distinct of ecode_id
select distinct(A.ecode_id),C.eligible_2018 AS monthlyBudgetAmt from vefm_dailybudget AS A JOIN vefm_comp_division AS B ON (B.id = A.division)
JOIN vefm_engineer_details AS C ON (A.ecode_id=C.ecode)
LEFT JOIN vefm_user AS F ON (A.approved_by=F.ecode)
JOIN vefm_region AS D ON (A.region=D.id)
where A.approval_amount != 0 AND A.division=1 and date(A.approval_date) BETWEEN '2018-04-01' AND '2018-04-30'
AND A.budget_type='monthly' GROUP BY A.ecode_id
The distinct A.ecode_id are directly managed by group by
SELECT A.ecode_id
, SUM(C.eligible_2018) monthlyBudgetAmt
FROM vefm_dailybudget A
JOIN vefm_comp_division B ON B.id = A.division
JOIN vefm_engineer_details C ON A.ecode_id=C.ecode
LEFT JOIN vefm_user F ON A.approved_by=F.ecode
JOIN vefm_region D ON A.region=D.id
WHERE A.approval_amount != 0
AND A.division=1
AND date(A.approval_date)
BETWEEN '2018-04-01' AND '2018-04-30'
AND A.budget_type='monthly'
GROUP BY A.ecode_id
if you need the overall sum for distinct result then you can try
SELECT SUM( t.monthlyBudgetAmt )
FROM (
SELECT DISTINCT A.ecode_id
,C.eligible_2018 AS monthlyBudgetAmt
FROM vefm_dailybudget AS A
JOIN vefm_comp_division AS B ON (B.id = A.division)
JOIN vefm_engineer_details AS C ON (A.ecode_id=C.ecode)
LEFT JOIN vefm_user AS F ON (A.approved_by=F.ecode)
JOIN vefm_region AS D ON (A.region=D.id)
WHERE A.approval_amount != 0
AND A.division=1
AND date(A.approval_date) BETWEEN '2018-04-01' AND '2018-04-30'
AND A.budget_type='monthly' GROUP BY A.ecode_id
)
Related
So far I have this query:
SELECT
c.id AS company_id,
c.company_name,
COUNT(*) AS employee_count
FROM
ct_companies c
INNER JOIN ct_employees e ON c.id = e.company_id
GROUP BY
c.domain,
c.postcode,
c.company_name
HAVING
(
employee_count >= 1
AND employee_count <= 5
)
ORDER BY
employee_count DESC
Now, this works great, it selects companies who have at least 1 employee but no more than 5 and shows me the employee count.
But what I want to be able to do, is select every employee within a company, but only where that company has between 1 and 5 employees like above.
So something like
SELECT e.id FROM ct_employees e WHERE (employee_count >= 1 AND employee_count <= 5)
You could try
SELECT e.id,
d.employee_count
FROM ct_employees e
INNER JOIN (SELECT c.id AS company_id,
COUNT(*) AS employee_count
FROM ct_companies c
INNER JOIN ct_employees e1
ON c.id = e1.company_id
GROUP BY c.domain,
c.postcode,
c.id
HAVING COUNT(*) >= 1
AND COUNT(*) <= 5) d
ON d.company_id = e.company_id
ORDER BY employee_count DESC
i dont't know how to select row with max column value group by another column. I have T-SQL
CREATE PROC GET_USER
AS
BEGIN
SELECT A.USER_ID, B.START_DATE , D.START_DATE, A.FULL_NAME,A.COST_CENTER,
F.DEPARTMENT_NAME,G.BU_NAME
FROM USERS A INNER JOIN USER_PERSON B ON A.USER_ID=B.USER_ID
INNER JOIN TYPE_PERSON C ON C.TYPE_PERSON_ID = B.TYPE_PERSON_ID
INNER JOIN USER_TRANSACTION D ON D.USER_ID = A.USER_ID
INNER JOIN TRANSACTIONS E ON E.TRANSACTION_ID = D.TRANSACTION_ID
INNER JOIN DEPARTMENT F ON F.DEPARTMENT_ID = D.DEPARTMENT_ID
INNER JOIN BUS_UNIT G ON G.BU_ID = D.BU_ID
INNER JOIN BRANCH H ON H.BRANCH_ID = D.BRANCH_ID
INNER JOIN POSITION J ON J.POSITION_ID = D.POSITION_ID
WHERE A.FLAG = 'TRUE'
END
the result will select max(B.START_DATE) and max(D.START_DATE) and Group by USER_ID
try this
SELECT T.USER_ID ,
MAX(T.START_DATE) AS [Max First Start Date] ,
MAX(T.[Second Start Date]) AS [Max Second Start Date]
FROM ( SELECT A.USER_ID ,
B.START_DATE ,
D.START_DATE AS [Second Start Date] ,
A.FULL_NAME ,
A.COST_CENTER ,
F.DEPARTMENT_NAME ,
G.BU_NAME
FROM USERS A
INNER JOIN USER_PERSON B ON A.USER_ID = B.USER_ID
INNER JOIN TYPE_PERSON C ON C.TYPE_PERSON_ID = B.TYPE_PERSON_ID
INNER JOIN USER_TRANSACTION D ON D.USER_ID = A.USER_ID
INNER JOIN TRANSACTIONS E ON E.TRANSACTION_ID = D.TRANSACTION_ID
INNER JOIN DEPARTMENT F ON F.DEPARTMENT_ID = D.DEPARTMENT_ID
INNER JOIN BUS_UNIT G ON G.BU_ID = D.BU_ID
INNER JOIN BRANCH H ON H.BRANCH_ID = D.BRANCH_ID
INNER JOIN POSITION J ON J.POSITION_ID = D.POSITION_ID
WHERE A.FLAG = 'TRUE'
) AS T
GROUP BY T.USER_ID
I have 4 table and i want to extract: id, nume, localitate, masina_id, nr_inmatriculare, an_fabricatie, rafinarie, marca, and sum (quantity+deliver_quantity) as total_quantity group by an_fabricatie , Order by marca, and put some having clouse.
I don’t know how to make this.
My query is as bellow , but I think isn't correct.
select c.id, c.nume,c.localitate,l.masina_id, i.nr_inmatriculare, i.an_fabricatie,
i.rafinarie, m.marca from clienti c inner join livrari l on c.id = l.id inner join incarcari I on l.incarcare_id = l.livrari_id inner join masina m on i.id_marca = m.id, sum(select quantity, deliver_quantity) as total_quantity group by an_fabricatie having quantity >1000 order by marca;
Incarcari table
Id|livrari_id|id_marca|nr_inmatriculare|an_fabricatie|rafinarie|aviz_incarcare|quantity|
Livrari table
Id|masina_id|client_id|incarcare_id|deliver_quantity|aviz_livrare
Masini table
Id|numar_inmatriculare|marca|an_fabricatie|
Clienti table
Id|nume|localitate|date_add|date_upd|
SELECT c.id, c.nume, c.localitate, l.masina_id, i.nr_inmatriculare, i.an_fabricatie, i.rafinarie, m.marca, (SUM(i.quantity) + SUM(l.deliver_quantity)) AS total_quantity
FROM clienti c
INNER JOIN livrari l ON c.id = l.id
INNER JOIN incarcari i ON l.incarcare_id = i.livrari_id
INNER JOIN masini m ON i.id_marca = m.id
GROUP BY i.an_fabricatie, c.id, c.nume,c.localitate,l.masina_id, i.nr_inmatriculare, i.rafinarie, m.marca
HAVING i.quantity > 1000
ORDER BY m.marca DESC;
SELECT DISTINCT (
A.`id`
), A.`id` , A.`no` , B.amount, SUM( A.`outward` * A.`price` ) AS total, A.`outward_date`
FROM `outward` A
INNER JOIN franchisees B
INNER JOIN store C
INNER JOIN shoppe D
WHERE B.user_id = C.user_id
AND (C.pos_id = A.no)//(C.pos_id = A.no OR (D.id = A.no)
OR (D.id = A.no)
AND A.outward_date = '2012-02-10'
GROUP BY A.req_id
ORDER BY A.no ASC , A.`d` ASC , B.amount ASC
The problem in this query is that the SUM( A.outward * A.price ) AS total comes differently which is not related to
ouptut
id sum(outward * price)
12021030738-105 485.220000000000
1202104186-104 2504.410000000000
output displayed
12021030738-105 32557.33
1202104186-104 6307.86
i guess the problem is with the OR statement? can anyone find the issue with the query
Try below. enclose OR Conditions in a parenthesis:
SELECT DISTINCT (
A.`id`
), A.`id` , A.`no` , B.amount, SUM( A.`outward` * A.`price` ) AS total, A.`outward_date`
FROM `outward` A
INNER JOIN franchisees B
INNER JOIN store C
INNER JOIN shoppe D
WHERE B.user_id = C.user_id
AND ((C.pos_id = A.no) OR (D.id = A.no))
AND A.outward_date = '2012-02-10'
GROUP BY A.req_id
ORDER BY A.no ASC , A.`d` ASC , B.amount ASC
SELECT a.id, a.name, a.ad, c.name, c.phone, c.email,
(
SELECT b.id_user
FROM price_b b
WHERE b.id = a.id
ORDER BY b.date DESC
LIMIT 1
) AS f_user_id
FROM a_emtp a
LEFT JOIN customer c ON
c.id = f_user_id
WHERE a.show = "1"
Hi, why show this error: Unknown column 'f_user_id' in 'on clause'
Thanks
SELECT a.id, a.name, a.ad, c.name, c.phone, c.email,
(
SELECT b.id_user
as f_user_id
FROM price_b b
WHERE b.id = a.id
ORDER BY b.date DESC
LIMIT 1
)
FROM a_emtp a
LEFT JOIN customer c ON
c.id = f_user_id
WHERE a.show = "1"
You can reference labelled fields from subqueries inside outer queries. You could even drop the relabelling and just use "b.id_user".
Probably a comma after c.email was missing.
SELECT a.id, a.name, a.ad, c.name, c.phone, c.email,
(
SELECT b.id_user
FROM price_b b
WHERE b.id = a.id
ORDER BY b.date DESC
LIMIT 1
) AS f_user_id
FROM a_emtp a
LEFT JOIN customer c ON
c.id = f_user_id
WHERE a.show = "1"