/    Sign up×
Community /Pin to ProfileBookmark

Double Count in JOIN

I have a few tables I’m joining together.

What I’m doing is counting rows from my master FROM table.

But doing a bunch of JOINS, WHERE’s GROUP to filter and find data I want to count.

Sometimes an applicable row is referenced twice in my query. How can I make it so it’s only counted **once**.

Almost like a distinct in my CASE or COUNT.

“`
CASE WHEN w.ColumnField = 1 THEN SUM(d.thisField) ELSE 0 END as MyCount,
COUNT(d.thisId) as MySecondCount,

“`

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJan 07.2021 — Not 100% sure this is what you need, but you can include a "distinct" within the count() itself, e.g.:
<i>
</i>COUNT(DISTINCT d.thisId) as MySecondCount,
Copy linkTweet thisAlerts:
@kiwisauthorJan 07.2021 — yes, I stumbled across this one.

So I've done this

COUNT(DISTINCT d.ColumnName, CASE WHEN d.ColumnName = 1 THEN 1 END) as MyCount,

This works perfectly.

What I need to do now though, is get the sum of a field.

SUM(DISTINCT d.ColumnName, CASE WHEN d.ColumnName = 1 THEN r.FieldName END) as MyCount,

r.FieldName will be an integer, between 1-10. So with 4 rows I'd expect a total below 50.
Copy linkTweet thisAlerts:
@NogDogJan 07.2021 — You may need to do a sub-query to do the distinct-ing (to coin a word). This is off the top of my head, so no guarantees:
<i>
</i>select sum(x.value)
from (
select distinct
ColumnName,
case when ColumnName = 1 then FieldName end as value
from RelevantTable -- where, etc....
) as x
-- joins, etc.
-- where, etc.
Copy linkTweet thisAlerts:
@kiwisauthorJan 07.2021 — @NogDog#1626585

Yeah, that's not working.

I'm essentially doing 2 counts. One for all my data. The second for when isFullLesson is set to one.

The following is working.

``<i>
</i>COUNT(DISTINCT d.thisId) as myData,
COUNT(DISTINCT e.thisId, CASE WHEN g.isFullLesson= 1 THEN 1 END) as ttlFullData,
SUM(d.IntField) as myCount,<i>
</i>
`</CODE>

But when I try and do the SUM CASE calse it fails. I can't add a sub query into my FROM command as It' not applicable to all.

I've tried, the following in my SELECT section prior to FROM

<C>
(SELECT DISTINCT e.thisId, SUM(d.intField) WHERE g.isFullLesson= 1) as myLimitedCount`

But I get a syntax error,
×

Success!

Help @kiwis spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...