Selecting distinct rows based on column - sql

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

Related

select rows with same ID but different value in another column between two tables

i have to similar tables table 1 and table 2 .I want to compare the 2 tables and show the rows that ID is the same but has a different value in another column.
table 1
ID
ACTIVE
100
1
221
1
341
1
and
table 2
ID
ACTIVE
100
1
221
0
341
1
the output should be like this:
ID
ACTIVE
221
1
select t1.*
from t1
join t2 on t1.id = t2.id
where t1.active <> t2.active

ORACLE SQL, Select non-matching fields from same table in a row

I have following table in Oracle:
Id Acct Name
==================
1 123 Anyone
1 234 Anyone
2 435 Someone
2 675 Someone
2 732 Someone
3 765 Anonymous
4 987 Hidden
4 987 Hidden
and I need the following output:
Id Acct1 Acct2 Name
==========================
1 123 234 Anyone
2 435 675 Someone
2 435 732 Someone
So, I only need to show the records with non-matching Acct#, but both the account numbers in one line as above.
Can someone help ?
Use a self join:
select t1.id, t1.name, t1.acct, t2.acct
from t t1 join
t t2
on t1.id = t2.id and t1.name = t2.name and t1.acct <> t2.acct;
Or, if you can have more than two accounts, then perhaps listagg() is more appropriate:
select t.id, t.name, listagg(acct, ',') within group (order by acct)
from t
group by t.id, t.name
having min(acct) <> max(acct);

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

Updating column in a table from another table using join

I have a two tables i need to copy from table 2 to table 1.
I want the table 1. column departure to be updated on the matching in table 2.
Condition
The table 2 have multiple items for the table 1,( table 1 have many relation with table 2). So in that case i need to sort it by RotationOrder and takes first row.
Table 1
Id Code Departure
479 JJ1256 NULL
480 SR1255 NULL
481 PFOBLEM NULL
482 SO1301 NULL
483 TS1302 NULL
484 YB1305 NULL
485 CU1303 NULL
Table 2
Id Departure RotationOrder CanLoad
479 NULL 1 1
480 NULL 1 2 1
481 NULL 1 3 1
482 NULL 1 4
482 NULL 3
482 NULL 2
482 NULL 4
483 2013-01-21 1 1
483 NULL 3
483 NULL 4
483 NULL 6
What i have tried
UPDATE table1 set Departure = (select top 1 table2.Departure from table2
INNER JOIN table1 on table1.Id = table2.Id where CanLoad =1 order by
RotationOrder )
FROM TABLE1 INNER JOIN TABLE2
ON TABLE1.Id = TABLE2.Id
Problem
This query copy first null value from the table2 and paste it on table1. which is incorrect.
Cross apply top 1, beloved classic:
UPDATE t1 set Departure = q.Departure
FROM table1 t1
cross apply
(
SELECT TOP 1 Departure
FROM table2
WHERE t1.Id=table2.Id
ORDER BY RotationOrder asc
)q
This is the way you'll want to do it:
UPDATE T1
SET Departure = T2.Departure
FROM Table1 T1
CROSS APPLY (SELECT ca.*
FROM Table2 ca
WHERE T1.Id = ca.Id
ORDER BY ca.RotationOrder) T2;
Notice the use of the table's alias in the UPDATE clause. If you use the table's actual name (not it's alias) you are technically declaring a 2nd Table1, which can/does produce odd behavior as it technically creates Cartesian product. It's therefore very important to use the syntax UPDATE [Table Alias] when using the FROM clause in an UPDATE statement.
Ignore null value rows. This assumes you have one non null row per Id
UPDATE Table1 set Departure = Table2.departure
FROM TABLE1 INNER JOIN TABLE2
ON TABLE1.Id = TABLE2.Id where Table2.canload =1 and Table2.deprture is not null

SQL Access help in sum from 2 different tables

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;