Joining tables + SUM & GROUP BY function - sql

I'm struggling in joining tables together with SUM and GROUP BY function. The query below works fine:
select ID, sum(amount)
from table1
group by ID
having sum(amount) between 1000 and 10000
As table1 only includes customer ID, I also need to join table CUSTOMERS, which contain customer name (column NAME). Following query will not work for me anymore:
select ID, name, sum(amount)
from table1
left join customers on table1.ID = customers.ID2
group by ID
having sum(amount) between 1000 and 10000
Ditching SUM and GROUP BY functionality does "fix" the issue as also column NAME will be available in the result, however I still need to sum and group the AMOUNT based on ID. How should I join the other table in this case to also present field NAME from table CUSTOMERS?
Column NAME or expression in SELECT list not valid'
is currently given as error message.

It needs to be in the group by:
select t1.ID, c.name, sum(t1.amount)
from table1 t1 left join
customers c
on t1.ID = c.ID2
group by t1.ID, c.name;
Note the use of table aliases.

Add "name" in group by clause
select table1.ID, customers.name, sum(table1.amount) amount
from table1,customers on table1.ID = customers.ID2
group by table1.ID,customers.name

try it
select t.ID, c.name, sum(t.amount)
from table1 t
left join customers c on table1.ID = customers.ID2
group by t.ID, c.name
having sum(t.amount) between 1000 and 10000
or without having depends on your requirement

Related

Join Tables Where Absolute of Occurrences Equal to Absolute of Single Occurrence

Problem: I am currently trying to join two tables in Access where the absolute value of the total of multiple occurrences of one field in one table is equal to the absolute value of the occurrence in another table.
Am I able to use the Abs function in the WHERE statement? Everything I saw involved the function being used in the SELECT statement. Do I need to create separate queries to get the absolute values? It would also work if I were to check to see if they balance out rather than getting the absolute value.
In one table, a certain value will repeat multiple times while it will only appear once in the other table. How can I get the absolute values of the totals in order to compare it to the single occurrence in the other table? Thanks!
Table 1
Reference
Amount
55555
$15
55555
$20
Table 2
Reference
Amount
55555
-$35
If these are equal or if they balance out, they should appear on a query. If these aren't equal but the reference number and a partial amount appears, they should appear on another query.
The matching query is relatively simple. Just aggregate and compare the values:
select t2.reference, t2.amount
from (select reference, sum(amount) as amount
from table2
group by reference
) as t2 inner join
(select reference, sum(amount) as amount
from table1
group by reference
) as t1
on t1.reference = t2.reference
where t2.amount + t1.amount = 0;
However, the non-matches are much trickier -- because presumably a reference could be missing from either table. And MS Access does not support full join. One method is:
select t2.reference, t2.amount, t1.amount
from (select reference, sum(amount) as amount
from table2
group by reference
) as t2 left join
(select reference, sum(amount) as amount
from table1
group by reference
) as t1
on t1.reference = t2.reference
where t2.amount + t1.amount <> 0 or t1.amount is null
union all
select t1.reference, null, sum(amount)
from table1 as t1
where not exists (select 1 from table2 as t2 where t2.reference = t1.reference)
group by t1.reference;
Aggregate data in Table1 and join to Table2.
Consider:
SELECT Table2.*, SumAmt FROM Table2
INNER JOIN (SELECT Reference, Sum(Amount) AS SumAmt FROM Table1 GROUP BY Reference) AS T
ON Table2.Reference = T.Reference
WHERE SumAmt = -Table2.Amount;
You can list if the amounts match or not, no Abs is needed:
Select
Table2.Reference,
Table2.Amount,
(T.Total = -Table2.Amount) As T1Match
From
Table2
Inner Join
(Select
Table1.Reference,
Sum(Table1.Amount) As Total
From
Table1
Group By
Table1.Reference) As T
On T.Reference On Table2.Reference
you can use sub-query as follows:
select t1.reference from
(select reference, sum(amount) as amount from table1 group by reference) t1
join table2 t2
on abs(t1.amount) = abs(t2.amount)

Show max to min count in SQL group by

I have a table with id, Name columns.
When selecting I want to group by on Name column (but show all records NO summary), and show result count max number of in one grouping to min number of one grouping.
SELECT Table1.id, Table1.name
FROM Table1
GROUP BY Table1.id, Table1.name;
This is table:
My idea:
but I get this result:
One approach uses a join to a subquery which finds the counts for each name:
SELECT a.name, a.ID
FROM Table1 AS a
INNER JOIN
(
SELECT name, COUNT(*) AS cnt
FROM Table1
GROUP BY name
) AS b
ON a.name = b.name
ORDER BY
b.cnt DESC,
a.ID;
When you group by ID you create a separate group for each element, because each element has a unique ID. To get the count of each name group, you will want to create a grouping by name and then join it with your original table so the values are preserved. Something like:
WITH Counts (name, cnt) AS
(SELECT name, COUNT(*)
FROM Table1
GROUP BY name)
SELECT Table1.id, Table1.name
FROM Table1, Counts
INNER JOIN Counts
ON Table1.name = Counts.name
ORDER BY Counts.cnt DESC
Oh, I see. You an use a subquery in the order by:
select t.*
from t
order by (select count(*) from t t2 where t2.name = t.name) desc, name;
Note that name is the second order by key. If two names have the same counts, then this keeps all the rows for a given name together.

How to fetch data from two different tables having equal value of sum for same sales id?

I have the following two tables:
saleid is the column in common. I can individually obtain the sum of the totals for each table:
select saleid, sum(quantity)
from table1
group by saleid;
select saleid, sum(extracted)
from table2
group by saleid;
I need to compare the sum of extracted, in table 2, with the sum of quantity in table 1 for each sale
What tool of SQL Server or SQL could I use to join the outputs of the sum query and get something like this:
Try below query with inner join and subquery:
select a.saleid, quantity ,extracted from
(select saleid, sum(quantity) as quantity from table1 group by saleid)a
inner join
(select saleid, sum(extracted) as extracted from table2 group by saleid)b
on a.saleid=b.saleid
group by a.saleid
Assuming saleid is same in both the tables, you can join both the tables over the column and get your data via the below query
select t1.saleid,
sum(t1.quantity) ,
sum(t2.extracted)
from table1 t1 join table2 t2
on t1.saleid = t2.saleid
group by t1.saleid;
Try this :
select t1.saleid, t1.quantitySum, t2.extractedSum from
(Select saleid, sum(quantity) as quantitySum from table1 group by saleid)t1
inner join (Select saleid, sum(extracted) as extractedSum from table2 group by saleid)t2 on t1.saleid= t2.saleid and t1.quantitySum=t2.extractedSum ;

Join two tables and retrieve relevant data and calculate the result

We have two tables table1 and table2
Table 1
Table 2
We need the resultant table as :
All this should be done in a single SQL query
Thanks in advance.
I think you can make it without the second query, I tested it and returned your expected values.
select table_2.id_pro,
product_name,
SUM(1) as Quantity,
priceprod,
SUM(1) * priceprod as 'quantity * priceprod'
from Table_2
inner join Table_1 t1 on table_2.id_pro = t1.id_pro
group by table_2.id_pro, product_name, priceprod
And my SqlFiddle test http://sqlfiddle.com/#!3/08c2ef/1
I believe this should be what you need, or fairly close anyway! You need to group up your results from your first table to get your quantity value and then join those results to your second table to be able to create your desired output.
SELECT t1.id_pro,
t2.product_name,
s.Quantity,
t2.priceperprod,
s.Quantity * t2.priceperprod
FROM table_2 t2
INNER JOIN (
SELECT COUNT(*) AS Quantity,
t.id_pro
FROM table_1 t
GROUP BY t.id_pro
) t1 ON t2.id_pro = t1.id_pro
i believe that this is correct, i just JOIN the two tables AND used group by then count how many records each group, i hope this will help you, thanks.
SELECT
A.id_pro,
A.product_name,
(count(*)) AS Quantity,
A.priceperprod,
(COUNT(*) * A.priceperprod) AS Total
FROM table_2 A
LEFT JOIN table_1 B
ON B.id_pro = A.id_pro
GROUP BY A.id_pro
Guess this should be helpful to you.
SELECT A.PRODUCT_CODE, A.PRODUCT_NAME, B.QUANTITY,
A.PRICE PRICE_PER_PRODUCT,
(B.QUANTITY * A.PRICE) TOTAL_PRICE FROM
TABLE2 A,
(SELECT X.PRODUCT, COUNT(X.PRODUCT) QUANTITY FROM TABLE1 AS X
GROUP BY X.PRODUCT) as B
WHERE A.PRODUCT_CODE = B.PRODUCT
ORDER BY A.PRODUCT_CODE
Am doing following :
Taking the entire TABLE2 aliased as A
Getting the products and its corresponding count using group by and aliasing it as B
Selecting corresponding fields from table_Aliases A and B provided A's ProductCode and B's Products are same.

SQL Query with Join, Count and Where

I have 2 tables and am trying to do one query to save myself some work.
Table 1: id, category id, colour
Table 2: category id, category name
I want to join them so that I get id, category id, category name, colour
Then I want to limit it so that no "red" items are selected (WHERE colour != "red")
Then I want to count the number of records in each category (COUNT(id) GROUP BY (category id).
I have been trying:
SELECT COUNT(table1.id), table1.category_id, table2.category_name
FROM table1
INNER JOIN table2 ON table1.category_id=table2.category_id
WHERE table1.colour != "red"
But it just doesn't work. I've tried lots of variations and just get no results when I try the above query.
You have to use GROUP BY so you will have multiple records returned,
SELECT COUNT(*) TotalCount,
b.category_id,
b.category_name
FROM table1 a
INNER JOIN table2 b
ON a.category_id = b.category_id
WHERE a.colour <> 'red'
GROUP BY b.category_id, b.category_name
SELECT COUNT(*), table1.category_id, table2.category_name
FROM table1
INNER JOIN table2 ON table1.category_id=table2.category_id
WHERE table1.colour <> 'red'
GROUP BY table1.category_id, table2.category_name
I have used sub-query and it worked great!
SELECT *,(SELECT count(*) FROM $this->tbl_news WHERE
$this->tbl_news.cat_id=$this->tbl_categories.cat_id) as total_news FROM
$this->tbl_categories