I have an access database with two tables.
Table 1: InCome
ID - Name - Quantity
Table 2: OutCome
ID - income_id - Quantity
And a relationship between ID in T1 and income_id in T2.
How to insert a calculated field in T1 to show the sum of outcoming quantity for each row in T1 from T2.
I want to calculate the remaining quantity for each record in T1.
Cannot have calculation in table that summarizes another table. This is what queries are for. Aggregate Table2 data by Income_ID in a query and join that query to Table1. Can nest aggregate query to create a single SQL statement. Consider:
SELECT Table1.*, Quantity-TotOut AS Bal FROM Table1
INNER JOIN (SELECT Income_ID, Sum(Quantity) AS TotOut FROM Table2 GROUP BY Income_ID) AS Out
ON Table1.Income_ID = Out.Income_ID;
Alternatively, build a report and do aggregate calcs in design. A report allows display of raw data as well as summary data.
Related
Suppose I have customer table and sales table with millions of records.
To find sales belonging to CustomerA, supppse I write query like below:
Select *
From customer c
Join sales s
In c.custid=S.custid
Where c.custname='customerA'
The logical order of execution is:
JOIN, WHERE, SELECT.
Does this mean that all records of customer and sales with be fetched and joined. And then records will be filtered based on the customer name? If so then is it more performant to write the following:
Select *
From (select * FROM customer ci where customername='customerA') c
Join sales s
On c.customerid=S.customerid
I need to join following two tables:
Transactions (multiple rows per person)
LedgerSummary (Summary table) - one row per person
How do I join these two tables, but not get LedgerSummary data attached to each row of Transactions? I only want LedgerSummary data attached to first row of transaction per person (ID).
Both of these tables have PersonID column I can join on. However, LedgerSummary has an Amount field. The SUM(Amount) would be wrong if I do a regular left or right join because how these two tables are related. That is the reason I want Amount to only appear once for each person instead of every single row.
;With cteTransactions As
(
Select *, Row_Number() Over (Partition By IdThatLinksToTransAndLedger, Order By
ColumnToGivePriorityOnSelectingOneTransaction) SortOrder
From Transactions
)
Select *
From LedgerSummary L
Join (Select * From cteTransactions Where SortOrder = 1) T
On L.IdThatLinksToTransAndLedger = T.IdThatLinksToTransAndLedger
IdThatLinksToTransAndLedger - You need a key for Person
ColumnToGivePriorityOnSelectingOneTransaction - Priority column, usually a date descending to give most recent.
I'm very new to SQL, I apologize if something doesn't make sense!
I have two tables each of which has a column 'client_nbr'. Some of the client_nbrs will overlap in the two tables. I'm needing to count the number of people with a certain value in column 'age' that is in both tables. For example, the results should have something like
age - 5 count - 3,000
And that will only count a client number once, even if it is in both tables.
When I do this for one table I run:
Select age, count(distinct(client_nbr))
From table1
Group by age
I tried to follow the example here: http://www.sqlservercurry.com/2011/07/sql-server-distinct-count-multiple.html?m=1
Using:
Select table1.age,table2.age,
Count(distinct(table1.client_nbr)) as total
From table1,table2
Where table1.client_nbr=table2.client_nbr
Group by table1.age,table2.age
It didn't work out though. The total count was less than when I run a distinct count on just table1.
Thank you in advance!
Try this instead:
SELECT age, COUNT(DISTINCT client_nbr) AS Total
FROM
(
SELECT age, client_nbr FROM table1
UNION ALL
SELECT age, client_nbr FROM table2
) AS t
GROUP BY age
You are using an implicit inner join in your query meaning only the values contained in both tables are returned. Use an outer join to get all the values in both tables
Select table1.age,table2.age,
Count(distinct(table1.client_nbr)) as total
From table1 FULL OUTER JOIN table2 ON table1.age = table2.age
Group by table1.age,table2.age
I am trying to retrieve data from 2 table with some conditions. When I just do the inner join with the conditions , I get a huge value (200000 data). But when I group by I get a very less value like (8000 data).
SELECT Tcg.SK_tID, Tcg.SK_ServiceProviderID
INTO #CHDetails
FROM #ClientGroup Tcg
INNER JOIN dbo.Component AS chd ON chd.SK_PID = Tcg.SK_PID
AND chd.SK_ServiceProviderID = Tcg.SK_ServiceProviderID
AND chd.SK_CompID = #CHD
AND chd.ReportDate < #ReportDate
GROUP BY Tcg.SK_PID ,Tcg.SK_ServiceProviderID
Can you please let me know the cause for this. Inner join always takes the common data.
The data in the #ClientGroup table is around 70000 , while data in the dbo.Component is very huge. When I query for common PID and Service provider logically it shoul give me the records equal to or less than #ClientGroup. How is it giving more ?
When I do group by i get 8000. But why should I do group by in a inner join for 2 tables.
The group by is essentially performing a distinct on the result. The reason you have to do this is likely because you have duplicates in both tables.
See this sqlfiddle: http://sqlfiddle.com/#!3/cbdca/2
In it, table1 has 3 rows and table2 has 3 rows. When joined together, they return 9 rows.
if a join is giving you more records than expected that means that your join criterea is not complete. The extreme case for this would be a cartesian join.
check your joining criteria.
Group by combines rows together. Usually it is done to produce aggregate data or produce a list of unique values. In your example, the #ClientGroup table is grouped together and the total number of rows you get returned shrinks. You should be getting less rows than are in #ClientGroup.
It will not reduce the number of rows because The INNER JOIN does not perform DISTINCT instead INNER JOIN finds all matching data from both tables.
Let’s say you have table A and table B. both has column ID.
Table A has total 3 rows in it and all values are unique. values are 1,2,3.
TABLE B has total 300 rows, but only 3 unique values. Values are 1,2,3.
(table B has 100 rows for each unique value)
Now in this example if I do INNER JOIN between Table A and Table B
then every row from A will get join Every matching row in table B.
So
value 1 from Table A will match 100 rows in table B
value 2 from Table A will match 100 rows in table B
value 3 from Table A will match 100 rows in table B
so after doing INNER join I will get total 300 rows.
On the contrary if Table B has completely different values (other than 1,2,3) then INNER JOIN would has produced no result.
I have two tables, each with some columns that are the same. However, each table also contains data that is unique. (Similar data includes a row name).
What I need to do is tack on the data from table two to it's matching row in table one (matching the name column).
Is there any way to do this?
I need stuff from table two to go into table 1 where the names match:
The following query should return all matching rows with columns from both tables. Note that any unique rows (that only exist in table one or two) will be excluded.
SELECT
one.matchingColum,
one.oddColum,
two.evenColumn
FROM one
JOIN two on one.matchingColumn = two.matchingColumn
If the data types are the same, then you can do a union
SELECT *
FROM table1
UNION
SELECT *
FROM table2
If the datatypes are not the same and you have a field that you can JOIN on, then you can do a JOIN
SELECT *
FROM table1 t1
LEFT JOIN table2 t2
ON t1.id = t2.id