1)表1表示table1,结构为:
moduleID | moduleName
10 | XYZ
20 | PQR
30 | ABC
2)表2说table2结构为:
moduleID | Level | Value
10 | 1 | 20
10 | 2 | 30
30 | 3 | 40
10 | 3 | 50
20 | 2 | 30
moduleID
是table1中的主键,列level
的值可以是值1到3
现在需要显示如下数据:
moduleID | moduleName | Level1 | Level2 | Level3
10 | XYZ | 20 | 30 | 50
20 | PQR | NULL | 30 | NULL
30 | ABC | NULL | NULL | 50
简而言之,table2中列Level
的值显示为Level1,Level2和Level3,并且对应于每个级别的值将填充在相应的moduleID
行中。
对此有何帮助?在SQL中初学者。与Views有关吗?
答案 0 :(得分:2)
您可以使用条件聚合:
select t1.moduleID, t1.moduleName,
MAX(CASE WHEN Level = 1 THEN Value END) Level1,
MAX(CASE WHEN Level = 2 THEN Value END) Level2,
MAX(CASE WHEN Level = 3 THEN Value END) Level3
from table1 as t1
left join table2 as t2 on t1.moduleID = t2.moduleID
group by t1.moduleID, t1.moduleName
答案 1 :(得分:0)
为表2中的每个级别执行select t1.*, t2_l1.value as Level1, t2_l2.value as Level2, t2_l3.value as Level3
from table1 t1
left join table2 t2_l1 on t1.moduleID = t2_l1.moduleID and t2_l1.Level = 'Level1'
left join table2 t2_l2 on t1.moduleID = t2_l2.moduleID and t2_l2.Level = 'Level2'
left join table2 t2_l3 on t1.moduleID = t2_l3.moduleID and t2_l3.Level = 'Level3'
。
let accountBoard = UIStoryboard(name: "Account", bundle: nil)
let accountNav = accountBoard.instantiateInitialViewController() as! CommonNavigationController
accountNav.tabBarItem = UITabBarItem(title: "Account", image: UIImage(named: "icon - account")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal), selectedImage: UIImage(named: "icon - account - white")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal))
accountNav.tabBarItem.setTitleTextAttributes([NSFontAttributeName: STYLES.avenirNextDemiBold11!,NSForegroundColorAttributeName: UIColor.whiteColor()], forState: .Normal)
accountNav.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.redColor()], forState:UIControlState.Selected)
如果moduleID具有多个不同的级别值,则将返回所有这些值。 (如果你想要总和,请看看Giorgos Betsos的回答。)
答案 2 :(得分:0)
请参阅此过程,它将适用于您的预期答案。
CREATE TABLE Table1
(moduleName VARCHAR(50),moduleID INT)
GO
--Populate Sample records
INSERT INTO Table1 VALUES('.NET',10)
INSERT INTO Table1 VALUES('Java',20)
INSERT INTO Table1 VALUES('SQL',30)
CREATE TABLE Table2
(moduleID INT,[Level] INT,Value INT)
GO
--Populate Sample records
INSERT INTO Table2 VALUES(10,1,20)
INSERT INTO Table2 VALUES(10,2,30)
INSERT INTO Table2 VALUES(30,3,40)
INSERT INTO Table2 VALUES(10,3,50)
INSERT INTO Table2 VALUES(20,2,30)
INSERT INTO Table2 VALUES(20,4,60)
GO
CREATE VIEW [dbo].[vw_tabledata]
AS
SELECT t1.[moduleID],[moduleName]
,[Level]
,[Value]
FROM [db_Sample].[dbo].[Table2] t2 inner join [db_Sample].[dbo].[Table1] t1 on t1.[moduleID] = t2.[moduleID]
GO
DECLARE @DynamicPivotQuery AS NVARCHAR(MAX)
DECLARE @ColumnName AS NVARCHAR(MAX)
--Get distinct values of the PIVOT Column
SELECT @ColumnName= ISNULL(@ColumnName + ',','')
+ QUOTENAME([Level])
FROM (SELECT DISTINCT [Level] FROM Table2) AS [Level]
--Prepare the PIVOT query using the dynamic
SET @DynamicPivotQuery =
N'SELECT moduleID,moduleName, ' + @ColumnName + '
FROM [vw_tabledata]
PIVOT(MAX(Value)
FOR [Level] IN (' + @ColumnName + ')) AS PVTTable'
--Execute the Dynamic Pivot Query
EXEC sp_executesql @DynamicPivotQuery