I would want to count entries in a column and display the count beside it.
However, I'm clueless on how can I do it.
Desired output:
arrangement_number tray_no rl_type flag(count of occurrence)
------------------ ------- ---- ----
2774818 381001 R 3
2774818 381001 R 3
2774818 381001 L 3
2778470 405128 R 1
2779702 265265 R 2
2779702 265265 R 2
I'm currently trying queries using #variables but I still cant get it right.
each row are unique and I need them not to be grouped.
Update: Expanded Table added source code
Note: I'm currently joining 5 tables now
Actual Query:
SELECT
log.arrangement_number,
header.tray_number,
detail.rl_type,
-- some more fields here
FROM
log
INNER JOIN
header ON log.arrangement_number = header.rxarrangement_number
AND log.production_place_code = header.production_place_code
INNER JOIN
detail ON log.arrangement_number = detail.rxarrangement_number
AND log.production_place_code = detail.production_place_code
INNER JOIN
deliveryperiod ON log.arrangement_number = deliveryperiod.arrangement_number
AND log.production_place_code = deliveryperiod.production_place_code
AND detail.rl_type = deliveryperiod.rl_type
INNER JOIN
calc ON calc.arrangement_number = log.arrangement_number
AND calc.production_place_code = log.production_place_code
AND deliveryperiod.rl_type = calc.rl_type
AND detail.rl_type = calc.rl_type
WHERE
header.status_code IN ('20' , '21')
AND log.process_code = '12'
AND deliveryperiod.process_code_current = '12'
AND deliveryperiod.sub_process_code_current IN ('100' , '105')
AND lot_number = '120131'
ORDER BY log.lot_number , log.sequence_number , deliveryperiod.rl_type DESC
SELECT t1.tray_no,
t2.flag
FROM yourTable
INNER JOIN
(
SELECT tray_no, COUNT(*) AS flag
FROM yourTable
GROUP BY tray_no
) t2
ON t1.tray_no = t2.tray_no
try this...
SELECT tray_no, COUNT(*) 'flag'
FROM table1
GROUP BY tray_no
Related
I am writing some software for a restaurant. They change prices on certain dates. I have two tables, one with the products ordered by order, and another with prices, historical, that is, every time they change a price, I set a bit to 0 to the old value, create new entry and assign bit 1 into "valid" column.
I use the following MySQL code to recall the valid actual price:
`
SELECT
`tbl_precios_productos`.`dbl_precio`
FROM `tbl_precios_productos`
left join `tbl_prods_x_orden` on `tbl_prods_x_orden`.`int_producto_id` = `tbl_precios_productos`.`id_producto`
where `tbl_prods_x_orden`.`int_producto_id` = `tbl_precios_productos`.`id_producto`
and `tbl_precios_productos`.`bit_activo` = 1
And `tbl_precios_productos`.`int_tipo_precio` = 1
and `tbl_prods_x_orden`.`int_producto_id` = 10 /* this is the ID of the product */
GROUP BY `tbl_precios_productos`.`dbl_precio`
`
When I try to nest that code into a bigger one, it doesn't work. Here is what I tried:
`
SELECT
ANY_VALUE( `tbl_productos`.`id_producto`) AS `ID Prod`,
ANY_VALUE( `tbl_productos`.`chr_nombre_prod`) AS `Producto`,
SUM(ANY_VALUE( `tbl_prods_x_orden`.`int_cantidad`)) AS `Cantidad`,
(SELECT
`tbl_precios_productos`.`dbl_precio`
FROM `tbl_precios_productos`
left join `tbl_prods_x_orden` on `tbl_prods_x_orden`.`int_producto_id` = `tbl_precios_productos`.`id_producto`
where `tbl_prods_x_orden`.`int_producto_id` = `tbl_precios_productos`.`id_producto`
and `tbl_precios_productos`.`bit_activo` = 1
And `tbl_precios_productos`.`int_tipo_precio` = 1
and `tbl_prods_x_orden`.`int_producto_id` = `tbl_precios_productos`.`id_producto`
GROUP BY `tbl_precios_productos`.`dbl_precio`) AS `Precio`,
ANY_VALUE( `tbl_tipos_precios`.`id_tipo_precio`) AS `Tipo Precio`,
ANY_VALUE( `tbl_tipos_precios`.`chr_nombre_precio`) AS `CHRTipoPrecio`,
ANY_VALUE( `tbl_ordenes_cerradas`.`int_forma_pago`) AS `ID TPago`,
ANY_VALUE( `tbl_formas_pago`.`chr_forma_pago`) AS `Tipo Pago`,
ANY_VALUE( `tbl_productos`.`fl_ordenar`) AS `Ordenar`
From `tbl_prods_x_orden`
LEFT JOIN `tbl_productos` ON `tbl_prods_x_orden`.`int_producto_id` = `tbl_productos`.`id_producto`
LEFT JOIN `tbl_precios_productos` ON (((`tbl_prods_x_orden`.`int_producto_id` = `tbl_precios_productos`.`id_producto`) And ( `tbl_precios_productos`.`int_tipo_precio` = 1)))
LEFT JOIN `tbl_precio_tipo_ordenes` ON `tbl_prods_x_orden`.`int_orden_id` = `tbl_precio_tipo_ordenes`.`id_orden`
LEFT JOIN `tbl_tipos_precios` ON `tbl_tipos_precios`.`id_tipo_precio` = `tbl_precio_tipo_ordenes`.`id_tipo_precio`
LEFT JOIN `tbl_ordenes_cerradas` ON `tbl_ordenes_cerradas`.`id_orden_id` = `tbl_prods_x_orden`.`int_orden_id`
left join `tbl_formas_pago` ON `tbl_ordenes_cerradas`.`int_forma_pago` = `tbl_formas_pago`.`id_forma_pago`
WHERE `tbl_productos`.`int_activo` = 1
And `tbl_prods_x_orden`.`bool_activo` = '1'
and `tbl_ordenes_cerradas`.`int_forma_pago` = 3
And `tbl_ordenes_cerradas`.`id_control_fecha` >= 101
And `tbl_ordenes_cerradas`.`id_control_fecha` <= 101
group BY `ID Prod`, `Tipo Precio`, `ID TPago`
order by `Ordenar`
`
I get an error that the subquery returns more than one result. How can I get the specific valid price from the prices table using the id of the product. Imagine prices table with 4 columns: id, id_producto, valid, date_set. Sorry some of the code is in spanish, that is my mother language and what I am using to program, but I hope you get the idea.
Am I doing it correctly or is there any other way?
Thanks in advance.
I am using the following query to get COUNT items from rows from the same table in LEFT JOIN.
This is the query:
SELECT
pac.id_sat as id_sat,
pac.nombre_contacto as nombre_contacto,
pac.centro_contacto as centro_contacto,
pac.tel_contacto as tel_contacto,
pac.horario_contacto as horario_contacto,
pac.email_contacto as email_contacto,
pac.num_factura as num_factura,
pac.fecha_factura as fecha_factura,
eq.nombre_equipo as modelo_equipo,
pac.num_serie as num_serie,
pac.tipo_incidencia as tipo_incidencia,
pac.cod_sat as cod_sat,
pac.estado as estado,
pac.clinica as clinica,
pac.fecha_sat as fecha_sat,
COUNT(medfotos.id_media_sat) as num_fotos,
COUNT(medvideos.id_media_sat) as num_videos
FROM tb_sat pac
LEFT JOIN tb_equipos eq ON pac.modelo_equipo = eq.id_equipo
LEFT JOIN tb_media_sat medfotos ON pac.cod_sat = medfotos.cod_sat AND medfotos.tipo = 1
LEFT JOIN tb_media_sat medvideos ON pac.cod_sat = medvideos.cod_sat AND medvideos.tipo = 2
WHERE pac.clinica = '".$idclinica."'
GROUP BY pac.id_sat
ORDER BY pac.fecha_sat DESC
My issue is that I am getting a wrong amount of COUNT items.
The real value for num_fotos should be 3 and for num_videos should be 2.
I am getting num_fotos = 6 and num_videos = 6.
EDIT
Table tb_sat
Table tb_media_sat
Sub-query will work better in your case like as follows:
SELECT pac.*, (SELECT COUNT(id_media_sat) FROM tb_media_sat WHERE cod_sat=pac.cod_sat AND tipo=1) AS num_fotos, (SELECT COUNT(id_media_sat) FROM tb_media_sat WHERE cod_sat=pac.cod_sat AND tipo=2) AS num_videos FROM tb_sat pac WHERE pac.clinica = '".$idclinica."' ORDER BY pac.fecha_sat DESC
Rest columns, please add yourself slowly slowly. I hope you will get correct output.
I have this statement that works fine without the below statement.
I think I may be using the incorrect statement. What I'm trying to do is only select the first productsapplied.applicationid. If another row has the same productsapplied.applicationid as one already selected it won't select it. There can be more than one of the same application id but I need it to only add 1.
DISTINCT('productsapplied'.applicationid)
'SELECT `productsApplied`.id, DISTINCT(`productsApplied`.applicationid)
FROM `productsapplied`
INNER JOIN `products`
ON `productsApplied`.productid = `products`.id
INNER JOIN `applications`
ON `productsApplied`.applicationid = `applications`.id
WHERE `applications`.clubid = ? AND `applications`.area = ? AND EXTRACT(YEAR FROM `applications`.date) = ? AND `products`.producttype = ?
If anyone has any ideas, would appreciate it!
If not I was thinking of just doing a COUNT DISTINCT
Lets say that here are the products applied
id: 3 clubid:6 applicationid: 5 ...
id: 4 clubid:6 applicationid: 5 ...
id: 5 clubid:6 applicationid: 5 ...
id: 4 clubid:6 applicationid: 6 ...
Presuming the rest of the statement holds e.g. year = ? etc..
Then the number of rows returned would be 2. AS there are 3 rows with the same application id. No matter how many rows there are with the same application id, one should be counted.
DISTINCT is applied is to whole row not to a single column for your concern there will be different productsApplied.ids per applicationid so you can grab all by using group_concat
SELECT
GROUP_CONCAT(pa.id),
`pa`.applicationid
FROM
`productsapplied` pa
INNER JOIN `products` p
ON `pa`.productid = `p`.id
INNER JOIN `applications` a
ON `pa`.applicationid = `a`.id
WHERE `a`.clubid = ?
AND `a`.area = ?
AND EXTRACT(YEAR FROM `a`.date) = ?
AND `p`.producttype = ?
GROUP BY `pa`.applicationid
If you are not concerned with productsApplied.ids then you can simple use the group by part but note group by without aggregate function will result in indeterminate order
Edit
This will give you one max id per applicationid
SELECT
SUBSTRING_INDEX(GROUP_CONCAT(pa.id ORDER BY pa.id DESC), 1),
`pa`.applicationid
FROM
`productsapplied` pa
INNER JOIN `products` p
ON `pa`.productid = `p`.id
INNER JOIN `applications` a
ON `pa`.applicationid = `a`.id
WHERE `a`.clubid = ?
AND `a`.area = ?
AND EXTRACT(YEAR FROM `a`.date) = ?
AND `p`.producttype = ?
GROUP BY `pa`.applicationid
or
SELECT
MAX(pa.id),
`pa`.applicationid
FROM
`productsapplied` pa
INNER JOIN `products` p
ON `pa`.productid = `p`.id
INNER JOIN `applications` a
ON `pa`.applicationid = `a`.id
WHERE `a`.clubid = ?
AND `a`.area = ?
AND EXTRACT(YEAR FROM `a`.date) = ?
AND `p`.producttype = ?
GROUP BY `pa`.applicationid
Select min(pa.id), pa.applicationid
From productsapplied pa
Join ...
...
Group by pa.applicationid
I have two tables:
t1
------------------
inv_ID
inv_memID
inv_projID
t2
------------------
is_ID
is_msgID
is_contID
I need to get all t2.is_contID into an array where
inv_projID = 5
t2.is_msgID = t1.inv_ID and
t1.inv_memID = 1
Seem pretty straight forward but I'm stuck... Tried this:
SELECT t2.is_contID
INNER JOIN t1 ON (t1.inv_ID = t2.is_msgID)
FROM t2
WHERE t1.inv_projID = 5
AND t1.inv_memID = 1
What am I missing?
FROM comes before JOIN.
SELECT t2.is_contID
FROM t2
INNER JOIN t1 ON (t1.inv_ID = t2.is_msgID)
WHERE t1.inv_projID = 5
AND t1.inv_memID = 1
SQL is very fussy about the order of the keywords.
The correct order is:
SELECT
FROM
JOIN
WHERE
HAVING
GROUP BY
ORDER
LIMIT <<-- MySQL only, other DB's user other keywords in other places.
To begin with I have 4 tables I am dealing with.
I have a classes table that is a 1->N relationship with a sections table which also has a 1->N relationship with a lessons table.
So to put it in perpective:
Classes
Sections
Lessons
The last table is an activityLog, when the student accesses a lesson this is recorded using the following:
ActivityLog Row -> actorID (user ID), classID, sectionID, lessonID
I want to pull out the last 5 unique lessons the student has visited. I tried using both DISTINCT and GROUP BY without success.
The same records are being returned each time, not the latest classes that they have visited.
Using GROUP BY
SELECT activityLog.actorID, activityLog.activityDate,
strClasses.classID, strClasses.className,
strSections.sectionID, strSections.sectionName,
strLessons.lessonID, strLessons.lessonName
FROM activityLog
LEFT JOIN strClasses ON strClasses.classID = activityLog.classID
LEFT JOIN strSections ON strSections.sectionID = activityLog.sectionID
LEFT JOIN strLessons ON strLessons.lessonID = activityLog.lessonID
WHERE activityLog.activityTypeID = 6 AND activityLog.actorID = 3
GROUP BY activityLog.lessonID
ORDER BY activityLog.activityDate DESC
LIMIT 5
Using DISTINCT
SELECT DISTINCT activityLog.actorID,
strClasses.classID, strClasses.className,
strSections.sectionID, strSections.sectionName,
strLessons.lessonID, strLessons.lessonName
FROM activityLog
LEFT JOIN strClasses ON strClasses.classID = activityLog.classID
LEFT JOIN strSections ON strSections.sectionID = activityLog.sectionID
LEFT JOIN strLessons ON strLessons.lessonID = activityLog.lessonID
WHERE activityLog.activityTypeID = 6 AND activityLog.actorID = 3
ORDER BY activityLog.activityDate DESC
LIMIT 5
I cannot figure out why the latest records are not being displayed.
Based on your change, how does this suit you?
SELECT activityLog.actorID, activityLog.activityDate,
strClasses.classID, strClasses.className,
strSections.sectionID, strSections.sectionName,
strLessons.lessonID, strLessons.lessonName
FROM activityLog
LEFT JOIN strClasses ON strClasses.classID = activityLog.classID
LEFT JOIN strSections ON strSections.sectionID = activityLog.sectionID
LEFT JOIN strLessons ON strLessons.lessonID = activityLog.lessonID
WHERE activityLog.activityTypeID = 6 AND activityLog.actorID = 3
AND activityLog.activityDate = (SELECT MAX(activityDate) FROM activityLog AS lookup WHERE lessonID = activityLog.lessonID)
ORDER BY activityLog.activityDate DESC
LIMIT 5
Based on your description, I'm not sure why you're using LEFT JOIN, but I've left it in just in case.
Try group by like below
GROUP BY activityLog.classID,activityLog.sectionID,activityLog.lessonID
I think it will work, or just sent me create scripts for these I will create that query
Well, there's got to be a datetime in the ActivityLog I hope... so Try this:
Select s.Name, c.ClassName
From Students s
left Join On Classes c
On c.ClassId In
(Select Distinct ClassId From Classes
Where (Select Count(Distinct ClassId) From Classes ic
Join ActivityLog l On l.UserId = s.UserId
And l.ClassId = c.ClassId
Where classId = c.ClassId
And activityDateTime > l.activityDateTime)
< 5)