2

Trying to write a query in SqlServer Dev 2017 with SSMS with Object Explorer.

I'm trying to extract all members ( PK) with totals ( Sum(ColumnValues)), below a certain amount. My code extracts all such values except for those that total 0, i.e., my query extracts only the positive values required but not those that are 0. It requires the join of columns C1, C2, from tables T1, T2, with common key K and totaling over column C.

Here's the code:

Select T1.C1, T2.C2, SUM(ColumnValues) FROM
T1 join T2 ON
T1.K=T2.K 
GROUP By T1.C1, T2.C2 HAVING
SUM(ColumnValues)<100

Code only outputs qualifying _ positive_ values, but not those that are 0. I tried adding a clause, in the ' Having' part as:

Having Sum(ColumnValues)<100 OR Sum(ColumnValues)=0

I even tried writing a stored pro endure that would add 1 to each column value and subtract it after the query is executed, but didn't work. I also tried unioning it with an identical query where I used Having Sum(ColumnValues)=0, which didn't work either. Any suggestions?

3
  • 2
    Maybe there are nulls in your values, maybe the join removes some combinations that would add to 0, maybe something else. Add some sample data so we can help Sep 24, 2022 at 23:10
  • 1
    Thanks for the input. Will do so ASAP.
    – MSIS
    Sep 25, 2022 at 4:01
  • Three anonymous downvotes. Gee, some of us are trying to learn , yet unable to read minds. Thanks for your unhelpful, unconstructive criticism.
    – MSIS
    Sep 25, 2022 at 9:32

0

Browse other questions tagged or ask your own question.