473,486 Members | 2,131 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to join two tables where one table keeps rows on a separate row

93 New Member
Hi All

I have one table where I have:

1) part number
2) location
3) Vendor ID

and I have another table where I have additional info - list price and buy price (which sometimes is not added in error)

1) part number
2) location
3) category: list price OR buy price
4) value
-- unfortuanately I don't have Vendor ID in this table

so I have several rows for the same number and location (list price and buy price)

I need to show

1) Vendor ID
2) unique number
3) location
4) value (list price) - if added
5) value (buy price) - if added

I tried to use self join but it is not showing me all the records, I tried to use subquery but I'm not too good with subqueries ;)

Many thanks in advance
Emil
Mar 5 '08 #1
7 1341
emsik1001
93 New Member
Here is what I used however it is not showing me records where UP or/and UC is missing (see below) - Any suggestions?


SET TRANSACTION ISOLATION LEVEL
READ UNCOMMITTED
BEGIN TRANSACTION


SELECT e1.number, e1.location, cast(e1.cost/10000 AS DECIMAL(12,2)) AS UP , CAST(e2.cost/10000 AS DECIMAL(12,2)) AS UC


FROM TblLocation as e1 FULL OUTER JOIN TblLocation as e2 ON e1.number+e1.location = e2.number+e2.location
FULL OUTER JOIN tblMaster ON RTRIM(MasterNumber)+RTRIM(MasterLocation) = RTRIM(e2.number)+RTRIM(e2.location)

WHERE e1.category = 'UP' and e2.category = 'UC' and VendorID = 'XXX'


COMMIT TRANSACTION
Mar 5 '08 #2
ck9663
2,878 Recognized Expert Specialist
Hi All

I have one table where I have:

1) part number
2) location
3) Vendor ID

and I have another table where I have additional info - list price and buy price (which sometimes is not added in error)

1) part number
2) location
3) category: list price OR buy price
4) value
-- unfortuanately I don't have Vendor ID in this table

so I have several rows for the same number and location (list price and buy price)

I need to show

1) Vendor ID
2) unique number
3) location
4) value (list price) - if added
5) value (buy price) - if added

I tried to use self join but it is not showing me all the records, I tried to use subquery but I'm not too good with subqueries ;)

Many thanks in advance
Emil

Is PartNumber and Location enough for you to use as join key even without VendorID?

-- CK
Mar 5 '08 #3
emsik1001
93 New Member
combination of part number and location in the first table is unique
Mar 5 '08 #4
ck9663
2,878 Recognized Expert Specialist
From which table the Cost column came from?

-- CK
Mar 5 '08 #5
emsik1001
93 New Member
From the second the table --4) value--
Mar 6 '08 #6
ck9663
2,878 Recognized Expert Specialist
I'm a little confuse. Your desired output include these:

4) value (list price) - if added
5) value (buy price) - if added

How would you know if they are list price or buy price? Can you just post some sample data and your desired output? Include some partnumber that are not in table2 and how you want to handle it.

-- CK
Mar 6 '08 #7
Delerna
1,134 Recognized Expert Top Contributor
is something like this what you are after
Expand|Select|Wrap|Line Numbers
  1. SELECT PartNumber,Location,VendorID,sum(UP) as UP,sum(UC) as UC
  2. FROM
  3. (   SELECT a.PartNumber,a.Location,a.VendorID,
  4.             case when b.category='UP' then Value else 0 end as UP,
  5.             case when b.category='UC' then Value else 0 end as UC
  6.    FROM tblMaster a
  7.    LEFT JOIN tblLocation b on a.partnumber=b.partnumber 
  8.                           and a.location=b.location
  9.    WHERE VendorID='XXX'
  10. ) c
  11. GROUP BY PartNumber,Location
  12.  
Mar 7 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

13
2616
by: StealthBananaT | last post by:
My database has two tables - films has 10,000 records and reviews has 20,000 records. Whenever I try to list all the films and the count of its reviews, MySQL locks and I have to restart the...
0
2347
by: Soefara | last post by:
Dear Sirs, I am experiencing strange results when trying to optimize a LEFT JOIN on 3 tables using MySQL. Given 3 tables A, B, C such as the following: create table A ( uniqueId int not...
2
18100
by: Martin | last post by:
I am now working on SQL Server 2000 having had previous experience on a different database. Both of the OUTER JOIN syntaxes is different from what I am used to and I am finding it slightly...
8
4957
by: Matt | last post by:
Hello I have to tables ar and arb, ar holds articles and a swedish description, arb holds descriptions in other languages. I want to retreive all articles that match a criteria from ar and...
1
20523
by: Gene Ariani | last post by:
I have one dataset that contains two DataTables: DataTable1: Customer_ID 1
3
3050
by: don | last post by:
I have written a program to hold a sales contact db. It is written in c# express. an update is sent from head office in xl format, i need to import only the new rows to an access database. I...
5
2721
by: Dot Net Daddy | last post by:
Hello, I have set up a database for movies. In one table (Movies) I have movie names, and production years, and also genres. In another table (Directors), I keep the directors and the movies...
3
16490
by: Zeff | last post by:
Hi all, I have a relational database, where all info is kept in separate tables and just the id's from those tables are stored in one central table (tblMaster)... I want to perform a query, so...
4
1112
prn
by: prn | last post by:
Hi Folks, Sorry if I have the forum wrong, It's more a SQL question than a SQL Server question, but it is intended to run on MS SQL Server at least. :) I'm trying to construct a query and am...
1
6842
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7330
tracyyun
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...
0
5434
agi2029
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,...
1
4865
isladogs
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...
0
4559
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...
0
3070
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1378
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 ...
0
262
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.