开发者

How to insert into sql server Many to many relationship tables

I imported data into csv table via .net app, pls help, how can i aInsert into all the tables so that i can query;

  1. student avg score to a question or test
  2. student answers to a question or answer frequency to a question etc

My tables are in this form;

Csv             Ans         Quest      Test    Ans_Quest_Test
---------      ------      ------     ------    -------------
LName           id          id         Id       Ansid
FName           Ans         Quest      name     Questid
stdname        StdScore    ActScore             Testid
Quest                               开发者_JAVA百科            Stdname
Ans
ActualScore
StdScore


INSERT INTO Ans_Quest_Test
SELECT FLOOR(RAND(CHECKSUM(NEWID())) * 10000) --random number
, FLOOR(RAND(CHECKSUM(NEWID())) * 10000)--random number
, FLOOR(RAND(CHECKSUM(NEWID())) * 10000)--random number
, Stdname FROM Csv

INSERT INTO Ans
SELECT AnsId, Ans, StdScore FROM Ans_Quest_Test
    JOIN Csv ON Ans_Quest_Test.Stdname=Csv.stdname

INSERT INTO Quest
SELECT QuestId, Quest, ActualScore FROM Ans_Quest_Test
    JOIN Csv ON Ans_Quest_Test.Stdname=Csv.stdname

I do not see any name for the Test in the Csv table, so I can not write an insert for table Test :)

student avg score to a question

SELECT Stdname, AVG(Quest.ActScore)
FROM Ans_Quest_Test
    JOIN Quest ON Ans_Quest_Test.Questid=Quest.id
GROUP BY Stdname

student answers to a question

SELECT Ans_Quest_Test.Stdname, Quest.Quest, Ans.Ans
FROM Ans_Quest_Test
    JOIN Quest ON Ans_Quest_Test.Questid=Quest.id
    JOIN Ans ON Ans_Quest_Test.Ansid=Ans.id

answer frequency to a question

SELECT  Quest.Quest, COUNT(Ans.id)
FROM Ans_Quest_Test
    JOIN Quest ON Ans_Quest_Test.Questid=Quest.id
    JOIN Ans ON Ans_Quest_Test.Ansid=Ans.id
GROUP BY Quest.Quest
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