Author |
Topic |
Gekko
Yak Posting Veteran
63 Posts |
Posted - 2012-02-01 : 11:28:02
|
hallo pls help with query. TabSaleitem...name...price1.......a......21.......a......11.......a......22.......b......32.......b......22.......b......3TabGroupitem........grp1...........2222...........222result:grp....name....price1222.....b........13description result:select distinct grp=222 (item 1,2 is join with grp 222)name=b because product "b" has price > name "a" (b=8, a=5)price1=13 sum price for group 222thanks for help |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-01 : 12:17:44
|
[code]SELECT grp,name,GPriceFROM(SELECT tg.grp,ti.name,ti.price,ROW_NUMBER() OVER (PARTITION BY tg.grp ORDER BY ti.price desc) AS rn,SUM(Price) OVER (PARTITION BY tg.grp) AS GPriceFROM TabGroup tgINNER JOIN (SELECT item,name,SUM(Price) AS Price FROM TabSale GROUP BY item,name )tsON ts.item = tg.item)tWHERE rn=1[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
Gekko
Yak Posting Veteran
63 Posts |
Posted - 2012-02-01 : 13:13:58
|
visakh16As always, thank you very |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-01 : 13:26:03
|
welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
Gekko
Yak Posting Veteran
63 Posts |
Posted - 2012-02-01 : 16:54:59
|
quote: [i]Originally posted by visakh16
SELECT grp,name,GPriceFROM(SELECT tg.grp,ti.name,ti.price,ROW_NUMBER() OVER (PARTITION BY tg.grp ORDER BY ti.price desc) AS rn,SUM(Price) OVER (PARTITION BY tg.grp) AS GPriceFROM TabGroup tgINNER JOIN (SELECT item,name,SUM(Price) AS Price FROM TabSale GROUP BY item,name )tsON ts.item = tg.item)tWHERE rn=1
when I add one column:TabSaleitem...name...price...town1.......a......2......8881.......a......1......8881.......a......2......4442.......b......3......4442.......b......2......4442.......b......3......444but following query returned:grp....name....price1....town2222.....b........13.......1(correctly is 2) it is count distinctwhere is error please?SELECT grp,name,GPrice,town1FROM(SELECT tg.grp,ti.name,ti.price,town1,ROW_NUMBER() OVER (PARTITION BY tg.grp ORDER BY ti.price desc) AS rn,SUM(Price) OVER (PARTITION BY tg.grp) AS GPrice, COUNT(DISTINCT town1) OVER (PARTITION BY tg.grp) as town2FROM TabGroup tgINNER JOIN (SELECT item,name,SUM(Price) AS Price, COUNT(DISTINCT town) as town1 FROM TabSale GROUP BY item,name )tsON ts.item = tg.item)tWHERE rn=1thanks |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-01 : 17:15:50
|
[code]SELECT grp,name,GPrice,town1FROM(SELECT tg.grp,ti.name,ti.price,town1,ROW_NUMBER() OVER (PARTITION BY tg.grp ORDER BY ti.price desc) AS rn,SUM(Price) OVER (PARTITION BY tg.grp) AS GPrice, SUM(town1) OVER (PARTITION BY tg.grp) as town2FROM TabGroup tgINNER JOIN (SELECT item,name,SUM(Price) AS Price, COUNT(DISTINCT town) as town1FROM TabSaleGROUP BY item,name)tsON ts.item = tg.item)tWHERE rn=1[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
Gekko
Yak Posting Veteran
63 Posts |
Posted - 2012-02-01 : 17:22:38
|
one mistake...please editvisakh16 your query returned town 3 (correctly is 2)TabSaleitem...name...price...town1.......a......2......8881.......a......1......8881.......a......2......4442.......b......3......4442.......b......2......4442.......b......3......444your query:item 1 has select distinct town = 2 (888 and 444)item 2 has select distinct town = 1 (444)result your query is 3, The result should be correctly 2 (town 888 and 444) Can you edit query please.? |
 |
|
kiran.bala
Starting Member
1 Post |
Posted - 2012-02-02 : 07:30:59
|
Select g.grp,(Select name from TabSale group by name havingSUM(price)in( (Select max(a.price) price1 from(select SUM(price)price from TabSale group by name )a)))Name,SUM(s.price)Price1from TabSale S join TabGroup G on s.item = G.item Group by g.grp |
 |
|
Gekko
Yak Posting Veteran
63 Posts |
Posted - 2012-02-02 : 08:06:26
|
thanks kiran.balabut it is no goodyour result is:grp......name....price222.......a.......13town missing |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-02 : 11:49:58
|
[code]SELECT grp,name,GPrice,town1FROM(SELECT tg.grp,ti.name,ti.price,town1,ROW_NUMBER() OVER (PARTITION BY tg.grp ORDER BY ti.price desc) AS rn,SUM(Price) OVER (PARTITION BY tg.grp) AS GPrice, MAX(town1) OVER (PARTITION BY tg.grp) as town2FROM TabGroup tgINNER JOIN (SELECT item,name,SUM(Price) AS Price, COUNT(DISTINCT town) as town1FROM TabSaleGROUP BY item,name)tsON ts.item = tg.item)tWHERE rn=1[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
Gekko
Yak Posting Veteran
63 Posts |
Posted - 2012-02-02 : 14:30:32
|
thanks visakh16I do not understand it is wrong somewhere ..... Town returned different numbersTabSaleitem...name...price...town1.......a......2......8881.......a......1......8881.......a......2......4442.......b......3......4442.......b......2......7772.......b......3......7772.......b......2......7772.......b......3......555result:grp....name....gprice....town1222.....b.......18.......4 (but your query return for example 3 or 2)I hope this is well described |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-02 : 14:34:21
|
can you tell how town will be 4? it should be 5 isnt it?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-02 : 14:52:12
|
ok got iti think this is what you want?;With Group_DetailsAS(SELECT tg.grp,item,name,Price,townFROM TabGroup tgINNER JOIN (SELECT item,name,SUM(Price) AS Price,townFROM TabSaleGROUP BY item,name,town)tsON ts.item = tg.item)SELECT gd.grp,gd.item,gd1.Price,gd1.town1FROM Group_Details gd INNER JOIN (SELECT grp,SUM(Price) AS Price, COUNT(DISTINCT town) AS town1,MAX(Price) AS MPrice FROM Group_Details GROUP BY grp) gd1On gd1.grp = gd.grpAND gd1.MPrice = gd.Price ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
Gekko
Yak Posting Veteran
63 Posts |
Posted - 2012-02-02 : 14:57:42
|
quote: Originally posted by visakh16 can you tell how town will be 4? it should be 5 isnt it?
4 it is, it is count distinct town for group 222item 1 and 2 is grp 222 (first topic)it means for grp 222 town1 will 4price not affecting town1understant? |
 |
|
Gekko
Yak Posting Veteran
63 Posts |
Posted - 2012-02-02 : 15:53:08
|
thats it and sorry for the misunderstandingThank you for your patience, visakh16 |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-02 : 18:21:52
|
welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|