Joining tables with where clause dynamically - sql-server

Table1
ID
WhereField
Value
1
Column1
2
2
Column2
3
Table2
ID
Column1
Column2
Somedata
1
5
2
Some data
2
6
3
Some more data
With the current scenario, I have 2 tables that need to be joined, the second table's field name is defined as value on the first table.
I need to get the data that needs to be joined with a where clause dynamically such as:
select * from Table1 T1
inner join T1.WhereField on T2. "Dynamic where field column"
where T2."Dynamic where field column" = t1.Value
Is this even achieveable?

Related

Merge 2 tables into 1 single Table in T-SQL

How do I merge 2 tables into 1 table in T-SQL? I tried merging with full outer join which helps in joining 2 tables but with "customer Account" 2 times. I need all the columns from table A and Table B with only once "Customer Account Field" and all the rest of the columns from table A and Table B.ields.
Here is my example in more detail:
Table A - my first Table with 5 columns:
Table B - my second table with 6 columns:
I'm expecting the output like this:
Output with all fields in table A and in Table B but the common field only once:
Thanks a lot.
Add the required(all) columns from t1 and t2 to the select statement
SELECT COALESCE(t1.customeraccount, t2.customeraccount) as customeraccount,
t1.BasicCardType,
t2.MonthlySet
FROM table1 t1
FULL JOIN table2 t2 ON t1.customeraccount = t2.customeraccount;
(Edited based on comments): Join the tables on the CustomerAccount ID field (giving you entries that exist in both tables), then add a union for all entries that only exist in table A, then add a union for entries that only exist in table B. In principle:
-- get entries that exist in both tables
select Table_A.CustomerAccount, TableAField1, TableAField2, TableBField1, TableBField2
from Table_A
join Table_B on Table_A.CustomerAccount = Table_B.CustomerAccount
-- get entries that only exist in table_a
union select Table_A.CustomerAccount, TableAField1, TableAField2, null, null
from Table_A
where Table_A.CustomerAccount not in (select CustomerAccount from Table_B)
-- get entries that only exist in table_B
union select Table_B.CustomerAccount, null, null, TableBField1, TableBField2
from Table_B
where Table_B.CustomerAccount not in (select CustomerAccount from Table_A)

Matching two columns of two tables for a particular value in oracle

Consider there are two tables:
TABLE1 and TABLE2 ,Both of them have same attributes i.e. ID,NAME,CLASS,STATUS.
These two tables has data for all the students in the school...but managed by two independent bodies.
On the basis of unique ID I want to check that for all the students whether the data is same in both the tables.
I would be interested in retrieving the unmatched data in both tables, But don't want the unmatched data where status of a student is 'lefttheschool' in both the tables(and if status is 'lefttheschool'in one table and'present'in other ,it is unmatched and it should be retrieved).
For example:
TABLE1 ID NAME CLASS STATUS
1 A 3 PRESENT
2 B 4 LEFT
3 B 7 LEFT
TABLE2 ID NAME CLASS STATUS
1 A 3 PRESENT
2 C 4 PRESENT
3 B 5 LEFT
RESULT:ID NAME CLASS STATUS ID NAME CLASS STATUS
2 B 4 LEFT 2 C 4 PRESENT
Select all when ID is the same and other attribute is different:
select table1.*, table2.*
from table1, table2
where table1.id = table2.id
and ( table1.name <> table2.name or
table1.class <> table2.class or table1.status <> table2.status)
and (table1.status <>'LEFT' and table2.status <>'LEFT')

How can I find all rows in master table with the same records in its child table?

