hi,
i need some help with a query, also to find out if this is even
possible with sql. currently i have a table with the following data:
CustomerNumber CustomerBranch
123 NULL
123 1
123 2
221 NULL
221 5
555 1
555 9
125 NULL
now in this data, CustomerNumber and CustomerBranch are the keys, each
customer MUST have a CustomerBranch with null, those NULL
CustomerBranch's represent the company headquaters, while the ones
with numbers are the other offices. Occassionally data is missing, in
the example above CustomerNumber 555 does not have a NULL
CustomerBranch, this is wrong. Is there anyway in SQL to find all
those CustomerNumbers who do not have a null, there can only be one
null per CustomerNumber. i was thinking about using a
count(CustomerBranch) but not sure how to write it to count all those
CustomerBranchs per CustomerNumber that are equal to 0, if that's the
right way to do it.
Thank you. 4 2457
Hello,
SQL-92:
select distinct customernumber
from t1
where customernumber not in (select customernumber from t1 where
customerbranch is null)
SQL-99 (probably faster):
select distinct customernumber from
(select customernumber, first_value(customerbranch) over (partition by
customernumber order by customerbranch desc) first
from t1)
where first is not null
VC
"soni29" <so****@hotmail.com> wrote in message
news:ca**************************@posting.google.c om... hi, i need some help with a query, also to find out if this is even possible with sql. currently i have a table with the following data:
CustomerNumber CustomerBranch 123 NULL 123 1 123 2 221 NULL 221 5 555 1 555 9 125 NULL
now in this data, CustomerNumber and CustomerBranch are the keys, each customer MUST have a CustomerBranch with null, those NULL CustomerBranch's represent the company headquaters, while the ones with numbers are the other offices. Occassionally data is missing, in the example above CustomerNumber 555 does not have a NULL CustomerBranch, this is wrong. Is there anyway in SQL to find all those CustomerNumbers who do not have a null, there can only be one null per CustomerNumber. i was thinking about using a count(CustomerBranch) but not sure how to write it to count all those CustomerBranchs per CustomerNumber that are equal to 0, if that's the right way to do it.
Thank you.
"VC" <bo*******@hotmail.com> wrote in message news:<K9kRb.158353$xy6.770548@attbi_s02>... "soni29" <so****@hotmail.com> wrote in message news:ca**************************@posting.google.c om... hi, i need some help with a query, also to find out if this is even possible with sql. currently i have a table with the following data:
CustomerNumber CustomerBranch 123 NULL 123 1 123 2 221 NULL 221 5 555 1 555 9 125 NULL
now in this data, CustomerNumber and CustomerBranch are the keys, each customer MUST have a CustomerBranch with null, those NULL CustomerBranch's represent the company headquaters, while the ones with numbers are the other offices. Occassionally data is missing, in the example above CustomerNumber 555 does not have a NULL CustomerBranch, this is wrong. Is there anyway in SQL to find all those CustomerNumbers who do not have a null, there can only be one null per CustomerNumber. i was thinking about using a count(CustomerBranch) but not sure how to write it to count all those CustomerBranchs per CustomerNumber that are equal to 0, if that's the right way to do it.
Thank you.
and VC wrote, (I moved the top post down for context)
Hello,
SQL-92:
select distinct customernumber from t1 where customernumber not in (select customernumber from t1 where customerbranch is null)
SQL-99 (probably faster):
select distinct customernumber from (select customernumber, first_value(customerbranch) over (partition by customernumber order by customerbranch desc) first from t1) where first is not null
VC
I've always disliked distinct in that it implies you did not have the
full search criteria. If nothing else, in most queries you can change
DISTINCT to a COUNT(*).
I'd solve this case something like this (uses an inline view):
select customernumber
from
(select customernumber, count(*) cnt_all , count(customerbranch)
cnt_branch
from t1 ) va
where va.cnt_all = va.cnt_branch ;
This makes use of the fact that count() does not count NULL values. So
if there is no main branch, then there is no record for that customer
where the branch is NULL.
HTH,
ed
Hello Ed,
The problem with your query is that it does not work:
SQL> create table t1(CustomerNumber int, CustomerBranch int);
Table created.
SQL> insert into t1 values(123, NULL);
1 row created.
SQL> insert into t1 values(123, 1);
1 row created.
SQL> insert into t1 values(123, 2);
1 row created.
SQL> insert into t1 values(221, NULL);
1 row created.
SQL> insert into t1 values(221, 5);
1 row created.
SQL> insert into t1 values(555, 1);
1 row created.
SQL> insert into t1 values(555, 9);
1 row created.
SQL> insert into t1 values(125, NULL
2
SQL> select customernumber
2 from
3 (select customernumber, count(*) cnt_all , count(customerbranch)
4 cnt_branch
5 from t1 ) va
6 where va.cnt_all = va.cnt_branch ;
(select customernumber, count(*) cnt_all , count(customerbranch)
*
ERROR at line 3:
ORA-00937: not a single-group group function
SQL>
VC
"Ed prochak" <ed********@magicinterface.com> wrote in message
news:4b**************************@posting.google.c om... "VC" <bo*******@hotmail.com> wrote in message
news:<K9kRb.158353$xy6.770548@attbi_s02>... "soni29" <so****@hotmail.com> wrote in message news:ca**************************@posting.google.c om... hi, i need some help with a query, also to find out if this is even possible with sql. currently i have a table with the following data:
CustomerNumber CustomerBranch 123 NULL 123 1 123 2 221 NULL 221 5 555 1 555 9 125 NULL
now in this data, CustomerNumber and CustomerBranch are the keys, each customer MUST have a CustomerBranch with null, those NULL CustomerBranch's represent the company headquaters, while the ones with numbers are the other offices. Occassionally data is missing, in the example above CustomerNumber 555 does not have a NULL CustomerBranch, this is wrong. Is there anyway in SQL to find all those CustomerNumbers who do not have a null, there can only be one null per CustomerNumber. i was thinking about using a count(CustomerBranch) but not sure how to write it to count all those CustomerBranchs per CustomerNumber that are equal to 0, if that's the right way to do it.
Thank you.
and VC wrote, (I moved the top post down for context)
Hello,
SQL-92:
select distinct customernumber from t1 where customernumber not in (select customernumber from t1 where customerbranch is null)
SQL-99 (probably faster):
select distinct customernumber from (select customernumber, first_value(customerbranch) over (partition by customernumber order by customerbranch desc) first from t1) where first is not null
VC
I've always disliked distinct in that it implies you did not have the full search criteria. If nothing else, in most queries you can change DISTINCT to a COUNT(*). I'd solve this case something like this (uses an inline view):
select customernumber from (select customernumber, count(*) cnt_all , count(customerbranch) cnt_branch from t1 ) va where va.cnt_all = va.cnt_branch ;
This makes use of the fact that count() does not count NULL values. So if there is no main branch, then there is no record for that customer where the branch is NULL.
HTH, ed
Hi
I'd use :
select
customernumber,
count(decode(customerbranch,null,1,null)) number_of_null_branches
from t1
group by customernumber
having count(decode(customerbranch,null,1,null)) != 1;
The count(decode()) syntax will count null values in the
customerbranch column.
The having clause ensures that you only get customernumbers with
either zero or 2 or more null-values - so it also checks your
requirement of only one null value :-)
Regards
KiBeHa This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: netpurpose |
last post by:
I need to extract data from this table to find the lowest prices of
each product as of today. The product will be listed/grouped by the
name only, discarding the product code - I use...
|
by: pw |
last post by:
Hi,
I am having a mental block trying to figure out how to code this.
Two tables:
"tblQuestions" (fields = quesnum, questype, question)
"tblAnswers" (fields = clientnum, quesnum, questype,...
|
by: K. Crothers |
last post by:
I administer a mechanical engineering database. I need to build a
query which uses the results from a subquery as its input or
criterion.
I am attempting to find all of the component parts of...
|
by: Richard Hollenbeck |
last post by:
Hello Newsgroup. You have all been very helpful in the past and I thank
you. I try to ask relevant questions so that they don't just benefit me,
but also benefit the group. I'm currently...
|
by: Tcs |
last post by:
I've been stumped on this for quite a while. I don't know if it's so simple
that I just can't see it, or it's really possible. (Obviously, I HOPE it IS
possible.)
I'm trying to get my queries...
|
by: google |
last post by:
I have a database with four table. In one of the tables, I use about
five lookup fields to get populate their dropdown list. I have read
that lookup fields are really bad and may cause problems...
|
by: L. R. Du Broff |
last post by:
I own a small business. Need to track a few hundred pieces of rental
equipment that can be in any of a few dozen locations. I'm an old-time C
language programmer (UNIX environment). If the only...
|
by: Rnykster |
last post by:
I know a little about Access and have made several single table
databases. Been struggling for about a month to do a multiple table
database with no success. Help!
There are two tables. First...
|
by: John Bode |
last post by:
I need a way to fake reflection in C++ code that makes as few
assumptions about the data types involved as possible. I suspect
there is no good answer for what I need to do, but I'll present the...
|
by: pbd22 |
last post by:
Hi.
I need some help with structuring my query strings.
I have a form with a search bar and some links. Each
link is a search type (such as "community"). The HREF
for the link's anchor looks...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
|
by: Teri B |
last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course.
0ne-to-many. One course many roles.
Then I created a report based on the Course form and...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
|
by: GKJR |
last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
| |