Showing posts with label multiple. Show all posts
Showing posts with label multiple. Show all posts

Wednesday, March 21, 2012

Problem with a join due to multiple Nulls

I want to join 2 tables by a unique ID field, but the ID field also has
multiple NULLS which I do not want to ignore and I fear they will cause
duplication.

Using TableA and TableB below i will demonstrate the problem.

TableA
TableA.ID Field1 Field2
1 Paul 1
Null John 1
2 John 1

TableB
TableB.ID Field3 Field4
1 25 1
Null 32 1
Null 23 1
2 26 1

The Table I want is

TableA.ID TableB.ID Field1 Field2 Field3 Field4
1 1 Paul 1 25 1
2 2 John 1 26 1
Null Null John 1 Null Null
Null Null Null Null 32 1
Null Null Null Null 26 1

I think a select distcinct statement with a full outer join may do what
I want, but I'm not certain so want to check.

Regards,

Ciarnchudson...@.hotmail.com wrote:
> I want to join 2 tables by a unique ID field, but the ID field also has
> multiple NULLS which I do not want to ignore and I fear they will cause
> duplication.
> Using TableA and TableB below i will demonstrate the problem.
> TableA
> TableA.ID Field1 Field2
> 1 Paul 1
> Null John 1
> 2 John 1
>
> TableB
> TableB.ID Field3 Field4
> 1 25 1
> Null 32 1
> Null 23 1
> 2 26 1
> The Table I want is
> TableA.ID TableB.ID Field1 Field2 Field3 Field4
> 1 1 Paul 1 25 1
> 2 2 John 1 26 1
> Null Null John 1 Null Null
> Null Null Null Null 32 1
> Null Null Null Null 26 1
>
> I think a select distcinct statement with a full outer join may do what
> I want, but I'm not certain so want to check.
> Regards,
> Ciarn

It appears that TableA doesn't have a key. Not clear what the key is in
TableB either. Please post DDL rather than sketches of tables otherwise
we just have to guess.

Based on what you've posted I'd say you need to fix some data model
issues (missing keys) before you attempt your query. Maybe that's what
you are trying to do but it isn't obvious how your requested output
will help you.

--
David Portas, SQL Server MVP

Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.

SQL Server Books Online:
http://msdn2.microsoft.com/library/...US,SQL.90).aspx

--|||TableA.ID and TableB.ID are the respective keys, but the problem is
that they contain multiple Nulls

Regards,
Ciarn|||(chudson007@.hotmail.com) writes:
> I want to join 2 tables by a unique ID field, but the ID field also has
> multiple NULLS which I do not want to ignore and I fear they will cause
> duplication.
> Using TableA and TableB below i will demonstrate the problem.
> TableA
> TableA.ID Field1 Field2
> 1 Paul 1
> Null John 1
> 2 John 1
>
> TableB
> TableB.ID Field3 Field4
> 1 25 1
> Null 32 1
> Null 23 1
> 2 26 1
> The Table I want is
> TableA.ID TableB.ID Field1 Field2 Field3 Field4
> 1 1 Paul 1 25 1
> 2 2 John 1 26 1
> Null Null John 1 Null Null
> Null Null Null Null 32 1
> Null Null Null Null 26 1

The IDs cannot really be keys if there are NULL values, even less if there
are multiple NULL.

If I'm taking a guess of what you are looking for, this might be it:

SELECT a.ID, b.ID, a.Field1, a.Field2, b.Field3, b.Field4
FROM TableA a
JOIN TableB b ON a.ID = b.ID
UNION ALL
SELECT NULL, NULL, a.Field1, a.Field2, NULL, NULL
FROM TableA a
WHERE a.ID IS NULL
UNION ALL
SELECT NULL, NULL, NULL, NULL, b.Field3, b.Field4
FROM TableB b
WHERE b.ID IS NULL

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx|||chudson007@.hotmail.com wrote:
> TableA.ID and TableB.ID are the respective keys, but the problem is
> that they contain multiple Nulls
> Regards,
> Ciarn

