| Author |
Topic |
|
AskSQLTeam
Ask SQLTeam Question
0 Posts |
Posted - 2002-01-29 : 17:22:27
|
| Milan writes "Hello there,this keeps on killing me, I have following SQL statement:SELECT *FROM product_traffic AS TableA INNER JOIN (SELECT * FROM product_price) AS TableB ON (TableA.pr_id = TableB.pr_id AND TableA.cl_id = TableB.cl_id) OR (TableA.pr_id = TableB.pr_id) So I'm getting rows for both (TableA.pr_id = TableB.pr_id AND TableA.cl_id = TableB.cl_id) and (TableA.pr_id = TableB.pr_id) conditions......I would like to get row for first condition and if it doesn't exist then I would like to get row for second condition after OR!?!?!Cheers!!!" |
|
|
rrb
SQLTeam Poet Laureate
1479 Posts |
Posted - 2002-01-29 : 21:35:14
|
| Milan I'm guessing here but it looks like you want to do:select *into #tempfrom product_traffic A inner join (select * from product_price) B on (A.pr_id = B.pr_id and A.cl_id = B.cl_id) select *from product_traffic A inner join (select * from product_price) B on A.pr_id = B.pr_idwhere A.pr_id not in (select A.pr_id from #temp)unionselect * from #tempdrop table #temp?--I hope that when I die someone will say of me "That guy sure owed me a lot of money" |
 |
|
|
Nazim
A custom title
1408 Posts |
Posted - 2002-01-29 : 23:38:51
|
| Try thisSELECT *FROM product_traffic AS TableA , product_price AS TableBwhere (TableA.pr_id = TableB.pr_id AND TableA.cl_id = TableB.cl_id) OR (TableA.pr_id = TableB.pr_id) HTH--------------------------------------------------------------Dont Tell God how big your Problem is , Tell the Problem how Big your God is |
 |
|
|
rrb
SQLTeam Poet Laureate
1479 Posts |
Posted - 2002-01-31 : 01:33:57
|
| Doh!I knew that.... Thanks Nazim--I hope that when I die someone will say of me "That guy sure owed me a lot of money" |
 |
|
|
ngm
Starting Member
3 Posts |
Posted - 2002-01-31 : 17:50:19
|
Nazim,that query does exactly the same thing as mentioned one from original posting:quote: SELECT *FROM product_traffic AS TableA INNER JOIN (SELECT * FROM product_price) AS TableB ON (TableA.pr_id = TableB.pr_id AND TableA.cl_id = TableB.cl_id) OR (TableA.pr_id = TableB.pr_id)
The point is to provide the query which will return records from TableA.pr_id = TableB.pr_id condition if TableA.pr_id = TableB.pr_id AND TableA.cl_id = TableB.cl_id condition isn't true, both it must not return records from both.Your query and the original one are returning exactly the same results.I believe that rrb's query does the right thing, but is it possible to write that in just one line statement or on some other easier way?Thanks a lot!!! |
 |
|
|
Lou
Yak Posting Veteran
59 Posts |
Posted - 2002-01-31 : 18:47:30
|
| ngm,No offense, but I don't think your sql can ever work.You have 2 conditions:1.TableA.pr_id = TableB.pr_id AND TableA.cl_id = TableB.cl_id2.TableA.pr_id = TableB.pr_idCondition #1 is really a "subset" of condition#2. Your statement says give me all the rows that meet condition#1 or meet condition#2. Because of the "or", you will always get "2" rows. In other words, you're saying give me the "bigset" or give me the "subset", so sql will always give you the bigset.- LouEdited by - lou on 01/31/2002 19:41:21 |
 |
|
|
Arnold Fribble
Yak-finder General
1961 Posts |
Posted - 2002-02-01 : 04:38:34
|
You can fold the temporary table in to make it one select, but it will probably run slower than rrb's version:SELECT *FROM product_traffic AINNER JOIN product_price B ON A.pr_id = B.pr_idWHERE A.cl_id = B.cl_id OR A.pr_id NOT IN ( SELECT A.pr_id FROM product_traffic A INNER JOIN product_price B ON A.pr_id = B.pr_id AND A.cl_id = B.cl_id) Edited by - Arnold Fribble on 02/01/2002 04:41:16 |
 |
|
|
Lou
Yak Posting Veteran
59 Posts |
Posted - 2002-02-01 : 11:53:38
|
| ngm,Let me try to elaborate. These are your 2 conditions. Notice #1 is really #2 with another AND clause.1.TableA.pr_id = TableB.pr_id AND TableA.cl_id = TableB.cl_id2.TableA.pr_id = TableB.pr_idSo what you're really saying is this. First give me all the rows that meet condition #2select * into #tempfrom tableA join tableBon TableA.pr_id = TableB.pr_idThen you're saying, take the previous result set (#temp) and filter it using the stricter condition#1. But if no result set exists, never mind. You're applying the stricter condition#1 then immediately 'undoing' it.Edited by - Lou on 02/01/2002 11:58:43 |
 |
