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
Related
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
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)
I have a problem
I have 2, same tables. My select should show this two columns and the last one "sup_cr" should be counted.
sup_id, sup_name, sup_cr.
This is my query without inner join. But i don't know how to use a inner join in this query.
SELECT sup.sup_id,
sup.sup_name_en AS name,
(SELECT COUNT (sup_cr.sup_id_from)
FROM t_sup_supplier AS sup_cr
WHERE sup_cr.sup_id_from = sup.sup_id ) AS cr_numbers
FROM t_sup_supplier AS sup
Sorry for my english.
You don't need a join or aggregation. You can use a window function:
SELECT sup.sup_id, sup.sup_name_en AS name,
COUNT(*) OVER (PARTITION BY sup_id) AS cr_numbers
FROM t_sup_supplier sup;
You could rephrase this a join using:
SELECT
sup.sup_id,
sup.sup_name_en AS name,
COALESCE(sup_cr.cnt, 0) AS cr_numbers
FROM t_sup_supplier
LEFT JOIN
(
SELECT sup_id_from, COUNT(*) AS cnt
FROM t_sup_supplier
GROUP BY sup_id_from
) sup_cr
ON sup_cr.sup_id_from = sup.sup_id;
I have a table with 50 columns named as public_report. I want count(distinct) of a specific column along with all the columns as I need to run this in Tableau
select count(distinct l.lead_key), l.*
from public_report l
group by l.lead_key
I am facing this error while i execute a query for
Error occurred while trying to execute a query: [SQLState 42803]
ERROR: column "l.enquiry_key" must appear in the GROUP BY clause or be
used in an aggregate function
II tried adding l.enquiry_key but it threw a other column name as well. have around 50 columns can anyone suggest.
Also tried this
select t.lk, t.c, lp.*
from (select lead_key lk, count(distinct lead_key) c
from public_report
group by lead_key) t
join public_report lp
on lp.lead_key = t.lk
but this is not giving me the correct count.
This table has 8700000 distinct lead_key values. But I am getting 14565498 as count d value
Please help
Perhaps a correlated subquery is what you want?
select p.*, (select count(*) from public_report p2
where p2.lead_key = p.lead_key)
from public_report p
Is this what you want?
select lp.*, t.count_distinct
from public_report lp cross join
(select count(distinct lead_key) as count_distinct
from public_report
) t;
Or, because most databases support count(distinct) as a window function:
select pr.*, count(distinct lead_key) over () as count_distinct
from public_report pr
If youwantknow the number of distinct lead_key You should not group by for the same row you want aggregated
select count(distinct l.lead_key)
from public_report l
if you want the count for each key then use group by ket and count(*)
select l.lead_key, count(*)
from public_report l
group by l.lead_key
if you want count(distinct l.lead_key) aside the others column you could use a cross join
select lp.*, t. my_count
from public_report lp
cross join (
select count(distinct l.lead_key) my_count
from public_report l
) t
if you want the count of each lead_key aside the the others columns you could use an inner join with subwuery with group by
select lp.*, t.my_lk_count
from public_report lp
inner join (
select l.lead_key, count(*) my_lk_count
from public_report l
group by l.lead_key
) t ON t.lead_key = lp.lead_key
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