我遇到SQL Server問題。我有表A中獲取列的值相同的行
id field1 field2 field3
1 a b 10
2 a c 20
3 b e 30
4 b a 50
5 c null 20
如果字段1 = 10%*場3 如果字段2 = 5%*場3
a= 10%*10+10%*20+5%*50=5.5
我想表
name values
a 5.5
b 8.5
c 3
e 1.5
請幫助我。謝謝!
我遇到SQL Server問題。我有表A中獲取列的值相同的行
id field1 field2 field3
1 a b 10
2 a c 20
3 b e 30
4 b a 50
5 c null 20
如果字段1 = 10%*場3 如果字段2 = 5%*場3
a= 10%*10+10%*20+5%*50=5.5
我想表
name values
a 5.5
b 8.5
c 3
e 1.5
請幫助我。謝謝!
我不明白你想要什麼,但希望你能在這裏得到點子。
這是你的表(請發表您的表結構和惰性查詢您的問題,同時發佈新的問題,所以你會得到答案迅速和明確所有)
create table #temp (id int identity(1,1),field1 varchar(50),field2 varchar(50),field3 int)
INSERT INTO #temp (field1 ,field2 ,field3)
values
('a', 'b' , 10),
('a', 'c' , 20),
('b', 'e' , 30),
('b', 'a' , 50),
('c', null , 20)
這裏查詢選擇不同的百分比爲所有字段3值
select field1,
field2 ,
field3,
(.1 * field3) as TenPer_field3,
(.05 * field3) as FivePer_field3
from #temp
輸出:
field1 field2 field3 TenPer_field3 Fiveper_field3
a b 10 1.0 0.50
a c 20 2.0 1.00
b e 30 3.0 1.50
b a 50 5.0 2.50
c NULL 20 2.0 1.00
當你評論我明白你想要什麼。
;with CTE AS(
select field1 as name, (.1 * field3) as value from #temp
Union
select field2 as name, (.05 * field3) as value from #temp
)
select name,sum(value) value
from CTE
WHERE name is not null
GROUP BY name
輸出
name value
a 5.50
b 8.50
c 3.00
e 1.50
謝謝。但我想要一個結果查詢==> a = 5.5(1.0 + 2.0 + 2.5) – user3313766
我明白你想要什麼,請檢查你想要的查詢我編輯我的答案。 –
太棒了!這就是我想要的。非常感謝Siddique Mahsud。 – user3313766
顯示到目前爲止你已經嘗試過的東西,這不是一個困難的查詢 – Sparky
我知道,但我剛剛瞭解SQL你能幫助我嗎? – user3313766
換言之,真的很難理解你想要什麼。 – Doug