Get records from 3 tables - sql-server

In SQL Server, Is there any chance to display results from 3 tables? (Actually 4)
The 4th table contains 1 column similar to others. The values of similar columns of other 3 tables are different. In other words, if you select '1' in the 4th table column, the 1st table's results should displayed, if you select '2', the 2nd etc...

select fourth.pk,
case fourth.columnx
when '1' then one.columnx
when '2' then two.columnx
when '3' then three.columnx
else 'Exception to the rule?'
end as 'Column A'
from fourth
join one on fourth.joinid=one.joinid
join two on fourth.joinid=two.joinid
join three on fourth.joinid=three.joinid

Related

Join with conditions

emphasized textI have 2 tables. First table have 3 columns namely SupplierId ,SupplierDetailID and entityid and second table has 2 column namely SupplierId and SupplierDetailID. In first table there are more than one combination of SUPPLIERID and Supplierdetailid due more than one entity id..I want to form left join on supplier Id field in such a way that only when suppliersDetailID is blank in first table it should take max value of SupplierDetailID from second tables for that particular SupplierId.
Formed query will give first_table.supplierID,if first_table SupplierDetailID is blank then max of second table SupplierDetailID for particular supplierId,first_table entityid.
enter image description here
This SQL Query will work for you.
SELECT s1.SupplierID,
ISNULL(s1.SupplierDetailID, MAX(s2.SupplierDetailID)) AS SupplierDetailID
FROM s1
LEFT JOIN s2
ON s1.SupplierID = s2.SupplierID
GROUP BY
s1.SupplierID,s1.SupplierDetailID;
s1 is first supplier table.
s2 is second supplier table.

delete email addresses that contain domain names in another column with this domain list

I have 2 tables with with one column each .Table one has a column 1 with email addresses(Emails) ,table 2 has column 2 with domains(domains) that are contained in the email addresses.This column 2 also contains emails that should be deleted.
I want to delete email addresses from column 1 that contain the domain names and email addresses in column 2 .
These two tables were imported from text files into a table in a db on mssql
Thanks
delete
t1
from table1 as t1
where
exists(select 1 from table2 as t2.column2 = right(t1.column1, len(t1.column1) - Charindex('#', t1.column1)) )
This is a very ugly query that should be very slow with a large table1. I would recommend you to add a persited computed column in table1 that would contain this computed domain using the ugly expression:
ALTER TABLE table1 ADD computedDomain as right(t1.column1, len(t1.column1) - Charindex('#', t1.column1)) persisted;
Then, you can add an index on the computed column if necessary

Merging and transposing in SQL Server

I have two tables:
Table 1
SOP Number is unique per order. For the table above, INVO23935 is one order and INV023937 is another order, etc.
Each unique item in an order has its own line and there could be 1 or more of those items. For INVO23935, there are three item X and one item Y. But other orders could have the same item. For example, order INV023948 has one item X. There are thousands of different items and new ones are constantly added.
The line column is unique to each line item on an order. It is incremented by 16384 for every line item. However, I don’t need line for the final table but I thought it might be useful to create the final table. If it’s not, you can delete it.
This is just a snapshot of the table, which has thousands of rows.
Table 2
Table 2 has one different column from table 1 and that is serial numbers. Only certain items have serial numbers and each serial number is unique even for the same item. For example, there are three item X in INV023935 and three different serial numbers for each X.However, some items won’t have serial numbers so it won’t show up in table 2. For example, Item N1 in INV023937 don’t have any serial numbers; hence, it will only show up in Table 1.
I would like to merge to two tables to look like this:
New Table I want to Make
There are thousands of rows and it’s constantly being updated so I can’t do this manually. I hope I can use SQL to create the table above.
I will really be grateful if you can help me with this!
This would work for SQL Server to always show Table1 values, then show concatenated serial numbers from Table2 if available (or blank otherwise)
SELECT
T1.[Date] AS [Document Date],
T1.[SOP Number],
T1.QTY,
T1.Line AS [Line Item Sequence],
T1.Item AS [Item Number],
COALESCE(STUFF((
SELECT ',' + T2.[Serial Number] AS [text()]
FROM Table2 T2
WHERE T1.[SOP Number] = T2.[SOP Number]
AND T1.[Line] = T2.[Line]
-- Any other join conditions (if item is also required or not)
FOR XML PATH('')
), 1, 1, ''), '') AS [Serial Number]
FROM Table1 T1

