Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

left join/inner join/outer join with repeat values

 
   Database Help (Home) -> Programming RSS
Next:  Cheap way to get a count on a large table  
Author Message
shripaldalal

External


Since: Jan 09, 2007
Posts: 3



(Msg. 1) Posted: Sat Mar 03, 2007 4:17 am
Post subject: left join/inner join/outer join with repeat values
Archived from groups: microsoft>public>sqlserver>programming (more info?)

hi david,

thanks for the reply.
basically what i need this for is:

there are two tables:

table1 is a master table, table2 is a detail table.

table1's primary_id is present in details_id of table2

table1 has only 1 record
table2 could have many records, usually upto 6 or seven

the problem is that when i inner join/left join/right join the above
tables in any way.......

the records of table1 repeat next to all records of table2, i know
this is a normal behaviour for any join

what i want is:

select table1.name, table2.orders from table1, table2 where table1.id
= table2.details

i get something like this:

name order
==== ========
shripal tv
shripal phone
shripal fridge
shripal dvd
shripal oven
shripal laptop

instead wat i want is:

name order
==== ========
shripal tv
<blank> phone
<blank> fridge
<blank> dvd
<blank> oven
<blank> laptop

is such a thing possible ? it could really save a lot of work.

regards,
shripal.

 >> Stay informed about: left join/inner join/outer join with repeat values 
Back to top
Login to vote
Dan Guzman1

External


Since: Aug 22, 2004
Posts: 840



