SQL Access help in sum from 2 different tables - sql

i have these tables
table 1
id price
1 30
2 40
3 50
table 2
id price
1 70
2 5
3 10
i want a query that would sum the the price value based on the ID
like if table1.id=table2.id then sum table1.price and table2.price
the end result should be something like this
table 3
id price
1 100
2 45
3 60

You can;
SELECT
TABLE1.ID,
TABLE1.PRICE+TABLE2.PRICE
FROM TABLE1
INNER JOIN TABLE2 ON TABLE1.ID = TABLE2.ID;
Or if there are duplicate IDs in either table;
SELECT
TABLE1.ID,
SUM(TABLE1.PRICE+TABLE2.PRICE)
FROM TABLE1
INNER JOIN TABLE2 ON TABLE1.ID = TABLE2.ID
GROUP BY TABLE1.ID;

Related

Update rows in a table based on grouping

I have a table with records of the same type with ID's and a grouping. I need to update the id's of table 1 into table 2 based on the record id from largest to smallest using the position.
Table 1
ID
Group
Name
1
A
Apple
2
A
Apple
3
B
Apple
4
B
Apple
Table 2
ID
recordid
position
group
1
1
250
A
2
2
350
A
3
null
450
A
4
null
550
A
5
3
250
B
6
4
350
B
7
null
450
B
8
null
550
B
update table2
set recordid=T1.ID
From Table1 T1
join Table2 T2 on T2.Group=T1.Group
This isn't distinct so I don't think it's right, but I can't figure it out.
where ?????
the problem is you have multiple Id per group , so you have to choose one :
update table2
set recordid= ( select top 1 T1.ID
From Table1 T1
join Table2 T2 on T2.Group=T1.Group
)
where recordid is null

Merging to table from 2 joined tables

For example I am having two tables with id,age,status and height. And there is a table RESULT which I need to merge to.
Table 1
*id age status*
1 15 1
2 16 1
3 17 0
Table 2
*id height*
1 160
2 170
3 180
And Result table is:
Result table
*id age height*
1 15 160
I need to insert into Result table id,height,age from Table 1 join Table 2 on ID ,where status is 1.
How can I write something like
Merge into Result
USING(Select ... from Table1
join Table2 on Table1.id=Table2.id where status=1)
When Not Matched THEN
Insert into Result VALUES(Table1.id,age,height)
I need to get
RESULT
*id age height*
1 15 160
2 16 170
So how can I implement that merge which will find user with id=2 in Result
Table and Insert and will not Insert user with id=1 because it is already in table?
Try this:
MERGE INTO RESULT R USING (
SELECT
T1.ID,
T1.AGE,
T1.STATUS,
T2.HEIGHT
FROM
TABLE1 T1
JOIN TABLE2 T2 ON T1.ID = T2.ID
WHERE
STATUS = 1
) DATAA
ON ( R.ID = DATAA.ID )
WHEN NOT MATCHED
THEN INSERT (
ID,
AGE,
HEIGHT )
VALUES (
DATAA.ID,
DATAA.AGE,
DATAA.HEIGHT )
Cheers!!
Below is the sql query in need to run whole query together
insert into result
Select t1.Id, t1.Age, t2.Height from Table1 t1 inner join Table2 t2 on t1.Id=t2.Id where t1.status=1

Selecting distinct rows based on column

A particular query of mine results data this way.
Id Size
123 1
123 1
123 2
123 2
134 1
134 1
134 2
I want the results get me the count eliminating the duplicate size like
Id Size
123 1
123 2
134 1
134 2
Above was result of joining two tables. Problem is I cant use distinct in this case.
Here is how tables are
Table1:
Id Created ... .. .. ..
123 date1 ....
134 date2 ....
Table2:
Id Size
123 1
123 2
134 1
134 2
I have my query that select from Table1 based on CreatedDate, its like this
select count(*)
from table1
join table2
on table1.id = table2.id
where table1.creates between '' and ''.
How do you get the distinct sizes.
If I use select count(distinct table2.size), it only returns 1 and 2 for all rows.
SELECT DISTINCT Id, Size
FROM table1
This should give you a list of distinct Id and Size combinations.
select count(distinct table1.id, table2.size)
from table1
join table2
on table1.id = table2.id
where table1.creates between '' and ''
see it working live in an sqlfiddle
Sometimes the solution is so obvious... :)
UPDATE: another way
select count(*) from (
select distinct table1.id, table2.size
from table1
join table2
on table1.id = table2.id
where table1.creates between '' and ''
) sq

sql with two table and group and result from that table?

I have two tables. I need to take away from the first table second table. Stim to another table without all the rows of the first table.
Table1
Table2
Result = Table1(value1) – Table2(value1) -----groupe no. 2 or no.1
Result (groupe no. 2)
Result
id value1 groupe
_______________________
1 10 2
2 9 2
3 10 2
5 5 2
6 11 2
7 12 2
I need the result of which I can write the group number and get result for that group.
Try this query:
Select
t1.id,
t1.value1-t2.value1 as value,
t1.groupe from
table1 t1,table2 t2
where t1.id=t2.id and t1.groupe=2;
try this:
SELECT
T1.id, T2.group, T1.valor - T2.valor AS value
FROM
Table1 T1 INNER JOIN
Table2 T2 ON T1.id = T2.id AND T1.group = T2.group
WHERE (T1.group = 2)

How to combine these tables?

In sql server 2005, how do I get the result set in table3 below? table3 is created by combining table1 and table2 like this:
table1.empid
table1.ticket
table2.identid
And update against table1 joined to table2 doesn't work because empid isn't unique. If need to increment to the next table2.indentid if the ID is already being used by that employee.
Also, table3 below isn't created yet. It's a generated set from table1 and table2. I'm using table3 as an example of what the generated set should look like.
table1
empid ticketid indentid
1 7 20
1 9 4
2 9 21
table2
indentid empid
90 1
91 1
92 2
table3
empid ticketid table1_indentid table2_identid
1 7 20 90
1 9 4 91
2 9 21 92
The only connection between table1 and table2 is empid.
The lines 1 and 2 of your table3 example have the same empid, so they should have the same table2_identid as well. Your example is not proper, or not possible to get with a query on the existing data.
But maybe this is what you want:
If you join the tables like so
SELECT empid, ticketid, t1.indentid as table1_indentid, t2.identid as table2_identid
FROM table1 AS t1
INNER JOIN table2 AS t2 ON t1.empid = t2.empid
you will get
table3
empid ticketid table1_indentid table2_identid
1 7 20 90
1 7 20 91
1 9 4 90
1 9 4 91
2 9 21 92
This is what you need:
select t1.empid, t1.ticketid,
t1.indentid as table1_indentid,
t2.indentid as table2_identid
from table1 t1
inner join table2 t2 on t1.empid = t2.empid
You should however consider reviewing your data structure.