我有一个数据库,当我们将每个计算机制造和模型的名称存入我们的系统作为票证时,我存储名称。我想要汇总唯一值并显示一个表格,显示计算机制造商名称的显示位置。
即。
HP 30%
Dell 10%
Toshiba 40%
Sony 20%
我的查询看起来像是显示每个故障单中的每个事件。我不确定如何转换到上面的表格:
SELECT
ComputerMake.ComputerMakeName
FROM
Ticket
INNER JOIN Asset ON Ticket.AssetID = Asset.AssetID
INNER JOIN ComputerMake ON Asset.ComputerMakeID = ComputerMake.ComputerMakeID
我确信它总结了每个ComputerMakeName,但这超出了我的基本SQL经验!
先谢谢你。
答案 0 :(得分:1)
首先,您需要计算每台计算机预订的次数,然后计算百分比:
SELECT
ComputerMake.ComputerMakeName
, count(*) * 100 / (select count(*) from FROM
Ticket
INNER JOIN Asset ON Ticket.AssetID = Asset.AssetID
INNER JOIN ComputerMake ON Asset.ComputerMakeID = ComputerMake.ComputerMakeID) as count_computers
FROM
Ticket
INNER JOIN Asset ON Ticket.AssetID = Asset.AssetID
INNER JOIN ComputerMake ON Asset.ComputerMakeID = ComputerMake.ComputerMakeID
group by ComputerMake.ComputerMakeName
答案 1 :(得分:1)
考虑使用CTE来简化查询并避免冗余
WITH computer_CTE(computerName) AS
(
SELECT ComputerMake.ComputerMakeName
FROM Ticket
INNER JOIN Asset ON Ticket.AssetID = Asset.AssetID
INNER JOIN ComputerMake ON Asset.ComputerMakeID = ComputerMake.ComputerMakeID
)
Select computerName, (Count(*) * 100 / (Select Count(*) From computer_CTE)) as Percent
From computer_CTE
Group By computerName