sql server programming:
try order by letter, case number when 4 then 0 else number end Rajesh Patel [quoted text, click to view] "Jedi" <SPAM_AWAY_azdjedi@hotmail.com> wrote in message news:maUtb.84831$HoK.13353@news01.bloor.is.net.cable.rogers.com... > Hi, > > I have the following theoretical table: > > Letter, Number > A, 1 > A, 4 > A, 3 > A, 2 > B, 3 > B, 1 > B, 4 > B, 2 > > What I would like to do is create a SQL statement that gives me the info in > numerical order, but also with 4 always first > > In pseudo terms: > > select * from table1 order by 4, Number ASC > > That will give me: > > A, 4 > A, 1 > A, 2 > A, 3 > B, 4 > B, 1 > B, 2 > B, 3 > > Is there any way of accomplishing this. I thought perhaps a union select, > but that puts all the 4's at the end, not at the beginning of each group. > > Thanks, > Bodi > >
You could try asking in microsoft.public.access.queries, but if access lets you use IIF in an order by clause, you can use this: order by letter, iif(number=4,0,number) SK [quoted text, click to view] Jedi wrote: >Thx, that probably would have worked, but I'm using Access XP, and It won't >take the CASE statement. Sorry, should have stated that before. > >Bodi > >"Rajesh Patel" <rdp647@hotmail.com> wrote in message >news:%235FiaBKrDHA.2892@TK2MSFTNGP10.phx.gbl... > > >>try >> >>order by letter, case number when 4 then 0 else number end >> >>Rajesh Patel >> >> >> > ><SNIP> > > > >
Hi, I have the following theoretical table: Letter, Number A, 1 A, 4 A, 3 A, 2 B, 3 B, 1 B, 4 B, 2 What I would like to do is create a SQL statement that gives me the info in numerical order, but also with 4 always first In pseudo terms: select * from table1 order by 4, Number ASC That will give me: A, 4 A, 1 A, 2 A, 3 B, 4 B, 1 B, 2 B, 3 Is there any way of accomplishing this. I thought perhaps a union select, but that puts all the 4's at the end, not at the beginning of each group. Thanks, Bodi
Thx, that probably would have worked, but I'm using Access XP, and It won't take the CASE statement. Sorry, should have stated that before. Bodi [quoted text, click to view] "Rajesh Patel" <rdp647@hotmail.com> wrote in message news:%235FiaBKrDHA.2892@TK2MSFTNGP10.phx.gbl... > try > > order by letter, case number when 4 then 0 else number end > > Rajesh Patel >
<SNIP>
Uri, If you look at the desired output Bodi included in the post, it's not descending order , it's 4 first, then the remaining integers in increasing order: 4 1 2 3 SK [quoted text, click to view] Uri Dimant wrote: >Jedi >Look at this one works for you >CREATE TABLE t1 >( > COL1 CHAR(1), > COL2 TINYINT >) >INSERT INTO t1 VALUES ('A',1) >INSERT INTO t1 VALUES ('A',2) >INSERT INTO t1 VALUES ('A',3) > >INSERT INTO t1 VALUES ('B',1) >INSERT INTO t1 VALUES ('B',2) >INSERT INTO t1 VALUES ('B',3) > >SELECT * FROM t1 ORDER BY COL1 ASC,COL2 DESC >DROP TABLE t1 > >"Jedi" <SPAM_AWAY_azdjedi@hotmail.com> wrote in message >news:maUtb.84831$HoK.13353@news01.bloor.is.net.cable.rogers.com... > > >>Hi, >> >>I have the following theoretical table: >> >>Letter, Number >>A, 1 >>A, 4 >>A, 3 >>A, 2 >>B, 3 >>B, 1 >>B, 4 >>B, 2 >> >>What I would like to do is create a SQL statement that gives me the info >> >> >in > > >>numerical order, but also with 4 always first >> >>In pseudo terms: >> >>select * from table1 order by 4, Number ASC >> >>That will give me: >> >>A, 4 >>A, 1 >>A, 2 >>A, 3 >>B, 4 >>B, 1 >>B, 2 >>B, 3 >> >>Is there any way of accomplishing this. I thought perhaps a union select, >>but that puts all the 4's at the end, not at the beginning of each group. >> >>Thanks, >>Bodi >> >> >> >> > > > >
Jedi Look at this one works for you CREATE TABLE t1 ( COL1 CHAR(1), COL2 TINYINT ) INSERT INTO t1 VALUES ('A',1) INSERT INTO t1 VALUES ('A',2) INSERT INTO t1 VALUES ('A',3) INSERT INTO t1 VALUES ('B',1) INSERT INTO t1 VALUES ('B',2) INSERT INTO t1 VALUES ('B',3) SELECT * FROM t1 ORDER BY COL1 ASC,COL2 DESC DROP TABLE t1 [quoted text, click to view] "Jedi" <SPAM_AWAY_azdjedi@hotmail.com> wrote in message news:maUtb.84831$HoK.13353@news01.bloor.is.net.cable.rogers.com... > Hi, > > I have the following theoretical table: > > Letter, Number > A, 1 > A, 4 > A, 3 > A, 2 > B, 3 > B, 1 > B, 4 > B, 2 > > What I would like to do is create a SQL statement that gives me the info in > numerical order, but also with 4 always first > > In pseudo terms: > > select * from table1 order by 4, Number ASC > > That will give me: > > A, 4 > A, 1 > A, 2 > A, 3 > B, 4 > B, 1 > B, 2 > B, 3 > > Is there any way of accomplishing this. I thought perhaps a union select, > but that puts all the 4's at the end, not at the beginning of each group. > > Thanks, > Bodi > >
Try, SELECT Table1.c1, Table1.c2 FROM Table1 ORDER BY Table1.c1, IIf([c2]=4,0,1), Table1.c2; AMB
Steve You are right , my mistake. [quoted text, click to view] "Steve Kass" <skass@drew.edu> wrote in message news:OFXKSNOrDHA.3612@TK2MSFTNGP11.phx.gbl... > Uri, > > If you look at the desired output Bodi included in the post, > it's not descending order , it's 4 first, then the remaining integers > in increasing order: > > 4 > 1 > 2 > 3 > > SK > > Uri Dimant wrote: > > >Jedi > >Look at this one works for you > >CREATE TABLE t1 > >( > > COL1 CHAR(1), > > COL2 TINYINT > >) > >INSERT INTO t1 VALUES ('A',1) > >INSERT INTO t1 VALUES ('A',2) > >INSERT INTO t1 VALUES ('A',3) > > > >INSERT INTO t1 VALUES ('B',1) > >INSERT INTO t1 VALUES ('B',2) > >INSERT INTO t1 VALUES ('B',3) > > > >SELECT * FROM t1 ORDER BY COL1 ASC,COL2 DESC > >DROP TABLE t1 > > > >"Jedi" <SPAM_AWAY_azdjedi@hotmail.com> wrote in message > >news:maUtb.84831$HoK.13353@news01.bloor.is.net.cable.rogers.com... > > > > > >>Hi, > >> > >>I have the following theoretical table: > >> > >>Letter, Number > >>A, 1 > >>A, 4 > >>A, 3 > >>A, 2 > >>B, 3 > >>B, 1 > >>B, 4 > >>B, 2 > >> > >>What I would like to do is create a SQL statement that gives me the info > >> > >> > >in > > > > > >>numerical order, but also with 4 always first > >> > >>In pseudo terms: > >> > >>select * from table1 order by 4, Number ASC > >> > >>That will give me: > >> > >>A, 4 > >>A, 1 > >>A, 2 > >>A, 3 > >>B, 4 > >>B, 1 > >>B, 2 > >>B, 3 > >> > >>Is there any way of accomplishing this. I thought perhaps a union select, > >>but that puts all the 4's at the end, not at the beginning of each group. > >> > >>Thanks, > >>Bodi > >> > >> > >> > >> > > > > > > > > >
hey, you don't need to put table1.c2 in order by clause. iif condition would be enough. Rajesh Patel [quoted text, click to view] "Alejandro Mesa" <amesa@icco.com> wrote in message news:%23ucAnsRrDHA.3504@TK2MSFTNGP11.phx.gbl... > Try, > > SELECT Table1.c1, Table1.c2 > FROM Table1 > ORDER BY Table1.c1, IIf([c2]=4,0,1), Table1.c2; > > > > AMB > >
Don't see what you're looking for? Try a search.
|