473,657 Members | 2,436 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Suggestion for select statement

I have this kind of information on a db table

COMPANY USER MYDATA
_______________ _______________
AA
01 AA01
USER AAUS
01 USER AA01US

BB
02 BB02
GROUP BBGR
02 GROUP BB02GR
USER is a "logical" reference to another table like this one:

USER GROUP
_______________ _____
USER GROUP
USER2 GROUP2
USER3 GROUP

I want retrieve column (MYDATA) according company and user/group.
For example:

If I have COMPANY = 01 and USSER = USER, I want retrieve row with
MYDATA value = AA01US
If I have COMPANY = KK and USSER = USER, I want retrieve row with
MYDATA value = AAUS
If I have COMPANY = 01 and USSER = USER2, I want retrieve row with
MYDATA value = AA01
If I have COMPANY = KK and USSER = USER2, I want retrieve row with
MYDATA value = AA
If I have COMPANY = KK and USSER = USER3, I want retrieve row with
MYDATA value = BBGR (because USER3 is part of GROUP)
What is the best way (for performance purpose) to retrieve it?
Maybe I can also change my table structure.

I prefer to avoi many select statements to retrive this information.

I'm using db2 8 and 9 on windows platform and DB2/400 on v5r3

Thank You
Roberto

Oct 30 '06 #1
13 1665
roberto wrote:
I have this kind of information on a db table

COMPANY USER MYDATA
_______________ _______________
AA
01 AA01
USER AAUS
01 USER AA01US

BB
02 BB02
GROUP BBGR
02 GROUP BB02GR
USER is a "logical" reference to another table like this one:

USER GROUP
_______________ _____
USER GROUP
USER2 GROUP2
USER3 GROUP

I want retrieve column (MYDATA) according company and user/group.
For example:

If I have COMPANY = 01 and USSER = USER, I want retrieve row with
MYDATA value = AA01US
If I have COMPANY = KK and USSER = USER, I want retrieve row with
MYDATA value = AAUS
If I have COMPANY = 01 and USSER = USER2, I want retrieve row with
MYDATA value = AA01
If I have COMPANY = KK and USSER = USER2, I want retrieve row with
MYDATA value = AA
If I have COMPANY = KK and USSER = USER3, I want retrieve row with
MYDATA value = BBGR (because USER3 is part of GROUP)
What is the best way (for performance purpose) to retrieve it?
Maybe I can also change my table structure.

I prefer to avoi many select statements to retrive this information.

I'm using db2 8 and 9 on windows platform and DB2/400 on v5r3

Thank You
Roberto
With the current setup, i'd suggest either a stored PROCEDURE.:

DECLARE GLOBAL TEMPORARY TABLE T1(Company VARCHAR(2), MyUser
VARCHAR(6), MyData VARCHAR(6))
INSERT INTO SESSION.T1 VALUES (NULL, NULL, 'AA'), ('01', NULL, 'AA01'),
(NULL, 'USER', 'AAUS'), ('01', 'USER', 'AA01US')
INSERT INTO SESSION.T1 VALUES (NULL, NULL, 'BB'), ('02', NULL, 'BB02'),
(NULL, 'GROUP', 'BBGR'), ('02', 'GROUP', 'BB02GR')

DECLARE GLOBAL TEMPORARY TABLE T2(MyUser VARCHAR(6), Group VARCHAR(6))
INSERT INTO SESSION.T2 VALUES ('USER', 'GROUP'), ('USER2', 'GROUP2'),
('USER3', 'GROUP')

DROP PROCEDURE Get_MyData
CREATE PROCEDURE Get_MyData
(
IN IN_Company VARCHAR(0002),
IN IN_MyUser VARCHAR(0006)
)
SPECIFIC Get_MyData
BEGIN

DECLARE List CURSOR WITH RETURN TO CLIENT FOR
SELECT
Company,
MyUser,
MyData
FROM
SESSION.T1
WHERE
NULLIF(Company, IN_Company) IS NULL
AND
(
NULLIF(MyUser, IN_MyUser) IS NULL
OR MyUser IN
(
SELECT
MyUser
FROM
SESSION.T2
WHERE
GROUP = IN_MyUser
)
)
ORDER BY
Company,
MyUser
FETCH FIRST ROW ONLY;

OPEN List;

END

CALL Get_MyData('01' , 'USER')
CALL Get_MyData('KK' , 'USER')
CALL Get_MyData('01' , 'USER2')
CALL Get_MyData('KK' , 'USER2')
CALL Get_MyData('KK' , 'GROUP')

However, the setup itself does not seem logical. Keeping a COLUMN that
can refer to one of two separate COLUMNs is in itself suspect.

B.

Oct 30 '06 #2
Thank You Brian,

Do you suggest another kind of setup (table structure)?

Roberto

Oct 31 '06 #3

roberto wrote:
I have this kind of information on a db table

COMPANY USER MYDATA
_______________ _______________
AA
01 AA01
USER AAUS
01 USER AA01US

BB
02 BB02
GROUP BBGR
02 GROUP BB02GR
USER is a "logical" reference to another table like this one:

