Bases de Datos - Agrupacion de Datos SQL Server

 
Vista:
sin imagen de perfil
Val: 1
Ha disminuido 1 puesto en Bases de Datos (en relación al último mes)
Gráfica de Bases de Datos

Agrupacion de Datos SQL Server

Publicado por Ray (1 intervención) el 16/03/2019 00:48:05
Buenas tardes compañeros, el dia de hoy vengo con una consulta algo compleja que me gustaria que me ayudaran, se trata de la siguiente (SQL Server):

- Encontrar por cada Categoria los 3 productos mas economicos:
El resultado de la consulta tiene que quedar asi:
Category Name | Product Name | Price | Nombre Proveedor
Beverages Product QOGNU 4.50 Supplier UNAHG
Beverages Product BWRLG 7.75 Supplier SVIYA
Beverages Product XLXQF 14.00 Supplier UHZRG
Condiments Product IMEHJ 10.00 Supplier SWRXU
Condiments Product LUNZZ 13.00 Supplier SVIYA
Condiments Product KSZOI 15.50 Supplier QWUSF
Confections Product XKXDO 9.20 Supplier BWGYE
Confections Product EZZPR 9.50 Supplier FNUXM
Confections Product VJZZH 10.00 Supplier BWGYE
Dairy Products Product ASTMN 2.50 Supplier NZLIF
Dairy Products Product XWOXC 12.50 Supplier KEREV
Dairy Products Product QMVUN 21.00 Supplier EQPNC

Los datos se tienen que sacar de la siguiente Tabla
Category Name | Product Name | Price | Nombre Proveedor
Beverages Product QOGNU 4.50 Supplier UNAHG
Beverages Product BWRLG 7.75 Supplier SVIYA
Beverages Product XLXQF 14.00 Supplier UHZRG
Beverages Product SWNJY 14.00 Supplier UHZRG
Beverages Product TOONT 15.00 Supplier GQRCV
Beverages Product LSOFL 18.00 Supplier LVJUA
Beverages Product NEVTJ 18.00 Supplier UHZRG
Beverages Product HHYDP 18.00 Supplier SWRXU
Beverages Product JYGFE 18.00 Supplier ELCRN
Beverages Product RECZE 19.00 Supplier SWRXU
Beverages Product ZZZHR 46.00 Supplier CIYNM
Beverages Product QDOMO 263.50 Supplier LVJUA
Condiments Product IMEHJ 10.00 Supplier SWRXU
Condiments Product LUNZZ 13.00 Supplier SVIYA
Condiments Product KSZOI 15.50 Supplier QWUSF
Condiments Product LQMGN 17.00 Supplier VHQZD
Condiments Product VJIEO 19.45 Supplier CIYNM
Condiments Product XYWBZ 21.05 Supplier VHQZD
Condiments Product EPEIM 21.35 Supplier VHQZD
Condiments Product KSBRM 22.00 Supplier VHQZD
Condiments Product VAIIV 25.00 Supplier STUAZ
Condiments Product XYZPE 28.50 Supplier OGLRK
Condiments Product WVJFP 40.00 Supplier STUAZ
Condiments Product ICKNK 43.90 Supplier GQRCV
Confections Product XKXDO 9.20 Supplier BWGYE
Confections Product EZZPR 9.50 Supplier FNUXM
Confections Product VJZZH 10.00 Supplier BWGYE
Confections Product TBTBL 12.50 Supplier BWGYE
Confections Product MYNXN 12.75 Supplier FNUXM
Confections Product LYLNI 14.00 Supplier ZPYVS
Confections Product BIUDV 16.25 Supplier ELCRN
Confections Product PAFRH 17.45 Supplier GQRCV
Confections Product FPYPN 20.00 Supplier ELCRN
Confections Product HLGZA 31.23 Supplier ZPYVS
Confections Product SMIOH 43.90 Supplier ZPYVS
Confections Product WUXYK 49.30 Supplier OGLRK
Confections Product QHFFP 81.00 Supplier BWGYE
Dairy Products Product ASTMN 2.50 Supplier NZLIF
Dairy Products Product XWOXC 12.50 Supplier KEREV
Dairy Products Product QMVUN 21.00 Supplier EQPNC
Dairy Products Product MYMOI 21.50 Supplier NZLIF
Dairy Products Product NUNAW 32.00 Supplier KEREV
Dairy Products Product WHBYK 34.00 Supplier OAVQT
Dairy Products Product GEEOO 34.80 Supplier KEREV
Dairy Products Product COAXA 36.00 Supplier NZLIF
Dairy Products Product OSFNS 38.00 Supplier EQPNC
Dairy Products Product UKXRI 55.00 Supplier OAVQT

Que solución me podrian dar, la tabla se llama Products, no debo usar Union
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de Leonardo Daniel A.
Val: 434
Oro
Ha mantenido su posición en Bases de Datos (en relación al último mes)
Gráfica de Bases de Datos

Agrupacion de Datos SQL Server

Publicado por Leonardo Daniel A. (211 intervenciones) el 16/03/2019 08:14:16
Hola. intenta con :


1
2
SELECT * FROM datos d1
         WHERE EXISTS (SELECT TOP 3 * FROM datos d2 WHERE d2.category = d1.category ORDER BY d2.price)
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
Imágen de perfil de Vega
Val: 28
Ha mantenido su posición en Bases de Datos (en relación al último mes)
Gráfica de Bases de Datos

Agrupacion de Datos SQL Server

Publicado por Vega (13 intervenciones) el 26/03/2019 19:49:14
Aqui tienes otra forma
1
2
3
4
5
SELECT * FROM (
select *, RANKING = rank() over(partition by [Category Name] ORDER BY PRICE ASC)
from datos
) X
WHERE RANKING <= 3
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar