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
Related
Hi I would like to join table by substring in string and return not join as new row. Is that possible? Here's an example
ID
price
07a
50
1b7
60
7TC
40
productCode
details
newhair_07a
detailA
black_1b7_chair
detailB
blueBed
detailC
into a table where it matches the ID from table ID with the product table's productCode. If substring is in string then join the tables together and return no match into a new row.
Output will be like so
ID
productCode
details
price
07a
newhair_07a
detailA
50
1b7
black_1b7_chair
detailB
60
blueBed
detailC
7TC
40
I don't know where to begin to join this. Please do help
Consider below approach
with temp as (
select id, productCode, details, price
from tableID
join tableProduct
on regexp_contains(productCode, id)
)
select * from temp
union all
select id, null, null, price
from tableID where not id in (select id from temp)
union all
select null, productCode, details, null
from tableProduct where not productCode in (select productCode from temp)
if applied to sample data in your question - output is
It looks like a FULL OUTER JOIN problem. Assuming that your productCode is a underbar separated value which includes ID, below approach would be possible:
WITH joined AS (
SELECT * EXCEPT (code)
FROM tableB b JOIN UNNEST(SPLIT(productCode, '_')) code
FULL JOIN tableA a ON a.ID = code
)
SELECT * FROM joined WHERE productCode IS NULL
UNION ALL
SELECT * FROM joined WHERE productCode IS NOT NULL
QUALIFY ROW_NUMBER() OVER (PARTITION BY productCode, details ORDER BY ID NULLS LAST) = 1;
output:
sample tables:
CREATE TEMP TABLE tableA AS (
SELECT '07a' ID, 50 price
UNION ALL
SELECT '1b7', 60
UNION ALL
SELECT '7TC', 40
);
CREATE TEMP TABLE tableB AS (
SELECT 'newhair_07a' productCode, 'detailA' details
UNION ALL
SELECT 'black_1b7_chair', 'detailB'
UNION ALL
SELECT 'blueBed', 'detailC'
);
I have a requirement for a report and I would like my sql query to repeat each row twice.
Example :
**Table 1**
Id Name
1 Ab
2 Cd
3 Ef
I want to write a query which outputs the following :
1 Ab
1 Ab
2 Cd
2 Cd
3 Ef
3 Ef
Is there a way I can do it ?
I cannot think of anything except using union
Select Id, name from Table1 union select Id, name from Table1
You can use a union all. A union will not work, because it will eliminate duplicates. Another way is a cross join:
select id, name
from table1 t1 cross join
(select 1 as n union all select 2) n;
You can also use UNION ALL, put them under CTE (Common Table Expression) and Order By Id:
WITH CTE AS
(
SELECT Id, Name FROM Table_1
UNION ALL
SELECT Id, Name FROM Table_1
)
SELECT Id, Name
FROM CTE
ORDER BY Id;
As this will reorder them and stacked them as duplicates
Solution will be like this:
select Id, name from Table1
union all
select Id, name from Table1
I'm having a hard time summing up a column on two tables. The scenario is something like this (refer to the image below)
Table 1 may have a lot of rows per Date. But Table 2 may only consists of two rows of data per Date. What I wanted to do is to sum up all Item/Price (Table1) according to their Date and ADD them with another SUM of Item/Price of Table2. The category of SUM is by Date.
I tried any joins statement (left, right or inner) but it does not produce the result that I am expecting to. My expected result is the Result table. But on my query, it produces a very high value.
Thanks.
Use a UNION clause like this:
WITH t(d, p) AS (
SELECT [Date], Price FROM Table1
UNION ALL
SELECT [Date], Price FROM Table2
)
SELECT d, SUM(p) FROM t GROUP BY d
You can do this with UNION ALL in either a subquery or a cte, cte shown here:
;WITH cte AS (SELECT [Date], Price
FROM Table1
UNION ALL
SELECT [Date], Price
FROM Table2
)
SELECT [Date], SUM(Price) AS Total_Price
FROM cte
GROUP BY [Date]
Demo: SQL Fiddle
Try This,
with cte (C_Date,C_Price)
as
(
SELECT date,SUM(price) FROM table_1
group by date
union
SELECT date,SUM(price) FROM table_2
group by date
)
select c_date,SUM(c_price) from cte
group by C_Date
Try this
Select t.date,P1+P2
from(
Select Date,sum(Price) P1
from table1 t
group by Date
) t
left join
(
Select Date,sum(Price) P2
from table t2
group by date
) t1 on t.date = t1.date
group by date
I am using the UNION syntax to retrieve a product code and description from several databases.
I want to retrieve only a unique product code, even if this product code has several descriptions. I want to retreive only the first result.
To do that, I am using this script:
SELECT * FROM (SELECT tab1.code, tab1.description FROM tab1
UNION
SELECT tab2.code, tab2.description FROM tab2
UNION
SELECT tab3.code, tab3.description FROM tab3)
Unfortunately, this script will retrieve several product codes if the specific product has more than one description.
How can this be modified to retrieve only the first occurrence with a description?
If you want ANY one description, you can go with max or min like this:
select code, max(description) from (your set of unions)
group by code
In this case, you can change UNION to UNION ALL to skip on sorting.
If you really want the first one, you would need to indicate it:
select code, description from (
select code, description, ord, min(ord) over (partition by code) min_ord from (
select code, description, 1 as ord from table1
union all
select code, description, 2 as ord from table2
union all
select code, description, 3 as ord from table3
)
) where ord = min_ord
I think that this solution works but maybe it is not elegant.
SELECT * FROM
(
SELECT tab1.code, tab1.description FROM tab1
UNION
SELECT tab2.code, tab2.description FROM tab2 WHERE tab2.code not in
(SELECT tab1.code FROM tab1)
UNION
SELECT tab3.code, tab3.description FROM tab3 WHERE tab3.code not in
(SELECT tab1.code FROM tab1
UNION
SELECT tab2.code FROM tab2)
)
Your query might be more efficient with a full outer join, if there are no duplicates within a table and description does not take on NULL values:
SELECT coalesce(tab1.code, tab2.code, tab3.code) as code,
coalesce(tab1.description, tab2.description, tab3.description) as description
FROM tab1 full outer join
tab2
on tab2.code = tab1.code full outer join
tab3
on tab3.code = coalesce(tab1.code, tab2.code);
This saves the duplicate elimination step (or aggregation) and allows better use of indexes.
If I am creating temporary tables, that have 2 columns. id and score. I want to to add them together.
The way I want to add them is if they each contain the same id then I do not want to duplicate the id but instead add the scores together.
if I have 2 temp tables called t1 and t2
and t1 had:
id 3 score 4
id 6 score 7
and t2 had:
id 3 score 5
id 5 score 2
I would end up with a new temp table containing:
id 3 score 9
id 5 score 2
id 6 score 7
The reason I want to do this is, I am trying to build a product search. I have a few algorithms I want to use, 1 using fulltext another not. And I want to use both algorithms so I want to create a temporary table based on algorithm1 and a temp table based on algorithm2. Then combine them.
How about:
SELECT id, SUM(score) AS score FROM (
SELECT id, score FROM t1
UNION ALL
SELECT id, score FROM t2
) t3
GROUP BY id
This is untested but you should be able to perform a union on the two tables and then perform a select on the results, grouping the fields and adding the scores
SELECT id,SUM(score) FROM
(
SELECT id,score FROM t1
UNION ALL
SELECT id,score FROM t2
) joined
GROUP BY id
Perform a full outer join on the ID. Select on the ID and the sum of the two "score" columns after coalescing the values to 0.
SELECT id, SUM(score) FROM
(
SELECT id, score FROM #t1
UNION ALL
SELECT id, score FROM #t2
) AS Temp
GROUP BY id
select id, sum(score)
from (
select * from table 1
union all
select * from table2
) tables
group by id
You need to create an union of those two tables then You can easily group the results.
SELECT id, sum(score) FROM
(
SELECT id, score FROM t1
UNION
SELECT id, score FROM t2
) as tmp
GROUP BY id;