2

I have a database of rooms made up of Room_Widths and Room_lengths. I want to be able to order the dataset using a TADOQuery in Delphi, so that the room with the longest side, whether it's the width or the length will be first in a dataset. I need this so that I can perform a bin packing algorithm on it later.

I hope, there is something that will look fairly similar to this:

ADORoomQuery.SQL.Add('ORDER BY GREATEST(Room_Width, Room_Length)');

For example if I have 3 rooms (9 x 9m), (10 x 2m) and (5 x 12m):

Room_Widths    Room_Lengths
-------------  -------------
9              9
10             2
5              12

Then it would return the following dataset:

Room_Widths    Room_Lengths
-------------  -------------
5              12
10             2
9              9

I'm using MS Access database.

4

3 に答える 3

3

多分このようなもの:

  select 
     room_widths, 
     room_lengths, 
     iif(room_widths>room_lengths, room_widths, room_lengths) as longest
  from 
     yourtable
  where 
     <your select criteria>
  order by 
     3 desc
于 2013-03-02T15:01:57.790 に答える
1

最初にデータを正規化してみてください。例えば:

select
  RoomID
, 'W' as Dimension
, Room_Widths as DimensionValue
from yourtable
union all
select
  RoomID
, 'L' as Dimension
, Room_Lengths as DimensionValue
from yourtable
order by RoomID

上記のクエリをNormalisedRoomsとして保存するとします。これにより、次のような結果が得られます。

RoomID  Dimension  DimensionValue
------  ---------  --------------
     1          W               9
     1          L               9
     2          W              10
     2          L               2
   ...        ...             ...

今、あなたはすることができます:

select
  RoomID
, max(DimensionValue) as LongestSide
from NormalisedRooms
group by RoomID
order by 2 desc

これはあなたに与えるはずです:

RoomID  LongestSide
------  -----------
     3           12
     2           10
     1            9
于 2013-03-02T20:27:58.060 に答える
1

提案ありがとうございます。最終的に@Tlama@GordanLinof@ user582118の提案を使用して、データセットを既に順序付けられた状態で返しました。

私が使用したdelphi構文とSQLは次のとおりです。

with ADOLayoutQuery do
  begin
    SQL.Clear;
    SQL.Add('SELECT Room_Width,Room_Length,IIF(Room_Width > Room_Length, Room_Width, Room_Length) AS Longest');
    SQL.Add('FROM RoomDetails');
    SQL.Add('WHERE OrderNumber = ' + inttostr(OrderNum));
    SQL.Add('ORDER BY IIF(Room_Width > Room_Length, Room_Width, Room_Length) DESC, (Room_Width + Room_Length) DESC');
    Open;
  end;

皆さんはこれをわずかに凝縮できるかもしれませんが、これは私にとってはうまくいきました。もう一度あなたの助けに感謝します、そして私は私の次の質問をするのを楽しみにしています

于 2013-03-03T16:59:51.083 に答える