I have two tables as below. I would like to keep everything from t1 and everything from t2 except Date,Id. The metrics value (Salary,Bonus) for both tables should not be changed. As there are multiple date and id in t1, I am getting duplicate in the output. My code is as below.Please assist.
select t1.*,t2.*except(Date,Id) from t1
left join t2
on t1.Date = t2. Date
and t1.Id= t2.Id
enter image description here
While the logic for your output table might be lacking of explanation, I can answer your main question. You can use LEFT JOIN and simply manually SELECT all the columns you desired from each table, writing them in the desired order.
Below is the syntax for that with some sample data I created.
with t1 as (
SELECT DATE(2020,01,22) as Date, 1 as id, "abc" as Name, "NYC" as City, "USA" as Country, 5000 as Salary UNION ALL
SELECT DATE(2020,01,23) as Date, 2 as id, "abc" as Name, "SF" as City, "USA" as Country, 8000 as Salary UNION ALL
SELECT DATE(2020,01,22) as Date, 2 as id, "abc" as Name, "SF" as City, "USA" as Country, 8000 as Salary
),
t2 as (
SELECT DATE(2020,01,22) as Date, 1 as id, "Man" as Position, "1st" as Rank, 1000 as Bonus UNION ALL
SELECT DATE(2020,01,22) as Date, 2 as id, "Man" as Position, "1st" as Rank, 1000 as Bonus
)
SELECT t1.Date, t1.id, t1.Name, t1.City, t2.Position, t2.Rank, t1.Country, t1.Salary,
t2.Bonus
FROM t1 LEFT JOIN t2 on t1.Date=t2.Date and t1.id=t2.id
And the output,
Notice that I have selected the columns in the order I wanted them to be in the output. Furthermore, Date and id come from table 1 as specified in the select statement. Another important point is that where t1.Date=t2.Date and t1.id=t2.id are not checked as true, the values assigned to the columns from t2 are null.
I would like to point that I have set manually the value for the Rank column just as a sample. Lastly, everything within with() is sample data.
Related
I have 2 tables containing different data, linked by a column "id", except the id is repeated multiple times
For example,
Table 1:
id grade
1 A
1 C
Table 2:
Id company
1 Alpha
1 Beta
1 Charlie
The number of rows would be inconsistent, table 1 may sometimes have more/less/equal rows compared to table 2. How am I able to combine/merge them into this outcome:
id grade company
1 A Alpha
1 C Beta
1 Charlie
I am using Microsoft access' query.
This is a real pain in MS Access. But you can do it by using a subquery to generate sequence numbers. Here is one method assuming that the rows are unique:
select id, max(grade) as grade, max(company) as company
from ((select id, grade, null as company,
(select count(*)
from table1 as tt1
where tt1.id = t1.id and tt1.grade <= t1.grade
) as seqnum
from table1 as tt1
) union all
(select id, null as grade, company,
(select count(*)
from table2 as tt2
where tt2.id = t2.id and tt2.company <= t1.company
) as seqnum
from table2 as tt2
)
) t12
group by id, seqnum;
This would be much simpler in almost any other database.
I have two tables in Sql Server Table1 and Table2.
The First Table has PartID, Code, Brand
The Second Table has ID, PartID, AddCode, AddBrand
The idea is that the first table is main table where Some Article is entered with his original code and Brand.
The Second Table is table where we can store additional Codes and Brands which original Article is related to them
Let say that in First Table We have following Data:
PartId Code Brand
100 15FY MCD
Second Table Has following data:
ID PartID AddCode AddData
1 100 1888 AddBrand1
2 100 FF0-1 AddBrand2
I want to display data with select like this:
PartId Code Brand
100 15FY MCD
100 1888 AddBrand1
100 FF0-1 AddBrand2
I've tried to use:
Select a.PartID, a.Code, a.Brand,b.AddCode,b.AddData
from table1 a left outer join
table2 b on a.PartId=b.PartId
but i cant figure out how to do it...
Thank you in advance
This sounds more like union all then join:
select PartId, Code, Brand
from ((select t1.PartId, t1.Code, t1.Brand, 1 as seq
from table1 t1
) union all
(select t2.PartId, t2.AddCode as Code, t2.AddBrand as brand, 2 as seq
from t2
)
) x
order by PartId, seq;
Note that this orders the results so all PartIds appear together in the result set, with the row from the first table appearing first.
Use UNION ALL Statement In SELECT Clause :
SELECT PartId, Code, Brand
FROM Table1
UNION ALL
SELECT PartID ,AddCode Code,AddData Brand
FROM Table2
SELECT *
FROM (
SELECT A.PARTID
,A.CODE
,A.BRAND
FROM TABLE1 A
UNION ALL
SELECT B.PARTID
,B.ADDCODE
,B.ADDDATA
FROM TABLE B
) RESULT
ORDER BY RESULT.PARTID
Use Union of both tables like this
Select PartId, Code, Brand from table1
UNION ALL
Select PartID, AddCode, addData
from table2
I have 2 series of unions which I wish to join by another union. In the first one, I have 3 Selects and in the second one I have 2 different Selects.
Select id, min(value)
from table1 t1
join (Select id, value
Union
Select id, value
Union
Select id, value) as foo
on foo.id=t1.id
Group by id
Select id, max(value)
from table1 t1
join (Select id, value
Union
Select id, value) as bar
on bar.id=t1.id
Group by id
I tried to do a union between these two, but it made things pretty complicated. My biggest issue is with my alias. My second is with the case linked to my value columns, which I wish to name value.
Select (alias).id,
Case
When foo.value= 0 or bar.value=1 THEN 1
Else 0
End as value
from table1 t1
Join (Select id, min(value)
from table1 t1
join (Select id, value
Union
Select id, value
Union
Select id, value) as foo
on foo.id=t1.id
Group by id
UNION
Select id, max(value)
from table1 t1
join (Select id, value
Union
Select id, value) as bar
on bar.id=t1.id
Group by id) as (alias)
on ??.id=??.id
I wrote my case the way I think it should be written, but normally, when there are more than one column with the same name, SQL states it as ambiguous. I am still unsure if I should use UNION or INTERSECT, but I assume either of them would be done the same way. How should I deal with this?
I'm reading this right, you probably want something like this
SELECT ...
FROM ( ... union #1 ) AS u1
JOIN (... union #2 ) AS u2 ON u1.id = u2.id
table1 -> id, time_stamp, value
This table consists of 10 id's. Each id would be having a value for each hour in a day.
So for 1 day, there would be 240 records in this table.
table2 -> id
Table2 consists of a dynamically changing subset of id's present in table1.
At a particular instance, the intention is to get sum(value) from table1, considering id's only in table2,
grouping by each hour in that day, giving the summarized values a rank and repeating this each day.
the query is at this stage:
select time_stamp, sum(value),
rank() over (partition by trunc(time_stamp) order by sum(value) desc) rn
from table1
where exists (select t2.id from table2 t2 where id=t2.id)
and
time_stamp >= to_date('05/04/2010 00','dd/mm/yyyy hh24') and
time_stamp <= to_date('25/04/2010 23','dd/mm/yyyy hh24')
group by time_stamp
order by time_stamp asc
If the query is correct, can this be made more efficient, considering that, table1 will actually consist of thousand's of id's instead of 10 ?
EDIT: I am using sum(value) 2 times in the query, which I am not able to get a workaround such that the sum() is done only once. Pls help on this
from table1
where exists (select t2.id from table2 t2 where value=t2.value)
The table2 doesn't have Value field. Why is the above query with t2.Value?
You could use a join here
from table1 t1 join table2 t2 on t1.id = t2.id
EDIT: Its been a while that I worked on Oracle. Pardon me, if my comment on t2.Value doesn't make sense.
I have a query that UNION's two somewhat similar datasets, but they both have some columns that are not present in the other (i.e., the columns have NULL values in the resulting UNION.)
The problem is, I need to ORDER the resulting data using those columns that only exist in one or the other set, to get the data in a friendly format for the software-side.
For example: Table1 has fields ID, Cat, Price. Table2 has fields ID, Name, Abbrv. The ID field is common between the two tables.
My query looks like something like this:
SELECT t1.ID, t1.Cat, t1.Price, NULL as Name, NULL as Abbrv FROM t1
UNION
SELECT t2.ID, NULL as Cat, NULL as Price, t2.Name, t2.Abbrv FROM t2
ORDER BY Price DESC, Abbrv ASC
The ORDER BY is where I'm stuck. The data looks like this:
100 Balls 1.53
200 Bubbles 1.24
100 RedBall 101RB
100 BlueBall 102BB
200 RedWand 201RW
200 BlueWand 202BW
...but I want it to look like this:
100 Balls 1.53
100 RedBall 101RB
100 BlueBall 102BB
200 Bubbles 1.24
200 RedWand 201RW
200 BlueWand 202BW
I'm hoping this can be done in T-SQL.
Select ID, Cat, Price, Name, Abbrv
From
(SELECT t1.ID, t1.Cat, t1.Price, t1.Price AS SortPrice, NULL as Name, NULL as Abbrv
FROM t1
UNION
SELECT t2.ID, NULL as Cat, NULL as Price, t1.Price as SortPrice, t2.Name, t2.Abbrv
FROM t2
inner join t1 on t2.id = t1.id
) t3
ORDER BY SortPrice DESC, Abbrv ASC
Somehow you have to know the data in table 2 are linked to table 1 and share the price. Since the Null in abbrv will come first, there is no need to create a SortAbbrv column.
You should use UNION ALL instead of UNION to save the cost of duplicate checking.
SELECT *
FROM
(
SELECT t1.ID, t1.Cat, t1.Price, NULL as Name, NULL as Abbrv FROM t1
UNION ALL
SELECT t2.ID, NULL as Cat, NULL as Price, t2.Name, t2.Abbrv FROM t2
) as sub
ORDER BY
ID,
CASE WHEN Price is not null THEN 1 ELSE 2 END,
Price DESC,
CASE WHEN Abbrv is not null THEN 1 ELSE 2 END,
Abbrv ASC
A quick solution would be to do 2 inserts into a temp table or a table variable and as part of insert into the temp table you can set a flag column to help with sorting and then order by that flag column.
Off the top of my head i would say the worst case scenario is you create a temporary table with all the fields do an INSERT INTO the temp table from both T1 & T2 then SELECT from the temp table with an order by.
ie. Create a temp table (eg. #temp) with fields Id, Cat, Price, Name, Abbrv, and then:
SELECT Id, Cat, Price, null, null INTO #temp FROM T1
SELECT Id, null, null, Name, Abbrv INTO #temp FROM T2
SELECT * FROM #temp ORDER BY Id, Price DESC, Abbrv ASC
NB: I'm not 100% sure on the null syntax from the inserts but i think it will work.
EDIT: Added ordering by Price & Abbrv after id... if Id doesn't link T1 & T2 then what does?