- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在这里看到过类似的问题,但要么我没有得到答案,要么它们不适用......这是我需要的,并且认为它非常简单:
我有一组项目,每个项目都有一组子项目。每个项目的子项目数会发生变化。例如:
Item 1
SubItem 1-1
SubItem 1-2
SubItem 1-3
Item 2
SubItem 2-1
Item 3
SubItem 3-1
SubItem 3-2
Item 1 Subitem 1-1 = True, Subitem 1-2 = True, Subitem 1-3 = True
Item 1 Subitem 1-1 = True, Subitem 1-2 = True, Subitem 1-3 = False
Item 1 Subitem 1-1 = True, Subitem 1-2 = False, Subitem 1-3 = True
Item 1 Subitem 1-1 = True, Subitem 1-2 = False, Subitem 1-3 = False
Item 1 Subitem 1-1 = False, Subitem 1-2 = True, Subitem 1-3 = True
... (the rest of Item 1 possible combinations)
Item 2 Subitem 2-1 = True
Item 2 Subitem 2-1 = False
Item 3 Subitem 3-1 = True, Subitem 3-2 = True
Item 3 Subitem 3-1 = True, Subitem 3-2 = False
Item 3 Subitem 3-1 = False, Subitem 3-2 = True
Item 3 Subitem 3-1 = False, Subitem 3-2 = False
-- Schema creation and data filling
DECLARE @Item TABLE (ItemId int, Name varchar(50))
DECLARE @Item_SubItem TABLE (ItemId int, SubitemId int)
DECLARE @SubItem TABLE (SubitemId int, Name varchar(50))
INSERT INTO @Item values (1, 'Item 1')
INSERT INTO @Item values (2, 'Item 2')
INSERT INTO @Item values (3, 'Item 3')
INSERT INTO @SubItem values (1, 'SubItem 1-1')
INSERT INTO @SubItem values (2, 'SubItem 1-2')
INSERT INTO @SubItem values (3, 'SubItem 1-3')
INSERT INTO @SubItem values (4, 'SubItem 2-1')
INSERT INTO @SubItem values (5, 'SubItem 3-1')
INSERT INTO @SubItem values (6, 'SubItem 3-2')
INSERT INTO @Item_SubItem values (1, 1)
INSERT INTO @Item_SubItem values (1, 2)
INSERT INTO @Item_SubItem values (1, 3)
INSERT INTO @Item_SubItem values (2, 4)
INSERT INTO @Item_SubItem values (3, 5)
INSERT INTO @Item_SubItem values (3, 6)
select I.Name, SI.Name
from @Item I
inner join @Item_SubItem ISI on ISI.ItemId = I.ItemId
INNER JOIN @SubItem SI on SI.SubitemId = ISI.SubitemId
order by I.Name, SI.Name
-- Actual query
SELECT ItemName = M.name, (SELECT iC.name + '=' + CASE AuxCode WHEN 1 THEN 'True' WHEN 0 THEN 'False' END + ' '
FROM Item_subitem AS iCGM
INNER JOIN Subitem AS iC ON iC.SubitemId = iCGM.SubitemId
CROSS JOIN (SELECT AuxCode = 1 UNION SELECT AuxCode = 0) Aux
WHERE iCGM.ItemId = M.ItemId
ORDER BY iC.name
FOR XML PATH(''))
FROM Item M
最佳答案
以下是如何达到 4 个级别(您的示例数据只需要 3 个级别,但我想确保这超出了这个级别)。你应该能够按照模式上升到 7、10,你有什么。哦,不要指望这会很快。
;WITH z AS
(
SELECT i,inm,si,snm,truth,c FROM
(
SELECT i = i.ItemId, inm = i.Name, si = isi.SubItemId, snm = s.Name,
c = COUNT(isi.SubItemId) OVER (PARTITION BY i.ItemId)
FROM @Item_SubItem AS isi
INNER JOIN @Item AS i ON isi.ItemId = i.ItemId
INNER JOIN @SubItem AS s ON isi.SubItemId = s.SubItemId
) AS y
CROSS JOIN (VALUES('true'),('false')) AS t(truth)
)
SELECT Item = z1.inm,
SubItems = COALESCE( z1.snm + ' = ' + z1.truth,'')
+ COALESCE(', ' + z2.snm + ' = ' + z2.truth,'')
+ COALESCE(', ' + z3.snm + ' = ' + z3.truth,'')
+ COALESCE(', ' + z4.snm + ' = ' + z4.truth,'')
FROM z AS z1
LEFT OUTER JOIN z AS z2
ON z1.i = z2.i AND z1.si < z2.si
LEFT OUTER JOIN z AS z3
ON z2.i = z3.i AND z2.si < z3.si
LEFT OUTER JOIN z AS z4
ON z3.i = z4.i AND z3.si < z4.si
WHERE (z1.c = 1)
OR (z1.c = 2 AND z2.i IS NOT NULL)
OR (z1.c = 3 AND z3.i IS NOT NULL)
OR (z1.c = 4 AND z4.i IS NOT NULL);
Item SubItems
------ ---------------------------------------------------------------
Item 1 SubItem 1-1 = true, SubItem 1-2 = true, SubItem 1-3 = true
Item 1 SubItem 1-1 = true, SubItem 1-2 = true, SubItem 1-3 = false
Item 1 SubItem 1-1 = true, SubItem 1-2 = false, SubItem 1-3 = true
Item 1 SubItem 1-1 = true, SubItem 1-2 = false, SubItem 1-3 = false
Item 1 SubItem 1-1 = false, SubItem 1-2 = true, SubItem 1-3 = true
Item 1 SubItem 1-1 = false, SubItem 1-2 = true, SubItem 1-3 = false
Item 1 SubItem 1-1 = false, SubItem 1-2 = false, SubItem 1-3 = true
Item 1 SubItem 1-1 = false, SubItem 1-2 = false, SubItem 1-3 = false
Item 2 SubItem 2-1 = true
Item 2 SubItem 2-1 = false
Item 3 SubItem 3-1 = true, SubItem 3-2 = true
Item 3 SubItem 3-1 = true, SubItem 3-2 = false
Item 3 SubItem 3-1 = false, SubItem 3-2 = true
Item 3 SubItem 3-1 = false, SubItem 3-2 = false
ItemId
排序,但假值排序更高):
SELECT c.i, c.inm, c.si, c.snm, c.c, t.truth
INTO #x
FROM
(
SELECT
i = i.ItemId, inm = i.Name, si = isi.SubItemId, snm = s.Name,
c = COUNT(isi.SubItemId) OVER (PARTITION BY i.ItemId)
FROM @Item_SubItem AS isi
INNER JOIN @Item AS i ON isi.ItemId = i.ItemId
INNER JOIN @SubItem AS s ON isi.SubItemId = s.SubItemId
) AS c
CROSS JOIN (VALUES('true'),('false')) AS t(truth);
CREATE UNIQUE CLUSTERED INDEX x ON #x(i,si,truth);
SELECT
Item = z1.inm,
SubItems = COALESCE( z1.snm + ' = ' + z1.truth,'')
+ COALESCE(', ' + z2.snm + ' = ' + z2.truth,'')
+ COALESCE(', ' + z3.snm + ' = ' + z3.truth,'')
+ COALESCE(', ' + z4.snm + ' = ' + z4.truth,'')
FROM #x AS z1
LEFT OUTER JOIN #x AS z2 ON z1.i = z2.i AND z1.si < z2.si
LEFT OUTER JOIN #x AS z3 ON z2.i = z3.i AND z2.si < z3.si
LEFT OUTER JOIN #x AS z4 ON z3.i = z4.i AND z3.si < z4.si
WHERE (z1.c = 1)
OR (z1.c = 2 AND z2.i IS NOT NULL)
OR (z1.c = 3 AND z3.i IS NOT NULL)
OR (z1.c = 4 AND z4.i IS NOT NULL);
DROP TABLE #x;
DECLARE @Item TABLE (ItemId int PRIMARY KEY, Name varchar(50));
DECLARE @Item_SubItem TABLE (ItemId int, SubitemId int,
PRIMARY KEY (ItemId,SubItemId));
DECLARE @SubItem TABLE (SubitemId int PRIMARY KEY, Name varchar(50));
关于sql - 使用 SQL Server 创建所有可能的组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18658631/
降本手段一招鲜,增效方法吃遍天; 01 互联网行业里; 降本策略千奇百怪,手段却出奇一致;增效方法五花八门,手段更是花里胡哨; 对于企业来说;
有什么方法可以使用 angularjs 中的部分进行代码分组吗? 原因 --- 我的 Controller 包含太多代码。该 Controller 包含了多个方法和大量功能的代码,降低了代码的可读性。
不幸的是,我的数据库的数据模型必须改变,所以我正在寻找最轻松的方式来迁移我的数据。 此时情况如何: create table cargo{ id serial primary key, per
在 QTextEdit 对象中,假设我想知道字符在鼠标光标下的位置。 我会写... void MyQTextEditObject::mousePressEvent(QMouseEvent* mouse
是否可以在 C++ 中返回一个 return 语句或做一些具有类似功能的事情? 例如,如果代码中有几个函数将指针作为输入,并且每个函数都检查指针是否为 nullptr,这将很方便。如果它是一个 nul
我的 PC 上有一个控制台应用程序,它是 signalR 服务器。 我有一个 html 页面,它是互联网上的 signalR 客户端。但我尝试连接服务器,但我有一个错误的请求 400 错误。如果服务器
我想将应用程序作为后台进程运行。当点击应用程序图标时,它不会显示任何 View ,只会启动后台进程。 最佳答案 对于 iOS 这是不可能的,但是对于 android,react native 有 he
我知道有(昂贵的)框架可以让你在 VS C# 中编写 android 应用程序并将其编译为 android apk。 我也知道,可以在 VS 中编写 Java 应用程序(link)。 是否有可能,甚至
我在做: can :manage, :all if user.role == 'admin' can :approve, Anuncio do |anuncio| anuncio.try(:apr
我是一名优秀的程序员,十分优秀!