USER GROUP
_______________ _____
USER GROUP
USER2 GROUP2
USER3 GROUP

I want retrieve column (MYDATA) according company and user/group.
For example:

If I have COMPANY = KK and USSER = USER2, I want retrieve row with
MYDATA value = AA
Why MYDATA value = BB was not retrieved?
Both of MYDATA value = AA and BB are COMPANY IS NULL and USER IS NULL.
>
I'm using db2 8 and 9 on windows platform and DB2/400 on v5r3

Thank You
Roberto

CREATE TABLE InputData
(Company VARCHAR(2)
,User VARCHAR(6)
);

INSERT INTO InputData
VALUES
('01', 'USER')
,('KK', 'USER')
,('01', 'USER2')
,('KK', 'USER2')
,('KK', 'USER3')
;

I assumed multiple rows satisfy the conditions, take MIN(MYDATA).

------------------------------ Commands Entered
------------------------------
SELECT InD.Company AS In_Company
, InD.User AS In_User
, MIN(MyData) AS MyData
FROM InputData InD
, T1
WHERE
(T1.Company = InD.Company
OR
T1.Company IS NULL
AND NOT EXISTS
(SELECT *
FROM T1 T1N
WHERE T1N.Company = InD.Company
)
)
AND
(T1.User = InD.User
OR
T1.User
= (SELECT T2.Group
FROM T2
WHERE InD.User = T2.User
)
AND NOT EXISTS
(SELECT *
FROM T1 T1N
WHERE T1N.User = InD.User
)
OR
T1.User IS NULL
AND NOT EXISTS
(SELECT *
FROM T1 T1N
LEFT OUTER JOIN
T2 T2N
ON T1N.User = T2N.Group
WHERE InD.User IN (T1N.User, T2N.User)
)
)
GROUP BY InD.Company, InD.User
ORDER BY InD.User, InD.Company;
------------------------------------------------------------------------------

IN_COMPANY IN_USER MYDATA
---------- ------- ------
01 USER AA01US
KK USER AAUS
01 USER2 AA01
KK USER2 AA
KK USER3 BBGR

5 record(s) selected.

Oct 31 '06 #4
I make a mistake...

Company and User are the unique key of the table.

MYDATE = BB is a wrong row.

:) sorry...

Oct 31 '06 #5
roberto wrote:
I make a mistake...

Company and User are the unique key of the table.

MYDATE = BB is a wrong row.

:) sorry...
So, the query can eliminate GROUP BY

------------------------------ Commands Entered
------------------------------
SELECT InD.Company AS In_Company
, InD.User AS In_User
, MyData
FROM InputData InD
, T1
WHERE
(T1.Company = InD.Company
OR
T1.Company IS NULL
AND NOT EXISTS
(SELECT *
FROM T1 T1N
WHERE T1N.Company = InD.Company
)
)
AND
(T1.User = InD.User
OR
T1.User
= (SELECT T2.Group
FROM T2
WHERE InD.User = T2.User
)
AND NOT EXISTS
(SELECT *
FROM T1 T1N
WHERE T1N.User = InD.User
)
OR
T1.User IS NULL
AND NOT EXISTS
(SELECT *
FROM T1 T1N
LEFT OUTER JOIN
T2 T2N
ON T1N.User = T2N.Group
WHERE InD.User IN (T1N.User, T2N.User)
)
);
------------------------------------------------------------------------------

IN_COMPANY IN_USER MYDATA
---------- ------- ------
01 USER AA01US
KK USER AAUS
01 USER2 AA01
KK USER2 AA
KK USER3 BBGR

5 record(s) selected.

Oct 31 '06 #6
Thank you Tonkuma,

but there is some way to avoid "InputData" table?

Roberto

Oct 31 '06 #7
roberto wrote:
Thank You Brian,

Do you suggest another kind of setup (table structure)?

Roberto
It would depend on the use of the data. This small snippet is hardly
enough to go on.

Even keeping the current structure, i'd ADD a second COLUMN for group,
so user and group are in separate COLUMNs. Then, ADD a CONSTRAINT
making sure only one is used: CHECK (NOT ((User IS NOT NULL) AND (Group
IS NOT NULL))). That alone would separate user and group as separate
entities.

B.

Oct 31 '06 #8

roberto wrote:
Thank you Tonkuma,

but there is some way to avoid "InputData" table?

Roberto
It depends what environment you are using this query and how to get the
input value of Company and User.
If the statement is embedded in HOST language, replace InD.Company and
InD.User with Host Variable(For example: :v_Company and :v_User)

SELECT :v_Company AS In_Company
, :v_User AS In_User
, MyData
FROM T1
WHERE
(T1.Company = :v_Company
OR
T1.Company IS NULL
AND NOT EXISTS
(SELECT *
FROM T1 T1N
WHERE T1N.Company = :v_Company
)
)
AND
(T1.User = :v_User
OR
T1.User
= (SELECT T2.Group
FROM T2
WHERE :v_User = T2.User
)
AND NOT EXISTS
(SELECT *
FROM T1 T1N
WHERE T1N.User = :v_User
)
OR
T1.User IS NULL
AND NOT EXISTS
(SELECT *
FROM T1 T1N
LEFT OUTER JOIN
T2 T2N
ON T1N.User = T2N.Group
WHERE :v_User IN (T1N.User, T2N.User)
)
);

Nov 1 '06 #9
Thank Very much for the support to all,
I have another case (this is my final requirements... .)

Starting from this table structure:

K1 K2 K3 K4 K5 V1 V2
-----------------------------------
AA BB C1 D1 D2
AA KK C1 D1 D2
01 AA KK C1 D101 D201
01 GR AA KK C1 D101GR D201GR
AA KK C2 D1 D2
01 AA KK C2 D101 D2
US AA KK C2 D1US D2
01 US AA KK C2 D101US D2
02 AA KK C2 D102 D2
GR AA KK C2 D1GR D2
02 GR AA KK C2 D102GR D2
01 GR AA KK C2 D101GR D2

where K1, K2, K3, K4 and K5 are the unique primary key of the table

I want retrieve all row where:
K3 = AA
K4 = KK
and for K1 and K2 the same behavior of the previous case so where
K2 is a "logical" reference to another table User/Group

So in this case 2 rows must be returned: one for K5 = C1 and another
one for K5=C2
I don't know how many rows will be returned because i don't know how
many K5 value will be for K1-K4 keys

Thank You !
Roberto

Nov 2 '06 #10

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

Similar topics

8
2276
by: neblackcat | last post by:
Would anyone like to comment on the following idea? I was just going to offer it as a new PEP until it was suggested that I post it here for comment & consideration against PEP 308. I'm far from being a "language internist" (on Python or anything else) so go easy on me if this is stupid - it just seemed quite elegant to me as a relative newbie in town :-) I also havent got a clue whether this would be easy or even possible
0
10187
by: Jan | last post by:
I store sql-commands in a database table. In the first step I get the sql command out of the database table with embedded sql. In the second step I try to execute the command, which i got from the database table, using dynamic sql. Executing 'EXEC SQL DESCRIBE SELECT LIST FOR S INTO select_dp;' the error code -2149 is returned That means "Specified partition does not exist". Does anybody know if it is a database problem or a problem of
3
5722
by: dumbledad | last post by:
Hi All, I'm confused by how to replace a SELECT statement in a SQL statement with a specific value. The table I'm working on is a list of words (a column called "word") with an index int pointing to the sentence they come from (a column called "regret"). I also have a table of stop words (called "GenericStopWords") that contains the words I do not want to consider. That table has a single column called "word". I started off using a...
3
6449
by: Tcs | last post by:
My backend is DB2 on our AS/400. While I do HAVE DB2 PE for my PC, I haven't loaded it yet. I'm still using MS Access. And no, I don't believe this is an Access question. (But who knows? I COULD be wrong... :) I've tried the access group...twice...and all I get is "Access doesn't like ".", which I know, or that my query names are too long, as there's a limit to the length of the SQL statement(s). But this works when I don't try to...
8
286
by: Mark Rae | last post by:
Hi, I'm writing a WinForms app in C# and need to store various items of lookup data which will be accessible and maintainable by the user. I'm looking for advice as to the best way to achieve this, according to the following provisos: 1) The app must be able to "match" records according to various criteria in a standard sort of "fetch me all the records where..." way. This doesn't have to be SQL becuase it will all be GUI driven. E,g...
1
3670
by: Grant McLean | last post by:
Hi First a simple question ... I have a table "access_log" that has foreign keys "app_id" and "app_user_id" that reference the "application_type" and "app_user" tables. When I insert into "access_log", the referential integrity triggers generate these queries: SELECT 1 FROM ONLY "public"."application_type" x
19
8362
by: Steve | last post by:
ASP error number 13 - Type mismatch with SELECT...FOR UPDATE statement I got ASP error number 13 when I use the SELECT...FOR UPDATE statement as below. However, if I use SELECT statement without FOR UPDATE, it is fine and no error. I also tried Set objRs = objConn.Execute("SELECT * FROM EMP UPDATE OF EMPNO"), but it still couldn't help. any ideas? I tried to search in the web but couldn't find similar
1
21666
by: microsoft.public.dotnet.languages.vb | last post by:
Hi All, I wanted to know whether this is possible to use multiple variables to use in the select case statement such as follows: select case dWarrExpDateMonth, dRetailDateMonth case "01" : dWarrExpDateMonth="Jan" : dRetailDateMonth="Jan" case "02" : dWarrExpDateMonth="Feb" : dRetailDateMonth="Feb" End Select
20
2171
by: Allan Ebdrup | last post by:
I have a suggestion for C# I would like reader/writer locks to be built in to the language. When you want to aquire a loct on an object o you write lock(o) { ...//critical region } I would like to be able to write: readlock(o)
0
8395
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8310
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8732
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
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 most users, this new feature is actually very convenient. If you want to control the update process,...
1
6166
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4155
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1955
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1615
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.