I need to find every record in TableA that has the same child records in TableB
for example :
tableA
keyA
1
2
3
tableB
keyA....keyB....valueB
1...........11...........4
1...........12...........5
2...........21...........4
2...........22...........5
3...........31...........4
3...........32...........6
So suppose I want to search for doubles.
It should return the two first rows in tableA because both these rows have the same amount of child records in tableB with the same values for valueB
the first row in tableA as 2 child records, one with valueB = 4 and one with valueB = 5
the second row in tableA also has 2 child records, and with the same values in field tableB
the third row also has 2 child records, but with different values in field valueB
so the 2 first rows in tableA should be returned if I search for doubles.
I tried this but it gives an error on the first subquery, it may not return more than one value :
select *
from tableA t1
where (select t2.valueB
from tableB t2
where t2.keyA = 1
)
in
(select t3.valueB
from tableB t3
where t3.keyA = t1.KeyA
)
So, can this be done ?
EDIT : the output for my example should be
tableA
keyA
1
2
Edit 2 : rephrasing the question :
1. tableB is a childtable for tableA
2. there will be records in tableA that have records in tableB with the same values for field valueB as other records in tableA
3. I want to find these records.
EDIT: findings so far :
this query seems to produce what I need :
declare #keyA int = 1
select distinct r.keyA
from tableA r
inner join tableB eb on r.keyA = eb.keyA
where (select count(1) from tableB eb1 where eb1.keyA = #keyA) = (select count(1) from tableB eb2 where eb2.keyA = r.keyA)
and eb.valueB in (select eb4.valueB from tableB eb4 where eb4.keyA = #keyA)
The first where clause only allows master records where the number of child records are the same as for the first row in tableA. (all rows in tableA are found)
The second where clause only allows master records where the valueB of the child records are also present in the child records for the first row in tableA. (only first 2 rows in tableA are found)
The idea is to get all master records (tableA) that have the same amount of child records as the first row, and where all the values for valueB in these child records are also present in the child records for the first row of tableA.
Both where clauses combined should give me what I need, that is what I am hoping.
It seems to produce the correct result, but I would like some confirmation if its correct or wrong.
select
t1.A
from
t1
where exists(
select 1 from t2 where t2.keya=t1.keya
group by t2.keya,t2.valueb
having count(*)>1
)

SQL Server - ISNULL not working on Update Query

Rather than have a NULL in a column, I want a 0 to be present.
Given the following two tables:
TABLE1
ClientID OrderCount
1 NULL
2 NULL
3 NULL
4 NULL
Table2
ClientID OrderCount
1 2
3 4
4 6
NOTE: The OrderCount column in both tables is INT datatype.
UPDATE TABLE1
SET OrderCount = ISNULL(TABLE2.OrderCount,0)
FROM TABLE1
INNER JOIN TABLE2 ON TABLE2.ClientID = TABLE1.CLIENTID
When I look at table1, I see this:
ClientID OrderCount
1 2
2 NULL
3 4
4 6
So, I thought to myself - "Obviously, I should be using NULLIF and not ISNULL", so I reversed them. Same result.
What am I doing wrong here? How do I get a 0 rather than a NULL in the column?
You need a LEFT JOIN rather than an INNER JOIN. The records that don't have a matching ClientID are not even being touched by your query.
you are using INNER JOIN but you don't have client ID 2 on table2 so your result set wont include a line with 2. Replace it with LEFT JOIN
Your join is probably filtering out rows.

SQL View - Combine multiple rows into 1 row

I am trying to make a new view using in MSSql 2008 from three tables:
Table 1 has customer id and transaction id
Table 1
CustomerID TransactionID
1 1
1 2
Table 2 has Purchases Transaction and Products ID
TransactionID ProductID
1 x
2 y
Table 3 has Products Names
ProductID Name
1 x
2 y
I view I would like to make should show
CustomerID Product Name
1 x, y
When I use the following query:
SELECT table1. CustomerID, table3.Name
FROM table1 LEFT OUTER JOIN
Table2 ON table2. TransactionID = table1.VisitId LEFT OUTER JOIN
Table3 ON table2. ProductID = Table3. ProductID
GROUP BY table1. CustomerID, table3.Name
I get
CustomerID Product Name
1 x
1 y
Thanks in Advance
One way to do this is to use a user defined function, something like:
SELECT table1.CustomerID, dbo.BuildStringOfCustomerProductNames(table1.CustomerID)
FROM table1
And create a user defined function like: dbo.BuildStringOfCustomerProductNames
that takes a customerid as input.
Inside there, you can run a query to loop through the table 2 and table 3 joined records to make a string, and return that.
Off hand I can't think of any other simple way to do it.

Resources