Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在SQL数据库中存储测量数据_Sql_Sql Server_Database_Database Design - Fatal编程技术网

在SQL数据库中存储测量数据

在SQL数据库中存储测量数据,sql,sql-server,database,database-design,Sql,Sql Server,Database,Database Design,我们有一个小型的表单托管应用程序,不同表单的答案数量不同。存储表单答案的当前表结构如下所示 ------------------------------------------------------------ |Survay_Id | question_ID | submission_id | answer | | 1 | 1 | 123 | test 1 | | 1 | 2

我们有一个小型的表单托管应用程序,不同表单的答案数量不同。存储表单答案的当前表结构如下所示

------------------------------------------------------------ |Survay_Id | question_ID | submission_id | answer | | 1 | 1 | 123 | test 1 | | 1 | 2 | 123 | test 2 | | 1 | 1 | 124 | tabc | | 1 | 2 | 124 | xyza | | 2 | 3 | 125 | xsfa | | 2 | 4 | 125 | xsgffa | | 2 | 5 | 125 | xsffa | | 2 | 3 | 126 | xsffa | | 2 | 4 | 126 | xssdgffa | | 2 | 5 | 126 | xsdffa | ----------------------------------------------------------- ------------------------------------------------------------ |Survay|u Id |问题| Id |提交| Id |回答| |1 | 1 | 123 |测试1 | |1 | 2 | 123 |测试2 | |1 | 1 | 124 | tabc | |1 | 2 | 124 | xyza | |2 | 3 | 125 | xsfa | |2 | 4 | 125 | xsgffa | |2 | 5 | 125 | xsffa | |2 | 3 | 126 | xsffa | |2 | 4 | 126 | xssdgffa | |2 | 5 | 126 | xsdffa | ----------------------------------------------------------- 调查1有两个问题,调查2有三个问题。唯一的提交由提交id标识。 问题

1) 如果需要,我可以稍微更改db结构。但它必须是SQL。因为MongoDB尚未被批准使用。有没有更好的结构

2) 查询以生成调查提交报告的最佳方式是什么。我需要像这样的东西

---------------------------------- |survery id | answer 1 | answer 2| | 1 | test 1 | test 2 | | 1 | tabc | xyza | ----------------------------------- --------------------------------------------- |survery id | answer 1 | answer 2| answer 3 | | 2 | xsfa | xsgffa | xsffa | | 2 | xsffa | xssdgffa| xsdffa | ----------------------------------- ---------------------------------- |survery id |答案1 |答案2 | |1 |测试1 |测试2| |1 | tabc | xyza| ----------------------------------- --------------------------------------------- |survery id |答案1 |答案2 |答案3| |2 | xsfa | xsgffa | xsffa| |2 | xsffa | xssdgffa | xsdffa| ----------------------------------- 在这里,我们可以将表转置到上面的视图中。有什么好办法吗


如果数据库是MSSQL服务器有帮助,则需要使用dynamic pivot

模式:

CREATE TABLE #TAB(Survay_Id INT, question_ID INT, submission_id INT, answer VARCHAR(50))
INSERT INTO #TAB
SELECT 1, 1, 123, 'test 1'
UNION ALL 
SELECT 1, 2, 123,'test 2'
UNION ALL 
SELECT 1, 1,124,'tabc'
UNION ALL 
SELECT 1, 2, 124,'xyza'
UNION ALL 
SELECT 2, 3, 125,'xsfa'
UNION ALL 
SELECT 2, 4, 125,'xsgffa'
UNION ALL 
SELECT 2, 5, 125,'xsffa'
UNION ALL 
SELECT 2, 3, 126,'xsffa'
UNION ALL 
SELECT 2, 4, 126,'xssdgffa'
UNION ALL 
SELECT 2, 5, 126,'xsdffa' 
现在使用动态轴,如下所示

DECLARE @SQL VARCHAR(MAX)='',@COLUMNS VARCHAR(MAX)='';

--preparing column list to use in pivot
SELECT @COLUMNS = @COLUMNS+ ANS_ID FROM(
SELECT DISTINCT '[answer '+ 
CAST(ROW_NUMBER() 
        OVER(PARTITION BY SUBMISSION_ID 
            ORDER BY (question_ID)) AS VARCHAR(5))+'],' AS ANS_ID
            FROM #TAB
)A


SELECT @COLUMNS = LEFT(@COLUMNS, LEN(@COLUMNS)-1)


--Dynamic Pivot Part
SELECT @SQL = 
'SELECT Survay_Id,'+@COLUMNS+' FROM (
SELECT Survay_Id,ANSWER, submission_id
, ''answer ''
+ CAST(ROW_NUMBER() 
        OVER(   PARTITION BY SUBMISSION_ID 
            ORDER BY (question_ID)) AS VARCHAR(5)) AS ANS_ID 
FROM #TAB
)A
PIVOT
(
    MAX(ANSWER) for ANS_ID  IN('+@COLUMNS+')
)PV'

--PRINT @SQL

EXEC (@SQL)
结果:

+-----------+----------+----------+----------+
| Survay_Id | answer 1 | answer 2 | answer 3 |
+-----------+----------+----------+----------+
|         1 | test 1   | test 2   | NULL     |
|         1 | tabc     | xyza     | NULL     |
|         2 | xsfa     | xsgffa   | xsffa    |
|         2 | xsffa    | xssdgffa | xsdffa   |
+-----------+----------+----------+----------+
第一输出

SELECT Survay_Id,[answer 1],[answer 2] 
FROM (SELECT Survay_Id,answer, submission_id, 'answer '
+CAST(ROW_NUMBER() OVER(PARTITION BY submission_id ORDER BY (question_ID)) AS VARCHAR(5)) as ans 
FROM TableName where Survay_Id = 1)test
PIVOT
(MAX(answer) for ans  IN([answer 1],[answer 2]))PIV
第二输出

SELECT Survay_Id,[answer 1],[answer 2],[answer 3] 
FROM (SELECT Survay_Id,answer, submission_id, 'answer '
+CAST(ROW_NUMBER() OVER(PARTITION BY submission_id ORDER BY (question_ID)) AS VARCHAR(5)) as ans 
FROM TableName where Survay_Id = 2)test
PIVOT
(MAX(answer) for ans  IN([answer 1],[answer 2],[answer 3]))PIV

我认为您需要使用数据透视查看此问题以了解相关信息: