Hi,
Is there an efficient way to do the following, I want max() to include NULL
as effectively a minimal value?
e.g
table1
col1 col2
---- ----
A 1
A 10
A NULL
select
col1, MAX(col2) as MAX
from
table1
group by
col1
to get
COL1 MAX
---- ---
A 10
rather than
COL1 MAX
---- ---
A 10
A NULL
Thanks
Paul. 6 12582
Paul Reddin wrote: Hi,
Is there an efficient way to do the following, I want max() to include NULL as effectively a minimal value? e.g table1 col1 col2 ---- ---- A 1 A 10 A NULL
select col1, MAX(col2) as MAX from table1 group by col1
to get COL1 MAX ---- --- A 10
This is what you will get when you run the query. The aggregate functions
(max, min, sum, avg,etc.) all ignore NULLs in the column. If every value
in the col2 is NULL, then MAX(col2) will return NULL.
Good luck,
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =----- http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
In article <1f**************************@posting.google.com >, Paul
Reddin (pa**@abacus.co.uk) says... Hi,
Is there an efficient way to do the following, I want max() to include NULL as effectively a minimal value?
I'm not sure this can be called efficient and I didn't optimize it
but it does work :)
create table t2 (COL1 char(2), COL2 int);
INSERT INTO T2
VALUES ('A', 0),('A', 10),('A',null);
with tt (c1,c2) as (
select case
when col2 is null then COL1 concat '_NUL'
else COL1
end,
COL2
from t2 ),
tt2 (c1b, c2b) as (
select c1, max(c2) from tt
group by c1)
select substr(c1b,1,2) as c1b,
case
when substr(c1b,3,4) = '_NUL' then 'NULL'
else char(c2b)
end as c2b
from tt2;
Kind regards, Gert
Perhaps you could use coalesce with a UNION ALL?
Paul Reddin wrote: Hi,
Is there an efficient way to do the following, I want max() to include NULL as effectively a minimal value? e.g table1 col1 col2 ---- ---- A 1 A 10 A NULL
select col1, MAX(col2) as MAX from table1 group by col1
to get COL1 MAX ---- --- A 10
rather than COL1 MAX ---- --- A 10 A NULL
Thanks
Paul.
On 2 Feb 2004 09:08:10 -0800, pa**@abacus.co.uk (Paul Reddin) wrote: Hi,
Is there an efficient way to do the following, I want max() to include NULL as effectively a minimal value? e.g table1 col1 col2 ---- ---- A 1 A 10 A NULL
select col1, MAX(col2) as MAX from table1 group by col1
to get COL1 MAX ---- --- A 10
rather than COL1 MAX ---- --- A 10 A NULL
Thanks
Paul.
Am I overlooking something obvious? Or is everyone else?
select
col1, MAX(col2) as MAX
from
table1
where col2 IS NOT NULL
group by
col1;
If you know that at least one value in col2 is greater than zero, you
might get better performance by using "WHERE col2 > 0" instead of
"WHERE col2 IS NOT NULL".
HTH
--
Bob Hairgrove No**********@Home.com
Paul Reddin wrote: Hi,
Is there an efficient way to do the following, I want max() to include NULL as effectively a minimal value?
The rank() OLAP function allows you to specify the order of NULLs for
key comparison purposes. In your example you could partition by col1,
order by col2 and request NULLs to collate low:
select t1ranked.col1, t1ranked.col2rank
from
(select col1,
rank() over(partition by col1 order by col2 desc nulls last)
from table1) as t1ranked(col1, col2rank)
where t1ranked.col2rank = 1;
Regards,
Miro
Sorry for this.
I think I was having a bit of a brainstorm at the end of yesterday.
I was convinced this was what was happening in a the middle of a
complex
view (i.e I extracted out the essence for simplicity)
But, indeed the MAX() does seem to ignore NULLs in the extracted
simple case, so the problem is a little more involved/dependent on
some other factor.
Thanks all.
Paul. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Guy Bowden |
last post by:
Hi,
I have two tables I'm trying to join up, and would like to know if the
following is possible:
table1 :
SELECT * from mediatype
+-------------+---------+
| mediatypeid | type |
|
by: Experienced but Undocumented |
last post by:
Hi,
I have a table with three columns: datetime, subject, & author. (There are
more columns, but these are the only relavent ones.)
Some rows will have identical subjects. For each subject, I need to select
the author with the most recent date/time stamp.
Any ideas?
|
by: Mikolaj J |
last post by:
Witam,
szukając w dokumentacji mysqla odpowiedzi na moje pytanie
trafiłem na taki przykład:
SELECT user, MAX(salary) as max_sel
FROM tmp
GROUP BY user HAVING max_sel = MAX(salary);
I faktycznie w tabeli, która na 2 kolumny wszystko działa dobrze
ale ja potrzebuję czegoś więcej :-)
|
by: DCM Fan |
last post by:
{CREATE TABLEs and INSERTs follow...}
Gents,
I have a main table that is in ONE-MANY with many other tables. For example, if
the main table is named A, there are these realtionships:
A-->B
A-->C
A-->D
|
by: Jaidev Paruchuri |
last post by:
I have two tables
1)Rollout_detail
start_date Datetime,
contract_date Datetime,
budget_amt Money
store_id int(foriegn key referring store.store_id)
pan_number varchar(20)
roll_id int
2)store
| |
by: Michael Hardy |
last post by:
I would like to reopen this discussion. I need to create this table
using SQL that will return this dataset.
BRAID RATEDEFID MIN(BRADATE) MAX(BRADATE RATECODE PRODNAME
614 14 12/1/2002 12/4/2004 Rack Beach
Tower Terrace
618 14 12/8/2002 12/11/2004 Rack Beach
Tower Terrace
622 14 12/15/2002 ...
|
by: August1 |
last post by:
This is a follow up to using these functions to produce lottery
numbers to an outfile, then read the numbers from the file to the
screen. Although other methods are certainly available.
#include <iostream>
#include <fstream>//for declaring objects of the ofstream and ifstream
#include <time.h>
#include <stdlib.h>
using namespace std;
|
by: desi.american |
last post by:
I'm using System.Web.Mail to send an email message from an ASP.NET web
page.
This is the main section of the code.
//************* start code *****************************
string mailHost =
(System.Configuration.ConfigurationSettings.AppSettings.GetValues("mailserver"));
//Get data from page
string toAddress =
|
by: bflemi3 |
last post by:
My previous post went unanswered. I now know what the problem is but can not think of a practical work around. Hopefully by making my question less confusing someone will be able to help...here goes...
I have 3 CASE expressions in my SELECT list (bolded font) that should be included in the GROUP BY clause. The problem is that the 3 CASE...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |