本文介紹了使用“組頭"動(dòng)態(tài)分組的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!
問(wèn)題描述
誰(shuí)能幫我解決這個(gè)問(wèn)題.我有一張表,上面有 Products、ProductsSales.這是它們的一些屏幕截圖.
Could someone help me with this problem. I have a table with Products, ProductsSales. Here are some screenshots of them.
現(xiàn)在我想做一個(gè) TSQL 查詢(xún),它可以對(duì)它們進(jìn)行分組并添加諸如標(biāo)題描述"之類(lèi)的內(nèi)容?類(lèi)似這樣的
Now I would like to make a TSQL query which can group them and add something like a "header description"? Something like this
如果是相對(duì)動(dòng)態(tài)的查詢(xún)就完美了.
It would be perfect, if it is a relatively dynamically query.
有人可以在這里幫助我或給我一些提示嗎?
推薦答案
我傾向于對(duì)類(lèi)別進(jìn)行逆透視,然后聚合:
I would be inclined to unpivot the categories and then aggregate:
select which, name, sum(price)
from products p cross apply
(values ('productName', productName),
('groupName', groupName),
('parentGroup', parentGroup)
) v(which, name) left join
productsales ps
on ps.productId = p.productId
group by which, name;
這篇關(guān)于使用“組頭"動(dòng)態(tài)分組的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!