也许我只是还不够清醒...我知道我过去做过类似的事情,当我在这里查看其他答案时,我认为我正在做相同的事情,但没有得到预期的结果。
我有这个查询:
SELECT
tPM.mast_rel as Mast_Rel
, row_Number() over(Partition by tPM.Mast_rel Order by tPM.Mast_rel) as CategoryCount
, S.[RC_TRANS] as [Category]
, SUM(P.[VAL]) as [Value]
FROM #caselist AS tPM
INNER JOIN [TIBURON].[PARSProperty] AS P ON tPM.[MAST_REL] = P.[MAST_REL]
--S.RC_KEY equals combination of P.CAT-P.ART when P.CAT ='Y' otherwise just P.CAT = RC_KEY
LEFT JOIN [TIBURON].[SSCTAB] AS S ON (CASE
WHEN P.[CAT] = 'Y' THEN P.[CAT] + '-' + P.[ART]
ELSE P.[CAT]
END) = S.[RC_KEY] AND S.[RC_TYPE] = 'CP'
WHERE P.[P_INVL] != 'EVD' and S.[RC_TRANS] is not null
GROUP BY tPM.mast_rel, S.[RC_TRANS]
我想对它们进行旋转,以便获得一个包含三列类别的Mast_Rel
select Mast_Rel,[1], [2], [3]
from
(
SELECT
tPM.mast_rel as Mast_Rel
, row_Number() over(Partition by tPM.Mast_rel Order by tPM.Mast_rel) as CategoryCount
, S.[RC_TRANS] as [Category]
, SUM(P.[VAL]) as [Value]
FROM #caselist AS tPM
INNER JOIN [TIBURON].[PARSProperty] AS P ON tPM.[MAST_REL] = P.[MAST_REL]
--S.RC_KEY equals combination of P.CAT-P.ART when P.CAT ='Y' otherwise just P.CAT = RC_KEY
LEFT JOIN [TIBURON].[SSCTAB] AS S ON (CASE
WHEN P.[CAT] = 'Y' THEN P.[CAT] + '-' + P.[ART]
ELSE P.[CAT]
END) = S.[RC_KEY] AND S.[RC_TYPE] = 'CP'
WHERE P.[P_INVL] != 'EVD' and S.[RC_TRANS] is not null
GROUP BY tPM.mast_rel, S.[RC_TRANS]
)
src
pivot
(
max(Category) for CategoryCount in ([1], [2], [3])
) piv
order by 1;
但我没有得到一行,而是将每一行放在自己的行中:
此外,我还需要在数据透视表上为“值”列提供一个“总计”。所以最终,我希望显示一条记录:
有人可以帮助我调整查询以获得所需的结果吗? 谢谢!
编辑: 这是一个将创建数据和当前结果的脚本:
declare @results table (Mast_Rel varchar(100), CategoryCount varchar(10), Category varchar(100) , [Value] varchar(100))
insert into @results (Mast_rel, CategoryCount, Category, [Value])
values
('1602030055590P2404','1','Money','80.00'),
('1602051033480P3481','1','Miscellaneous/other (None of the above)','1000.00'),
('1602051033480P3481','2','Personal accessories (incl serial jewelry)','5000.00'),
('1602051033480P3481','3','Radio, TV, and sound entertainment devices',''),
('1602070005106P2804','1','Miscellaneous/other (None of the above)',''),
('1602080020374P3352','1','Money','128.09'),
('1602080020374P3352','2','Radio, TV, and sound entertainment devices',''),
('1602132349110P5208','1','Money','160.00'),
('1602132349110P5208','2','Radio, TV, and sound entertainment devices',''),
('1602171004296P3848','1','Consumable Goods','21.73'),
('1602201425504P2876','1','Radio, TV, and sound entertainment devices',''),
('16022115223610P3282','1','Consumable Goods','60.00'),
('16022115223610P3282','2','Money','300.00'),
('16022115223610P3282','3','Narcotic Equipment/Paraphernalia','10.00'),
('1602250140284P2804','1','Money','165.00'),
('1602250140284P2804','2','Radio, TV, and sound entertainment devices',''),
('16022916203812P2702','1','Guns/Firearms',''),
('16022916203812P2702','2','Radio, TV, and sound entertainment devices','')
select Mast_Rel,[1], [2], [3]
from
(
SELECT
* from @results
)
src
pivot
(
max(Category) for CategoryCount in ([1], [2], [3])
) piv
order by 1;
答案 0 :(得分:1)
我会进行条件聚合:
select mast_rel,
max(case when categorycount = 1 then category end),
max(case when categorycount = 2 then category end),
max(case when categorycount = 3 then category end),
sum(value)
from @results r
group by mast_rel;
答案 1 :(得分:1)
您应该能够更改原始查询以获得所需的结果。问题在于其中的GROUP BY
和SUM()
。由于您要按S.[RC_TRANS]
对SUM()
进行分组,因此您将返回多行,这将改变PIVOT
的最终结果。
您可以在内部子查询中删除GROUP BY
并改用SUM() OVER()
。将原始查询更改为以下内容将为您带来所需的结果:
select Mast_Rel,[1], [2], [3], [Value]
from
(
SELECT
tPM.mast_rel as Mast_Rel
, row_Number() over(Partition by tPM.Mast_rel Order by tPM.Mast_rel) as CategoryCount
, S.[RC_TRANS] as [Category]
-- change the following line
, SUM(P.[VAL]) OVER(PARTITION BY tPM.Mast_rel) as [Value]
FROM #caselist AS tPM
INNER JOIN [TIBURON].[PARSProperty] AS P ON tPM.[MAST_REL] = P.[MAST_REL]
--S.RC_KEY equals combination of P.CAT-P.ART when P.CAT ='Y' otherwise just P.CAT = RC_KEY
LEFT JOIN [TIBURON].[SSCTAB] AS S ON (CASE
WHEN P.[CAT] = 'Y' THEN P.[CAT] + '-' + P.[ART]
ELSE P.[CAT]
END) = S.[RC_KEY] AND S.[RC_TYPE] = 'CP'
WHERE P.[P_INVL] != 'EVD' and S.[RC_TRANS] is not null
)
src
pivot
(
max(Category) for CategoryCount in ([1], [2], [3])
) piv
order by 1;
通过将SUM(P.[VAL])
和GROUP BY
更改为SUM(P.[VAL]) OVER(PARTITION BY tPM.Mast_rel)
,您将获得每个tPM.Mast_rel
的总和,这就是您试图在最终结果集。 SUM(P.[VAL]) OVER
应该为Mast_Rel
中的每一行计算相同的值,这样将不会在最终结果集中创建多个行。
答案 2 :(得分:-1)
不确定,因为我无法直接对其进行测试..但是,当您将最大(Mast_Rel)添加到枢纽子句时会发生什么?
pivot (
max(Mast_Rel), max(Category) for CategoryCount in ([1], [2], [3])
) piv
答案 3 :(得分:-1)
您可以将其包装在另一个from bs4 import BeautifulSoup
html = "<li style="text-align: left;">
<span style="line-height: 19px;">
For Female/SC/ST/ PH: <strong>NIL</strong></span></li>,
<li style="text-align: left;">
<span style="line-height: 19px;">For Others:
<strong>Rs. 200/-</strong></span></li>,
<li style="text-align: left;">
Candidates can pay either by depositing the money in any Branch
of SBI by cash or by using net banking facility of SBI.</li>"
soup = BeautifulSoup(html,'html.parser')
text = soup.get_text()
print(text)
GROUP BY