Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
wangyc77
Yak Posting Veteran
65 Posts |
Posted - 2007-01-31 : 04:14:38
|
| Hi,I have a table looks like thisItemNo PartNo price1 10 1001 20 1001 30 NULL 2 10 1502 20 1002 30 100I did this to find out what is the Total price for Item 1 and 2[Select ItemNo, sum(price) from Tb group by ItemNo]That will returnItemNo price1 2002 350which looks great because SQL server will ignore the NULL price, but this is not what I want. Because if I don't don't look at the saw data then I won't be able to know there is NULL in one of the item. So I may want to achieveeitherItemNo Price1 NULL2 350OR any mothod that will exclude ItemNo 1 Thanks**Jonathan** |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-01-31 : 04:18:16
|
| [code]-- prepare sample datadeclare @t table (ItemNo int, PartNo int, price int)insert @tselect 1, 10, 100 union allselect 1, 20, 100 union allselect 1, 30, NULL union allselect 2, 10, 150 union allselect 2, 20, 100 union allselect 2, 30, 100SELECT ItemNo, SUM(Price)FROM @tGROUP BY ItemNoHAVING COUNT(*) = COUNT(Price)SELECT ItemNo, SUM(Price)FROM @tGROUP BY ItemNoHAVING COUNT(*) <> COUNT(Price)[/code]Peter LarssonHelsingborg, Sweden |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2007-01-31 : 04:18:30
|
"any mothod that will exclude ItemNo 1 "This maybe?Select ItemNo, sum(price) from Tb AS TWHERE ItemNo NOT IN( SELECT ItemNo FROM Tb WHERE PRICE IS NULL)group by ItemNo Kristen |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-01-31 : 04:23:47
|
| [code]-- prepare sample datadeclare @t table (ItemNo int, PartNo int, price int)insert @tselect 1, 10, 100 union allselect 1, 20, 100 union allselect 1, 30, NULL union allselect 2, 10, 150 union allselect 2, 20, 100 union allselect 2, 30, 100SELECT ItemNo, CASE WHEN COUNT(*) = COUNT(Price) THEN SUM(Price) ELSE NULL ENDFROM @tGROUP BY ItemNo[/code]Peter LarssonHelsingborg, Sweden |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-01-31 : 04:40:12
|
Exclude ItemNo = 1-- prepare sample datadeclare @t table (ItemNo int, PartNo int, price int)insert @tselect 1, 10, 100 union allselect 1, 20, 100 union allselect 1, 30, NULL union allselect 2, 10, 150 union allselect 2, 20, 100 union allselect 2, 30, 100select t.ItemNo, TotalPrice = sum(price)from @t as t left join ( select ItemNo from @t where price is null ) as n on t.ItemNo = n.ItemNowhere n.ItemNo is nullgroup by t.ItemNo/*ItemNo TotalPrice ----------- ----------- 2 350*/ KH |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-01-31 : 04:43:50
|
[code]-- prepare sample datadeclare @t table (ItemNo int, PartNo int, price int)insert @tselect 1, 10, 100 union allselect 1, 20, 100 union allselect 1, 30, NULL union allselect 2, 10, 150 union allselect 2, 20, 100 union allselect 2, 30, 100 -- Query 1select t.ItemNo, TotalPrice = sum(price)from @t as t left join ( select ItemNo from @t where price is null ) as n on t.ItemNo = n.ItemNowhere n.ItemNo is nullgroup by t.ItemNo/*ItemNo TotalPrice ----------- ----------- 2 350*/ -- Query 2select t.ItemNo, TotalPrice = sum(case when n.ItemNo is null then price end)from @t as t left join ( select ItemNo from @t where price is null ) as n on t.ItemNo = n.ItemNogroup by t.ItemNo/* ItemNo TotalPrice ----------- ----------- 1 NULL2 350*/[/code] KH |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2007-01-31 : 05:21:15
|
| So which is the most efficient then? |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-01-31 : 05:27:31
|
Both of mine With a staggering 25%-- prepare sample datadeclare @t table (ItemNo int, PartNo int, price int)insert @tselect 1, 10, 100 union allselect 1, 20, 100 union allselect 1, 30, NULL union allselect 2, 10, 150 union allselect 2, 20, 100 union allselect 2, 30, 100SELECT ItemNo, SUM(Price)FROM @tGROUP BY ItemNoHAVING COUNT(*) = COUNT(Price)Select ItemNo, sum(price) from @T AS TWHERE ItemNo NOT IN( SELECT ItemNo FROM @t WHERE PRICE IS NULL)group by ItemNoselect t.ItemNo, TotalPrice = sum(price)from @t as t left join ( select ItemNo from @t where price is null ) as n on t.ItemNo = n.ItemNowhere n.ItemNo is nullgroup by t.ItemNoselect t.ItemNo, TotalPrice = sum(price)from @t as t left join ( select ItemNo from @t where price is null ) as n on t.ItemNo = n.ItemNowhere n.ItemNo is nullgroup by t.ItemNoSELECT ItemNo, CASE WHEN COUNT(*) = COUNT(Price) THEN SUM(Price) ELSE NULL ENDFROM @tGROUP BY ItemNoselect t.ItemNo, TotalPrice = sum(case when n.ItemNo is null then price end)from @t as t left join ( select ItemNo from @t where price is null ) as n on t.ItemNo = n.ItemNogroup by t.ItemNo Peter LarssonHelsingborg, Sweden |
 |
|
|
wangyc77
Yak Posting Veteran
65 Posts |
Posted - 2007-01-31 : 06:35:47
|
quote: Originally posted by Peso
-- prepare sample datadeclare @t table (ItemNo int, PartNo int, price int)insert @tselect 1, 10, 100 union allselect 1, 20, 100 union allselect 1, 30, NULL union allselect 2, 10, 150 union allselect 2, 20, 100 union allselect 2, 30, 100SELECT ItemNo, CASE WHEN COUNT(*) = COUNT(Price) THEN SUM(Price) ELSE NULL ENDFROM @tGROUP BY ItemNo Peter LarssonHelsingborg, Sweden
**Jonathan** |
 |
|
|
wangyc77
Yak Posting Veteran
65 Posts |
Posted - 2007-01-31 : 06:42:43
|
quote: Originally posted by Peso
-- prepare sample datadeclare @t table (ItemNo int, PartNo int, price int)insert @tselect 1, 10, 100 union allselect 1, 20, 100 union allselect 1, 30, NULL union allselect 2, 10, 150 union allselect 2, 20, 100 union allselect 2, 30, 100SELECT ItemNo, SUM(Price)FROM @tGROUP BY ItemNoHAVING COUNT(*) = COUNT(Price)SELECT ItemNo, SUM(Price)FROM @tGROUP BY ItemNoHAVING COUNT(*) <> COUNT(Price) Peter LarssonHelsingborg, Sweden
I found this really efficient and clean and it runs well.But I encounter a new problem when I need to do some logic inside the SUM()as FollowedSELECT ItemNo, SUM(CASE..WHEN..THEN price END) as ApriceFROM @tGROUP BY ItemNoHAVING COUNT(*) = COUNT(APrice)Sorry I didn't expect this to happen**Jonathan** |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-01-31 : 06:45:13
|
| What is the logic?Peter LarssonHelsingborg, Sweden |
 |
|
|
wangyc77
Yak Posting Veteran
65 Posts |
Posted - 2007-01-31 : 07:06:59
|
| SELECT ItemNo, SUM(CASE price WHEN price>100 THEN price+10 WHEN price>150 THEN price+20END) as ApriceFROM @tGROUP BY ItemNoHAVING COUNT(*) = COUNT(APrice)**Jonathan** |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2007-01-31 : 07:17:56
|
(CASE priceWHEN price>100 THEN price+10 WHEN price>150 THEN price+20ELSE priceEND)Try that!Kristen |
 |
|
|
wangyc77
Yak Posting Veteran
65 Posts |
Posted - 2007-01-31 : 07:22:01
|
| Just for informationSome of the things I posted in the forum may not be a complete question, so I am sorry if some of you may think I am wasting other people time by doing so. But the thing I am building in my job sometimes requires me to add in new feature into existing one. For example, they may want to add different profit for different price range, or when there are more suppliers, they may want currency convertion features added in. Things are relatively simple when there are not many items in the inventory, but it grows complicated when there are other factors added in.(such as pricing, currency etc)So sorry if the problem just grows bigger and bigger and you guy need to re-think the problem.**Jonathan** |
 |
|
|
wangyc77
Yak Posting Veteran
65 Posts |
Posted - 2007-01-31 : 07:26:29
|
quote: Originally posted by Kristen (CASE priceWHEN price>100 THEN price+10 WHEN price>150 THEN price+20ELSE priceEND)Try that!Kristen
Sorry for the typoIf I am not wrong, the problem lies with I cant use [Count(Aprice )] when it is used [SUM(..) as Aprice] in this way.**Jonathan** |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2007-01-31 : 07:33:17
|
| "HAVING COUNT(*) = COUNT(APrice)"Correct, you can't do that.But isn't it the same asHAVING COUNT(*) = COUNT(price)??"Some of the things I posted in the forum may not be a complete question, so I am sorry if some of you may think I am wasting other people time by doing so."Suggest you consider it this way:People give their time for free here. If you don't ask accurate questions, and thus people waste their time providing answers and then being asked to answer a more accurate question, then the likihood is that they will choose to give their time to other people!Speaking for myself: rightly or wrongly there are people who post on this forum whos questions I don't bother to read any more ...Kristen |
 |
|
|
wangyc77
Yak Posting Veteran
65 Posts |
Posted - 2007-01-31 : 07:37:25
|
quote: Originally posted by Kristen "HAVING COUNT(*) = COUNT(APrice)"Correct, you can't do that.But isn't it the same asHAVING COUNT(*) = COUNT(price)??"Some of the things I posted in the forum may not be a complete question, so I am sorry if some of you may think I am wasting other people time by doing so."Suggest you consider it this way:People give their time for free here. If you don't ask accurate questions, and thus people waste their time providing answers and then being asked to answer a more accurate question, then the likihood is that they will choose to give their time to other people!Speaking for myself: rightly or wrongly there are people who post on this forum whos questions I don't bother to read any more ...Kristen
Thank you for your advise, to be honest, Peter had some good scolding to me already. I will definitely take the lesson and try to be as accurate as I possibly can.Thank you.**Jonathan** |
 |
|
|
|
|
|
|
|