| Author |
Topic |
|
sqldba2k6
Posting Yak Master
176 Posts |
Posted - 2007-02-16 : 14:02:33
|
| --Please help in getting the outputRule:Query should exclude the records which has more than 1 custid in @prod2 table..declare @Cust1 table(Custid int,Csid bigint, name1 varchar(5),state varchar(4),zip varchar(8))insert into @Cust1select 4102,103, 'Sam', 'NY', '2356' union allselect 4101,110, 'Pat', 'CA', '9526' union allselect 4102,106, 'Rob', 'NY', '2356' declare @Prod2 table(Csid bigint,Prod1 varchar(4),Prod2 varchar(4),EPRT int,createddate datetime)insert into @Prod2select 103, '11', '03', 1, '2007-02-02 14:13:43.000' union allselect 106, '01', '04', 2, '2007-02-02 14:13:31.000' union allselect 110, '11', '03', 0, '2007-02-02 14:13:31.000'Desired Results:Custid csid state name1 zip prod1 prod2 EPRT createddate ----------- -------------------- ----- ----- -------- ----- ----- ----------- ------------------------4101 110 CA Pat 9526 11 03 0 2007-02-02 14:13:31.000 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2007-02-16 : 14:12:15
|
Here's what I came up with:SELECT c.Custid, c.csid, c.state, c.name1, c.zip, p.prod1, p.prod2, p.eprt, p.createddateFROM @Cust1 cINNER JOIN @Prod2 pON c.Csid = p.CsidINNER JOIN( SELECT c.Custid, COUNT(*) AS CustidCount FROM @Cust1 c INNER JOIN @Prod2 p ON c.Csid = p.Csid GROUP BY c.Custid HAVING COUNT(c.Custid) = 1) tON c.Custid = t.Custid Tara Kizer |
 |
|
|
sqldba2k6
Posting Yak Master
176 Posts |
Posted - 2007-02-16 : 14:22:16
|
| Thanks Tara Kizer!.How about below one...select t1.Custid,t2.csid, t1.state, t1.name1, t1.zip, t2.prod1, t2.prod2, t2.EPRT,t2.createddatefrom @Cust1 t1 inner join @Prod2 t2on t1.Csid = t2.Csidwhere t1.Custid not in (select t1.Custid from @Cust1 t1 inner join @Prod2 t2on t1.Csid = t2.Csid GROUP BY t1.Custid HAVING COUNT(*) > 1) |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-02-16 : 14:23:33
|
| [code]-- prepare sample datadeclare @Cust1 table (Custid int, Csid bigint, name1 varchar(5), state varchar(4), zip varchar(8))insert into @Cust1select 4102,103, 'Sam', 'NY', '2356' union allselect 4101,110, 'Pat', 'CA', '9526' union allselect 4102,106, 'Rob', 'NY', '2356' declare @Prod2 table (Csid bigint, Prod1 varchar(4), Prod2 varchar(4), EPRT int, createddate datetime)insert into @Prod2select 103, '11', '03', 1, '2007-02-02 14:13:43.000' union allselect 106, '01', '04', 2, '2007-02-02 14:13:31.000' union allselect 110, '11', '03', 0, '2007-02-02 14:13:31.000'-- Show the resultSELECT t1.CustID, t1.CsID, t1.State, t1.Name1, t1.Zip, t2.Prod1, t2.Prod2, t2.Eprt, t2.CreatedDateFROM @Cust1 AS t1INNER JOIN @Prod2 AS t2 ON t2.CsID = t1.CsIDINNER JOIN ( SELECT e.CustID FROM @Cust1 AS e INNER JOIN @Prod2 AS t ON t.CsID = e.CsID GROUP BY e.CustID HAVING COUNT(*) = 1 ) AS x ON x.CustID = t1.CustID[/code]Peter LarssonHelsingborg, Sweden |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2007-02-16 : 14:29:54
|
quote: Originally posted by sqldba2k6 Thanks Tara Kizer!.How about below one...select t1.Custid,t2.csid, t1.state, t1.name1, t1.zip, t2.prod1, t2.prod2, t2.EPRT,t2.createddatefrom @Cust1 t1 inner join @Prod2 t2on t1.Csid = t2.Csidwhere t1.Custid not in (select t1.Custid from @Cust1 t1 inner join @Prod2 t2on t1.Csid = t2.Csid GROUP BY t1.Custid HAVING COUNT(*) > 1)
I prefer to use joins rather than subqueries.Tara Kizer |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2007-02-16 : 14:30:29
|
| Peter, isn't that what I posted already?Tara Kizer |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-02-16 : 14:36:13
|
Of course. I opened the topic and copied the code to SSMS. When done, I pasted the suggestion back to SQLTeam without first refreshing the webpage.Sorry if I offended you...Peter LarssonHelsingborg, Sweden |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2007-02-16 : 14:38:21
|
| No problem, I just wanted to make sure I wasn't going blind! I see very slight differences between the two, but nothing that would change the execution plan.Tara Kizer |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
|
|
sqldba2k6
Posting Yak Master
176 Posts |
Posted - 2007-02-16 : 19:09:51
|
| Is it possible to dynamically exported the results with column headers and data of the sql query to csv file? |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-02-17 : 02:14:54
|
| BCP.Peter LarssonHelsingborg, Sweden |
 |
|
|
sqldba2k6
Posting Yak Master
176 Posts |
Posted - 2007-02-17 : 10:19:35
|
| Thanks peter!!Is there any limitation of the query size which can be used in BCP..Can i make the query as stored procedure.So that i can call the sp in BCP.. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-02-17 : 13:13:07
|
| I think it is best you place the code in a VIEW, and do a simpleSELECT * FROM MyViewin the BCP commmandPeter LarssonHelsingborg, Sweden |
 |
|
|
sqldba2k6
Posting Yak Master
176 Posts |
Posted - 2007-02-20 : 14:20:22
|
| Peter!I am using temporary tables in my query which i can't use the query in view.So can i use sp's in the bcp.. |
 |
|
|
|
|
|