查询多次求和值
Query is summing values multiple times
您好,我的以下查询是根据 table 中的@cropseasons 对多个值求和。由于我有 4 个作物季节,它似乎将值乘以 4,因为我的作物季节为 1、2、3 或 4。我想要的只是 1 个作物季节的值。有人可以帮忙吗?我在两个 table 都有作物季节。
With Summary as (
Select B_NAME as Branch, LOC as Location
,SUM(payment) as Gallons
,SUM(case when printed = 1 THEN Fee ELSE NULL END) as FeeCollected
,SUM(case when printed = 0 THEN Fee ELSE NULL END) as FeeNotCollected
,SUM(case when printed = 1 THEN Payment ELSE NULL END) as GallonsIssued
,SUM(case when printed = 0 THEN Payment ELSE NULL END) as GallonsNotIssued
From SicbWeeklyDeliveriesFuelArchive F Inner Join FarmerGroups G ON G.BSI_CODE = F.BSI_CODE
Where F.CROP_SEASON = @cropseason
Group By B_NAME, LOC
)
SELECT Branch
,Location
,Gallons
,GallonsIssued
,GallonsNotIssued
,FeeCollected
,FeeNotCollected
,((GallonsIssued/Gallons) * 100) as pct_GallonsCollected
FROM Summary
Order by Location, Branch
SicbWeeklyDeliveriesFuelArchive
+-------+----------+-------------+-----+---------+------+-------------+---------+
| ID | BSI_CODE | B_NAME | LOC | PAYMENT | FEE | CROP_SEASON | PRINTED |
+-------+----------+-------------+-----+---------+------+-------------+---------+
| 18735 | 2176 | SAN NARCISO | CZ | 85 | 8.5 | 4 | 0 |
| 18738 | 2176 | SAN NARCISO | CZ | 65 | 6.5 | 4 | 0 |
| 18739 | 10494 | SAN NARCISO | CZ | 85 | 8.5 | 3 | 0 |
+-------+----------+-------------+-----+---------+------+-------------+---------+
农民团体
+-------+----------+-------------+-------------+
| ID | BSI_CODE | CROP_SEASON | BRANCH |
+-------+----------+-------------+-------------+
| 10473 | 2176 | 4 | SAN NARCISO |
| 11478 | 2176 | 3 | SAN NARCISO |
| 12787 | 10494 | 4 | SAN ROMAN |
+-------+----------+-------------+-------------+
您的加入条件似乎不完整。 table 共享 BSI_CODE
和 CROP_SEASON
,所以我猜你想要:
FROM sicbweeklydeliveriesfuelarchive f
JOIN farmergroups g ON g.bsi_code = f.bsi_code AND g.crop_season = f.crop_season
WHERE f.crop_season = @cropseason
但这只是猜测。只有您知道 table 是如何真正相关的,它们的行代表什么,哪些列使行唯一以及您实际追求的结果是什么。你为什么要加入 farmergroups
?看起来您并没有真正在查询中使用 table。
您好,我的以下查询是根据 table 中的@cropseasons 对多个值求和。由于我有 4 个作物季节,它似乎将值乘以 4,因为我的作物季节为 1、2、3 或 4。我想要的只是 1 个作物季节的值。有人可以帮忙吗?我在两个 table 都有作物季节。
With Summary as (
Select B_NAME as Branch, LOC as Location
,SUM(payment) as Gallons
,SUM(case when printed = 1 THEN Fee ELSE NULL END) as FeeCollected
,SUM(case when printed = 0 THEN Fee ELSE NULL END) as FeeNotCollected
,SUM(case when printed = 1 THEN Payment ELSE NULL END) as GallonsIssued
,SUM(case when printed = 0 THEN Payment ELSE NULL END) as GallonsNotIssued
From SicbWeeklyDeliveriesFuelArchive F Inner Join FarmerGroups G ON G.BSI_CODE = F.BSI_CODE
Where F.CROP_SEASON = @cropseason
Group By B_NAME, LOC
)
SELECT Branch
,Location
,Gallons
,GallonsIssued
,GallonsNotIssued
,FeeCollected
,FeeNotCollected
,((GallonsIssued/Gallons) * 100) as pct_GallonsCollected
FROM Summary
Order by Location, Branch
SicbWeeklyDeliveriesFuelArchive
+-------+----------+-------------+-----+---------+------+-------------+---------+ | ID | BSI_CODE | B_NAME | LOC | PAYMENT | FEE | CROP_SEASON | PRINTED | +-------+----------+-------------+-----+---------+------+-------------+---------+ | 18735 | 2176 | SAN NARCISO | CZ | 85 | 8.5 | 4 | 0 | | 18738 | 2176 | SAN NARCISO | CZ | 65 | 6.5 | 4 | 0 | | 18739 | 10494 | SAN NARCISO | CZ | 85 | 8.5 | 3 | 0 | +-------+----------+-------------+-----+---------+------+-------------+---------+
农民团体
+-------+----------+-------------+-------------+ | ID | BSI_CODE | CROP_SEASON | BRANCH | +-------+----------+-------------+-------------+ | 10473 | 2176 | 4 | SAN NARCISO | | 11478 | 2176 | 3 | SAN NARCISO | | 12787 | 10494 | 4 | SAN ROMAN | +-------+----------+-------------+-------------+
您的加入条件似乎不完整。 table 共享 BSI_CODE
和 CROP_SEASON
,所以我猜你想要:
FROM sicbweeklydeliveriesfuelarchive f
JOIN farmergroups g ON g.bsi_code = f.bsi_code AND g.crop_season = f.crop_season
WHERE f.crop_season = @cropseason
但这只是猜测。只有您知道 table 是如何真正相关的,它们的行代表什么,哪些列使行唯一以及您实际追求的结果是什么。你为什么要加入 farmergroups
?看起来您并没有真正在查询中使用 table。