/    Sign up×
Community /Pin to ProfileBookmark

SQL Count Issue

Okay, I have these two tables.

In table A, aID is unique index ID and cID is a customer ID. In Table B, gID is an index which relates to ID1 or ID2 from Table A. and AB is a 1 or 2 indicator.

>

Table A:
> aID | cID | DateTime | ID1 | ID2

>
>

Table B:
> aID | ItemCnt | gID | AB

I’m trying to conduct a count, from Table A. Showing the COUNT of Table A entries and SUM of ItemCnt.

Then grouping them via year and ordering them.

Here’s what I have but I’m getting weird numbers from my count.

“`
SELECT distinct YEAR(a.DateTime ), SUM(b.ItemCnt) as TTL, count(a.aID)
FROM `TableA` a
JOIN `TableB` b ON a.aID = b.aId
WHERE

CASE
WHEN a.ID1 = 9 THEN b.AB= 1
WHEN a.ID2 = 9 THEN b.AB= 2
END
GROUP BY
YEAR(a.DateTime)
HAVING SUM(b.ItemCnt) > 0
ORDER BY
SUM(b.ItemCnt) DESC, a.ItemCnt ASC
limit 10
“`

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@NogDogOct 20.2021 — > @kiwis80#1638352 count(a.aID)

You may want to do count(distinct a.aID) if multiple rows in b could relate to one row of a, but you only want to know how many distinct a rows are included.
Copy linkTweet thisAlerts:
@kiwisauthorOct 20.2021 — @NogDog#1638353

I wasn't aware you could put it in there like that. It seems to work perfectly.
×

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.16,
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,
)...