Please bare with me in trying to get my point across, I'm new to SQL and
would appreciate the help so much.
I have two table Table1 and Table2. There is a one to many relationship
between Table1 and Table2. Table2 has a list of widgets, I need to create a
query that would show the records from Table1 along with it's related
records and all non related records from Table2. So if I have 5 widgets and
only two have related records in Table1 results should be
Table1ID(2) fkTable2 Table2(widgetname1)
Table1ID(2) fkTable2 Table2(widgetname2)
null null Table2(widgetname3)
null null Table2(widgetname4)
null null Table2(widgetname5)
When I filter the query on another Table1ID, I would need the same sort of
result for each Table1ID.try this
SELECT TABLE1.COL1,TABLE1.COL2,TABLE2.COL1,TABLE2.COL2 FROM TABLE1 RIGHT
OUTER JOIN TABLE2 ON TABLE1.COL1 = TABLE2.COL1
--
Regards
R.D
--Knowledge gets doubled when shared
"Tim Harvey" wrote:
> Please bare with me in trying to get my point across, I'm new to SQL and
> would appreciate the help so much.
> I have two table Table1 and Table2. There is a one to many relationship
> between Table1 and Table2. Table2 has a list of widgets, I need to create
a
> query that would show the records from Table1 along with it's related
> records and all non related records from Table2. So if I have 5 widgets an
d
> only two have related records in Table1 results should be
> Table1ID(2) fkTable2 Table2(widgetname1)
> Table1ID(2) fkTable2 Table2(widgetname2)
> null null Table2(widgetname3)
> null null Table2(widgetname4)
> null null Table2(widgetname5)
> When I filter the query on another Table1ID, I would need the same sort of
> result for each Table1ID.
>
>
>
Showing posts with label table2. Show all posts
Showing posts with label table2. Show all posts
Friday, March 30, 2012
Monday, March 12, 2012
Help with query
Hi,
I have this two tables:
TABLE1 TABLE2
-- --
A A
B B
C D
D
E
I want my result to be
A Yes
B Yes
C No
D Yes
E No
Joining both tables, having all the values on the first
one and sort of a flag indicating whether it exists in
the second table or not.
Thanks for your help!Try this:
SELECT TableA.ColA,
CASE WHEN (TableB.ColB IS NULL) THEN 'No'
ELSE 'Yes' END
FROM TableA LEFT OUTER JOIN TableB
ON (TableA.ColA = TableB.ColB)
HTH,
Peter.
>--Original Message--
>Hi,
>I have this two tables:
>TABLE1 TABLE2
>-- --
>A A
>B B
>C D
>D
>E
>I want my result to be
>A Yes
>B Yes
>C No
>D Yes
>E No
>Joining both tables, having all the values on the first
>one and sort of a flag indicating whether it exists in
>the second table or not.
>Thanks for your help!
>.
>
I have this two tables:
TABLE1 TABLE2
-- --
A A
B B
C D
D
E
I want my result to be
A Yes
B Yes
C No
D Yes
E No
Joining both tables, having all the values on the first
one and sort of a flag indicating whether it exists in
the second table or not.
Thanks for your help!Try this:
SELECT TableA.ColA,
CASE WHEN (TableB.ColB IS NULL) THEN 'No'
ELSE 'Yes' END
FROM TableA LEFT OUTER JOIN TableB
ON (TableA.ColA = TableB.ColB)
HTH,
Peter.
>--Original Message--
>Hi,
>I have this two tables:
>TABLE1 TABLE2
>-- --
>A A
>B B
>C D
>D
>E
>I want my result to be
>A Yes
>B Yes
>C No
>D Yes
>E No
>Joining both tables, having all the values on the first
>one and sort of a flag indicating whether it exists in
>the second table or not.
>Thanks for your help!
>.
>
Friday, February 24, 2012
Help with max()
I have two tables:
TABLE1 has policy_id and subm_no, multiple subm_no's for every policy_id.
TABLE2 has policy_id
I'm doing a join, WHERE table1.policy_id = table2.policy_id.
I need to display the value of many fields from both tables where
table1.subm_no is the max value of that subm_no FOR THAT POLICY_ID. Can
someone point me in the right direction? Thanks.SELECT Table1.*, Table2.*
FROM Table1
JOIN Table2 ON Table1.policy_id = Table2.policy_id
WHERE Table1.Subm_No =
(
SELECT MAX(Subm_No)
FROM Table1 Tx
WHERE Tx.policy_id = Table1.policy_id
)
Adam Machanic
SQL Server MVP
http://www.datamanipulation.net
--
"Rick Charnes" <rickxyz--nospam.zyxcharnes@.thehartford.com> wrote in message
news:MPG.1d98a20592f0f5359898f9@.msnews.microsoft.com...
> I have two tables:
> TABLE1 has policy_id and subm_no, multiple subm_no's for every policy_id.
> TABLE2 has policy_id
> I'm doing a join, WHERE table1.policy_id = table2.policy_id.
> I need to display the value of many fields from both tables where
> table1.subm_no is the max value of that subm_no FOR THAT POLICY_ID. Can
> someone point me in the right direction? Thanks.
TABLE1 has policy_id and subm_no, multiple subm_no's for every policy_id.
TABLE2 has policy_id
I'm doing a join, WHERE table1.policy_id = table2.policy_id.
I need to display the value of many fields from both tables where
table1.subm_no is the max value of that subm_no FOR THAT POLICY_ID. Can
someone point me in the right direction? Thanks.SELECT Table1.*, Table2.*
FROM Table1
JOIN Table2 ON Table1.policy_id = Table2.policy_id
WHERE Table1.Subm_No =
(
SELECT MAX(Subm_No)
FROM Table1 Tx
WHERE Tx.policy_id = Table1.policy_id
)
Adam Machanic
SQL Server MVP
http://www.datamanipulation.net
--
"Rick Charnes" <rickxyz--nospam.zyxcharnes@.thehartford.com> wrote in message
news:MPG.1d98a20592f0f5359898f9@.msnews.microsoft.com...
> I have two tables:
> TABLE1 has policy_id and subm_no, multiple subm_no's for every policy_id.
> TABLE2 has policy_id
> I'm doing a join, WHERE table1.policy_id = table2.policy_id.
> I need to display the value of many fields from both tables where
> table1.subm_no is the max value of that subm_no FOR THAT POLICY_ID. Can
> someone point me in the right direction? Thanks.
Subscribe to:
Posts (Atom)