我按seconditemid
对数据进行分组。是否可以计算每行sum(extcost)
在总和中的百分比(所有表格数据的总结合)?
seconditemid|ratio
--------------------
A1 |.45
--------------------
A2 |.55
我的查询是
select seconditemid,
round(100.0*(
sum(case when seconditemid = ---the current row's seconditemid
then 1 else 0 end)/sum(extcost)
),1) as ratio
from inventory_fact f inner join item_master_dim i using (itemmasterkey)
where transtypekey = 1
group by seconditemid
order by 2 desc;
这不起作用。我尝试先创建一个视图
create view v1 as(
select sum(extcost) as sumExtcost from inventory_fact
);
并从中选择
select seconditemid, round(100.0*(
sum(extcost)/sum(v1.sumextcost)
),1) as ratio
from from inventory_fact f inner join item_master_dim i using (itemmasterkey), v1
where transtypekey = 1
group by seconditemid
order by 2 desc;
然后每列的比例变为0。
答案 0 :(得分:0)
我们来看看这个样本架构:
<!DOCTYPE html>
<html>
<head>
<title> </title>
</head>
<body>
<script>
var alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",]
var input = prompt("Enter In The Text That You Want To Decode")
var results = []
//Decoding
var ceaser_alphabet = []
var cAlphabet_creation_progress = 1
var ceaser_progress = 1
var decoding_progress = 1
var decoded_message = ""
var slice1 = []
var slice2 = []
var decoder = function () {
//Uses 25 instead of 26 because the first item in an array is counted as "0"
slice1 = alphabet.slice(0, (26 - ceaser_progress))
slice2 = alphabet.slice((26 - ceaser_progress), 26)
while (cAlphabet_creation_progress < 26) {
if (cAlphabet_creation_progress < slice1.length) {
ceaser_alphabet.push(slice1[cAlphabet_creation_progress])
} else {
ceaser_alphabet.push(slice2[cAlphabet_creation_progress])
}
}
//Decoding
while(decoding_progress < input.length) {
if (!input.charAt(decoding_progress) == " ") {
decoded_message = decoded_message + ceaser_alphabet[ceaser_alphabet.indexOf(alphabet.indexOf(input.charAt(decoding_progress)))]
decoding_progress++
} else {
decoded_message = decoded_message + " "
}
}
if (decoding_progress == input.length) {
results.push(decoded_message)
ceaser_progress++
decoding_progress = 1
ceaser_alphabet = []
decoded_message = ""
}
}
var possibilities_left = 26
while (possibilities_left > 0) {
possibilities_left--
decoder()
}
console.log(results)
</script>
</body>
</html>
以下是查询:
CREATE TABLE c (
seconditemid text,
total int
);
INSERT INTO c (seconditemid, total) VALUES ('A1', 4500);
INSERT INTO c (seconditemid, total) VALUES ('A2', 5500);
- &GT;
SELECT seconditemid, total,
total::float / (SUM(total) OVER()) as ratio
FROM c;
答案 1 :(得分:0)
您的第二个查询应该没问题但是0
已经float
了。您需要明确地将总和值转换为SELECT g.seconditemid, g.extcost::float / t.total::float percent -- << here
FROM (
SELECT seconditemid, SUM(extcost) extcost
FROM inventory_fact
GROUP BY seconditemid
) g CROSS JOIN (
SELECT SUM(extcost) total
FROM inventory_fact
) t
ORDER BY percent DESC
。
以下是没有视图的示例
V/SDL (29083): SDL audio: opening device
V/SDL (29083): SDL audio: wanted stereo 16-bit 44.1kHz, 4096 frames buffer
I/AudioPolicyManager( 2267): getOutputForAttr() device 0x2, samplingRate 44100, format afcad8d8, channelMask 1, flags 0
I/AudioPolicyManager( 2267): getOutputForAttr() output 2
W/AudioPolicyIntefaceImpl( 2267): Skipped to add effects on session 182
I/AudioPolicyManager( 2267): startOutput() output 2, stream 3, session 182
I/AudioPolicyManager( 2267): changeRefCount() stream 3, count 1
I/AudioPolicyManager( 2267): setOutputDevice() setting same device 0002 or null device for output 2
V/SDL (29083): SDL audio: got stereo 16-bit 44.1kHz, 4096 frames buffer
I/python (29083): [WARNING] [AudioSDL2 ] Unable to load sounds/applause.wav: Mix_LoadWAV_RW with NULL src
D/AudioMixer( 2267): setResampler format 1, data ch 2, src 44100, dst 48000, dnmix 0, reformat 0
D/SoundAliveResampler( 2267): [SoundAliveResampler] Init+++
I/AudioHardwareTinyALSA( 2267): AudioStreamOutALSA::write setDevice
D/AudioHardwareTinyALSA( 2267): OutALSA::setDevice: mode = 0, newDevice=0x2, currentDevice=0x2 ,force= 0
D/AudioHardwareTinyALSA( 2267): OutALSA::setDevice: mDevice 0x2, newDevice = 0x2
D/AudioHardwareTinyALSA( 2267): setOutputVolume
D/TinyUCM ( 2267): setModifier Normal, en=1
I/AudioHardwareTinyALSA( 2267): OutALSA::setDevice: mHandle NULL mode[0], Device[00000002] nDevice:3
I/AudioHardwareTinyALSA( 2267): Open:+ mDefaults->direction=0 device=3
D/AudioHardwareTinyALSA( 2267): Channel: 2, Samplerate: 48000, Format: 0, Period Size: 960, Period Count: 4
输出:
| seconditemid | percent | |--------------|---------| | A2 | 0.55 | | A1 | 0.45 |