Entries from October 2007 ↓

Ranking results in SQL

A fun SQL project for beginners!

 If you want to rank your results in order by frequency of occurrence, you can use COUNT() in any major RDBMS to get your results.

 Try using a query like below :

SELECT name, COUNT(id) FROM table

WHERE type=’User’ AND id<>3 AND id IN (116,18,28) GROUP BY (id) ORDER BY COUNT(id) DESC;

Note that all you have to do is add GROUP BY to the counted column, and order by that count!  I like to set DESC to make sure they’re descending (largest items on top) and adding LIMIT 20 in MySQL will only show you the top 20.

 Cool stuff for noobs!

Outputting from MS SQL to XML

A friend of mine just asked me, and the solution is to use bulk copy (the command line utility)

Run this at the command prompt from your bin directory :

bcp “SELECT * FROM TABLE FOR XML RAW” queryout c:\table.xml -Sserver -Uusername -Ppassword -c -r -t

Quick, and dirty!