If they contain nulls then they aren't keys. Every table should have a
key. Fix the design first.

--
David Portas, SQL Server MVP

Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.

SQL Server Books Online:
http://msdn2.microsoft.com/library/...US,SQL.90).aspx

--|||I like your logic Erland.
That'll work perfectly thanks.|||>> want to join 2 tables by a unique ID field [sic], but the ID field [sic] also has multiple NULLS which I do not want to ignore and I fear they will cause duplication. <<

Unh? "Unique identifier" is redundant and columns are not anything
like fields. Looking at your personal narrative, I see the magical,
vague "id" column violating the basic principle of data modeling that
data elements name distinct things and are not magical uiversal vague
creatures.

Please post some DDL with a key in each table. Without a key, these
things are not tables by definition.

You might also wantto take the time to learn RDBMS, so you so you do
not embarass yourself in future postings.|||David,

Unfortunately I'm working with data exttracts as opposed to a well
designed system.
The code below demonstrates what i am trying to do.
In TableA My_ID is is either Null or a unique number.
In TableB My_ID is either Null or a number which may not be unique.

I tried using a full outer join the other day, but after 15 hours it
still had not worked so I think I am doing somethiing wrong and need
help.

In my real data TableA contains just under 2 million records 1.2million
of which My_ID is null and TableB conatins 5million records of which
almost 3 million of which My_ID is null.

CREATE TABLE TableA ( My_ID nvarchar(4000),Field1
nvarchar(4000),CounterA nvarchar(4000))
GO

INSERT INTO TableA
SELECT
'1', 'Paul','1'
UNION all SELECT
'2', 'John','1'
UNION all SELECT
'3', 'Mark','1'
UNION all SELECT
Null, 'Simon','1'
UNION all SELECT
Null, 'Peter','1'

CREATE TABLE TableB ( My_ID nvarchar(4000),Field2
nvarchar(4000),CounterB nvarchar(4000))
GO

INSERT INTO TableB
SELECT
'1', '23','1'
UNION all SELECT
'1', '24','1'
UNION all SELECT
'4', '26','1'
UNION all SELECT
Null, '27','1'
UNION all SELECT
Null, '28','1'

SELECT *
FROM TableA FULL OUTER JOIN
TableB ON TableA.My_ID = TableB.My_ID

Regards,
Ciarn|||chudson007@.hotmail.com wrote:
> David,
> Unfortunately I'm working with data exttracts as opposed to a well
> designed system.
> The code below demonstrates what i am trying to do.
> In TableA My_ID is is either Null or a unique number.
> In TableB My_ID is either Null or a number which may not be unique.
> I tried using a full outer join the other day, but after 15 hours it
> still had not worked so I think I am doing somethiing wrong and need
> help.
> In my real data TableA contains just under 2 million records 1.2million
> of which My_ID is null and TableB conatins 5million records of which
> almost 3 million of which My_ID is null.

I can see that the system isn't well designed. What I'm suggesting is
that you fix it. I assume what you mean by "data extracts" is that you
are importing some data into a database from an outside source over
which you don't have any control. Can't you create your own tables and
import the data into them? That's what I recommend: convert your source
data into a normalized data model BEFORE you attempt any further
processing. You obviously haven't done that yet and I don't understand
what you are trying to achieve with this FULL JOIN. Why would you want
to return a join consisting of 7 million rows?

If you aren't permitted to implement a better design then maybe you'll
have to live with sub-optimal performance. I can't redesign your tables
for you because I don't know what your data means or what the result
you've asked for means.

--
David Portas, SQL Server MVP

Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.

SQL Server Books Online:
http://msdn2.microsoft.com/library/...US,SQL.90).aspx
--|||Unfortunately I am not able to implement a better design.
Is there any more optimal way to ensure I have all transactions from
each table and a match wherever an inner join exists?

Regards,
Ciarn|||don't worry, celko says this to everyone. he can't actually help
anyone, he just finds excuses as to why he can't.

Monday, February 20, 2012

problem using "com.microsoft.jdbc.sqlserver.SQLServerDriver"

