issue in joining two tables using UNION - sql

I need to find target minus source table.Following is the details of tables:
Columns of Target table - customer_match :
- ID
- CUST_ID
- USER_ID - has default value 'A'
Columns of Source table - sales1 :
- id
Column of Source table - sales2
- cu_id
Mapping looks like this:
1. customer_match.ID = sales1.id
2. customer_match.CUST_ID = sales2.cu_id
3. customer_match.USER_ID = 'A'
Source tables should have a union join.
I have written query in teradata based on above requirement as:
sel
ID
, CUST_ID
, USER_ID
from customer_match
MINUS
sel
id
,'A'
from sales1
UNION
sel
cu_id
,'A'
from sales2
But this query does not satisfy my requirement as my both id and cu_id are getting mapped to only one column.
Can you please help me to correct my query as per the requirement.

You can't union two values in query. Use field name instead of it.
select
id
from sales1
UNION
select
cu_id
from sales2

Related

Adding a Varchar auto-increment column to an select query

I'm trying to design a database and I have grouped a certain data as follows
select Name, prod_cat, prod_country, count(*)
from table1
group by Name, prod_cat, prod_country
union
select Name, prod_cat, prod_country, count(*)
from table2
group by Name, prod_cat, prod_country
Now for the resultant data from the query above, I would like to add an auto-increment ID as a VARCHAR column which should start as 'U000001' , 'U000002' , 'U000003' , 'U000004' ... all the way to the last column
For a better database, is it better to put the resultant query into a temporary table/view or is it better to use a stored procedure as there could be more data coming in
How can I add an auto-incrementing ID VARCHAR column to the above mentioned select query? Should I use Declare and increment?
Expected result :
Prod_ID
Name
Prod_cat
Prod_country
U000001
abc
12
USA
U000002
efg
1
IND
U000003
def
3
MEX
U000004
ijk
21
CHN

Join query in specific column table

i have table with query i can get final price , i want join query to table as new column.
my query:
SELECT ID , Name FROM dbo.AshkhasList
select dbo.Person_Mande(40,'1398/01/01','1400/12/29',DEFAULT)
line 1 query call table with id and name , line 2 get final price as id.
i want join final price query line 2 as column name Price inside name in result.
i found my problem:
SELECT ID , Name , dbo.Person_Mande(ID,'1398/01/01','1400/12/29',DEFAULT) AS Mande FROM dbo.AshkhasList

union unusual behavior

Trying to union two tables with the same field into one master table but for some reason im getting a weird result.
select count(*)
from staging.sandoval_parcels
where parcel_id = 0;
returns 0
select count(*)
from staging.bernalillo_parcels
where parcel_id = 0;
returns 0
but when i merge the tables using
CREATE TABLE staging.master_parcels
AS
SELECT * FROM bernalillo_parcels
UNION ALL
SELECT * FROM sandoval_parcels
;
then
select count(*)
from staging.master_parcels
where parcel_id = 0;
returns 85553
both tables have the same fields and the fields are the same data type,also, no of the values for any field are missing, thus no nulls, why am i getting ids = 0 when either of the table have parcel_ids = 0?
The order of the fields matter, replace the * for the explicit name, other wise the second query field will be inserted on the first query position. But not necessarily on the same field you want.
CREATE TABLE staging.master_parcels
AS
SELECT parcel_id, field1 ... FROM bernalillo_parcels
UNION ALL
SELECT parcel_id, field1 ... FROM sandoval_parcels
;
Union will merge tables even if the column order is not the same. If all of the columns match and are in the same order, it will union distinct values and not create duplicates if the rows are the same for each table. Having the order and data type be the same is important.

Sql select with grouping rows

In mssql-db I have a table with 5 columns.
id, 1st_t1, 1st_t2, 2nd_t1, 2nd_t2
it's single "task" with 2 parameters(t1,t2) "grouped" by person type(1st,2nd)
Is it possible to select info like this on picture in Reporting Service?:
Or the only way is divide this table on two tables for each person type?
In your report, your detail must have two rows.
On the first you will drag and drop the data of the 1st person and on the second row for the second like in the image below
Please try below query:
WITH T(ID, First_t1, First_t2, Second_t1, Second_t2)
AS
(
SELECT *
FROM
(
SELECT 1 ID, '1st_t1' First_t1, '1st_t2' First_t2, '2nd_t1' Second_t1, '2nd_t2' Second_t2
UNION
SELECT 2 ID, '1st_t1' First_t1, '1st_t2' First_t2, '2nd_t1' Second_t1, '2nd_t2' Second_t2
UNION
SELECT 3 ID, '1st_t1' First_t1, '1st_t2' First_t2, '2nd_t1' Second_t1, '2nd_t2' Second_t2
) A
)
SELECT * FROM
(
SELECT '1st person' person, Id,First_t1 t1,First_t2 t2 FROM T
union
SELECT '2nd person' person, Id,Second_t1 t1,Second_t2 t2 FROM T
) B
ORDER BY id

Matching a substring to a string

Please advise me on the following question:
I have two tables in an Oracle db, one that contains full numbers and the other that contains parts of them.
Table 1:
12323543451123
66542123345345
16654232423423
12534456353451
64565463345231
34534512312312
43534534534533
Table 2:
1232
6654212
166
1253445635
6456546
34534
435345
Could you please suggest a query that joins these two tables and shows the relation between 6456546 and 64565463345231, for example. The main thing is that Table 2 contains a lot more data than Table 1, and i need to find all the substrings from Table 2 that are not present in Table 1.
Thanks in advance!
Try this:
with t as (
select 123 id from dual union all
select 567 id from dual union all
select 891 id from dual
), t2 as (
select 1112323 id from dual union all
select 32567321 id from dual union all
select 44891555 id from dual
)
select t.id, t2.id
from t, t2
where t2.id||'' like '%'||t.id||'%'
You could try using the CONTAINS operator like this :
SELECT * FROM Table2 JOIN Table1 ON Table1.id=Table2.id
WHERE NOT CONTAINS (Table2.data, Table1.data)
Are numbers from table two in a set place in table 1? For example is the 1232 in the same place each time or do you have to search a sting for the numbers. If it's set you could use an inline select or a temp table and create a substring of the string your searching and then join the table or temp table on that field.
you first need to say if the number in Table 1 and 2 are repeated, if is not then I think this query would help you:
SELECT *
FROM Table_1
JOIN Table_2 ON Table_1.ID = Table_2.ID
WHERE Table_2.DATA LIKE Table_1.DATA