(Msg. 2) Posted: Sat Mar 03, 2007 6:55 am
Post subject: Re: left join/inner join/outer join with repeat values [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

> instead wat i want is:
>
> name order
> ==== ========
> shripal tv
> <blank> phone
> <blank> fridge

This is a common reporting requirement and is best handled in the front end
rather than in Transact-SQL. Have you looked at Reporting Services? It's
included with SQL 2005 and available for SQL 2000 as a free add-on.

--
Hope this helps.

Dan Guzman
SQL Server MVP

"shripaldalal" wrote in message

> hi david,
>
> thanks for the reply.
> basically what i need this for is:
>
> there are two tables:
>
> table1 is a master table, table2 is a detail table.
>
> table1's primary_id is present in details_id of table2
>
> table1 has only 1 record
> table2 could have many records, usually upto 6 or seven
>
> the problem is that when i inner join/left join/right join the above
> tables in any way.......
>
> the records of table1 repeat next to all records of table2, i know
> this is a normal behaviour for any join
>
> what i want is:
>
> select table1.name, table2.orders from table1, table2 where table1.id
> = table2.details
>
> i get something like this:
>
> name order
> ==== ========
> shripal tv
> shripal phone
> shripal fridge
> shripal dvd
> shripal oven
> shripal laptop
>
> instead wat i want is:
>
> name order
> ==== ========
> shripal tv
> <blank> phone
> <blank> fridge
> <blank> dvd
> <blank> oven
> <blank> laptop
>
> is such a thing possible ? it could really save a lot of work.
>
> regards,
> shripal.
>

 >> Stay informed about: left join/inner join/outer join with repeat values 
Back to top
Login to vote
Erland Sommarskog2

External


Since: May 30, 2004
Posts: 2061



(Msg. 3) Posted: Sat Mar 03, 2007 2:59 pm
Post subject: Re: left join/inner join/outer join with repeat values [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

shripaldalal ( ) writes:
> select table1.name, table2.orders from table1, table2 where table1.id
>= table2.details
>
> i get something like this:
>
> name order
>==== ========
> shripal tv
> shripal phone
> shripal fridge
> shripal dvd
> shripal oven
> shripal laptop
>
> instead wat i want is:
>
> name order
>==== ========
> shripal tv
><blank> phone
><blank> fridge
><blank> dvd
><blank> oven
><blank> laptop
>
> is such a thing possible ? it could really save a lot of work.

If you are on SQL 2005, you could do like this:

SELECT CASE rn WHEN 1 THEN CustomerID ELSE '' END,
OrderID
FROM (SELECT CustomerID, OrderID,
rn = row_number() OVER(PARTITION BY CustomerID
ORDER BY OrderID)
FROM Orders
WHERE OrderDate BETWEEN '19970101' AND '19970401') AS s
ORDER BY CustomerID, OrderID

But as Dan said, this is really the task of a report writer.


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

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
 >> Stay informed about: left join/inner join/outer join with repeat values 
Back to top
Login to vote
shripaldalal

External


Since: Jan 09, 2007
Posts: 3



(Msg. 4) Posted: Sun Mar 04, 2007 2:33 am
Post subject: Re: left join/inner join/outer join with repeat values [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Mar 3, 7:59 pm, Erland Sommarskog wrote:
> shripaldalal( ) writes:
> > select table1.name, table2.orders from table1, table2 where table1.id
> >= table2.details
>
> > i get something like this:
>
> > name order
> >==== ========
> > shripal tv
> > shripal phone
> > shripal fridge
> > shripal dvd
> > shripal oven
> > shripal laptop
>
> > instead wat i want is:
>
> > name order
> >==== ========
> > shripal tv
> ><blank> phone
> ><blank> fridge
> ><blank> dvd
> ><blank> oven
> ><blank> laptop
>
> > is such a thing possible ? it could really save a lot of work.
>
> If you are on SQL 2005, you could do like this:
>
> SELECT CASE rn WHEN 1 THEN CustomerID ELSE '' END,
> OrderID
> FROM (SELECT CustomerID, OrderID,
> rn = row_number() OVER(PARTITION BY CustomerID
> ORDER BY OrderID)
> FROM Orders
> WHERE OrderDate BETWEEN '19970101' AND '19970401') AS s
> ORDER BY CustomerID, OrderID
>
> But as Dan said, this is really the task of a report writer.
>
> --
> Erland Sommarskog, SQL Server MVP, esq....RemoveThis@sommarskog.se
>
> Books Online for SQL Server 2005 athttp://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books...
> Books Online for SQL Server 2000 athttp://www.microsoft.com/sql/prodinfo/previousversions/books.mspx- Hide quoted text -
>
> - Show quoted text -

Hi,

Thanks for the reply. I am using MSAccess/JET.

The problem is.....

I dont really require this for reports or anything......

I am doing this because I want to basically join three tables with a
left join / inner join

TABLE1, TABLE2, TABLE3

SELECT * from (table1 left join table2 on table1.id = table2.subid)
left join table3 on table1.id = table3.subid

it gives me:

table1.name table2.orders table3.contact_no
shripal dalal TV 12345678
shripal dalal TV 12345679
shripal dalal DVD 12345678
shripal dalal DVD 12345679
shripal dalal Fridge 12345678
shripal dalal Fridge 12345679
shripal dalal Laptop 12345678
shripal dalal Laptop 12345679
shripal dalal Phone 12345678
shripal dalal Phone 12345679

as you can see all the information is repeated

wat i want is:

table1.name table2.orders table3.contact_no
shripal dalal TV 12345678
shripal dalal DVD <blank>
shripal dalal Fridge <blank>
shripal dalal Laptop <blank>
shripal dalal Phone <blank>

please help thanks.

regards,
shripal dalal.
 >> Stay informed about: left join/inner join/outer join with repeat values 
Back to top
Login to vote
Erland Sommarskog2

External


Since: May 30, 2004
Posts: 2061



(Msg. 5) Posted: Sun Mar 04, 2007 10:53 am
Post subject: Re: left join/inner join/outer join with repeat values [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

shripaldalal ( ) writes:
> Thanks for the reply. I am using MSAccess/JET.

Then my friend you are in the wrong newsgroup. In this newsgroup we can
offer solutions for SQL Server. There are surely people here with
experience from Access that may be able to offer you a solution. However,
if you post in SQL Server newsgroup without even saying that you are
using Access, how would they be able to know what you are looking for?

> SELECT * from (table1 left join table2 on table1.id = table2.subid)
> left join table3 on table1.id = table3.subid
>
> it gives me:
>
> table1.name table2.orders table3.contact_no
> shripal dalal TV 12345678
> shripal dalal TV 12345679
> shripal dalal DVD 12345678
> shripal dalal DVD 12345679
> shripal dalal Fridge 12345678
> shripal dalal Fridge 12345679
> shripal dalal Laptop 12345678
> shripal dalal Laptop 12345679
> shripal dalal Phone 12345678
> shripal dalal Phone 12345679
>
> as you can see all the information is repeated

This is apparently because your join conditions are incomplete. But without
knowledge of your tables I can't say where the error is. There is a standard
recommendation for this type of queries, and that is you post:

o CREATE TABLE statements for your tables, including definitions of keys.
o INSERT statments with sample data that demonstrtes the problem with
your current query.
o The desired result given this sample.

I don't know what culture they have in the Access newsgroup, but I would
expect that they too appreciate this sort of information.

> wat i want is:
>
> table1.name table2.orders table3.contact_no
> shripal dalal TV 12345678
> shripal dalal DVD <blank>
> shripal dalal Fridge <blank>
> shripal dalal Laptop <blank>
> shripal dalal Phone <blank>

And this thing with the contract number being blank instead of repeated
is a fairly odd thing. The solution I posted was based on ANSI SQL, but
being ANSI does not mean implemented. The solution fot this in Access
is likely to be completely different, so I can only recommend you to
find an Access newsgroup.
--
Erland Sommarskog, SQL Server MVP, esquel.TakeThisOut@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
 >> Stay informed about: left join/inner join/outer join with repeat values 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
LEFT OUTER JOIN problem - Hello, I have 2 tables , tblTOSub , and tblTrScanSub with this schema , the 2 identities are the PKeys. CREATE TABLE [dbo].[tblTOSub]( [TOSubID] [int] IDENTITY(1,1) NOT NULL, [TOID] [int] NOT NULL, [ItemID] [int] NOT NULL, [Qty] [int] NOT NULL, ..

Left outer join/hash issue -

SQL Left Outer Join with WHERE clause - Hi, I have 2 tables with a one to many relationship. I want to return all rows from the 1st table (manager table) regardless so I use a left outer join with the 2nd table (work item table). I also need to put a WHERE clause in the 2nd table to filter wor...

SELECT criteria on LEFT OUTER JOIN columns - I have four tables: Company, Branch, Address, Contact. The following query works fine for retrieving my information: SELECT Branch.BranchID, Branch.CompanyID, Company.CompanyName, Company.IsListed, Address.AddressID, Contact.ContactID FROM...

Left join is equal to inner join? - Hi All I try to calculate the result by using left join but it still returns the output same as using inner join. ************************************ Table1(T1) Status_id Score_id SumOfAmount Chief Excellent 1 Chief Fair 1 Chief ....
   Database Help (Home) -> Programming All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]