En ROW_NUMBER()
ROW_NUMBER() OVER( [PARTITION BY column_1, column_2, …] [ORDER BY column_3, column_4, …] )
Analicemos la sintaxis anterior:
- NUMERO DE FILA()
- PARTICIÓN POR
- PEDIR POR ÚLTIMO
CREATE TABLE Animal_groups ( animal_id serial PRIMARY KEY, animal_name VARCHAR (255) NOT NULL ); CREATE TABLE Mammals ( mammal_id serial PRIMARY KEY, mammal_name VARCHAR (255) NOT NULL, lifespan DECIMAL (11, 2), animal_id INT NOT NULL, FOREIGN KEY (animal_id) REFERENCES Animal_groups (animal_id) );
Ahora añádele algunos datos:
INSERT INTO Animal_groups (animal_name) VALUES ('Terrestrial'), ('Aquatic'), ('Winged'); INSERT INTO Mammals(mammal_name, animal_id, lifespan) VALUES ('Cow', 1, 10), ('Dog', 1, 7), ('Ox', 1, 13), ('Wolf', 1, 11), ('Blue Whale', 2, 80), ('Dolphin', 2, 5), ('Sea Horse', 2, 3), ('Octopus', 2, 8), ('Bat', 3, 4), ('Flying Squirrels', 3, 1), ('Petaurus', 3, 2);
ORDEN BYROW_NUMBER()
SELECT mammal_id, mammal_name, animal_id, ROW_NUMBER () OVER ( ORDER BY mammal_name ) FROM Mammals;
Producción:
Ejemplo 2:
ROW_NUMBER()productos
SELECT DISTINCT lifespan, ROW_NUMBER () OVER (ORDER BY lifespan) FROM Mammals ORDER BY lifespan;
Producción:
Publicación traducida automáticamente
Artículo escrito por RajuKumar19 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA