Select count and group by from two tables? - sql

I have two tables Affaire(ID, Obj, ID_TA ...) and TypeAffaire(ID_TA, Label), i want to count number of each ID_TA in Affaire and show the label instead if ID_TA, I tried this but it doesnt work :
SELECT A.ID_TA
,T.Label
,count(*) AS TotalAmount
FROM Affaire AS A
LEFT JOIN TypeAffaire AS T ON T.ID_TA = A.TA
GROUP BY A.ID_TA;
it says that label can't be found

You need to group by T.Label as well. All non-aggegated fields in the select clause, must also be in the group by clause.

Add T.Label in Group By
Try this
SELECT A.ID_TA
,T.Label
,count(*) AS TotalAmount
FROM Affaire AS A
LEFT JOIN TypeAffaire AS T ON T.ID_TA = A.TA
GROUP BY A.ID_TA,T.Label;

You need to Insert T.Label in the group by clause, and correct the ON T.ID_TA = A.TA in to ON T.ID_TA = A.ID_TA
SELECT A.ID_TA ,T.Label ,count(*) AS TotalAmount
FROM Affaire AS A
LEFT JOIN TypeAffaire AS T ON T.ID_TA = A.ID_TA
GROUP BY A.ID_TA, T.Label;

Related

SQL Problem i don't know how to change by queries on inner join

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;

SQL: How to group by with two tables?

I have the tables products and history and I need to group by name:
products = (id_product, name)
history = (id_history, id_product, amount)
I tried this SQL query but it isn't grouped by name:
SELECT
products.name,
sum(history.amount)
FROM history
INNER JOIN products ON history.id_product = products.id_product
GROUP BY
products.name,
history.amount,
history.id_history;
This is the result:
You should only be grouping by the attributes you need to be aggregated. In this case, you need only products.name.
SELECT
products.name,
sum(history.amount) AS [Amount]
FROM history
INNER JOIN products ON history.id_product = products.id_product
GROUP BY
products.name;
If you need to include products without history (assuming sum should be 0 instead of null in this case), then you can use an OUTER JOIN instead of INNER JOIN to include all products:
SELECT
products.name,
COALESCE(sum(history.amount), 0) AS [Amount]
FROM history
RIGHT OUTER JOIN products ON history.id_product = products.id_product
GROUP BY
products.name;
This is no answer, but too long for a comment.
For readability's sake the product table should be first. After all it is products that we select from, plus a history sum that we can access via [left] join history ... followed by an aggregation, or [left] join (<history aggregation query>), or a subselect in the select clause.
Another step to enhance readability is the use of alias names.
Join the table, then aggregate
select p.name, coalesce(sum(h.amount), 0) as total
from products p
left join history h on h.id_product = p.id_product
group by p.name
order by p.name;
Aggregate, then join
select p.name, coalesce(h.sum_amount, 0) as total
from products p
left join
(
select sum(h.amount) as sum_amount
from history
group by id_product
) h on h.id_product = p.id_product
order by p.name;
Get the sum in the select clause
select
name,
(select sum(amount) from history h where h.id_product = p.id_product) as total
from products p
order by p.name;
And as you were confused on how to use GROUP BY, here is an explanation: GROUP BY ___ means you want one result row per ___. In your original query you had GROUP BY products.name, history.amount, history.id_history saying you wanted one result row per name, amount, and id, while you actually wanted one row per name only, i.e. GROUP BY products.name.

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

Count and group the number of times each town is listed in the table

SELECT PEOPLE.TOWNKEY, TOWN_LOOKUP.TOWN FROM PEOPLE
INNER JOIN TOWN_LOOKUP
ON PEOPLE.TOWNKEY = TOWN_LOOKUP.PK
ORDER BY TOWN
Current Table Output:
You are missing the group by clause entirely:
SELECT tl.town, COUNT(*)
FROM people p
INNER JOIN town_lookup ON p.townkey = tl.pk
GROUP BY tl.town
ORDER BY tl.town

SQL Select Statement with nested group by clause

I have 2 tables. I need to select the column name and a calculated field from Invoices called balance_due.
The result of the query should be the name and their balance due from all of their records combined.
Thanks for any help.
SELECT v.vendor_name, i.totalbalance
FROM Vendors as v
INNER JOIN (
SELECT vendor_id, sum(invoice_total-payment_total) as totalbalance
FROM invoices
GROUP BY vendor_id
) as i on i.vendor_id = v.vendor_id
Or there is another syntax:
;With i As
(
SELECT vendor_id, sum(invoice_total-payment_total) as totalbalance
FROM invoices
WHERE payment_total is not null
GROUP BY vendor_id
)
SELECT Vendors.vendor_name, i.totalbalance
From Vendors LEFT JOIN i ON Vendors.vendor_id = i.vendor_id