473,387 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Need help with SQL query

I need a help with the following problem:

There's 2 tables tbla(primary, foreign, data) and tblb(primary, order)
with data like this:

tbla
'company 1', 1000, 'New York'
'company 1', 1001, 'Boston'
'company 2', 1000, 'Atlanta'
'company 2', 1002, 'Los Angeles'
'company 3', 1001, 'Seattle'
'company 3', 1002, 'Orlando'
'company 4', 1002, 'Denver'

tblb
1000, 97
1001, 98

Resulting table should have only one of each company WHERE
tbla.foreign = tblb.primary ORDER BY tblb.order -- like this:
'company 1', 1000, 'New York', 97
'company 2', 1000, 'Atlanta', 97
'company 3', 1001, 'Seattle', 98

I desperately need any advice.

--
Michael

Feb 19 '07 #1
4 1330
On Feb 19, 10:18 am, "regul8or" <regul...@gmail.comwrote:
I need a help with the following problem:

There's 2 tables tbla(primary, foreign, data) and tblb(primary, order)
with data like this:

tbla
'company 1', 1000, 'New York'
'company 1', 1001, 'Boston'
'company 2', 1000, 'Atlanta'
'company 2', 1002, 'Los Angeles'
'company 3', 1001, 'Seattle'
'company 3', 1002, 'Orlando'
'company 4', 1002, 'Denver'

tblb
1000, 97
1001, 98

Resulting table should have only one of each company WHERE
tbla.foreign = tblb.primary ORDER BY tblb.order -- like this:
'company 1', 1000, 'New York', 97
'company 2', 1000, 'Atlanta', 97
'company 3', 1001, 'Seattle', 98

I desperately need any advice.

--
Michael
Hi, Michael,

Give this a try:

WITH
TBLA
(CONAME, PKEY, CITY)
AS
(VALUES
('company 1', 1000, 'New York'),
('company 1', 1001, 'Boston'),
('company 2', 1000, 'Atlanta'),
('company 2', 1002, 'Los Angeles'),
('company 3', 1001, 'Seattle'),
('company 3', 1002, 'Orlando'),
('company 4', 1002, 'Denver')),
TBLB
(FKEY, QTY)
AS
(VALUES
(1000, 97),
(1001, 98))
SELECT
CONAME,
PKEY,
CITY,
QTY
FROM
(
SELECT
A.CONAME,
A.PKEY,
A.CITY,
B.QTY,
ROWNUMBER() OVER (PARTITION BY A.CONAME) RN
FROM
TBLA A
JOIN
TBLB B
ON
B.FKEY = A.PKEY
) ILV
WHERE
ILV.RN = 1;

Note that you will not need the WITH TBLA VALUES...TBLB VALUES stuff--
I used those CTE's since I don't have access to your tables :-)

HTH,

--Jeff

Feb 19 '07 #2
SELECT primary
, foreign
, data
, order
FROM (SELECT a.*, b.order
, ROWNUMBER() OVER(PARTITON BY a.primary
ORDER BY b.order) rn
FROM tbla a
INNER JOIN
tblb b
ON a.foreign = b.primary
) R
WHERE rn = 1

Feb 19 '07 #3
Thanks guys, that's great!

(Leaving to blow the dust from Ye Olde SQL Book of Magic)

--
Michael

Feb 19 '07 #4
On 19 Feb 2007 10:18:15 -0800, "regul8or" <re******@gmail.comwrote:
>I need a help with the following problem:

There's 2 tables tbla(primary, foreign, data) and tblb(primary, order)
with data like this:

tbla
'company 1', 1000, 'New York'
'company 1', 1001, 'Boston'
'company 2', 1000, 'Atlanta'
'company 2', 1002, 'Los Angeles'
'company 3', 1001, 'Seattle'
'company 3', 1002, 'Orlando'
'company 4', 1002, 'Denver'

tblb
1000, 97
1001, 98

Resulting table should have only one of each company WHERE
tbla.foreign = tblb.primary ORDER BY tblb.order -- like this:
'company 1', 1000, 'New York', 97
'company 2', 1000, 'Atlanta', 97
'company 3', 1001, 'Seattle', 98

I desperately need any advice.

Just thought i'd join the party.

SELECT
Primary,
Foreign,
Data,
Order
FROM
(
SELECT
a1.Primary Primary,
a1.Foreign Foreign,
a1.Data Data,
b1.Order Order,
(
SELECT
MIN(Order)
FROM
SESSION.tbla a2,
SESSION.tblb b2
WHERE
b2.Primary = a2.Foreign
AND a2.Primary = a1.Primary
) The_Min
FROM
SESSION.tbla a1,
SESSION.tblb b1
WHERE
a1.Foreign = b1.Primary
) c
WHERE
Order = The_Min
ORDER BY
Order
B.
Feb 20 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: lawrence | last post by:
I've been bad about documentation so far but I'm going to try to be better. I've mostly worked alone so I'm the only one, so far, who's suffered from my bad habits. But I'd like other programmers...
9
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...
6
by: paii | last post by:
I have a table that stores job milestone dates. The 2 milestones I am interested in are "Ship Date" TypeID 1 and "Revised Ship Date" TypeID 18. All jobs have TypeID 1 only some jobs have TypeID 18....
3
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,...
7
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...
3
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...
0
by: ward | last post by:
Greetings. Ok, I admit it, I bit off a bit more than I can chew. I need to complete this "Generate Report" page for my employer and I'm a little over my head. I could use some additional...
10
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...
7
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...
3
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
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...
1
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.