我可以让它在两个单独的查询中工作,但不能在一个查询中工作。有人可以帮帮我吗?我需要这样的输出:
+-----------------------------------------------------------------------------------------------------+
| PARENT_AK | PARENT_RK |PARENT_RESOURCE_NAME| C_RESOURCE_NAME | C_AK | C_RK |
+-----------------------------------------------------------------------------------------------------+
| CONTAINER | LOB |SYSTEMROLE | DEV |CONTAINER |LOB Options |
+-----------------------------------------------------------------------------------------------------+
| CONTAINER | LOB |SYSTEMROLE | PRODUCTION |CONTAINER |LOB Options |
+-----------------------------------------------------------------------------------------------------+
| CONTAINER | LOB |SYSTEMROLE | TEST |CONTAINER |LOB Options |
+-----------------------------------------------------------------------------------------------------+
| CONTAINER | LOB |SYSTEMROLE | UAT |CONTAINER |LOB Options |
+-----------------------------------------------------------------------------------------------------+
| CONTAINER | LOB |SERVER_FUNCTION | APPLICATION SERVER |CONTAINER |LOB Options |
+-----------------------------------------------------------------------------------------------------+
| CONTAINER | LOB |SERVER_FUNCTION | DATABASE SERVER |CONTAINER |LOB Options |
+-----------------------------------------------------------------------------------------------------+
| CONTAINER | LOB |SERVER_FUNCTION | WEB SERVER |CONTAINER |LOB Options |
+-----------------------------------------------------------------------------------------------------+
查询1:
select
'CONTAINER' as PARENT_AK,
'LOB' as PARENT_RK,
'SYSTEMROLE' as PARENT_RESOURCE_NAME,
SYSTEMROLE as C_RESOURCE_NAME,
'CONTAINER' as C_AK,
'LOB Options' as C_RK
FROM CMDB
GROUP by C_RESOURCE_NAME;
查询2:
select
'CONTAINER' as PARENT_AK,
'LOB' as PARENT_RK,
'SERVER_FUNCTION' as PARENT_RESOURCE_NAME,
SERVER_FUNCTION as C_RESOURCE_NAME,
'CONTAINER' as C_AK,
'LOB Options' as C_RK
FROM CMDB
GROUP by C_RESOURCE_NAME;
表(CMDB):
+-------------------------------------------------+
| NAME | SYSTEMROLE | SERVER_FUNCTION |
+-------------------------------------------------+
| Server1 | Test |APPLICATION SERVER |
+-------------------------------------------------+
| Server2 | PRODUCTION |APPLICATION SERVER |
+-------------------------------------------------+
| Server3 | UAT |DATABASE SERVER |
+-------------------------------------------------+
| Server4 | DEV |WEB SERVER |
+-------------------------------------------------+
| Server5 | DEV |WEB SERVER |
+-------------------------------------------------+
SQLFiddle:http://www.sqlfiddle.com/#!2/08e6a/12
答案 0 :(得分:1)
你正在尝试UNPIVOT你的表,但据我所知MySQL没有内置的UNPIVOT功能。因此,您将不得不诉诸于您正在做的事情。请注意,虽然您可以使用union来向服务器发出单个请求,而不是两个(SQL Fiddle demo):
select
'CONTAINER' as PARENT_AK,
'LOB' as PARENT_RK,
'SYSTEMROLE' as PARENT_RESOURCE_NAME,
SYSTEMROLE as C_RESOURCE_NAME,
'CONTAINER' as C_AK,
'LOB Options' as C_RK
FROM CMDB
GROUP by C_RESOURCE_NAME;
UNION ALL
select
'CONTAINER' as PARENT_AK,
'LOB' as PARENT_RK,
'SERVER_FUNCTION' as PARENT_RESOURCE_NAME,
SERVER_FUNCTION as C_RESOURCE_NAME,
'CONTAINER' as C_AK,
'LOB Options' as C_RK
FROM CMDB
GROUP by C_RESOURCE_NAME;
如果您正在使用SQL Server 2008,正如您在评论中所述,您可以使用UNPIVOT来获得结果:
select
'CONTAINER' as PARENT_AK,
'LOB' as PARENT_RK,
PARENT_RESOURCE_NAME, C_RESOURCE_NAME,
'CONTAINER' as C_AK,
'LOB Options' as C_RK
FROM
(
SELECT CI_NAME, SYSTEMROLE, SERVER_FUNCTION
FROM CMDB
) x
UNPIVOT
(
C_RESOURCE_NAME FOR PARENT_RESOURCE_NAME IN (SYSTEMROLE, SERVER_FUNCTION)
) p
答案 1 :(得分:0)
您可以在MySQL中使用UNION语句:http://dev.mysql.com/doc/refman/5.0/en/union.html
SELECT
'CONTAINER' as PARENT_AK,
'LOB' as PARENT_RK,
'SYSTEMROLE' as PARENT_RESOURCE_NAME,
SYSTEMROLE as C_RESOURCE_NAME,
'CONTAINER' as C_AK,
'LOB Options' as C_RK
FROM CMDB
GROUP by C_RESOURCE_NAME
UNION SELECT
'CONTAINER' as PARENT_AK,
'LOB' as PARENT_RK,
'SERVER_FUNCTION' as PARENT_RESOURCE_NAME,
SERVER_FUNCTION as C_RESOURCE_NAME,
'CONTAINER' as C_AK,
'LOB Options' as C_RK
FROM CMDB
GROUP by C_RESOURCE_NAME;
答案 2 :(得分:0)
您可以使用UNION组合两个或多个SELECT语句的结果集。
注意:UNION中的每个SELECT语句必须具有相同的列数。列还必须具有类似的数据类型。此外,每个SELECT语句中的列必须具有相同的顺序
这样会对你有所帮助。 http://www.w3schools.com/sql/sql_union.asp
SELECT
'CONTAINER' AS PARENT_AK,
'LOB' AS PARENT_RK,
'SYSTEMROLE' AS PARENT_RESOURCE_NAME,
SYSTEMROLE AS C_RESOURCE_NAME,
'CONTAINER' AS C_AK,
'LOB Options' AS C_RK
FROM CMDB
GROUP BY C_RESOURCE_NAME
UNION
SELECT
'CONTAINER' AS PARENT_AK,
'LOB' AS PARENT_RK,
'SERVER_FUNCTION' AS PARENT_RESOURCE_NAME,
SERVER_FUNCTION AS C_RESOURCE_NAME,
'CONTAINER' AS C_AK,
'LOB Options' AS C_RK
FROM CMDB
GROUP BY C_RESOURCE_NAME