| Author |
Topic |
|
louiedogg418
Starting Member
28 Posts |
Posted - 2011-05-02 : 11:51:00
|
| so i have a dino database and i want to select and return thedino name and the tallest in the table, this is kinda where i am going but its not working:select dino_main.Dino_name and max(dino_main.DINO_HEIGHT) from dbo.DINO_MAINgetting error Msg 156, Level 15, State 1, Line 1Incorrect syntax near the keyword 'and'. in sqlserver 08 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
louiedogg418
Starting Member
28 Posts |
Posted - 2011-05-02 : 12:02:27
|
| this returns all the results of the data base, how would i return just the tallest dinosaur and just the name of it? here is table create and insert codecreate table DINO_MAIN(DINO_ID int primary key not null,DINO_NAME varchar(50) not null,DINO_NICK varchar(50) not null,DINO_HEIGHT int null,DINO_LENGTH int null,DINO_WEIGHT int null)insert into Dino_main(DINO_ID,DINO_NAME,DINO_NICK,DINO_HEIGHT,DINO_LENGTH,DINO_WEIGHT)values(1,'Acrocanthosaurus','Top-Spined lizard',18,40,4000),(2,'Albertosaurus','Alberta lizard',15,30,6000),(3,'Allosaurus','Aother lizard',17,40,4000),(4,'Apatosaurus','deceptive lizard',30,75,66000),(5,'Archaeopteryx','ancient wing',1,3,21),(6,'Argentinosaurus','Argentina lizard',70,120,222000),(7,'Baryonyx','heavy claws',6,30,4000),(8,'Brachiosaurus','Arm Lizard',50,100,100000),(9,'Ceratosaurus','horned lizard',13,20,3000), (10,'Coelophysis','hollow form',3,9,100),(11,'Compsognathus','elegant jaw',2,3,8),(12,'Deinonychus','terrible claw',5,9,175),(13,'Diplodocus','double beam',24,89,50000),(14,'Dromiceiomimus','emu mimic',5,11,220), (15,'Gallimimus','fowl mimic',8,18,260),(16,'Mamenchisaurus','Mamenchi lizard',35,69,60000),(17,'Megalosaurus','big lizard',12,26,2000),(18,'Microvenator','small hunter',3,4,14),(19,'Ornithomimus','bird mimic',8,15,300), (20,'Oviraptor','egg robber',3,1,80), (21,'Plateosaurus','flat lizard',7,26,4000), (22,'Saurornithoides','narrow-clawed lizard',3,7,60),(23,'Seismosaurus','tremor lizard',84,150,200000),(24,'Spinosaurus','spiny lizarrd',8,40,14000), (25,'Supersaurus','super lizard',66,100,120000),(26,'Tyrannosaurus','tyrant lizard',23,50,14000), (27,'Ultrasauros','ultra lizard',53,100,140000), (28,'Velociraptor','swift robber',2,6,250); |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
louiedogg418
Starting Member
28 Posts |
Posted - 2011-05-02 : 12:52:47
|
| So if i wanted to combine this with another table and get results on the biggest carnivore how would this work?select dino_name, dino_height,DINO_FEEDfrom DINO_MAIN,DINO_FEEDwhere DINO_MAIN.DINO_ID=DINO_FEED.DINO_ID AND DINO_HEIGHT = (SELECT MAX(dino_MAIN.dino_height)FROM DINO_MAIN, DINO_FEED AND DINO_FEED = 'CARNIVORE')??this isnt working but am i close? |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
louiedogg418
Starting Member
28 Posts |
Posted - 2011-05-02 : 12:56:13
|
| well i want to get results on the biggest carnivore in the database, by name and height and with only showing me the one result likeTREX,50ft carnivore |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
louiedogg418
Starting Member
28 Posts |
Posted - 2011-05-02 : 13:26:15
|
| create table DINO_MAIN(DINO_ID int primary key not null,DINO_NAME varchar(50) not null,DINO_NICK varchar(50) not null,DINO_HEIGHT int null,DINO_LENGTH int null,DINO_WEIGHT int null)create table DINO_FEED(DINO_ID int null,DINO_FEED varchar(50) null)insert into Dino_main(DINO_ID,DINO_NAME,DINO_NICK,DINO_HEIGHT,DINO_LENGTH,DINO_WEIGHT)values(1,'Acrocanthosaurus','Top-Spined lizard',18,40,4000),(2,'Albertosaurus','Alberta lizard',15,30,6000),(3,'Allosaurus','Aother lizard',17,40,4000),(4,'Apatosaurus','deceptive lizard',30,75,66000),(5,'Archaeopteryx','ancient wing',1,3,21),(6,'Argentinosaurus','Argentina lizard',70,120,222000),(7,'Baryonyx','heavy claws',6,30,4000),(8,'Brachiosaurus','Arm Lizard',50,100,100000),(9,'Ceratosaurus','horned lizard',13,20,3000), (10,'Coelophysis','hollow form',3,9,100),(11,'Compsognathus','elegant jaw',2,3,8),(12,'Deinonychus','terrible claw',5,9,175),(13,'Diplodocus','double beam',24,89,50000),(14,'Dromiceiomimus','emu mimic',5,11,220), (15,'Gallimimus','fowl mimic',8,18,260),(16,'Mamenchisaurus','Mamenchi lizard',35,69,60000),(17,'Megalosaurus','big lizard',12,26,2000),(18,'Microvenator','small hunter',3,4,14),(19,'Ornithomimus','bird mimic',8,15,300), (20,'Oviraptor','egg robber',3,1,80), (21,'Plateosaurus','flat lizard',7,26,4000), (22,'Saurornithoides','narrow-clawed lizard',3,7,60),(23,'Seismosaurus','tremor lizard',84,150,200000),(24,'Spinosaurus','spiny lizarrd',8,40,14000), (25,'Supersaurus','super lizard',66,100,120000),(26,'Tyrannosaurus','tyrant lizard',23,50,14000), (27,'Ultrasauros','ultra lizard',53,100,140000), (28,'Velociraptor','swift robber',2,6,250);insert into DINO_FEED(DINO_ID,DINO_FEED)values(1,'Carnivore'),(2,'Carnivore'),(3,'Carnivore'),(4,'Herbivore'),(5,'Carnivore'),(6,'Herbivore'),(7,'Carnivore'),(8,'Herbivore'),(9,'Carnivore'),(10,'Carnivore'),(11,'Carnivore'),(12,'Carnivore'),(13,'Herbivore'),(14,'Carnivore'),(15,'Carnivore'),(16,'Herbivore'),(17,'Carnivore'),(18,'Carnivore'),(19,'Carnivore'),(20,'Carnivore'),(21,'Herbivore'),(22,'Carnivore'),(23,'Herbivore'),(24,'Carnivore'),(25,'Herbivore'),(26,'Carnivore'),(27,'Herbivore'),(28,'Herbivore'); |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
louiedogg418
Starting Member
28 Posts |
Posted - 2011-05-02 : 14:08:57
|
| This is returning the column names but with no data in the rows....IE dino_name]Dino_height]dino_feed]and underneath the column names there is no date |
 |