Fastest Way to Match Column Values of Tables In SQL Server

I have two tables, Table1 and Table2.
Suppose Table1 have the following columns: T1c1, T1c2, T1c3
and Table2 have the following columns: T2c1, T2c2, T2c3
I need to add values of Table2.T2c3 to Table1.T1c3 based on matching pairs between the two tables and their other two columns or just matching one column values if a column has NULL values in one table or both. That is, I need to match Table1.T1c1 values with Table2.T2c1 values and Table1.T1c2 values with Table2.T2c2 values or just match Table1.T1c1 with Table2.T2c1 values and etc if there's NULL.
The problem is, my tables are of very large size; several hundred millions of rows. I need the fastest matching algorithm to fill out values on Table1.T1c3 values.
I think you could abuse Coalesce() to do this in the ON clause of your join:
SELECT table2.c3 + table1.c3
FROM table1
INNER JOIN table2
ON COALESCE(table1.c1, table2.c1) = COALESCE(table2.c1 = table1.c1)
AND COALESCE(table1.c2, table2.c2) = COALESCE(table2.c2 = table1.c2);
That feels like it might be speedier than dropping CASE all of the over the place or hitting this with a bunch of OR conditions but it might be 6 of one half a dozen of the other...
This is assuming that there would never be a null in both c1 and c2 of either table at the same time. Otherwise you may end up with some funky cross-join shenanigans.

SQL unique PK for grouped data in SP

I am trying to build a temp table with grouped data from multiple tables (in an SP), I am successful in building the data set however I have a requirement that each grouped row have a unique id. I know there are ways to generate unique ids for each row, However the problem I have is that I need the id for a given row to be the same on each run regardless of the number of rows returned.
Example:
1st run:
ID Column A Column B
1 apple 15
2 orange 10
3 grape 11
2nd run:
ID Column A Column B
3 grape 11
The reason I want this is because i am sending this data up to SOLR and when I do a delta I need to have the ID back for the same row as its trying to re-index
Any way I can do this?
Not sure if this will help, not entirely confident of your wider picture, but ...
As your new data is assembled, log each [column a] value in a table of your own.
Give that table an IDENTITY column to do the numbering for you.
Now you can join any new data sets to your lookup table and you'll have a persistent number for each column A.
You just need to ensure that each time you query new data, you add new values to the lookup table.
create table dbo.myRef(
idx int identity(1,1)
,[A] nvarchar(100)
)
General draft as below ...
--- just simulating some input data here
with cte as (
select 'apple' as [A], 15 as [B]
UNION
select 'orange' as [A], 10 as [B]
UNION
select 'banana' as [A], 4 as [B]
)
select * into #temp from cte;
-- Put any new values into the lookup table
-- and they will be assigned a new index number by the identity column
insert into dbo.myRef([A])
select distinct [A]
from #temp where [A] not in (select [A] from dbo.myRef)
-- now pull your original data for output, joining to the lookup table to get a ref number.
select T.*,R.idx
from #temp T
inner join
oer.myRef R
on T.[A] = R.[A]
Sorry for the late reply, i was stuck with something else, however i solved my own issue.
I built 2 temp tables one with all the data from the various tables (#master) and another temp table (#final) to house all the grouped data with an empty column for ID
Next i did a concat(column1, '-',column2,'-', column3) on 3 columns from the #master and updated the #final table based on the type
this helped me to get the same concat ids on each run

Resources