|
|
Arnold Fribble
Yak-finder General
1961 Posts |
Posted - 2002-02-01 : 12:17:55
|
Are you saying that rrb's query (or my version) does the same as something simpler? If so, then tell us what the simpler query is! |
 |
|
|
Lou
Yak Posting Veteran
59 Posts |
Posted - 2002-02-01 : 15:06:04
|
| What I'm trying to say is that the sql statements end up doing this:select * into #tempfrom tableA join tableBon TableA.pr_id = TableB.pr_idmeaning they don't really do a whole lot- Lou |
 |
|
|
Lou
Yak Posting Veteran
59 Posts |
Posted - 2002-02-01 : 15:14:45
|
| I'm not trying offend any of you. All of the SQL posted does what the original questioner asks. I think the question itself is twisted. I think the questioner probably had a case statement in mind. Case when this then do that...etc. However, the questioner tried to use JOINS which I think is inappropriate.- Lou |
 |
|
|
Arnold Fribble
Yak-finder General
1961 Posts |
Posted - 2002-02-01 : 18:15:53
|
/* Just for the sake of sanity, let's assume the columns in question are composite pks */CREATE TABLE product_traffic ( pr_id int NOT NULL, cl_id int NOT NULL, CONSTRAINT pk_pt PRIMARY KEY CLUSTERED (pr_id, cl_id))CREATE TABLE product_price ( pr_id int NOT NULL, cl_id int NOT NULL, CONSTRAINT pk_pp PRIMARY KEY CLUSTERED (pr_id, cl_id))/* Give the tables 15000 p(0.5) Bernoulli trials (that's posh for coin-tosses) each */DECLARE @pr_max int, @cl_max intSET @pr_max = 1000SET @cl_max = 15DECLARE @pr int, @cl intSET NOCOUNT ONSET @pr = 0WHILE @pr < @pr_maxBEGIN SET @cl = 0 WHILE @cl < @cl_max BEGIN IF RAND() < 0.5 INSERT INTO product_traffic VALUES (@pr, @cl) IF RAND() < 0.5 INSERT INTO product_price VALUES (@pr, @cl) SET @cl = @cl + 1 END SET @pr = @pr + 1ENDSET NOCOUNT OFF/* just check how many in each */SELECT (SELECT COUNT(*) FROM product_traffic) AS pt_count, (SELECT COUNT(*) FROM product_price) AS pp_count/* So one of these... */SELECT * FROM product_traffic AINNER JOIN product_price B ON A.pr_id = B.pr_idSELECT * FROM product_traffic AINNER JOIN product_price B ON A.pr_id = B.pr_id AND A.cl_id = B.cl_id/* ...is supposed to be the same as either this... *//* (ok, so I had to edit Robert's to get it to parse) */SELECT A.pr_id AS A_pr, A.cl_id AS A_cl, B.pr_id AS B_pr, B.cl_id AS B_clINTO #tempFROM product_traffic AINNER JOIN product_price B ON A.pr_id = B.pr_id AND A.cl_id = B.cl_id/* UNION ALL would be ok since they're disjoint by definition */SELECT * FROM #tempUNIONSELECT *FROM product_traffic AINNER JOIN product_price B ON A.pr_id = B.pr_idWHERE A.pr_id NOT IN (SELECT A_pr FROM #temp)DROP TABLE #temp/* ...or this */SELECT *FROM product_traffic AINNER JOIN product_price B ON A.pr_id = B.pr_idWHERE A.cl_id = B.cl_id OR A.pr_id NOT IN ( SELECT A.pr_id FROM product_traffic A INNER JOIN product_price B ON A.pr_id = B.pr_id AND A.cl_id = B.cl_id) I find it easiest to cut and paste into Query Analyzer from the reply box, which doesn't screw up the formatting and capriciously lose characters.Edited by - Arnold Fribble on 02/01/2002 18:32:57 |
 |
|
|
Lou
Yak Posting Veteran
59 Posts |
Posted - 2002-02-04 : 14:49:50
|
| No I don't think you understand the orignal statement. This is the original statement:SELECT *FROM product_traffic AS TableA INNER JOIN (SELECT * FROM product_price) AS TableB ON (TableA.pr_id = TableB.pr_id AND TableA.cl_id = TableB.cl_id) OR (TableA.pr_id = TableB.pr_id) which logically is the same as this:SELECT *FROM product_traffic AS TableA INNER JOIN (SELECT * FROM product_price) AS TableB ON (TableA.pr_id = TableB.pr_id) Frankly, I'm surprised we're still discussing this thread???-- Lou |
 |
|
|
Arnold Fribble
Yak-finder General
1961 Posts |
Posted - 2002-02-04 : 15:08:57
|
| I know that. But pardon me, I thought you were claiming that rrb's query was doing the same thing. I find it quite difficult to read "All of the SQL posted does what the original questioner asks" any other way. |
 |
|
|
Lou
Yak Posting Veteran
59 Posts |
Posted - 2002-02-04 : 15:33:24
|
| You're right. I'm wrong. Can we move on? |
 |
|
|
|