i have this query that it works ok but doesn't retrieve exact information (firstname,lastname), it only gives second name
SELECT b.email, c.value AS firstname, a.updated_at, d.added_at
FROM `wishlist` AS a
INNER JOIN customer_entity AS b
ON a.customer_id = b.entity_id
INNER JOIN customer_entity_varchar AS c
ON a.customer_id = c.entity_id
AND c.attribute_id = (
SELECT attribute_id
FROM eav_attribute
WHERE attribute_code = 'lastname'
AND entity_type_id = b.entity_type_id )
INNER JOIN wishlist_item AS d
ON a.wishlist_id = d.wishlist_id
INNER JOIN catalog_product_entity AS e
ON d.product_id = e.entity_id
LEFT JOIN sales_flat_order AS f ON
f.customer_email = b.email
LEFT JOIN sales_flat_order_item AS g
ON ( f.entity_id = g.order_id
AND g.sku LIKE CONCAT( e.sku, '%' )
AND g.product_type = 'simple' )
WHERE d.added_at
BETWEEN '2015-01-01'
AND '2015-02-01'
GROUP BY b.email
This query if i execute will result all customers fullname correctly
SELECT entity_id, group_concat(VALUE SEPARATOR ' ') AS fullname
FROM customer_address_entity_varchar AS val
INNER JOIN eav_attribute AS attr
ON attr.attribute_id = val.attribute_id
WHERE attr.attribute_code IN ( 'firstname', 'lastname' )
How can i edit the 1st query part of retrieving the customer name as the example on the 2nd query?
I am a little bit confused , i tried many combination in sql but non of them will work.
EDIT: i have updated the 1st long query, price should not be there. Also i understand that the group makes no sense but i need only 1 row per customer so i will have to hide the products. If possible all the products to be like wishproduct_1,wishproduct2,wishproduct3 .. in the same row be awesome.
Try this:
SELECT b.email,
h.price as firstname,
c.price AS lastname,
a.updated_at,
d.added_at
FROM `wishlist` AS a
INNER JOIN customer_entity AS b
ON a.customer_id = b.entity_id
INNER JOIN customer_entity_varchar AS c
ON a.customer_id = c.entity_id
AND c.attribute_id =
(
SELECT attribute_id
FROM eav_attribute
WHERE attribute_code = 'lastname'
AND entity_type_id = b.entity_type_id
)
INNER JOIN customer_entity_varchar AS h
ON a.customer_id = c.entity_id
AND c.attribute_id =
(
SELECT attribute_id
FROM eav_attribute
WHERE attribute_code = 'firstname'
AND entity_type_id = b.entity_type_id
)
INNER JOIN wishlist_item AS d
ON a.wishlist_id = d.wishlist_id
INNER JOIN catalog_product_entity AS e
ON d.product_id = e.entity_id
LEFT JOIN sales_flat_order AS f
ON f.customer_email = b.email
LEFT JOIN sales_flat_order_item AS g
ON ( f.entity_id = g.order_id
AND g.sku LIKE CONCAT( e.sku, '%' )
AND g.product_type = 'simple' )
WHERE d.added_at
BETWEEN '2015-01-01' AND '2015-02-01'
GROUP BY b.email, h.price, c.price, a.updated_at, d.added_at
LIMIT 0 , 30
I assumed that the field containing the actual first name would be the same as the one containing the last name (although price seems to be an odd choice). Also since I used correct grouping, you may notice that you will get more records. In that case one or more of teh fields in the group by need to be changed to an aggregate function such as Max() or Min() in order to have one record per email. Your business rules would need to dictate which aggregate function you would apply though.
Related
SELECT t.id
, t.department
, t.owner
, t.client
, u.username as owner_name
, c.name as catagery
, d.dept_name as deptname
, t.periority
, t.status
, t.estimate
, cl.takeaway_name
from tbl_task t
JOIN tbl_user u
ON u.id = t.owner
JOIN tbl_task_catagery c
ON c.id = t.catagery
JOIN tbl_department d
ON d.id = t.department
JOIN tbl_clients cl
ON cl.id = t.client
and t.status = 0
and (t.id in (select task_id
from tbl_task_note tn
where tn.user_id = '69'
and tn.id in (select max(id)
from tbl_task_note tt
where tt.task_id = tn.task_id
)
)
)
order by t.id
Note : The above query is used for check users hold tasks. tbl_task_note table is used for check task notes for separate users task.
With this query you will get the task that have the last task_note registered, including the user, departament, client, and some other.
If it is what you need you can just do this.
select
t.id,
t.department,
t.owner,
t.client,
u.username as owner_name,
c.name as catagery,
d.dept_name as ptname,
t.periority,
t.status,
t.estimate,
cl.takeaway_name
from tbl_task t
INNER JOIN tbl_user u ON u.id=t.owner
INNER JOIN tbl_task_catagery c ON c.id=t.catagery
INNER JOIN tbl_department d ON d.id=t.department
INNER JOIN tbl_clients cl ON cl.id=t.client and t.status=0
INNER JOIN (select * from tbl_task_note where id =
(select max(id) from tbl_task_note)
)tb on tb.task_id = t.id
order by t.id
That way you can improve your query.
You shoud also ensure that your keys compared are foreign keys to get faster consults.
I'm trying to create
select c.FullName, b.*, e.Description as Posisilama, f.Description as Posisibaru, g.Description,
GROUP_CONCAT('b.nilai' SEPARATOR ',') AS nilai
from penilaian_header a
left join penilaian_detail b on a.KodePenilaian = b.KodePenilaianH
left join employee c on a.Nip = c.Nip
left join HistoryPosition d on a.KodePenilaian = d.KodePenilaian
left join Position e on d.OldPosition = e.PositionCode
left join Position f on d.NewPosition = f.PositionCode
left join Outlet g on c.OutletCode = g.OutletCode
Can you check my query, because I got this error:
Incorrect syntax near 'SEPARATOR'
What I need to do is to make multiple row result in to one row SQL. Because my result is like this
I found a solution. I use pivot.
select * from
(
select row, c.Nilai,b.Fullname,a.KodePenilaian,d.Description from penilaian_header a
left join employee b on a.Nip = b.Nip
left join outlet d on a.Outlet = d.OutletCode
left join (select ROW_NUMBER() OVER(PARTITION BY KodePenilaianH ORDER BY idPenilaiand DESC) AS Row, Nilai,KodePenilaianH from penilaian_Detail
) c on a.KodePenilaian = c.KodePenilaianH where a.Outlet like '%%' and Periode like '%%'
) nilai
pivot
(
sum(nilai)
for row in ([1],[2],[3],[4],[5])
) piv;
miss a group by clause,check this :
How to use GROUP_CONCAT in a CONCAT in MySQL
i will write like this :
select c.FullName, b.*, e.Description as Posisilama
, f.Description as Posisibaru, g.Description,
GROUP_CONCAT('b.nilai' SEPARATOR ',') AS nilai
from penilaian_header a
left join penilaian_detail b on a.KodePenilaian = b.KodePenilaianH
left join employee c on a.Nip = c.Nip
left join HistoryPosition d on a.KodePenilaian = d.KodePenilaian
left join Position e on d.OldPosition = e.PositionCode
left join Position f on d.NewPosition = f.PositionCode
left join Outlet g on c.OutletCode = g.OutletCode
group by c.FullName, b.*, e.Description
, f.Description , g.Description
Hello I have a problem with the rollup mysql query. I want to get total of each column. I use mysql SUM() and ROLLUP to get the result. But on the new row that is added it copies the data of the last row. How can I remove those data. I provided an image on what I really needed to achieve.
Here is my query.
SELECT IFNULL(payments.id, "General Total"), payments.driver_id, payments.vehicle_specifications_id,payments.admins_id, payments.vehicle_id, payments.boundaries, payments.cashbond_payments, payments.loans, payments.penalties, SUM(payments.total_payments), a.first_name, a.mid_name, a.last_name, d2.date, d.first_name, d.mid_name, d.last_name, v.plate_number, v2.car_rate FROM payments INNER JOIN admins AS a ON payments.admins_id = a.id INNER JOIN drivers AS d ON payments.driver_id = d.id INNER JOIN dispatch AS d2 ON payments.dispatch_id =d2.id INNER JOIN vehicles AS v ON payments.vehicle_id = v.id INNER JOIN vehicle_specifications AS v2 ON payments.vehicle_specifications_id = v2.id GROUP BY payments.id WITH ROLLUP
Try this using union all
SELECT
payments.id,
payments.driver_id,
payments.vehicle_specifications_id,
payments.admins_id,
payments.vehicle_id,
payments.boundaries,
payments.cashbond_payments,
payments.loans,
payments.penalties,
SUM(payments.total_payments),
a.first_name,
a.mid_name,
a.last_name,
d2.date,
d.first_name,
d.mid_name,
d.last_name,
v.plate_number,
v2.car_rate
FROM payments
INNER JOIN admins AS a ON payments.admins_id = a.id
INNER JOIN drivers AS d ON payments.driver_id = d.id
INNER JOIN dispatch AS d2 ON payments.dispatch_id =d2.id
INNER JOIN vehicles AS v ON payments.vehicle_id = v.id
INNER JOIN vehicle_specifications AS v2 ON payments.vehicle_specifications_id = v2.id
GROUP BY payments.id
union all
select
'General Total' as id,
'' as driver_id,
'' as vehicle_specifications_id,
'' as admins_id,
'' as vehicle_id,
'' as boundaries,
'' as cashbond_payments,
'' as loans,
'' as penalties,
SUM(payments.total_payments),
'' as first_name,
'' as mid_name,
'' as last_name,
'' as date,
'' as first_name,
'' as mid_name,
'' as last_name,
'' as plate_number,
'' as car_rate
FROM payments
INNER JOIN admins AS a ON payments.admins_id = a.id
INNER JOIN drivers AS d ON payments.driver_id = d.id
INNER JOIN dispatch AS d2 ON payments.dispatch_id =d2.id
INNER JOIN vehicles AS v ON payments.vehicle_id = v.id
INNER JOIN vehicle_specifications AS v2 ON payments.vehicle_specifications_id = v2.id
i have 2 sql statements which produce the same result, but wondering which one to choose?
lets say 1 have 3 tables:
supplier
supplier_status
supplier_contact
statement 1)
SELECT a.*, b.status_name
(SELECT c.name FROM contact c
WHERE c.supplier_id = a.supplier_id
ORDER BY c.contact_id DESC limit 1) AS contact_name
FROM supplier a LEFT JOIN supplier_status b
ON a.status_id = b.status_id
statement 2)
SELECT a.*, b.status_name, c.name AS contact_name
FROM supplier a LEFT JOIN supplier_status b
ON a.status_id = b.status_id
LEFT JOIN (SELECT name, supplier_id
FROM contact
ORDER BY contact_id DESC
) c ON a.supplier_id = c.supplier_id
GROUP BY a.supplier_id
Try this query:
SELECT a.*, b.status_name, c.name AS contact_name
FROM supplier a
LEFT JOIN supplier_status b ON a.status_id = b.status_id
LEFT JOIN contact c ON a.supplier_id = c.supplier_id
LEFT JOIN contact d ON c.supplier_id = d.supplier_id AND c.contact_id < d.contact_id
WHERE d.contact_id IS NULL
It's possible that it doesn't produce the same result as yours (I didn't test it) but if it does then all you have to do is to make sure the fields that appear in the JOIN conditions are indexed (they probably are FKs, so they already are).
I'm getting "Error Code: 1054. Unknown column 'E1.extensao' in 'field list'". I changed table names but my query is as follows:
SELECT E1.stateName, E1.city, E1.boughtEnough, E2.bought
FROM
(SELECT count(DISTINCT idClient),city,stateName FROM
(SELECT
max(n.ordervalue) as boughtEnough,
m.idClient,
i.nome AS city,
i.id AS cityId,
i.stateName
FROM Clients c
INNER JOIN client_order m ON c.idClient = m.idClient
INNER JOIN orders n ON m.client_order = n.client_order
INNER JOIN orderDetail p ON n.idorder = p.idorder
AND p.idCurso = m.idCurso
INNER JOIN cities i ON c.city = i.id
WHERE
m.idCurso = '10'
GROUP BY
m.idClient,
i.nome,
i.id,
i.stateName
HAVING max(n.orders) >= 6) t
GROUP BY t.city, t.stateName
ORDER BY t.stateName,t.city) E1
JOIN (SELECT count(DISTINCT idClient),city,stateName FROM
(SELECT
count(n.ordervalue) as bought,
m.idClient,
i.nome AS city,
i.id AS cityId,
i.stateName
FROM Clients c
INNER JOIN client_order m ON c.idClient = m.idClient
INNER JOIN orders n ON m.client_order = n.client_order
INNER JOIN orderDetail p ON n.idorder = p.idorder
AND p.idCurso = m.idCurso
INNER JOIN cities i ON c.city = i.id
WHERE
m.idCurso = '10'
GROUP BY
m.idClient,
i.nome,
i.id,
i.stateName
HAVING ((max(n.orders) < 6) AND (count(n.orders) >= 1))) t
GROUP BY t.city, t.stateName
ORDER BY t.stateName,t.city) E2 ON E1.cityId = E2.cityId
I'm more used to SQL Server, not MySQL. What am I getting wrong?
I assume that E1.extensao means E1.boughtEnough? Look closely at how E1 is defined:
(SELECT count(DISTINCT idClient),city,stateName FROM
(SELECT
max(n.ordervalue) as boughtEnough,
...) t
...) E1
There's a t.boughtEnough, but you're not "passing it up" to E1.