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. 3 1245
This might work, I would have to test it.
Select customernumber
from table
where customernumber not in ( select customernumber from table where
customernumber is NOT null)
This is untested....an not so efficient so don't do this on a 1 million row
database but for a small database is OKAY.
Oscar
"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.
Soni,
Try this:
select customerNumber
from test
where not exists (select 1 from test t
where t.customerNumber = test.customerNumber
and t.customerBranch is null)
group by CustomerNumber
But it needs to do 2 table scans, which is pretty slow. If you'll do this
often, you should put an index on CustomerNumber.
You also might check out our SQL Server 2000 videos at http://www.TechnicalVideos.net. They include lots of info on tuning and
performance as well as many other SQL Server 2000 topics. We also have
ASP.NET and Flash MX 2002 videos.
Hope this helps,
Chuck Conover http://www.TechnicalVideos.net
"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.
SELECT customernumber
FROM Sometable
GROUP BY customernumber
HAVING COUNT(customerbranch)=COUNT(*) CustomerNumber and CustomerBranch are the keys
Not so if Customerbranch is nullable. It helps if you include DDL with
questions like this so that it's clear what your keys and constraints are.
--
David Portas
SQL Server MVP
-- This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: James |
last post by:
Please help - getting very desperate! Sun, 12 October 2003 05:39
I have PHPDEV 4.2.3 from Firepages.com.au as the upgrade to 4.3.0 did
not work. I also had an abortive download from PHP.NET as...
|
by: Orion |
last post by:
Hi, This is kind of last minute, I have a day and a half left to figure
this out. I'm working on a project using ms-sqlserver. We are
creating a ticket sales system, as part of the system, I...
|
by: Martin Hampl |
last post by:
Hi,
I am using PostgreSQL 7.4, but I did have the same problem with the
last version.
I indexed the column word (defined as varchar(64)) using lower(word).
If I use the following query,...
|
by: Amanda |
last post by:
From a guy in Microsoft newsgroups:
| In *comp.databases.ibm-db2* there are always IBM guys
| from the Toronto labs on line.Post with the
| -for the love of god please help-
| line...
|
by: Andrew McNab |
last post by:
Hi folks,
I have a problem with an MS Access SQL query which is being used in an
Access Report, and am wondering if anyone can help.
Basically, my query (shown below) gets some records from a...
|
by: John Baker |
last post by:
Hi:
As those who have looked at this newsgroup recently will realize, I am a neophyte with
Access, although I have experienced with Approach (the Lotus product). There are things I
could easily...
|
by: hope |
last post by:
Hi
Access 97
I'm lost on this code please can you help
=================================
Below is some simple code that will concatenate a single field's value from multiple records into a...
|
by: David |
last post by:
Hi,
I cannot get the following (MS Access) SQL statement working in my asp
page, please can anyone help me ? Thanks :-)
------------------------------------------------
<%
strQuery =...
|
by: John Austin |
last post by:
I need to understand why if I add a control and use AddHandler to connect its
click event, it will work in Page_Load, but not in a Button_Click.
The idea is that the user types some data, presses...
|
by: PEB |
last post by:
POSTING GUIDELINES
Please follow these guidelines when posting questions
Post your question in a relevant forum
Do NOT PM questions to individual experts - This is not fair on them and...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
|
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: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
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...
| |