Friday, January 8, 2010

Sql coding question min/max?

How can I report min max with quantities? I have a column DOB, with the following report:





0 7246


1 90134


2 123776


3 9884313


4 98457441


5 23456372


6 2456787


7 34265


8 8756


9 7894545








how do I get it to return just the 0 and the 9 with their respective quantities? I try coding in the min and max, but those two functions are a little touchy for me.Sql coding question min/max?
Hi, I'm not a SQL pro, but here's how you might get the first and last one, well there are many ways...





Let's call first column col1 et second column col2, the table is table.





SELECT col1, col2 FROM table


ORDER BY col1 ASC // not needed this time if they are inserted in order


LIMIT 0,1


UNION


SELECT col1, col2 FROM table


ORDER BY col1 DESC


LIMIT 0,1;





If the first one will always be col1=0, just change the first select of the union by


SELECT col1, col2 FROM table WHERE col1=0





Good luck!Sql coding question min/max?
select * from YourTable


where DOB in(select min(dob) from YourTable


union all


select max(dob) from YourTable)

No comments:

Post a Comment