1

私の会社のユーザーの部門を表示するために、SQL Server 2012 でクエリを実行しています。

SELECT mdl_user_info_data.data
FROM 
    dbo.mdl_user, 
    dbo.mdl_course, 
    dbo.mdl_user_enrolments, 
    dbo.mdl_enrol, 
    dbo.mdl_course_categories,
    dbo.mdl_user_info_data
WHERE 
    mdl_course.id = mdl_enrol.courseid AND 
    mdl_enrol.id = mdl_user_enrolments.enrolid AND 
    mdl_user_enrolments.userid = mdl_user.id AND 
    mdl_course.category = mdl_course_categories.id AND 
    mdl_course.id = 3 and
    mdl_user_info_data.fieldid = 3 and
    mdl_user.id = mdl_user_info_data.userid

そして、私のクエリの結果は次のとおりです。

|data|
|CIS|
|CIS|
|TOP|

しかし、私が欲しいのは次のようなものです:

|DATA| TOTAL |
|CIS | 2 |
|TOP | 1 |

誰かが私を助けることができますか?とりあえずありがとう。

私の mdl_user_info_data プロパティ:

[id] [bigint] IDENTITY(1,1) NOT NULL,
[userid] [bigint] NOT NULL,
[fieldid] [bigint] NOT NULL,
[data] [ntext] NOT NULL,
[dataformat] [smallint] NOT NULL,

mdl_user_info_data からのいくつかの行:
id userid fieldid data dataformat
3 3 3 CIS 0

4

2 に答える 2

2

これを試してみてください -

SELECT id.data, COUNT(1)
FROM dbo.mdl_user u
JOIN dbo.mdl_user_enrolments ue ON ue.userid = u.id
JOIN dbo.mdl_enrol e ON e.id = ue.enrolid
JOIN dbo.mdl_course c ON c.id = e.courseid
--JOIN dbo.mdl_course_categories cc ON c.category = cc.id
JOIN dbo.mdl_user_info_data id ON u.id = id.userid
WHERE c.id = 3
     AND id.fieldid = 3
GROUP BY id.data

アップデート:

SELECT t.data, Total = COUNT(1) 
FROM (
     SELECT data = CAST(id.data AS NVARCHAR(MAX))
     FROM dbo.mdl_user u
     JOIN dbo.mdl_user_enrolments ue ON ue.userid = u.id
     JOIN dbo.mdl_enrol e ON e.id = ue.enrolid
     JOIN dbo.mdl_course c ON c.id = e.courseid
     --JOIN dbo.mdl_course_categories cc ON c.category = cc.id
     JOIN dbo.mdl_user_info_data id ON u.id = id.userid
     WHERE c.id = 3
          AND id.fieldid = 3
) t
GROUP BY t.data
于 2013-07-30T07:07:06.777 に答える