August 8, 2008 at 1:11 pm
Please, I need resolv this problem.
Table A TableC
id_Menu(pk) id_User
Name id_Menu (FK)
Expected result, below
Table Result
id_Menu | id_Menu
1 1
2 null
null 3
4 4
TSQL - I need to JOIN the result of the Selects
Select TableA.id_menu
INNER JOIN TableB
ON TableA.id_Menu = TableB.id_Menu
Where TableA.id_User = 1
Select TableA.id_menu
INNER JOIN TableB
ON TableA.id_Menu = TableB.id_Menu
Where TableA.id_User = 2
It's possible?
Sorry for my English
Tks
Fábio
Brazil
August 8, 2008 at 1:18 pm
Here are 2 options where option 2 is probably the better option:
[font="Courier New"]SELECT
TableA.id_menu
FROM
TableA INNER JOIN
TableB ON
TableA.id_Menu = TableB.id_Menu
WHERE
TableA.id_User = 1
UNION ALL
SELECT
TableA.id_menu
FROM
TableA INNER JOIN
TableB ON
TableA.id_Menu = TableB.id_Menu
WHERE
TableA.id_User = 2
--OR EVEN BETTER
SELECT
TableA.id_menu
FROM
TableA INNER JOIN
TableB ON
TableA.id_Menu = TableB.id_Menu
WHERE
TableA.id_User IN (1,2)[/font]
Jack Corbett
Consultant - Straight Path Solutions
Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
August 8, 2008 at 1:41 pm
Tks for your help, but your query result in just one Field (id_menu), with all records to user 1 and user 2, and I need, if is possible, two Fields like id_Menu1 and id_Menu2, at the same time.
Example.
id_Menu1 | id_Menu2
1 1
null 2
3 3
4 null
My result have 4 records. Now I cant view what records is null per user.
The idea are compare the records of User1 vs User2.
Tks.
August 8, 2008 at 2:04 pm
All right, I was not sure what you wanted the first time, try this:
[font="Courier New"]DECLARE @TableA TABLE(id_user INT IDENTITY(1,1) PRIMARY KEY, Name VARCHAR(10))
DECLARE @TableB TABLE(id_user INT, id_menu INT)
INSERT INTO @TableA
(
Name
)
SELECT
'Jack'
UNION ALL
SELECT
'Joe'
UNION ALL
SELECT
'Steve'
UNION ALL
SELECT
'Ron'
INSERT INTO @TableB
SELECT
1,1
UNION ALL
SELECT
1,2
UNION ALL
SELECT
1,4
UNION ALL
SELECT
2,1
UNION ALL
SELECT
2,3
UNION ALL
SELECT
2,4
SELECT
Menu_1,
Menu_2
FROM
(SELECT
B.id_menu AS menu_1
FROM
@TableA A INNER JOIN
@TableB B ON
A.id_user = B.id_user
WHERE
A.id_User = 1) AS A FULL OUTER JOIN
(SELECT
B.id_menu AS menu_2
FROM
@TableA A INNER JOIN
@TableB B ON
A.id_user = B.id_user
WHERE
A.id_User = 2) AS B ON
menu_1 = menu_2
ORDER BY
COALESCE(menu_1, menu_2)[/font]
Jack Corbett
Consultant - Straight Path Solutions
Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
August 9, 2008 at 6:17 am
Yes, its run very well.
First, I'm sorry for my english, I know it's horrible.
Second, I'm very glad and grateful for your help.
God Bless You!
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply