{"id":1124,"date":"2024-07-31T11:20:30","date_gmt":"2024-07-31T09:20:30","guid":{"rendered":"https:\/\/blog.sutilweb.eu\/?page_id=1124"},"modified":"2024-07-31T11:20:31","modified_gmt":"2024-07-31T09:20:31","slug":"20-clausula-group-by","status":"publish","type":"page","link":"https:\/\/sutilweb.eu\/index.php\/databases\/sql\/20-clausula-group-by\/","title":{"rendered":"20. Cla\u00fasula GROUP BY"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Cuando estamos ejecutando una <strong>funci\u00f3n de agrupamiento<\/strong>, necesitamos utilizar la clausula <strong><em>GROUP BY<\/em><\/strong> permite utilizar el mismo campo dos veces y no nos de error. Veamos un ejm de su uso.<\/p>\n\n\n\n<!--more-->\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Ejm<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">SELECT precio, MAX(precio) AS precio_maximo FROM productos GROUP BY precio;\nSELECT nombre, precio, MAX(precio) AS precio_maximo FROM productos GROUP BY\nprecio, nombre<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Veamos otro ejm.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Ejm<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">-- Creamos la DB musica\nCREATE DATABASE musica\n\n-- Usamos la DB musica\nUSE musica;\n\n-- Creamos tabla bandas\nCREATE TABLE bandas (\nid INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,\nnombre VARCHAR(250),\nestilo VARCHAR(250)\n);\n\n-- Inclu\u00edmos registros\nINSERT INTO bandas (id, nombre, estilo) VALUES\n(0, \"Iron Maiden\",\"Heavy Metal\"),\n(0, \"Metallica\", \"Thrash Metal\"),\n(0, \"Judas Priest\", \"Heavy Metal\")\n\n-- Hacemos consulta tipo GROUP BY que nos cuente por estilos\nSELECT estilo, COUNT(*)&nbsp; AS total FROM bandas GROUP BY estilo;<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Si quisi\u00e9ramos incluir una clausula <strong><em>WHERE<\/em><\/strong> para acotar nuestras consultas, dicho <strong><em>WHERE<\/em><\/strong> tiene que ir antes de la clausula <strong><em>GROUP BY<\/em><\/strong>, de la siguiente manera.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">SELECT estilo, COUNT(*) AS total FROM bandas WHERE pais = \"EEUU\" GROUP BY estilo;<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Esta clausula nos mostrar\u00e1 el total de estilos de las bandas incluidas en nuestra base de datos que sean de EEUU.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Cuando estamos ejecutando una funci\u00f3n de agrupamiento, necesitamos utilizar la clausula GROUP BY permite utilizar el mismo campo dos veces [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1076,"menu_order":19,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"class_list":["post-1124","page","type-page","status-publish","hentry"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"Sutil Web","author_link":"https:\/\/sutilweb.eu\/index.php\/author\/sutilweb\/"},"uagb_comment_info":0,"uagb_excerpt":"Cuando estamos ejecutando una funci\u00f3n de agrupamiento, necesitamos utilizar la clausula GROUP BY permite utilizar el mismo campo dos veces [&hellip;]","_links":{"self":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/1124","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/comments?post=1124"}],"version-history":[{"count":1,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/1124\/revisions"}],"predecessor-version":[{"id":1125,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/1124\/revisions\/1125"}],"up":[{"embeddable":true,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/1076"}],"wp:attachment":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/media?parent=1124"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}