简体   繁体   English

查询多次对值求和

[英]Query is summing values multiple times

Hi my query below is summing multiple values based on @cropseasons in my table.您好,我下面的查询是根据我表中的@cropseasons 对多个值求和。 Since i have 4 crop seasons it seems to be multiplying the values by 4 since i have crop season as 1, 2, 3 or 4. All i want is values for 1 crop season.由于我有 4 个作物季节,它似乎将值乘以 4,因为我的作物季节为 1、2、3 或 4。我想要的只是 1 个作物季节的值。 Can anyone assist?有人可以帮忙吗? I have crop season in both tables.我在两张桌子上都有作物季节。

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 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 |
+-------+----------+-------------+-----+---------+------+-------------+---------+

FarmerGroups农民团体

+-------+----------+-------------+-------------+
|  ID   | BSI_CODE | CROP_SEASON |   BRANCH    |
+-------+----------+-------------+-------------+
| 10473 |     2176 |           4 | SAN NARCISO |
| 11478 |     2176 |           3 | SAN NARCISO |
| 12787 |    10494 |           4 | SAN ROMAN   |
+-------+----------+-------------+-------------+

It seems your join criteria is incomplete.您的加入标准似乎不完整。 The tables share BSI_CODE and CROP_SEASON , so I guess you want:这些表共享BSI_CODECROP_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

But that's just guessing.但这只是猜测。 Only you know how the tables are really related, what their rows represent, what columns make a row unique and what result you are actually after.只有您知道这些表是如何真正相关的,它们的行代表什么,哪些列使一行独一无二,以及您实际追求的结果是什么。 Why do you join farmergroups at all?你为什么要加入farmergroups It looks like you are not really using the table in your query.看起来您并没有真正在查询中使用该表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM