我有以下数据。
ID UserID Grade 1 1 A 2 1 A 3 1 a 4 1 a 5 1 b 6 1 C 7 1 c 8 1 b 9 2 b 10 2 C 11 1 b 12 2 A
我想要下面的结果。
RunningNumber UserID Result Count 1 1 a 4 2 1 b 1 3 1 c 2 4 1 b 2 5 2 b 1 6 2 c 1 7 2 a 1
结果将计算列级别(不区分大小写),并且必须按用户ID分组。
答案 0 :(得分:3)
使用如下
SELECT UserID, Grade, COUNT(ID) AS Count
FROM Table1
GROUP BY UserID,Grade;
更新
SELECT ROW_NUMBER()
OVER (ORDER BY Grade) AS RunningNumber ,
UserID, Grade, COUNT(ID) AS Count
FROM Table1
GROUP BY UserID,Grade;
答案 1 :(得分:1)
只需使用Group by&数
SELECT ID ,UserID,Grade
COUNT(ID) FROM Tabel
GROUP BY Grade,UserID;
答案 2 :(得分:1)
您可以DENSE_RANK
使用RunningNumber
,每个ID使用COUNT(*)OVER
,ROW_NUMBER
仅使用第一行:
WITH CTE AS
(
SELECT
RunningNumber = DENSE_RANK() OVER (ORDER BY UserID, Grade),
UserID, Result = Grade,
[Count] = COUNT(*) OVER (PARTITION BY UserID, Grade),
RowNum = ROW_NUMBER() OVER (PARTITION BY UserID, Grade ORDER BY ID)
FROM dbo.TableName
)
SELECT RunningNumber, UserID, Result, [Count]
FROM CTE
WHERE RowNum = 1
答案 3 :(得分:1)
$query_not="SELECT count(status) AS sum FROM `user_leave_details`WHERE (status='2' Or status='3') AND user_id_no='$user_id_no'";
$result=mysqli_query($bd,$query_not);
while($arr=mysqli_fetch_array($result))
{
$sum=$arr['sum'];
}
connect.php
<?php
$mysql_hostname = "localhost";
$mysql_user = "root";
$mysql_password = "";
$mysql_database = "";
$bd=mysqli_connect($mysql_hostname,$mysql_user,$mysql_password,$mysql_database);
?>