473,587 Members | 2,568 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sql question - returning multiple rows as one record

Hi,

In the process of localizing the 'regions' table, we added three new
tables. The localized data will be stored in the TokenKeys and
TokenValues tables. It would be easier if we did away with the
TokeyKeys/TokenValues tables and just added a localeid in the regions
table, but this is the desired schema by the client. Here's the
schema:

Table: regions
id name abbreviation
1 United States US

Table: locales
id locale
1 en_US
2 fr_CA

Table: TokenKeys
id key
1 db.regions.name
2 db.regions.abbr eviation

Table: TokenValues
id keyid value localeid
1 1 Etas Unis 2
2 2 EU 2
The old sql was simply this:
select name, abbreviation from regions

which returns:
United States, US

But the new sql needs to link in the localized data from the tokeykeys
and tokenvalues tables using the localeid... I’m trying to figure out
what the sql statement would look like to return this:
Etats Unis, EU (This is supposed to be the French version)

My confusion is we are trying to return multiple column values from
the same column (TokenValues.va lue) and make them act as separate
columns in the same record, like it was with the original.

Thanks
Jul 20 '05 #1
1 2265
Justin (ng@NO_SPAMmari timeNO_SPAMsour ce.ca) writes:
Table: regions
id name abbreviation
1 United States US

Table: locales
id locale
1 en_US
2 fr_CA

Table: TokenKeys
id key
1 db.regions.name
2 db.regions.abbr eviation

Table: TokenValues
id keyid value localeid
1 1 Etas Unis 2
2 2 EU 2
The old sql was simply this:
select name, abbreviation from regions

which returns:
United States, US

But the new sql needs to link in the localized data from the tokeykeys
and tokenvalues tables using the localeid... I?m trying to figure out
what the sql statement would look like to return this:
Etats Unis, EU (This is supposed to be the French version)


SELECT name = tvn.value, abbr = tva.value
FROM (TokenValues tvn
JOIN TokenKeys tkn ON tvn.keyid = tkn.id
AND tkn.key = 'db.regions.nam e')
CROSS JOIN (TokenValues tva
JOIN TokenKeuys tkn ON tva.keyid = tkn.id
AND tva.key = 'db.regions.abb rivation')
WHERE tvn.localeid = @localeid
AND tvn.localeid = @localeid

This is untested as you did not provide the input as CREATE TABLE and
INSERT statements, and I'm not typing those myself.

The schema is certainly not appealing. An initial comment is that
TokenValues.id does not seem to serve any use. If I understand things
correctly (keyid, location) is the natural primary key in that table.

We have a lot of name tables in our database. The base table looks
something like this:

CREATE TABLE langauges(lngid smallint NOT NULL,
lngname varchar(30) NOT NULL,
lngcode char(2) NOT NULL,
CONSTRAINT pk_lngid PRIMARY KEY (lngid),
CONSTRAINT pk_lngcode UNIQUE (lngcode))

The language ID here is the major language id Windows, so 9 is English,
29 is Swedish. The language code is the two letter code from an ISO
standard of which the number escapes me right now. "en" for English,
"sv" for Swedish are two examples.

The base table like "regions" looks like this:

CREATE TABLE regions (regid smallint NOT NULL,
regname varchar(30) NOT NULL,
regcode char(3) NOT NULL,
...
CONSTRAINT pk_regions PRIMARY KEY (regid))

(Regions in our case is a user-defined amalgation of countries. The
code is also user-defined.)

There there is a table regionnames:

CREATE TABLE regionnames (regid smallint NOT NULL,
lngid smallint NOT NULL,
regname varchar(30) NOT NULL,
regcode char(3) NOT NULL,
CONSTRAINT pk_regnames PRIMARY KEY (regid, lngid),
CONSTRINAT fk_regid FOREIGN KEY (regid)
REFERENCES regions(regid),
CONSTRINAT fk_lngid FOREIGN KEY (lngid)
REFERENCES languages(lngid ))
A fairly straightforward design. The reader may note that regname and
regcode are both in the main table and in the name table. In our system
there is always a "home language", that is the default language for
the system. Some sites have Swedish, others Finnish etc. The names in
the home languages are duplicated so that they are both in the name
tabel and in the main table. To get a list of names in the user's
language we say:

SELECT r.regid, regname = coalesce(n.regn ame, r.regname)
FROM regions r
LEFT JOIN regionnames n ON r.regid = n.regid
AND n.lngid = @lngid

That is, the "home language" acts as fallback, in the case that there
are no values in the user's language.

That is, of course a situation you may to consider in your complicated
design that I cannot say that I don't really like. Not the least the
part where you have to reference to column names as strings. That makes
the code very sensitive to misspellings.

--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #2

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

Similar topics

3
10759
by: Shuaib | last post by:
Is there any way for Oracle (using standard SQL rather than PL/SQL) to suppress the newline character when returning a list of rows. For example, if I have a table EMP with several records. When I issue the statement: SELECT name FROM emp I would like to get a single row output back comprising the following:
9
10757
by: Rowland Hills | last post by:
I have a table which is returning inconsistent results when I query it! In query analyzer: If I do "SELECT * FROM TABLE_NAME" I get no rows returned. If I do "SELECT COL1, COL2 FROM TABLE_NAME" I get 4 rows returned. In Enterprise manager:
2
4658
by: BenM | last post by:
I am a novice user of ASP.net, so the following might be trivial for many of you. I am trying to iterate through a datareader object and add rows to a table based on the recordset. Here is my code: ws1_Members.Open() Dim tmpSQLCmd As New OleDbCommand(tmpSQL, ws1_Members) Dim tmpMemberReader As OleDbDataReader =...
13
2544
by: Karl Groves | last post by:
I'm missing something very obvious, but it is getting late and I've stared at it too long. TIA for responses I am writing a basic function (listed at the bottom of this post) that returns data from a query into an array. The intent is that the following code:
2
216
by: Shuaib | last post by:
Is there any way for Oracle (using standard SQL rather than PL/SQL) to suppress the newline character when returning a list of rows. For example, if I have a table EMP with several records. When I issue the statement: SELECT name FROM emp I would like to get a single row output back comprising the following:
0
7918
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7843
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8206
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7967
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8220
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6621
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3840
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3875
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.