我有一张桌子,有三列:Price1、Price2和Price3.对于每个价格列,还有一个专用的金额列Amount t1、Amount t2和Amount t3. 还有列ID和批次.
我想做的是找到最小值all the three Amount columns combined.
以下是我现在使用的SQL
Select
MIN(Q.Min_Size) As Min_Min_Size
From
(Select
Min("Archive"."Amount1") As Min_Size
From
"Archive"
Where
"Archive"."ID" = '123' And
"Archive"."Price1" = 7.4 And
"Archive"."Batch" > 2986 And
"Archive"."Batch" < 6243) Q
Union
Select
Q.Min_Size
From
(Select
Min("Archive"."Amount2") As Min_Size
From
"Archive"
Where
"Archive"."ID" = '123' And
"Archive"."Price2" = 7.4 And
"Archive"."Batch" > 2986 And
"Archive"."Batch" < 6243) Q
Group By
Q.Min_Size
Union
Select
Q.Min_Size
From
(Select
Min("Archive"."Amount3") As Min_Size
From
"Archive"
Where
"Archive"."ID" = '123' And
"Archive"."Price3" = 7.4 And
"Archive"."Batch" > 2986 And
"Archive"."Batch" < 6243) Q
Group By
Q.Min_Size
在这个语句中,我有三行,第一行最小的列是"Amount t1",第二行的最小行是"Amount t2",第三行的最小行是"Amount t3".相反,我想要达到的是one line only with the minimum of the three
EDIT个
我发现这个新说法很管用,但我想知道是否还有更好的说法.
Select
Min(Least("Archive"."Amount1", "Archive"."Amount2", "Archive"."Amount3")) As MinAmount
From
"Archive"
Where
("Archive"."ID" = '123' And
"Archive"."Price1" = 7.4 And
"Archive"."Batch" > 2986 And
"Archive"."Batch" < 6243) Or
("Archive"."ID" = '123' And
"Archive"."Batch" > 2986 And
"Archive"."Batch" < 6243 And
"Archive"."Price2" = 7.4) Or
("Archive"."ID" = '123' And
"Archive"."Batch" > 2986 And
"Archive"."Batch" < 6243 And
"Archive"."Price3" = 7.4)