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
Related
I´m trying to make a query listing all clients, and also get the last comment and
the date of that comment inside the table of history_client inside a single query for it to be listed.
select a.id_client,a.name,a.lastname,(select b.date_created,b.comentary
from history_of_client b where a.id_client = b.id_client_asociate) from clients_main_table
You could use an inner join on max(date_created) for id_client on history table and join
SELECT a.id_client,a.name,a.lastname, h.commentary
FROM clients_main_table a
INNER join (
select b.id_client_asociate, max(b.date_created) max_date
from history_of_client
group by b.id_client_asociate ) t on t.id_client_asociate = a.id_client
INNER JOIN history_of_client h on h.id_client_asociate = t.id_client_asociate
and h.date_created = t.max_date
Use the LEFT JOIN and INNER join to get your desired result set.
select a.id_client,
a.name,
a.lastname,
hc.date_created,
hc.comentary
from clients_main_table c
left join (select id_client_asociate,max(date_created) dt from history_of_client group by id_client_asociate) h
on (c.id_client = b.id_client_asociate)
inner join history_of_client hc
on (hc.id_client_asociate = b.id_client_asociate and hc.date_created = h.date_created)
I have this MySQL query:
SELECT * FROM A
LEFT JOIN B
ON B.id = A.id
LEFT JOIN C
ON C.other = A.other
WHERE
(SELECT SUM(C.quantity) FROM C WHERE C.other = A.other) < A.quantity
Is any way to get values from SELECT from WHERE clause? For example for PHP to use? Something like:
WHERE
(SELECT SUM(C.quantity) FROM C WHERE C.other = A.other) AS quantity < A.quantity
I need to compare this two values in mysql and then i need them both in my script. I can of course put this subquery into first SELECT - but i belive there is better way to do this than repeat code in many places.
You could do that as follows:
SELECT A.*, B.*, C.*, D.sumq
FROM A
LEFT JOIN B
ON B.id = A.id
LEFT JOIN C
ON C.other = A.other
LEFT JOIN (
SELECT other, SUM(quantity) sumq
FROM C
GROUP BY other
) AS D
ON D.other = A.other
WHERE COALESCE(D.sumq, 0) < A.quantity
I have a big multiple table query join select where some values are optional.
This is the query:
SELECT a.date_in, a.date_out, b.name, b.phone, b.birthdate,
b.country, b.hotel, b.room_nr, b.passport_nr, c.email,
d.size, e.name, GROUP_CONCAT(DISTINCT g.service), GROUP_CONCAT(DISTINCT h.service)
, GROUP_CONCAT(DISTINCT i.time), GROUP_CONCAT(DISTINCT j.location)
FROM reservation a, rider b, user c,
bike_size d, bike e, services_reservation f, services g,
bike_shipping h, bike_shipping_reservation i
, bike_shipping_location j WHERE a.rider_id = b.id
AND b.user_id = c.id AND a.bike_size_id = d.id AND
d.bike_id = e.id AND a.id = f.reservation_id AND
f.services_id = g.id
AND h.id = i.bike_shipping_id AND a.id = i.reservation_id
AND i.bike_shipping_location_id = j.id
AND a.id = 80
In the tables from the query above, the table named services_reservation with the following columns (id, services_id, reservation_id) is completely empty in this case, which makes the values that I select from the table bike_shipping_reservation NULL.
How can I make some tables that I select from optional in case they are empty?
Here is the SQL Fiddle with 1 empty table, you can see the NULL results at the end (only GROUP_CONCAT(DISTINCT g.service) should be NULL).
http://sqlfiddle.com/#!9/ee31b/6
Here is the SQL Fiddle with all tables having values in there columns, you can see that all values are returned not NULL.
http://sqlfiddle.com/#!9/8bc033/34
Any thoughts?
Use left join where the table (or the row) are empty on don't match
SELECT a.date_in, a.date_out, b.name, b.phone, b.birthdate,
b.country, b.hotel, b.room_nr, b.passport_nr, c.email,
d.size, e.name, GROUP_CONCAT(DISTINCT g.service), GROUP_CONCAT(DISTINCT h.service)
, GROUP_CONCAT(DISTINCT i.time), GROUP_CONCAT(DISTINCT j.location)
FROM reservation a
INNER JOIN rider b on a.rider_id = b.id
INNER JOIN user c on b.user_id = c.id
INNER JOIN bike_size d on a.bike_size_id = d.id
INNER JOIN bike e ON d.bike_id = e.id
LEFT JOIN services_reservation f on a.id = f.reservation_id
INNER JOIN services g on f.services_id = g.id
INNER JOIN bike_shipping_reservation i on a.id = i.reservation_id
INNER JOIN bike_shipping h ON h.id = i.bike_shipping_id
INNER JOIN bike_shipping_location j on i.bike_shipping_location_id = j.id
where a.id = 80
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.
I'm facing an issue where I have several tables joined, but I need to combine the e.url and f.url_new columns. Otherwise, the result is as expected. Here is my query.
SELECT a.`added_date`,b.`type`,c.`action`,e.`url`,f.`url_new`
FROM `websites_submitted_main` a
INNER JOIN `websites_submitted_type` b ON a.`type_id` = b.`type_id`
INNER JOIN `websites_submitted_action` c ON a.`action_id` = c.`action_id`
INNER JOIN `users` d ON a.`user_id` = d.`user_id`
LEFT JOIN `websites` e ON a.`url_id` = e.`id`
LEFT JOIN `websites_submitted_new` f ON a.`url_new_id` = f.`url_new_id`
WHERE a.`user_id` = 1 ORDER BY a.`added_date` DESC
I've tried CONCAT, but the column contained all NULL values. Here's that query.
SELECT a.`added_date`,b.`type`,c.`action`,CONCAT(e.`url`,f.`url_new`) AS url
FROM `websites_submitted_main` a
INNER JOIN `websites_submitted_type` b ON a.`type_id` = b.`type_id`
INNER JOIN `websites_submitted_action` c ON a.`action_id` = c.`action_id`
INNER JOIN `users` d ON a.`user_id` = d.`user_id`
LEFT JOIN `websites` e ON a.`url_id` = e.`id`
LEFT JOIN `websites_submitted_new` f ON a.`url_new_id` = f.`url_new_id`
WHERE a.`user_id` = 1 ORDER BY a.`added_date` DESC
Is there a minor modification I can make to this query to merge these columns?
try using CONCAT_WS() instead of CONCAT()
SELECT `CONCAT_WS(' ',e.url, f.url_new )` ....
CONCAT_WS() will concatenate the values if the values are not null.
From the manual of CONCAT()
CONCAT() returns NULL if any argument is NULL.
SELECT a.`added_date`,b.`type`,c.`action`,CONCAT_WS("", e.`url`,f.`url_new`) AS url
FROM `websites_submitted_main` a
INNER JOIN `websites_submitted_type` b ON a.`type_id` = b.`type_id`
INNER JOIN `websites_submitted_action` c ON a.`action_id` = c.`action_id`
INNER JOIN `users` d ON a.`user_id` = d.`user_id`
LEFT JOIN `websites` e ON a.`url_id` = e.`id`
LEFT JOIN `websites_submitted_new` f ON a.`url_new_id` = f.`url_new_id`
WHERE a.`user_id` = 1 ORDER BY a.`added_date` DESC