精华答题收藏之逻辑难题(字符拆分)

IF OBJECT_ID('dbo.统计表') IS NOT NULL DROP TABLE 统计表
CREATE TABLE 统计表 (方便面 INT,茶叶 INT,面包 INT,口香糖 INT,牛奶 INT,玻璃杯 INT,可乐 INT,冰糖 INT,牙膏 INT,牙刷 INT,故事书 INT,味素 INT,食盐 INT,榨菜 INT,食醋 INT)
INSERT INTO 统计表
SELECT 1,0,0,0,0,1,0,0,0,1,0,0,1,0,0 UNION ALL
SELECT 1,1,0,0,0,1,0,0,0,0,1,1,0,1,0 UNION ALL
SELECT 0,1,1,1,0,1,1,1,0,1,0,0,1,1,0 UNION ALL
SELECT 1,1,0,0,1,1,0,1,1,0,0,1,1,0,0 UNION ALL
SELECT 0,1,1,0,0,1,0,1,0,0,0,0,1,1,1 UNION ALL
SELECT 1,1,0,0,1,1,0,1,1,0,0,0,0,1,0 UNION ALL
SELECT 1,1,1,0,0,1,0,1,0,0,0,1,1,0,0 UNION ALL
SELECT 0,0,1,0,0,1,0,1,0,0,0,0,1,1,1 UNION ALL
SELECT 1,1,1,0,0,1,0,1,1,0,1,1,0,1,0 UNION ALL
SELECT 1,1,1,1,0,1,1,1,0,1,1,0,1,0,1 UNION ALL
SELECT 0,0,1,1,1,1,0,1,0,1,0,1,1,1,0 UNION ALL
SELECT 1,1,1,1,1,1,0,1,0,1,0,0,1,1,1
Select * from 统计表


统计表的商品名为n列,不只上面列出的那么多,如何实现n维数组输出,即有多少个字段就输出从一维直到n维的数组!
具体目标:现有一参考值0.25,要输出满足条件的1到n维数组,其中一维数组统计单个商品名出现1的合计数,即每一个商品名出现一个1就算一次。

二维数组统计为从第一列方便面开始起与相邻的茶叶都为1,1的关系(依次组合查询每相邻两个满足条件的关系)。

详细:即从第一行找到最后一行,如发现循环组合满足都为1,1这样关系有5个,即sum记为5,依次再找从第一列方便面开始起与相隔一个位置的面包都为1,1的关系,如满足这样的关系从第一行到最后有3个即sum值记为3,依次类推再从第一列方便面开始与相隔两位置的口香糖组合都为1,1的关系,这样一直找到方便面与最后一列食醋(即第n列)的关系都为1,1的位置,再从开始的第二列循环与之后的每列依次组合查找1,1的关系,直到循环到从榨菜开始与最后一列食醋(第n列)为1,1的关系,二维数组循环完毕。

输出:如每以组合列满足条件的sum值/所有行数>=0.25则输出 该组合商品名称,例如(茶叶,冰糖)组合关系为(1,1)的sum数/总行数>=0.25 则需将结果(茶叶,冰糖)输出到 结果表 的values 字段,三维数组与n维数组算法与二维数组相似

结果表输出样式:(中间以逗号分开)

结果表
values
{方便面,口香糖,牛奶,食醋}
{(茶叶,冰糖)(口香糖,玻璃杯)(榨菜,食醋)}
{(方便面,茶叶,牛奶)(茶叶,口香糖,冰糖)(玻璃杯,食盐,食醋) }
{(n维数组1)(n维数组2)(n维数组3)(n维数组3)(...)(n维数组n)}


请问上面的数据查询如何实现!请贴出代码!!

IF OBJECT_ID('dbo.统计表') IS NOT NULL DROP TABLE 统计表
CREATE TABLE 统计表 (方便面 INT,茶叶 INT,面包 INT,口香糖 INT,牛奶 INT,玻璃杯 INT,可乐 INT,冰糖 INT,牙膏 INT,牙刷 INT,故事书 INT,味素 INT,食盐 INT,榨菜 INT,食醋 INT)
INSERT INTO 统计表
SELECT 1,0,0,0,0,1,0,0,0,1,0,0,1,0,0 UNION ALL
SELECT 1,1,0,0,0,1,0,0,0,0,1,1,0,1,0 UNION ALL
SELECT 0,1,1,1,0,1,1,1,0,1,0,0,1,1,0 UNION ALL
SELECT 1,1,0,0,1,1,0,1,1,0,0,1,1,0,0 UNION ALL
SELECT 0,1,1,0,0,1,0,1,0,0,0,0,1,1,1 UNION ALL
SELECT 1,1,0,0,1,1,0,1,1,0,0,0,0,1,0 UNION ALL
SELECT 1,1,1,0,0,1,0,1,0,0,0,1,1,0,0 UNION ALL
SELECT 0,0,1,0,0,1,0,1,0,0,0,0,1,1,1 UNION ALL
SELECT 1,1,1,0,0,1,0,1,1,0,1,1,0,1,0 UNION ALL
SELECT 1,1,1,1,0,1,1,1,0,1,1,0,1,0,1 UNION ALL
SELECT 0,0,1,1,1,1,0,1,0,1,0,1,1,1,0 UNION ALL
SELECT 1,1,1,1,1,1,0,1,0,1,0,0,1,1,1
Go
Create PROC xb_GetArray(@维数 INT, @系数 float ,@结果 VARCHAR(1000) out )
AS
SET NOCOUNT ON
IF OBJECT_ID('dbo.Tmp') IS NOT NULL DROP TABLE dbo.Tmp
CREATE TABLE Tmp(id INT,NAME VARCHAR(20))
INSERT Tmp SELECT colid,NAME FROM syscolumns s WHERE id=OBJECT_ID('统计表')
IF OBJECT_ID('tempdb.dbo.#') IS NOT NULL DROP TABLE #
CREATE TABLE #(ws INT,Result VARCHAR(1000))

DECLARE @num INT
DECLARE @f FLOAT
DECLARE @sql nVARCHAR(1000)
DECLARE @id VARCHAR(100),@w VARCHAR(100),@wh VARCHAR(100),@c VARCHAR(100)
DECLARE @Step int ,@i INT ,@j INT ,@s INT,@l INT

SET @f=@系数
SELECT @num=count(1) FROM tmp

SET @i=1
SET @Step=@维数
set @结果=''
WHILE @i<=@num
BEGIN
SET @wh=REPLICATE('1,',@Step)
SET @j=@i
WHILE @j<=@num-(@Step-1)
BEGIN
SELECT @l=1,@id='',@w='',@c=NULL
WHILE @l<=@Step-1
BEGIN
SET @id=@id+LTRIM(@j+@l)+','
SET @l=@l+1
END
SELECT @id=ltrim(@i)+','+@id
SELECT @w=@w+'+ltrim('+NAME+')+'+''',''',@c=ISNULL(@c+',','')+Name FROM tmp WHERE CHARINDEX(','+LTRIM(id)+',',','+@id)>0 ORDER BY id
SET @w=STUFF(@w,1,1,'')
SET @sql=N'if ((select count(*)*1.0 from 统计表 where '+@w+'='''+@wh+''')/'+LTRIM(@num)+'>' +LTRIM(@f) +') insert # select '+ltrim(@Step)+',''('+ @c+'),'''
EXEC(@sql)
SET @j=@j+1
END
SET @i=@i+1
END
SET @sql=NULL
SELECT @sql=ISNULL(@sql+'','')+result FROM (SELECT DISTINCT result FROM # ) aa
SET @结果='{'+@sql+'}'


GO


DECLARE @sql VARCHAR(1000)
exec xb_GetArray 2,0.3,@sql OUT
SELECT @sql 二维

--result
/*

一维
---------------------------------------------------------------------------
{(冰糖),(玻璃杯),(茶叶),(方便面),(面包),(食盐),(味素),(牙刷),(榨菜),}

(所影响的行数为 1 行)
*/

DECLARE @sql VARCHAR(1000)
exec xb_GetArray 2,0.3,@sql OUT
SELECT @sql 二维
/*二维
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
{(冰糖,食盐),(冰糖,榨菜),(玻璃杯,冰糖),(玻璃杯,食盐),(玻璃杯,味素),(玻璃杯,牙刷),(玻璃杯,榨菜),(茶叶,冰糖),(茶叶,玻璃杯),(茶叶,面包),(茶叶,食盐),(茶叶,榨菜),(方便面,冰糖),(方便面,玻璃杯),(方便面,茶叶),(方便面,食盐),(面包,冰糖),(面包,玻璃杯),(面包,食盐),(面包,榨菜),(食盐,榨菜),(牙刷,食盐),}

(所影响的行数为 1 行)
*/

你可能感兴趣的:(sql,C++,c,C#,J#)