SQLServer建立交叉表查询

SQLServer建立交叉表查询,第1张

/* 使用方法 直接执行 传入参数(series_guid 查询条件)返回一个数据集 如 查询该系列Cylindrical 下所有产品 dbo P_GetSeriesProductDetail Cylindrical 查询系列Cylindrical 下含有BK 的产品 dbo P_GetSeriesProductDetail Cylindrical product_name like %BK % */ CREATE PROCEDURE P_GetSeriesProductDetail(@series_guid varchar( ) @condition varchar( )) AS DECLARE @ParamNo nvarchar( ) DECLARE @SQL nvarchar( ) Set @SQL= DECLARE P_cursor CURSOR local fast_forward FOR SELECT param_no FROM V_product_params where series_guid=@series_guid OPEN P_cursor

FETCH next FROM P_cursor INTO @ParamNo WHILE (@@fetch_status = ) BEGIN Set @SQL = @SQL + MAX(CASE param_no WHEN + @ParamNo + THEN param_value ELSE END) AS F + @ParamNo + char( ) FETCH next FROM P_cursor INTO @ParamNo END

CLOSE P_cursor DEALLOCATE P_cursor Set @SQL= SELECT type_guid series_guid product_no product_name + @SQL + FROM V_product_params WHERE series_guid= + @series_guid + if (LTrim(@condition)<>) Set @SQL= @SQL + and + @condition Set @SQL= @SQL + GROUP BY type_guid series_guid product_no product_name

lishixinzhi/Article/program/MySQL/201311/29566

SET @EE=''

SELECT @EE:=CONCAT(@EE,'SUM(IF(C2=/'',C2,'/'',',C3,0)) AS ',C2,',') FROM (SELECT DISTINCT C2 FROM TX) A

SET @QQ=CONCAT('SELECT ifnull(c1,\'total\'),',LEFT(@EE,LENGTH(@EE)-1),' ,SUM(C3) AS TOTAL FROM TX GROUP BY C1 WITH ROLLUP')

PREPARE stmt2 FROM @QQ

EXECUTE stmt2

这个好复杂,你学的是mysq吗?


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/6161305.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-16
下一篇 2023-03-16

发表评论

登录后才能评论

评论列表(0条)

保存