|
|
louiedogg418
Starting Member
28 Posts |
Posted - 2011-05-02 : 14:10:10
|
| and i guess the expected out would show the something likedino_name]Dino_height]dino_feed]TREX 60 carnivore |
 |
|
|
Mikeyboy01
Starting Member
4 Posts |
Posted - 2011-05-02 : 14:36:03
|
| The reason why you're not getting any result is because the dino with the MaxHeight is not a carnivore. I'm sure there's probably a better way of writing this but it gets the job done.SELECT A.DINO_NAME, A.DINO_HEIGHT, B.DINO_FEEDFROM #Dino_main ALEFT OUTER JOIN #DINO_FEED B ON A.DINO_ID = B.DINO_IDWHERE A.DINO_HEIGHT = (SELECT MAX(DINO_HEIGHT) FROM #DINO_MAIN A JOIN #DINO_FEED B ON A.DINO_ID = B.DINO_ID WHERE B.DINO_FEED = 'Carnivore') |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
louiedogg418
Starting Member
28 Posts |
Posted - 2011-05-02 : 16:25:15
|
| that works! so tara what is the reasoning by using "top 1" and ordering the 2 desc? |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
louiedogg418
Starting Member
28 Posts |
Posted - 2011-05-03 : 01:23:36
|
| but how though? I dont understand how adding order by desc 2 works... What does this specific piece do that retunrs the biggest carnivore? if i erase this part and leave it at select top 1 dino_name, max(dino_height) from dino_main dmjoin dino_feed dfon dm.dino_id = df.dino_idwhere dino_feed = 'carnivore'group by dino_nameit does not return the biggest carnivore |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
louiedogg418
Starting Member
28 Posts |
Posted - 2011-05-03 : 15:35:24
|
| ahhhh! Thank you so much! Your a pro! :D |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
|
|
|