| Author |
Topic |
|
Tina1225
Starting Member
1 Post |
Posted - 2010-10-06 : 11:08:34
|
| Hello,CREATE TABLE [dbo].[test2]( [name] [varchar](20) NULL, [id] [varchar](20) NOT NULL, [country] [varchar](20) NULL, [state] [varchar](20) NULL) ON [PRIMARY]CREATE TABLE [dbo].[costdetail]( [name] [varchar](20) NULL, [cost] [decimal](20, 0) NULL) ON [PRIMARY]GOI need the name,country, state, totalcost and record count(in cost detail table) for each vendor name and I am trying the following query select drv.name,id,state,country,drv.spend,drv.recordcnt from (select test2.name,SUM(cost) as spend,COUNT(*) as recordcnt from test2 join costdetail on test2.name=costdetail.name where country<>'usa' group by test2.name) as drv join (select name,id,state,country from test2 where country<>'usa' ) as dvr on drv.name=dvr.nameBut i getting duplicated results in the Query output. Some of the records are being duplicated 3 to 4 times in the output. I am getting something as follows.Walmart,alberta,canada,320, 3jcpenney,alberta,canada,564,1Walmart,alberta,canada,320, 3Walmart,alberta,canada,320, 3walgreens,sydney,australia, 6730 12walgreens,sydney,australia, 6730 12cubs,sydney,australia,4230,1cubs,sydney,australia,4230,1Please help me with the above problem. |
|
|
pk_bohra
Master Smack Fu Yak Hacker
1182 Posts |
Posted - 2010-10-06 : 11:22:55
|
| Try this:Select test2.name,state,country,SUM(cost) as spend,COUNT(*) as recordcnt from test2 join costdetail on test2.name=costdetail.namewhere country<>'usa'Group by test2.name,state,countryRegards,BohraI am here to learn from Masters and help new bees in learning. |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-10-06 : 11:23:45
|
I think this will do it:select t2.name,t2.id,t2.country,t2.state,dt.spend,dt.recordcntfrom test2 as t2join(select name,sum(cost) as spend,count(*) as recordcnt from costdetail group by name)dton dt.name=t2.name No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
|
|
|