Ceci est un exemple d'information de ma database afin que l'image complète puisse être montrée quant à ce que je dois accomplir
Create Table #Information ( salesID int, storelocation varchar(100), salespersonName varchar(100) ) Insert Into #Information Values (1, 'New York', 'Michael'), (2, 'New York', 'Michael'), (3, 'New York', 'Michael'), (4, 'New York', 'Michael'), (5, 'Texas', 'Richard'), (6, 'Texas', 'Richard'), (7, 'Texas', 'Richard'), (8, 'Texas', 'Richard'), (9, 'Texas', 'Richard'), (10, 'Texas', 'Richard'), (11, 'Washington', 'Sam'), (12, 'Washington', 'Sam'), (13, 'Washington', 'Sam'), (14, 'Washington', 'Sam'), (15, 'Washington', 'Sam') SELECT storelocation, COUNT(salesID/storelocation) FROM #Information
Je veux get le nombre total de salesID puis split par le salesID pour ce storelocation. Donc, la division que je veux arriver serait
New York - 15/4 = .266 Texas - 15/6 = .4 Washington - 15/5 = .333
La façon dont j'ai fait cela est comme ça – mais cela ne returnne pas des résultats précis.
declare @TotalCount as int select @TotalCount = convert(decimal(18,4), count(salesID)) from #information Select convert(decimal(18,4), Count(salesID))/@TotalCount From #information
Rendre la requête Total Count sous-requête et le split par storelocation
groupes de storelocation
SELECT storelocation, (SELECT CONVERT(DECIMAL(18, 4), Count(1)) FROM #Information) / Count(1) FROM #Information GROUP BY storelocation
CREATE TABLE #Information ( salesID INT, storelocation VARCHAR(100), salespersonName VARCHAR(100) ) INSERT INTO #Information VALUES (1, 'New York', 'Michael'), (2, 'New York', 'Michael'), (3, 'New York', 'Michael'), (4, 'New York', 'Michael'), (5, 'Texas', 'Richard'), (6, 'Texas', 'Richard'), (7, 'Texas', 'Richard'), (8, 'Texas', 'Richard'), (9, 'Texas', 'Richard'), (10, 'Texas', 'Richard'), (11, 'Washington', 'Sam'), (12, 'Washington', 'Sam'), (13, 'Washington', 'Sam'), (14, 'Washington', 'Sam'), (15, 'Washington', 'Sam') DECLARE @TotalCount AS INT SELECT @TotalCount = CONVERT(DECIMAL(18, 4), COUNT(salesID)) FROM #information SELECT storelocation, @TotalCount / CONVERT(DECIMAL(18, 4), COUNT(storelocation)) AS Division FROM #Information GROUP BY storelocation
Essaye ça:
DECLARE @TotalCount as int; SELECT @TotalCount = COUNT(salesID) FROM #information; SELECT storeLocation, (@TotalCount / COUNT(salesID)) AS division FROM #information GROUP BY storeLocation;