我在SQL中有一个包含3列的表:
然后我有另一张有4列的表:
此表中的每一行都有2条基于CategoryName,GroupName和IncludedExcluded的唯一记录。例如,作为json,它看起来像这样:
ctx._requestHeader().session().get("AUTH");
有3个组(Products,Reps和BusinessCategories),因此每个Category在此表中只有6行数据。 我想生成一个SQL语句,将表拉入列,然后我可以将其解析为json对象,如下所示:
{
categoryName: 'Folders',
groupName: 'Products',
includeExcluded: 'E',
match: 'SomeProduct'
}, {
categoryName: 'Folders',
groupName: 'Products',
includeExcluded: 'I',
match: 'SomeOtherProduct'
}
我希望SQL语句加入2个表并将数据显示到这样的列中:
我真的希望我解释得那么好。有人可以帮我解决这个问题吗? PS,它是Oracle:)
答案 0 :(得分:0)
也许这就是你所追求的。查询确实将category_matches表分成一组六个数据集 - 三个组*两个包含/排除标志。
您问题中的列列表包括" ProductTitle(这是组名)"由于每个组都在每行数据中表示,因此没有意义。但也许我误解了
WITH
category_intervals (category_name, warn_interval, error_interval) AS
(SELECT 'Folders',40,60 FROM dual UNION ALL
SELECT 'catA',10,20 FROM dual UNION ALL
SELECT 'catB',50,100 FROM dual
)
,category_matches(category_name, group_name, include_exclude, match_name) AS
(SELECT 'Folders','Products','E','Product 1' FROM dual UNION ALL
SELECT 'Folders','Products','I','Product 2' FROM dual UNION ALL
SELECT 'Folders','Reps','E','Rep 1' FROM dual UNION ALL
SELECT 'Folders','Reps','I','Rep 2' FROM dual UNION ALL
SELECT 'Folders','Business Categories','E','Category 1' FROM dual UNION ALL
SELECT 'Folders','Business Categories','I','Category 2' FROM dual UNION ALL
--
SELECT 'catA','Products','E','Product 3' FROM dual UNION ALL
SELECT 'catA','Products','I','Product 4' FROM dual UNION ALL
SELECT 'catA','Reps','E','Rep 3' FROM dual UNION ALL
SELECT 'catA','Reps','I','Rep 4' FROM dual UNION ALL
SELECT 'catA','Business Categories','E','Category 3' FROM dual UNION ALL
SELECT 'catA','Business Categories','I','Category 4' FROM dual UNION ALL
--
SELECT 'catB','Products','E','Product 5' FROM dual UNION ALL
SELECT 'catB','Products','I','Product 6' FROM dual UNION ALL
SELECT 'catB','Reps','E','Rep 5' FROM dual UNION ALL
SELECT 'catB','Reps','I','Rep 6' FROM dual UNION ALL
SELECT 'catB','Business Categories','E','Category 5' FROM dual UNION ALL
SELECT 'catB','Business Categories','I','Category 6' FROM dual
)
SELECT
ci.category_name
,ci.warn_interval
,ci.error_interval
,ip.match_name product_include
,ep.match_name product_exclude
,ir.match_name rep_include
,er.match_name rep_exclude
,ibc.match_name bus_cat_include
,ibc.match_name bus_catt_exclude
FROM
category_intervals ci
,(SELECT *
FROM category_matches
WHERE group_name = 'Products'
AND include_exclude = 'I'
) ip
,(SELECT *
FROM category_matches
WHERE group_name = 'Products'
AND include_exclude = 'E'
) ep
,(SELECT *
FROM category_matches
WHERE group_name = 'Reps'
AND include_exclude = 'I'
) ir
,(SELECT *
FROM category_matches
WHERE group_name = 'Reps'
AND include_exclude = 'E'
) er
,(SELECT *
FROM category_matches
WHERE group_name = 'Business Categories'
AND include_exclude = 'I'
) ibc
,(SELECT *
FROM category_matches
WHERE group_name = 'Business Categories'
AND include_exclude = 'E'
) ebc
WHERE 1=1
AND ci.category_name = ip.category_name
AND ci.category_name = ep.category_name
AND ci.category_name = ir.category_name
AND ci.category_name = er.category_name
AND ci.category_name = ibc.category_name
AND ci.category_name = ebc.category_name
ORDER BY
ci.category_name
;