{"id":2484,"date":"2021-01-10T21:43:11","date_gmt":"2021-01-10T21:43:11","guid":{"rendered":"http:\/\/manualsqlserver.com\/?p=2484"},"modified":"2021-01-10T21:43:12","modified_gmt":"2021-01-10T21:43:12","slug":"construyendo-cte-en-sql-server","status":"publish","type":"post","link":"https:\/\/manualsqlserver.com\/?p=2484","title":{"rendered":"Construyendo CTE en SQL Server"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/manualsqlserver.com\/wp-content\/uploads\/2021\/01\/ManualSQL_CreandoCTE__D.png\"><img loading=\"lazy\" decoding=\"async\" width=\"220\" height=\"260\" src=\"https:\/\/manualsqlserver.com\/wp-content\/uploads\/2021\/01\/ManualSQL_CreandoCTE__D.png\" alt=\"\" class=\"wp-image-2485\"\/><\/a><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Construyendo CTE<\/h2>\n\n\n\n<p>En este art\u00edculo se explicar\u00e1 como construir una CTE, Common Table Expressi\u00f3n o Expresi\u00f3n de tabla com\u00fan, sus usos son muy diversos y necesarios para simplificar consultas con referencias a varias tablas o con muchos filtros.<\/p>\n\n\n\n<!--more-->\n\n\n\n<script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script>\n<ins class=\"adsbygoogle\"\n     style=\"display:block; text-align:center;\"\n     data-ad-layout=\"in-article\"\n     data-ad-format=\"fluid\"\n     data-ad-client=\"ca-pub-2636008503986218\"\n     data-ad-slot=\"1902193434\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n\n\n\n<p><strong>Para m\u00e1s informaci\u00f3n Ver: <a href=\"https:\/\/manualsqlserver.com\/?p=514\" target=\"_blank\" rel=\"noreferrer noopener\">CTE en SQL Server<\/a><\/strong><\/p>\n\n\n\n<p>Una expresi\u00f3n de tabla com\u00fan (CTE) es un conjunto de resultados temporal definido en la ejecuci\u00f3n de una instrucci\u00f3n SELECT, INSERT, UPDATE, DELETE o CREATE VIEW. Es como asignar un nombre a una consulta pero sin almacenarla en la base de datos como el caso de las vistas. (<a href=\"https:\/\/manualsqlserver.com\/?p=310\" target=\"_blank\" rel=\"noreferrer noopener\">Ver Vistas<\/a>)<\/p>\n\n\n\n<p>Una CTE es similar a una tabla derivada (<strong><a rel=\"noreferrer noopener\" href=\"https:\/\/manualsqlserver.com\/?p=664\" target=\"_blank\">Ver Subconsultas como tablas derivadas<\/a><\/strong>) en que no se almacena como un objeto y dura s\u00f3lo el tiempo que dura la consulta. A diferencia de una tabla derivada, una CTE puede hacer referencia a s\u00ed misma y se puede hacer referencia a ella varias veces en la misma consulta.<\/p>\n\n\n\n<p style=\"font-size:22px\"><strong>Una CTE se puede usar para:<\/strong><\/p>\n\n\n\n<ul class=\"has-background wp-block-list\" style=\"background-color:#b4e9fa\"><li><strong>Crear una consulta recursiva.<\/strong><\/li><li><strong>Sustituir la creaci\u00f3n de una vista cuando el uso de una vista no sea necesario; es decir, cuando no se tenga que almacenar la definici\u00f3n de la vista en la base de datos.<\/strong><\/li><li><strong>Hacer referencia a la tabla resultante varias veces en la misma instrucci\u00f3n.<\/strong><\/li><li><strong>Las CTE tiene ventajas de legibilidad mejorada y facilidad de mantenimiento de consultas complejas.<\/strong><\/li><li><strong>Las CTE se pueden definir en rutinas definidas por el usuario, como funciones, procedimientos almacenados, desencadenadores o vistas.<\/strong><\/li><\/ul>\n\n\n\n<p style=\"font-size:24px\"><strong>Ejercicios<\/strong><\/p>\n\n\n\n<p><strong>Usando la base de datos Northwind<br><\/strong>use Northwind<br>go<\/p>\n\n\n\n<p style=\"font-size:22px\"><strong>Ejercicio 1<\/strong><\/p>\n\n\n\n<p><strong>Crear una CTE para los productos, incluir el proveedor y la categoria<br>La instrucci\u00f3n select es como sigue<\/strong><br>select<br>P.ProductID, P.ProductName, P.UnitPrice, P.UnitsInStock,<br>S.CompanyName, C.CategoryName<br>from Products As P<br>join Suppliers As S on P.SupplierID = S.SupplierID<br>join Categories As C on P.CategoryID = C.CategoryID<br>go<br><strong>El resultado se muestra en la siguiente imagen<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><a href=\"https:\/\/manualsqlserver.com\/wp-content\/uploads\/2021\/01\/ManualSQL_CreandoCTE_01.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/manualsqlserver.com\/wp-content\/uploads\/2021\/01\/ManualSQL_CreandoCTE_01-1024x550.png\" alt=\"\" class=\"wp-image-2486\" width=\"770\" height=\"413\" srcset=\"https:\/\/manualsqlserver.com\/wp-content\/uploads\/2021\/01\/ManualSQL_CreandoCTE_01-1024x550.png 1024w, https:\/\/manualsqlserver.com\/wp-content\/uploads\/2021\/01\/ManualSQL_CreandoCTE_01-300x161.png 300w, https:\/\/manualsqlserver.com\/wp-content\/uploads\/2021\/01\/ManualSQL_CreandoCTE_01-768x413.png 768w, https:\/\/manualsqlserver.com\/wp-content\/uploads\/2021\/01\/ManualSQL_CreandoCTE_01-795x427.png 795w, https:\/\/manualsqlserver.com\/wp-content\/uploads\/2021\/01\/ManualSQL_CreandoCTE_01.png 1033w\" sizes=\"auto, (max-width: 770px) 100vw, 770px\" \/><\/a><\/figure>\n\n\n\n<p><strong>Como se puede ver, los campos resultantes de la consulta son:<\/strong><br>ProductID, ProductName, UnitPrice, UnitsInStock,<br>CompanyName, CategoryName<br><strong>Para crear una CTE se puede especificar un nombre para cada uno de los campos de la consulta<br><\/strong>ProductID ser\u00e1 Codigo<br>ProductName ser\u00e1 Descripci\u00f3n<br>UnitPrice ser\u00e1 Precio<br>UnitsInStock ser\u00e1 Stock<br>CompanyName ser\u00e1 Proveedor<br>CategoryName ser\u00e1 Categor\u00eda<\/p>\n\n\n\n<p><strong>Teniendo en cuenta la estructura de la CTE<br><\/strong>WITH NombreCTE [ ( NombreColumna [,\u2026n] ) ]<br>AS<br>( Consulta compleja )<\/p>\n\n\n\n<p><strong>La CTE para este ejercicio ser\u00e1 como sigue, note que al final se debe listar los registros usando la CTE creada<br><\/strong>With ProductosDatos<br>(C\u00f3digo, Descripci\u00f3n, Precio,<br>Stock, Proveedor, Categor\u00eda)<br>As<br>(<br>select<br>P.ProductID, P.ProductName, P.UnitPrice, P.UnitsInStock,<br>S.CompanyName, C.CategoryName<br>from Products As P<br>join Suppliers As S on P.SupplierID = S.SupplierID<br>join Categories As C on P.CategoryID = C.CategoryID<br>)<br>select * from ProductosDatos<br>go<br><strong>El resultado se muestra en la siguiente imagen<\/strong><br>Note que los nombres de las columnas en el resultado son los especificados en la definici\u00f3n de la CTE.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><a href=\"https:\/\/manualsqlserver.com\/wp-content\/uploads\/2021\/01\/ManualSQL_CreandoCTE_02.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/manualsqlserver.com\/wp-content\/uploads\/2021\/01\/ManualSQL_CreandoCTE_02-1024x494.png\" alt=\"\" class=\"wp-image-2487\" width=\"702\" height=\"338\" srcset=\"https:\/\/manualsqlserver.com\/wp-content\/uploads\/2021\/01\/ManualSQL_CreandoCTE_02-1024x494.png 1024w, https:\/\/manualsqlserver.com\/wp-content\/uploads\/2021\/01\/ManualSQL_CreandoCTE_02-300x145.png 300w, https:\/\/manualsqlserver.com\/wp-content\/uploads\/2021\/01\/ManualSQL_CreandoCTE_02-768x371.png 768w, https:\/\/manualsqlserver.com\/wp-content\/uploads\/2021\/01\/ManualSQL_CreandoCTE_02-795x384.png 795w, https:\/\/manualsqlserver.com\/wp-content\/uploads\/2021\/01\/ManualSQL_CreandoCTE_02.png 1129w\" sizes=\"auto, (max-width: 702px) 100vw, 702px\" \/><\/a><\/figure>\n\n\n\n<p style=\"font-size:22px\"><strong>Ejercicio 2<\/strong><\/p>\n\n\n\n<p><strong>Crear una CTE para las \u00f3rdenes no atendidas en 1998, incluir el nombre del cliente y el empleado La instrucci\u00f3n select es como sigue<br><\/strong>select<br>O.OrderID, O.OrderDate,<br>CONCAT_WS(space(1), E.LastName, E.FirstName),<br>C.CompanyName<br>from Orders As O<br>join Customers As C on O.CustomerId= C.CustomerID<br>join Employees As E on O.EmployeeID = E.EmployeeID<br>where O.ShippedDate is null and year(O.OrderDate) = 1998<br>go<br><strong>El resultado se muestra en la siguiente imagen<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><a href=\"https:\/\/manualsqlserver.com\/wp-content\/uploads\/2021\/01\/ManualSQL_CreandoCTE_03.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/manualsqlserver.com\/wp-content\/uploads\/2021\/01\/ManualSQL_CreandoCTE_03.png\" alt=\"\" class=\"wp-image-2488\" width=\"746\" height=\"541\" srcset=\"https:\/\/manualsqlserver.com\/wp-content\/uploads\/2021\/01\/ManualSQL_CreandoCTE_03.png 730w, https:\/\/manualsqlserver.com\/wp-content\/uploads\/2021\/01\/ManualSQL_CreandoCTE_03-300x218.png 300w\" sizes=\"auto, (max-width: 746px) 100vw, 746px\" \/><\/a><\/figure>\n\n\n\n<p><strong>Como se puede ver, los campos resultantes de la consulta son:<br><\/strong>OrderID<br>OrderDate<br>CONCAT_WS(space(1), E.LastName, E.FirstName)<br>CompanyName<br><strong>Para crear una CTE se puede especificar un nombre para cada uno de los campos de la consulta<br><\/strong>OrderID ser\u00e1 N\u00ba Orden<br>OrderDate ser\u00e1 Fecha<br>CONCAT_WS(space(1), E.LastName, E.FirstName) ser\u00e1 Empleado<br>CompanyName ser\u00e1 Cliente<br><strong>La CTE para este ejercicio ser\u00e1 como sigue<\/strong><br>with OrdenesNoAtendidas1998<br>([N\u00ba Orden], Fecha, Empleado, Cliente)<br>As<br>(<br>select<br>O.OrderID, O.OrderDate,<br>CONCAT_WS(space(1), E.LastName, E.FirstName),<br>C.CompanyName<br>from Orders As O<br>join Customers As C on O.CustomerId= C.CustomerID<br>join Employees As E on O.EmployeeID = E.EmployeeID<br>where O.ShippedDate is null and year(O.OrderDate) = 1998<br>)<br>Select * from OrdenesNoAtendidas1998<br>go<br><strong>El resultado se muestra en la siguiente imagen<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><a href=\"https:\/\/manualsqlserver.com\/wp-content\/uploads\/2021\/01\/ManualSQL_CreandoCTE_04.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/manualsqlserver.com\/wp-content\/uploads\/2021\/01\/ManualSQL_CreandoCTE_04.png\" alt=\"\" class=\"wp-image-2489\" width=\"798\" height=\"594\" srcset=\"https:\/\/manualsqlserver.com\/wp-content\/uploads\/2021\/01\/ManualSQL_CreandoCTE_04.png 714w, https:\/\/manualsqlserver.com\/wp-content\/uploads\/2021\/01\/ManualSQL_CreandoCTE_04-300x224.png 300w\" sizes=\"auto, (max-width: 798px) 100vw, 798px\" \/><\/a><\/figure>\n\n\n\n<p style=\"font-size:22px\"><strong>Ejercicio 3<\/strong><\/p>\n\n\n\n<p><strong>Crear una CTE para los clientes, la cantidad de \u00f3rdenes y el total de cada orden.<br>La cantidad de \u00f3rdenes y el total de cada orden obtenerlas usando una FDU.  Las FDU para este ejercicio son como sigue<\/strong>. (<a href=\"https:\/\/manualsqlserver.com\/?p=335\" target=\"_blank\" rel=\"noreferrer noopener\">Ver Funciones definidas por el usuario<\/a>)<br><strong>La funci\u00f3n definida por el usuario que devuelve la cantidad de \u00f3rdenes<\/strong><br>Create or alter function dbo.fduCantidadOrdenesCliente (@CodigoCliente nchar(5))<br>Returns Int<br>As<br>Begin<br>Declare @Cantidad Int<br>Set @Cantidad =<br>(Select Count(O.OrderID) from Orders As O where O.CustomerId = @CodigoCliente)<br>Return @Cantidad<br>End<br>go<br><strong>La funci\u00f3n definida por el usuario que devuelve el total de cada orden<br><\/strong>Create or alter function dbo.fduTotalOrden (@NumeroOrden int)<br>Returns Numeric(9,2)<br>As<br>Begin<br>Declare @Total Numeric(9,2)<br>Set @Total =<br>(Select sum((D.Quantity * D.UnitPrice)*( 1- D.Discount))<br>from [Order Details] As D where D.OrderID= @NumeroOrden)<br>Return @Total<br>End<br>go<br><strong>La instrucci\u00f3n Select es como sigue<br><\/strong>Select<br>C.CustomerID, C.CompanyName, C.Country,<br>dbo.fduCantidadOrdenesCliente(C.CustomerID),<br>Sum(dbo.fduTotalOrden(O.OrderID))<br>from Customers As C<br>join Orders As O on C.CustomerID = O.CustomerID<br>group by<br>C.CustomerID, C.CompanyName, C.Country,<br>dbo.fduCantidadOrdenesCliente(C.CustomerID)<br>go<br><strong>La CTE para este ejercicio es como sigue<br><\/strong>With ClientesVentas<br>([C\u00f3d. Cliente],Cliente, Pa\u00eds,<br>[Cantidad \u00d3rdenes], [Monto total])<br>As<br>(<br>Select<br>C.CustomerID, C.CompanyName, C.Country,<br>dbo.fduCantidadOrdenesCliente(C.CustomerID),<br>Sum(dbo.fduTotalOrden(O.OrderID))<br>from Customers As C<br>join Orders As O on C.CustomerID = O.CustomerID<br>group by<br>C.CustomerID, C.CompanyName, C.Country,<br>dbo.fduCantidadOrdenesCliente(C.CustomerID)<br>)<br>Select * from ClientesVentas<br>go<br><strong>El resultado se muestra en la siguiente imagen<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><a href=\"https:\/\/manualsqlserver.com\/wp-content\/uploads\/2021\/01\/ManualSQL_CreandoCTE_05.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/manualsqlserver.com\/wp-content\/uploads\/2021\/01\/ManualSQL_CreandoCTE_05.png\" alt=\"\" class=\"wp-image-2490\" width=\"807\" height=\"543\" srcset=\"https:\/\/manualsqlserver.com\/wp-content\/uploads\/2021\/01\/ManualSQL_CreandoCTE_05.png 791w, https:\/\/manualsqlserver.com\/wp-content\/uploads\/2021\/01\/ManualSQL_CreandoCTE_05-300x202.png 300w, https:\/\/manualsqlserver.com\/wp-content\/uploads\/2021\/01\/ManualSQL_CreandoCTE_05-768x517.png 768w\" sizes=\"auto, (max-width: 807px) 100vw, 807px\" \/><\/a><\/figure>\n\n\n\n<script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script>\n<ins class=\"adsbygoogle\"\n     style=\"display:block; text-align:center;\"\n     data-ad-layout=\"in-article\"\n     data-ad-format=\"fluid\"\n     data-ad-client=\"ca-pub-2636008503986218\"\n     data-ad-slot=\"1902193434\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n\n\n\n<p style=\"font-size:22px\"><strong>Ejercicio 4<\/strong><\/p>\n\n\n\n<p><strong>Creando una CTE sin especificar el nombre de las columnas.<br>En este ejercicio se va a listar los productos y luego usando una CTE listar los productos con precios entre 20 y 50 ordenados por precio descendente.<br><\/strong>With Productos2050<br>As<br>(select P.ProductID, P.ProductName, P.UnitPrice<br>from Products As P)<br>Select * from Productos2050<br>where UnitPrice between 20 and 50<br>order by UnitPrice desc<br>go<br><strong>El resultado se muestra en la siguiente imagen<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><a href=\"https:\/\/manualsqlserver.com\/wp-content\/uploads\/2021\/01\/ManualSQL_CreandoCTE_06.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/manualsqlserver.com\/wp-content\/uploads\/2021\/01\/ManualSQL_CreandoCTE_06.png\" alt=\"\" class=\"wp-image-2491\" width=\"675\" height=\"703\" srcset=\"https:\/\/manualsqlserver.com\/wp-content\/uploads\/2021\/01\/ManualSQL_CreandoCTE_06.png 511w, https:\/\/manualsqlserver.com\/wp-content\/uploads\/2021\/01\/ManualSQL_CreandoCTE_06-288x300.png 288w\" sizes=\"auto, (max-width: 675px) 100vw, 675px\" \/><\/a><\/figure>\n\n\n\n<p style=\"font-size:22px\"><strong>Ejercicio 5<\/strong><\/p>\n\n\n\n<p style=\"font-size:22px\"><strong>CTE para una relaci\u00f3n recursiva<\/strong><\/p>\n\n\n\n<p><strong>En este ejercicio se va a listar los Empleados y sus subordinados.<\/strong><br>with ReporteJefeSubordinado<br>([C\u00f3d. Jefe],[Apellido] ,[Nombre])<br>As<br>(select EmployeeID, LastName, FirstName from Employees)<br>Select<br>R.[C\u00f3d. Jefe] ,<br>CONCAT_WS(Space(1), R.Apellido , R.Nombre ) As &#8216;Jefe&#8217;,<br>E.EmployeeID As &#8216;C\u00f3d. Empleado&#8217;,<br>CONCAT_WS(Space(1), E.FirstName, E.LastName ) As &#8216;Subordinado&#8217;<br>from ReporteJefeSubordinado As R<br>join Employees As E on R.[C\u00f3d. Jefe] = E.ReportsTo<br>go<br><strong>El resultado se muestra en la siguiente imagen<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><a href=\"https:\/\/manualsqlserver.com\/wp-content\/uploads\/2021\/01\/ManualSQL_CreandoCTE_07.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/manualsqlserver.com\/wp-content\/uploads\/2021\/01\/ManualSQL_CreandoCTE_07.png\" alt=\"\" class=\"wp-image-2492\" width=\"820\" height=\"409\" srcset=\"https:\/\/manualsqlserver.com\/wp-content\/uploads\/2021\/01\/ManualSQL_CreandoCTE_07.png 585w, https:\/\/manualsqlserver.com\/wp-content\/uploads\/2021\/01\/ManualSQL_CreandoCTE_07-300x150.png 300w\" sizes=\"auto, (max-width: 820px) 100vw, 820px\" \/><\/a><\/figure>\n\n\n\n<p style=\"font-size:22px\"><strong>Ejercicio 6<\/strong><\/p>\n\n\n\n<p><strong>Listar los clientes y el total de compras por a\u00f1o. En este ejercicio vamos a usar una CTE para realizar un Pivot. Se van a usar la FDU del ejercicio 3 que calcula el total de la orden.<br><\/strong>With ClientesVentas<br>([C\u00f3d. Cliente],Cliente, A\u00f1o,<br>[Monto total])<br>As<br>(<br>Select<br>C.CustomerID, C.CompanyName, year(O.OrderDate),<br>Sum(dbo.fduTotalOrden(O.OrderID))<br>from Customers As C<br>join Orders As O on C.CustomerID = O.CustomerID<br>group by<br>C.CustomerID, C.CompanyName, year(O.OrderDate)<br>)<br>Select *<br>from ClientesVentas As CV<br>pivot (sum([Monto Total])<br>for A\u00f1o in ([1996],[1997],[1998])) Pvt<br>go<br><strong>El resultado se muestra en la siguiente imagen<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><a href=\"https:\/\/manualsqlserver.com\/wp-content\/uploads\/2021\/01\/ManualSQL_CreandoCTE_08.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/manualsqlserver.com\/wp-content\/uploads\/2021\/01\/ManualSQL_CreandoCTE_08.png\" alt=\"\" class=\"wp-image-2493\" width=\"736\" height=\"684\" srcset=\"https:\/\/manualsqlserver.com\/wp-content\/uploads\/2021\/01\/ManualSQL_CreandoCTE_08.png 710w, https:\/\/manualsqlserver.com\/wp-content\/uploads\/2021\/01\/ManualSQL_CreandoCTE_08-300x279.png 300w\" sizes=\"auto, (max-width: 736px) 100vw, 736px\" \/><\/a><figcaption><em>Clientes y el total por a\u00f1o.<\/em><\/figcaption><\/figure>\n\n\n\n<p style=\"font-size:22px\"><strong>Ejercicio 7<\/strong><\/p>\n\n\n\n<p><strong>Listar los clientes y la cantidad de \u00f3rdenes por a\u00f1o. En este ejercicio vamos a usar una CTE para realizar un Pivot. (<a rel=\"noreferrer noopener\" href=\"https:\/\/manualsqlserver.com\/?p=507\" target=\"_blank\">Ver Pivot<\/a>). Se van a usar la FDU del ejercicio 3 que calcula la cantidad de \u00f3rdenes<br><\/strong>With ClientesVentas<br>([C\u00f3d. Cliente],Cliente, A\u00f1o,<br>[Cantidad de \u00f3rdenes])<br>As<br>(<br>Select<br>C.CustomerID, C.CompanyName, year(O.OrderDate),<br>Count(dbo.fduCantidadOrdenesCliente(C.CustomerID))<br>from Customers As C<br>join Orders As O on C.CustomerID = O.CustomerID<br>group by<br>C.CustomerID, C.CompanyName, year(O.OrderDate)<br>)<br>Select *<br>from ClientesVentas As CV<br>pivot (sum([Cantidad de \u00f3rdenes])<br>for A\u00f1o in ([1996],[1997],[1998])) Pvt<br>go<br><strong>El resultado se muestra en la siguiente imagen<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><a href=\"https:\/\/manualsqlserver.com\/wp-content\/uploads\/2021\/01\/ManualSQL_CreandoCTE_09.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/manualsqlserver.com\/wp-content\/uploads\/2021\/01\/ManualSQL_CreandoCTE_09.png\" alt=\"\" class=\"wp-image-2494\" width=\"756\" height=\"773\" srcset=\"https:\/\/manualsqlserver.com\/wp-content\/uploads\/2021\/01\/ManualSQL_CreandoCTE_09.png 642w, https:\/\/manualsqlserver.com\/wp-content\/uploads\/2021\/01\/ManualSQL_CreandoCTE_09-293x300.png 293w\" sizes=\"auto, (max-width: 756px) 100vw, 756px\" \/><\/a><figcaption>Clientes y la cantidad de \u00f3rdenes por a\u00f1o.<\/figcaption><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Construyendo CTE En este art\u00edculo se explicar\u00e1 como construir una CTE, Common Table Expressi\u00f3n o Expresi\u00f3n de tabla com\u00fan, sus usos son muy diversos y necesarios para simplificar consultas con referencias a varias tablas o con muchos filtros.<\/p><p><a class=\"more-link btn\" href=\"https:\/\/manualsqlserver.com\/?p=2484\">Seguir leyendo<\/a><\/p>\n","protected":false},"author":1,"featured_media":2485,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,231],"tags":[24,281,51],"class_list":["post-2484","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-programacion","category-subconsultas","tag-cte","tag-expresiones-de-tabla","tag-select","nodate","item-wrap"],"_links":{"self":[{"href":"https:\/\/manualsqlserver.com\/index.php?rest_route=\/wp\/v2\/posts\/2484","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/manualsqlserver.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/manualsqlserver.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/manualsqlserver.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/manualsqlserver.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2484"}],"version-history":[{"count":1,"href":"https:\/\/manualsqlserver.com\/index.php?rest_route=\/wp\/v2\/posts\/2484\/revisions"}],"predecessor-version":[{"id":2495,"href":"https:\/\/manualsqlserver.com\/index.php?rest_route=\/wp\/v2\/posts\/2484\/revisions\/2495"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/manualsqlserver.com\/index.php?rest_route=\/wp\/v2\/media\/2485"}],"wp:attachment":[{"href":"https:\/\/manualsqlserver.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2484"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/manualsqlserver.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2484"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/manualsqlserver.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2484"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}