refer to this question:
Get count of items and their values in one column
how I can get percent of record count in single query like this:
ItemId count Percent
------------------------------------
1 2 33.3
2 0 0
3 1 16.6
4 3 50.0
thanks
%s is a placeholder used in functions like sprintf. Check the manual for other possible placeholders. $sql = sprintf($sql, "Test"); This would replace %s with the string "Test".
You can use the SQL SELECT statement with the COUNT() function to select and display the count of rows in a table of a database.
COUNT(*) OVER()
gives you the total count.
Edit But actually you need SUM(COUNT(MyTbl.ItemID)) OVER()
as you are summing the values in that column.
SELECT Items.ItemID,
[count] = COUNT(MyTbl.ItemID),
[Percent] = 100.0 * COUNT(MyTbl.ItemID) / SUM(COUNT(MyTbl.ItemID)) OVER()
FROM (VALUES (1,'N1'),
(2,'N2'),
(3,'N4'),
(4,'N5')) Items(ItemID, ItemName)
LEFT JOIN (VALUES(1),
(1),
(3),
(4),
(4),
(4)) MyTbl(ItemID)
ON ( MyTbl.ItemID = Items.ItemID )
GROUP BY Items.ItemID
ORDER BY Items.ItemID
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With