Hi.
the problem is probably just that your servlet code is not thread-safe.
Multiple threads are probably trying to use the same jdbc objects.
Define all your jdbc objects as method-level variables and you should
be OK...
Joe Weinstein at BEA
Simon Lee wrote:
> hi
> I am using "com.microsoft.jdbc.sqlserver.SQLServerDriver" driver to connect
> the SQL Server 2000.
> Everything is ok if there is one request to the SQL server.
> When there are more than one requests to the SQL server, exception was
> thrown
> I want to ask if that driver has some problems to handle multi-request.
> Thanks
> The expcetions are:
> java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Object has
> been closed.
> at com.microsoft.jdbc.base.BaseExceptions.createExcep tion(Unknown
> Source)
> at com.microsoft.jdbc.base.BaseExceptions.getExceptio n(Unknown
> Source)
> at com.microsoft.jdbc.base.BaseResultSet.validateClos edState(Unknown
> Source)
> at
> com.microsoft.jdbc.base.BaseResultSet.commonFetchI nitialize(Unknown Source)
> at com.microsoft.jdbc.base.BaseResultSet.next(Unknown Source)
> at GetData.GetData.getColumn(GetData.java:192)
> at GetData.GetData.insert_fileInstance_table(GetData. java:1156)
> at RadicaPocketServer2.handleRequest(RadicaPocketServ er2.java:1056)
> at RadicaPocketServer2.doPost(RadicaPocketServer2.jav a:55)
> at javax.servlet.http.HttpServlet.service(HttpServlet .java:760)
> at javax.servlet.http.HttpServlet.service(HttpServlet .java:853)
> at
> org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(Application
> FilterChain.java:247)
> at
> org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterCh
> ain.java:193)
> at
> org.apache.catalina.core.StandardWrapperValve.invo ke(StandardWrapperValve.ja
> va:256)
> at
> org.apache.catalina.core.StandardPipeline$Standard PipelineValveContext.invok
> eNext(StandardPipeline.java:643)
> at
> org.apache.catalina.core.StandardPipeline.invoke(S tandardPipeline.java:480)
> at
> org.apache.catalina.core.ContainerBase.invoke(Cont ainerBase.java:995)
> at
> org.apache.catalina.core.StandardContextValve.invo ke(StandardContextValve.ja
> va:191)
> at
> org.apache.catalina.core.StandardPipeline$Standard PipelineValveContext.invok
> eNext(StandardPipeline.java:643)
> at
> org.apache.catalina.core.StandardPipeline.invoke(S tandardPipeline.java:480)
> at
> org.apache.catalina.core.ContainerBase.invoke(Cont ainerBase.java:995)
> at
> org.apache.catalina.core.StandardContext.invoke(St andardContext.java:2415)
> at
> org.apache.catalina.core.StandardHostValve.invoke( StandardHostValve.java:180
> )
> at
> org.apache.catalina.core.StandardPipeline$Standard PipelineValveContext.invok
> eNext(StandardPipeline.java:643)
> at
> org.apache.catalina.valves.ErrorDispatcherValve.in voke(ErrorDispatcherValve.
> java:171)
> at
> org.apache.catalina.core.StandardPipeline$Standard PipelineValveContext.invok
> eNext(StandardPipeline.java:641)
> at
> org.apache.catalina.valves.ErrorReportValve.invoke (ErrorReportValve.java:172
> )
> at
> org.apache.catalina.core.StandardPipeline$Standard PipelineValveContext.invok
> eNext(StandardPipeline.java:641)
> at
> org.apache.catalina.valves.AccessLogValve.invoke(A ccessLogValve.java:509)
> at
> org.apache.catalina.core.StandardPipeline$Standard PipelineValveContext.invok
> eNext(StandardPipeline.java:641)
> at
> org.apache.catalina.core.StandardPipeline.invoke(S tandardPipeline.java:480)
> at
> org.apache.catalina.core.ContainerBase.invoke(Cont ainerBase.java:995)
> at
> org.apache.catalina.core.StandardEngineValve.invok e(StandardEngineValve.java
> :174)
> at
> org.apache.catalina.core.StandardPipeline$Standard PipelineValveContext.invok
> eNext(StandardPipeline.java:643)
> at
> org.apache.catalina.core.StandardPipeline.invoke(S tandardPipeline.java:480)
> at
> org.apache.catalina.core.ContainerBase.invoke(Cont ainerBase.java:995)
> at
> org.apache.ajp.tomcat4.Ajp13Processor.process(Ajp1 3Processor.java:466)
> at
> org.apache.ajp.tomcat4.Ajp13Processor.run(Ajp13Pro cessor.java:585)
> at java.lang.Thread.run(Thread.java:534)
>
Simon Lee wrote:

> Hello Joe
> Thanks for you suggestion, but I still have questions about it.
> My servlet is run in tomcat 5.0 and I know that tomcat help us to handle
> multiple threading problem
> Could you explain your idea to me more. Thank you
> Also, I have written a class called GetData.java which represent to connect
> and execute SQL statement.
> GetData is used by my servlet program to handle database connections
> Do you mean that I need to handle mutli-threading in GetData.java's
> functions
> Thank you very much
A servlet is a single class that can be run simultaneously by many
threads. Therefore you must make sure that no two threads are
using the same jdbc objects. If you have a single GetData class,
it must create, use, and close all the jdbc objects it is going to use
within each method it has. It shouldn't have permanent jdbc objects
that get re-used, unless you make your own thread-safe pooling code
that makes sure that no two threads use the same objects.
Otherwise you must make your servlet so that only one thread
can run it at a time.
Joe Weinstein at BEA
> Simon Lee
> "Joe Weinstein" <joeNOSPAM@.bea.com> ? news:407FFE48.30900@.bea.com ?...
>
> connect
>
> has
>
> com.microsoft.jdbc.base.BaseExceptions.createExcep tion(Unknown
>
> com.microsoft.jdbc.base.BaseResultSet.validateClos edState(Unknown
>
> Source)
>
> RadicaPocketServer2.handleRequest(RadicaPocketServ er2.java:1056)
>
> org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(Application
>
> org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterCh
>
> org.apache.catalina.core.StandardWrapperValve.invo ke(StandardWrapperValve.ja
>
> org.apache.catalina.core.StandardPipeline$Standard PipelineValveContext.invok
>
> org.apache.catalina.core.StandardPipeline.invoke(S tandardPipeline.java:480)
>
> org.apache.catalina.core.StandardContextValve.invo ke(StandardContextValve.ja
>
> org.apache.catalina.core.StandardPipeline$Standard PipelineValveContext.invok
>
> org.apache.catalina.core.StandardPipeline.invoke(S tandardPipeline.java:480)
>
> org.apache.catalina.core.StandardContext.invoke(St andardContext.java:2415)
>
> org.apache.catalina.core.StandardHostValve.invoke( StandardHostValve.java:180
>
> org.apache.catalina.core.StandardPipeline$Standard PipelineValveContext.invok
>
> org.apache.catalina.valves.ErrorDispatcherValve.in voke(ErrorDispatcherValve.
>
> org.apache.catalina.core.StandardPipeline$Standard PipelineValveContext.invok
>
> org.apache.catalina.valves.ErrorReportValve.invoke (ErrorReportValve.java:172
>
> org.apache.catalina.core.StandardPipeline$Standard PipelineValveContext.invok
>
> org.apache.catalina.valves.AccessLogValve.invoke(A ccessLogValve.java:509)
>
> org.apache.catalina.core.StandardPipeline$Standard PipelineValveContext.invok
>
> org.apache.catalina.core.StandardPipeline.invoke(S tandardPipeline.java:480)
>
> org.apache.catalina.core.StandardEngineValve.invok e(StandardEngineValve.java
>
> org.apache.catalina.core.StandardPipeline$Standard PipelineValveContext.invok
>
> org.apache.catalina.core.StandardPipeline.invoke(S tandardPipeline.java:480)
>
>