Tuesday 24 February 2009

mysql order by

problem: selecting some columns from table, one sum of entries. cant seem to order by sum...

what im not doing: reverting to batch sorting or anything like that


3 columns, 'qty' and 'code', int, varchar(20) loaded from csv using the following;

create database inv;
create table in (
qty int,
code varchar(20)
);
load data local infile 'c:/inv.csv' into table in
fields terminated by ',' optionally enclosed by '"'
lines terminated by '\n'
(qty, code)
;

and im looking for sum of all qtys, grouped by codes, all qtys < 6;

select sum(qty) as count from in group by code having count < 6
fields terminated by ','
lines terminated by '\r\n'
;

obviously this outputs to a local file. the key is 'as count' & 'having count < 6'. job done :D

No comments: