Count and result from Inner Join - sql

I have this query
SELECT COUNT(*) FROM (
SELECT
ART.LFDNR,
ARTSTLST.ARTIKELLFDNR,
Count(*) AS counts
FROM
ARTIKEL ART
INNER JOIN ARTSTLST ON ARTSTLST.OARTIKELLFDNR = ART.LFDNR
WHERE
ART.SUCHARTIKELNR = '22424'
GROUP BY
ART.LFDNR,
ARTSTLST.ARTIKELLFDNR )
which results in my case in
3
But I want to show also the result of the inner query
SELECT
ART.LFDNR,
ARTSTLST.ARTIKELLFDNR,
Count(*) AS counts
FROM
ARTIKEL ART
INNER JOIN ARTSTLST ON ARTSTLST.OARTIKELLFDNR = ART.LFDNR
WHERE
ART.SUCHARTIKELNR = '22424'
GROUP BY
ART.LFDNR,
ARTSTLST.ARTIKELLFDNR
so the result would look like this
12345 34567 3
Which is the result of the selct plus the Count() result.

This would be easy using Windowed Aggregate Function:
SELECT
ART.LFDNR,
ARTSTLST.ARTIKELLFDNR,
Count(*) AS counts,
SUM(Count(*)) OVER () -- Group sum
FROM
ARTIKEL ART
INNER JOIN ARTSTLST ON ARTSTLST.OARTIKELLFDNR = ART.LFDNR
WHERE
ART.SUCHARTIKELNR = '22424'
GROUP BY
ART.LFDNR,
ARTSTLST.ARTIKELLFDNR
But AFAIK only Firebird 3 (currently in beta) supports those functions, so you need a more complicated query utilizing a Common Table Expression:
WITH cte AS
(
SELECT
ART.LFDNR,
ARTSTLST.ARTIKELLFDNR,
Count(*) AS counts
FROM
ARTIKEL ART
INNER JOIN ARTSTLST ON ARTSTLST.OARTIKELLFDNR = ART.LFDNR
WHERE
ART.SUCHARTIKELNR = '22424'
GROUP BY
ART.LFDNR,
ARTSTLST.ARTIKELLFDNR
)
SELECT cte.*, (SELECT SUM(counts) FROM cte)
FROM cte

Related

Counting number of rows resulting from a nested SQL select statement with a JOIN

I have the following SQL statement.
SELECT COUNT(a.id), a.project_id, p.is_recommended
FROM assessments a
INNER JOIN projects p ON p.id=a.project_id
WHERE p.is_recommended = 1
GROUP BY project_id
HAVING COUNT(a.id) >= 3
I would like to count the resulting number of rows.
If not for the INNER JOIN, all I'd have to do is something like this...
SELECT * FROM
(SELECT COUNT(id), project_id
FROM assessments
GROUP BY project_id
HAVING COUNT(id) >= 3) assessments
But how do I calculate the resulting number of rows if my statement has a JOIN? What syntax should I be using?
SELECT COUNT(*)
FROM (SELECT COUNT(a.id), a.project_id, p.is_recommended
FROM assessments a
INNER JOIN projects p ON p.id=a.project_id
WHERE p.is_recommended = 1
GROUP BY project_id
HAVING COUNT(a.id) >= 3
) AS counts

Can I sum the count of two columns from two different tables?

I'm trying to add together the counts of two different tables and group them by the same variable
Here is what I have so far:
SELECT a.storenumber,
Count (howmanytotal) AS total_counts_store
FROM (
SELECT month_counts.howmany,
new_counts.howmany) AS howmanytotal
from (
SELECT a.storenumber,
count (b.riid_) AS howmany
FROM $b$ b
INNER JOIN $a$ a
ON b.riid_=a.riid_
GROUP BY a.storenumber) month_counts
FROM (
SELECT a.storenumber,
count (c.riid_) AS howmany
FROM $c$ c
INNER JOIN $a$ a
ON c.riid_=a.riid_
GROUP BY a.storenumber) new_counts
ON month_counts.storenumber = new_counts.storenumber) theend
where I'm at now:
SELECT howmanytotal AS total_counts_store
FROM (
SELECT Count (howmany) AS howmanytotal)
FROM (
SELECT month_counts.howmany,
new_counts.howmany)
FROM (
SELECT a.storenumber,
count (b.riid_) AS howmany
FROM $b$ b
inner join $a$ a
ON b.riid_=a.riid_
GROUP BY a.storenumber) month_counts
UNION
(
SELECT count (c.riid_) AS howmany
FROM $c$ c
inner join $a$ a
ON c.riid_=a.riid_
GROUP BY a.storenumber) new_counts
ON month_counts.storenumber = new_counts.storenumber) ORDER BY $a$.storenumber
Getting this error: Error: java.sql.SQLSyntaxErrorException: ORA-00923: FROM keyword not found where expected
Please correct SELECT statement:
Join the subqueries:
select
storenumber,
month_counts.howmany as month_count,
new_counts.howmany as new_count,
month_counts.howmany + new_counts.howmany as total_count
from (...) month_counts
join (...) new_counts using (storenumber)
order by storenumber;
If it is possible for a storenumber to be missing from one of the subquery results, then outer join and use COALESCE or NVL to deal with the nulls. Here is a query with a full outer join, which is not available in MySQL, but in Oracle and many other DBMS.
select
storenumber,
month_counts.howmany as month_count,
new_counts.howmany as new_count,
nvl(month_counts.howmany, 0) + nvl(new_counts.howmany, 0) as total_count
from (...) month_counts
full outer join (...) new_counts using (storenumber)
order by storenumber;
Ending up using sum and union to complete. Thank you for your help.
SELECT storenumber,
SUM(howmany) AS howmanytotal
FROM (SELECT a.storenumber,
Count (b.riid_) AS howmany
FROM $b$ b
inner join $a$ a
ON b.riid_ = a.riid_
GROUP BY a.storenumber
UNION
SELECT a.storenumber,
Count (c.riid_) AS howmany
FROM $c$ c
inner join $a$ a
ON c.riid_ = a.riid_
GROUP BY a.storenumber)
GROUP BY storenumber
ORDER BY storenumber
This gave me a list of store ids and how many active subscribers we have at each store (taken from two separate tables)

Extract state with maximum count from subquery

Select a.states,MAX(a.cnt) from
(Select ci_location.state as states,Count(CI_Location.State) Cnt from
Fact_Transactions ft
Inner join CI_Location on ft.ID_Location = CI_Location.ID_Location
Where id_model like 'smsg%'
Group by State)a
Group by a.states,a.Cnt
Output:-
State Count
Alabama 7
Arizona 15
Arkansas 4
California 100
Colorado 7
Required output:-
state output
California 100
This code isn't extracting STATE with maximum count value from my table...Is something wrong??
Try the following
SELECT TOP 1 -- or TOP 1 WITH TIES
ci_location.state as states,
Count(CI_Location.State) Cnt
FROM Fact_Transactions ft
INNER JOIN CI_Location ON ft.ID_Location = CI_Location.ID_Location
WHERE id_model like 'smsg%'
GROUP BY ci_location.state
ORDER BY Cnt DESC
just remove the a.cnt from group by
select a.states,MAX(a.cnt) from
(Select ci_location.state as states,Count(CI_Location.State) Cnt from
Fact_Transactions ft
Inner join CI_Location on ft.ID_Location = CI_Location.ID_Location
Where id_model like 'smsg%'
Group by State)a
Group by a.states
You should use row_number() function approach to get the maximum count state
SELECT * FROM
(
SELECT
l.state as states,
Count(l.State) as Cnt,
row_number() over (order by Count(l.State) desc) Sq
FROM Fact_Transactions ft
INNER JOJN CI_Location l on ft.ID_Location = l.ID_Location
WHERE l.id_model like 'smsg%'
GROUP BY l.State
) a
WHERE sq = 1
Note : For your current query you are further grouping the derived table result. Try, to remove GROUP BY clause & just use max() to get the highest count state only.

Alternative SQL ANSI for TOP WITH TIES

I have two tables:
product
id_product
description
price
id_category
category
id_category
description
I would like to know the categories that have more products. For example, the category food has 10 products and the eletronics too. They are the same.
Now I'm using SQL Server and I'm using TOP WITH TIES.
SELECT TOP 1 WITH TIES p.id_category, COUNT(*) as amount FROM product p
JOIN category c ON p.id_category = c.id_category
GROUP BY p.id_category
ORDER BY amount
Is there another way to solve this with good performance?
I tried also with DENSE_RANK where the position is = 1.
It also works.
SELECT * FROM (
SELECT p.id_category, COUNT(*) as amount, DENSE_RANK() OVER (ORDER BY COUNT(*) DESC) position FROM product p
JOIN category c ON p.id_category = c.id_category
GROUP BY p.id_category
) rnk
WHERE rnk.position = 1
But I want this solution in SQL ANSI.
I tried using MAX(COUNT(*)) but it doesn't work.
Is there a general solution? Is This solution better than using TOP WITH TIES?
Here is a third option for SQL Server:
WITH cte AS (
SELECT p.id_category, COUNT(*) AS cnt
FROM product p
INNER JOIN category c
ON p.id_category = c.id_category
GROUP BY p.id_category
)
SELECT *
FROM cte
WHERE cnt = (SELECT MAX(cnt) FROM cte);
If you also cannot rely on CTEs being available, you can easily enough just inline the CTE into the query. From a performance point of view, DENSE_RANK would probably outperform my answer.
With the CTE removed this becomes:
SELECT *
FROM
(
SELECT p.id_category, COUNT(*) AS cnt
FROM product p
INNER JOIN category c
ON p.id_category = c.id_category
GROUP BY p.id_category
)
WHERE cnt = (SELECT MAX(cnt) FROM (
SELECT p.id_category, COUNT(*) AS cnt
FROM product p
INNER JOIN category c
ON p.id_category = c.id_category
GROUP BY p.id_category
));
This query would even run on MySQL. As you can see, the query is ugly, which is one reason why things like CTE and analytic functions were introduced into the ANSI standard.

how to get single row in select count(*) in following query postgresql

select count(*) from ordrer
inner join ordrelinjer on ordrelinjer.ordrenr = ordrer.ordrenr
group by ordrelinjer.varetekst
This query return 4 rows, but I want to return 4 in count(*), how to do so?
You are getting 4 row because of group by. If you need distinct group count, you can try subquery.
select count(*)
from (
select count(*)
from ordrer
inner join ordrelinjer on ordrelinjer.ordrenr=ordrer.ordrenr
group by ordrelinjer.varetekst
) t
It seem that you're looking for the distinct number of values for ordrelinjer.varetekst, which would be:
select count(distinct ordrelinjer.varetekst)
from ordrer
join ordrelinjer on ordrelinjer.ordrenr = ordrer.ordrenr;
invoke without group by
select count(*) from ordrer inner join ordrelinjer on ordrelinjer.ordrenr=ordrer.ordrenr