Hi I want to select the minium of two fields:
create table a (f1 int, f2 int, f3 int)
insert into a (f1, f2, f3) values (1,19, 150)
insert into a (f1, f2, f3) values (2,18, 160)
insert into a (f1, f2, f3) values (5, 17, 200)
insert into a (f1, f2, f3) values (3, 18, 250)
I want to select the minimum of the combination of two fields.
In pseudo syntax I want to do something like this
select min(f1, f2) from a where f3 > 160
And I want the result set to be:
3 18
This two result values must come from the same record...
And of course f1 is more significant than f2.
I want to get ONE record in the result set (I do not want to use "order by
f1, f2", because that retrieves ALL the records from the server.)
(The fields f1 and f2 represents date and clock respectively, and I want the
earliest time as the result.)
Any suggestions?
TIA
Gunnar Vøyenli
EDB-konsulent as 3 2774
Here is my suggestion:
SELECT MIN(f1) f1, MIN(f2) f2
FROM a
WHERE f1 = (SELECT MIN(f1) f1 FROM a WHERE f3 > 160)
AND f3 > 160
HTH
Yannick
"Gunnar Vøyenli" <gv@edbkonsulent_REMOVE_THIS.no> wrote in message
news:40********@news.broadpark.no... Hi I want to select the minium of two fields:
create table a (f1 int, f2 int, f3 int)
insert into a (f1, f2, f3) values (1,19, 150) insert into a (f1, f2, f3) values (2,18, 160) insert into a (f1, f2, f3) values (5, 17, 200) insert into a (f1, f2, f3) values (3, 18, 250)
I want to select the minimum of the combination of two fields.
In pseudo syntax I want to do something like this select min(f1, f2) from a where f3 > 160 And I want the result set to be: 3 18 This two result values must come from the same record... And of course f1 is more significant than f2. I want to get ONE record in the result set (I do not want to use "order by f1, f2", because that retrieves ALL the records from the server.) (The fields f1 and f2 represents date and clock respectively, and I want
the earliest time as the result.)
Any suggestions? TIA Gunnar Vøyenli EDB-konsulent as
Thanks for including the DDL. It helps if you include the primary key in the
DDL as well. That could make a difference to the answer.
Here's one method:
SELECT f1, MIN(f2) AS f2
FROM A
WHERE f1 =
(SELECT MIN(f1)
FROM A
WHERE f3>160)
AND f3>160
GROUP BY f1
You could also use the proprietary TOP modifier:
SELECT TOP 1 f1, f2
FROM A
WHERE f1 IS NOT NULL
AND f2 IS NOT NULL
AND f3>160
ORDER BY f1, f2 I do not want to use "order by f1, f2", because that retrieves ALL the records from the server.
Not if you use TOP or have a WHERE clause.
--
David Portas
SQL Server MVP
--
Thanks to both of you!
Now, I'll make it work!
-Gunnar
"David Portas" <RE****************************@acm.org> wrote in message
news:eO********************@giganews.com... Thanks for including the DDL. It helps if you include the primary key in
the DDL as well. That could make a difference to the answer.
Here's one method:
SELECT f1, MIN(f2) AS f2 FROM A WHERE f1 = (SELECT MIN(f1) FROM A WHERE f3>160) AND f3>160 GROUP BY f1
You could also use the proprietary TOP modifier:
SELECT TOP 1 f1, f2 FROM A WHERE f1 IS NOT NULL AND f2 IS NOT NULL AND f3>160 ORDER BY f1, f2
I do not want to use "order by f1, f2", because that retrieves ALL the records from the server.
Not if you use TOP or have a WHERE clause.
-- David Portas SQL Server MVP --
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Marc A. Lefebvre US-775 |
last post by:
I have table FOO, fields F1, F2, F3, F4, and F5.
I need a single SELECT statement on FOO that returns the LOWEST date
between the five fields.
So, if table records looked like
FOO:...
|
by: Rob R. Ainscough |
last post by:
I have an interesting problem that is either a bug or just some local issue
with my PC (probably the later).
If I place my cursor on a word and hit F1 for help, it goes thru a process
(see a...
|
by: George Hardy |
last post by:
knowlegeable people:
i uninstalled msdn completely, and also deleted every registry value i could
find for msdn, and also deleted the directory in c:\program files\msdn.
this should give me a...
|
by: Todd |
last post by:
Apologies up front if this is completely confusing. Currently, I'm
working on a query that resembles this:
Select A, B, C, MIND, E2.E, F2.F
From (Select A, B, C, MIN(D) As MIND
From TableABC...
|
by: lovecreatesbeauty |
last post by:
Hello,
I want to know whether programming languages such as C or C++ provide
something like "event".
In function "f1", a handler/function "h2" is triggered. But f1 won't be
blocked by h2,...
|
by: Dw70 |
last post by:
I need write simple program with work in background
and have only one function when it is run F1 I disable
and when I pres for example ctrl+k it (program is terminate)
maybe somebody have...
|
by: CMM |
last post by:
Is it me or has anyone noticed that F1 is really dumb in VS2005. Since VB3 I
have been able to click F1 on an ambiguous method in code and the IDE
automatically determines the type based on the...
|
by: malahal |
last post by:
I am planning to write a very simple python script that displays files
based on user input. E.g. I need to display f1.msg file if the user
presses <F1key, f2.msg file if the user presses <F2key. ...
|
by: Mensanator |
last post by:
Beacuse in 2.6, Python apparently has fixed a discrepency that existed
in previous versions.
In the IDLE that comes with 2.5, typing "as", to wit "import random as
ran",
the words "import" and...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| | |