本文介紹了使用“組頭"動態分組的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
誰能幫我解決這個問題.我有一張表,上面有 Products、ProductsSales.這是它們的一些屏幕截圖.
Could someone help me with this problem. I have a table with Products, ProductsSales. Here are some screenshots of them.
現在我想做一個 TSQL 查詢,它可以對它們進行分組并添加諸如標題描述"之類的內容?類似這樣的
Now I would like to make a TSQL query which can group them and add something like a "header description"? Something like this
如果是相對動態的查詢就完美了.
It would be perfect, if it is a relatively dynamically query.
有人可以在這里幫助我或給我一些提示嗎?
推薦答案
我傾向于對類別進行逆透視,然后聚合:
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;
這篇關于使用“組頭"動態分組的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!