sql中group by with rollup怎么用?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql中group by with rollup怎么用?相关的知识,希望对你有一定的参考价值。

参考技术A CREATETABLE#test(\\x0d\\x0aNameVARCHAR(10),\\x0d\\x0a[procedure]CHAR(1),\\x0d\\x0amodelVARCHAR(5),\\x0d\\x0aquantityINT\\x0d\\x0a);\\x0d\\x0a\\x0d\\x0aINSERTINTO#test\\x0d\\x0aSELECT\'A\',\'1\',\'φ50\',500UNIONALL\\x0d\\x0aSELECT\'A\',\'1\',\'φ50\',600UNIONALL\\x0d\\x0aSELECT\'A\',\'1\',\'φ100\',500UNIONALL\\x0d\\x0aSELECT\'A\',\'2\',\'φ50\',700UNIONALL\\x0d\\x0aSELECT\'A\',\'2\',\'φ100\',200UNIONALL\\x0d\\x0aSELECT\'B\',\'1\',\'φ50\',1000;\\x0d\\x0a\\x0d\\x0aSELECT\\x0d\\x0aCASE\\x0d\\x0aWHENGROUPING(Name)=1THEN\'总计\'\\x0d\\x0aWHENGROUPING(Name)=0ANDGROUPING([procedure])=1THENName+\'合计\'\\x0d\\x0aWHENGROUPING(Name)=0ANDGROUPING([procedure])=0ANDGROUPING([model])=1THENName+\'的\'+[procedure]+\'小计\'\\x0d\\x0aELSEName\\x0d\\x0aENDASName,\\x0d\\x0aCASE\\x0d\\x0aWHENGROUPING([model])=1THEN\'\'\\x0d\\x0aELSE[procedure]\\x0d\\x0aENDAS[procedure],\\x0d\\x0aISNULL(model,\'\')ASmodel,\\x0d\\x0aSUM(quantity)ASquantity\\x0d\\x0aFROM\\x0d\\x0a#test\\x0d\\x0agroupby\\x0d\\x0aName,\\x0d\\x0a[procedure],\\x0d\\x0amodelwithrollup;\\x0d\\x0a\\x0d\\x0aNameproceduremodelquantity\\x0d\\x0a------------------------------------------\\x0d\\x0aA1φ100500\\x0d\\x0aA1φ501100\\x0d\\x0aA的1小计1600\\x0d\\x0aA2φ100200\\x0d\\x0aA2φ50700\\x0d\\x0aA的2小计900\\x0d\\x0aA合计2500\\x0d\\x0aB1φ501000\\x0d\\x0aB的1小计1000\\x0d\\x0aB合计1000\\x0d\\x0a总计3500\\x0d\\x0a\\x0d\\x0a(11行受影响)\\x0d\\x0a\\x0d\\x0aSQLServer2008Express下测试通过.

以上是关于sql中group by with rollup怎么用?的主要内容,如果未能解决你的问题,请参考以下文章

MySQL GROUP BY 分组语句:语法及案例剖析使用 WITH ROLLUP

sql GROUP BY,GROUPING SETS,ROLLUP,CUBE,GROUPING_ID

sql语句中的group by要怎么用!!

扩展group by语句

Group By Rollup

高级分组group by group by cube group by rollup 使用说明