I'm using SSIS 2008 R2 for a few weeks now and I'm stuck at this moment.
I need to merge two tables from two different SQL Servers into a new table.
This is what I try to achieve:
Table 1:
Name Price Quantity
------------------------
Item 1 23 5
Item 2 50 2
Item 3 30 10
Table 2:
ID Name Price Quantity
------------------------------
101 Item 1 60 7
203 Item 3 80 15
Result:
ID Name Price Quantity
-------------------------------------
101 Item 1 60 7
NULL Item 2 50 2
203 Item 3 80 15
I've tried to use merge but that duplicates Item 1 and Item 3.
I'm trying to create it within Development Studio so there is no real code that I can show.
This is what I've created
Select T2.ID, T1.Name,Price=case when isnull(T1.Price,0)>isnull(T2.Price,0) then
isnull(T1.Price,0) else isnull(T2.Price,0) end,
Quantity=case when isnull(T1.Quantity,0)>isnull(T2.Quantity,0)
then isnull(T1.Quantity,0) else isnull(T2.Quantity,0) end
from Table1 T1
left outer join Table2 T2 on T1.Name=T2.Name
Related
I have TrsViewPay view with this sample data:
id DocTypeRef TrsDocPayItemref
---------------------------------
1 10 16
2 20 17
3 30 18
4 40 1
First I don't want to show record with DocTypeRef 40.
Then I don't want to show the records where the id is equal with that record's TrsDocPayItemref.
So I want to show this result (without record 1 and 4)
id DocTypeRef TrsDocPayItemref
---------------------------------
2 20 17
3 30 18
Ravi's answer is close, but I think this one will be better:
SELECT Id, DocTypeRef, TrsDocPayItemref
FROM TrsViewPay
WHERE DocTypeRef <> 40
AND Id <> (SELECT TrsDocPayItemref FROM TrsViewPay WHERE DocTypeRef = 40)
You can go for inner queries or sub queries. You can first Select the value of
DocTypeRef and then compare it with id. use first point as inner query. After that you can retrieve data using the result of first query.
You can try this:
SELECT *
FROM TrsViewPay
WHERE DocTypeRef!=40
AND NOT TrsDocPayItemref IN (SELECT id FROM TrsViewPay )
I have a scenario where in I want a field in a table to be incremented sequentially.
Suppose I have a table Test, with columns TestID, TestResult1,2 etc.. and TestCount.
I have data bulk inserted into the table. Some of the records may be retests, which means new data to be inserted matches existing records in the table, Test Count should be updated.Matching is done on TestID
If the table is as follows:
TestID TestResult1 TestResult2.. TestCount
12 1 1 1
12 2 2 2
13 4 1 1
Data to be inserted in
TestID TestResult1 TestResult2..
12 3 5
12 2 2
The table should be updated as
TestID TestResult1 TestResult2.. TestCount
12 1 1 1
12 2 2 2
13 4 1 1
12 3 5 3
12 2 2 4
I tried adding a trigger on the table to update the TestCount Counting the number of records that matches. But it was updating the table as follows
TestID TestResult1 TestResult2.. TestCount
12 1 1 1
12 2 2 2
13 4 1 1
12 3 5 3
12 2 2 3
CREATE TRIGGER trgTestCount
on Test
AFTER INSERT
AS
Update g
Set TestCount= (Select Count(*)+1 from Test g join INSERTED g1 where g.TestID=g1.TestID )
from Test g
This is a SSIS package and I use a dataflow task to load data from STg table to test table.
Can you tell me what I am doing wrong here?
If you can change the table structure, I would suggest adding an Identity column, change the TestCount column to a computed column, and have it's value as the count of distinct test ids that are the same is the current row test id and the create date is lower than the current value of the Identity column.
This will eliminate the need for triggers and will handle inserting multiple records with the same test id automatically.
I have a table ratings, bookmark, checkin, food in food table there is a unique key sno and this sno key is used in remaining three tables.
food table
sno name totalrating totalcheckin
1 nitesh 52 45
2 abhishek 4 9
3 divye 42 30
ratings table
sno datakey rated name
1 3 3.0 divye
1 6 4.0 shashank
bookmark table
sno datakey name
1 3 divye
1 6 shashank
Checkin table
sno datakey name
1 2 abhishek
1 6 shashank
I need data where datakey is 3 if not present show null values and data key column not repeated
like
0 1 2 3 4 5 6 7 8 9 10
sno name totalrating totalcheckin sno rated name sno name sno name
3 divye 42 30 1 3.0 divye 1 divye null null
your query should look like this:
SELECT f.sno, f.name, f.totalrating, f.totalcheckin,
r.sno, r.rated, r.name,
b.sno, b.name,
c.sno, c.name
FROM food AS f
LEFT JOIN ratings AS r
ON f.sno = r.datakey
LEFT JOIN bookmark AS b
ON f.sno = b.datakey
LEFT JOIN checkin AS c
ON f.sno = c.datakey
WHERE f.sno = 3
Here is SQL Fiddle to see how it's work.
Also I agree with the guys in the comment which are told you to read something about JOIN syntax. It's pretty and you can start here, or more specific for your problem is LEFT JOIN, that is the begin and good place to start. Also you can see that I use aliases in my query about that read here.
GL!
P.S. (edit) and if you have any question fill free to ask... Also I notice that you have name column in every table, if I understand relation between your table it's not necessary. You should store name only in first table (food) and with simple JOIN from there you can pull that data whenever you need it!
I am in need of stored procedure, I have searched but I didn't get any relevant code.
My requirement is the stored procedure wants to loop the first subquery based on inner subquery.
Select *
from StockInward,
Setup
where StockInward.StockLocation=Setup.Id
AND ProductId in( Select ProductId
from ProductOutward
where Orderid ='38')
The sample table data and output below:
Product Outward table
Id Orderid Productid Qty
1 38 7 2
2 38 6 1
Stockinward table
Id ProductId BranchId Qty
1 7 1 12
2 6 1 2
3 7 2 2
Setup table
BranchId Branchname
1 Xyz
2 ABC
The output need to be:
ProductId Branches
7 Xyz(12) Abc(2 )
6 Xyz(2) -
I have two simple SELECT statements:
The first shows a list of Features.
SELECT * FROM Features
id name
-- ----
1 24 Hour Access
2 24 hour CCTV monitoring
3 Airport location
4 Break-Out Areas
5 Business Lounge
6 Business park location
snip..
and the second statement shows a list of feature information that has changed
SELECT
*
FROM
#SmartFeaturesToUpdate new_features
ORDER BY
new_features.centre_translation_id,
new_features.feature_id,
new_features.feature_selected
feature_id centre_translation_id feature_selected
---------- --------------------- ----------------
1 1 1
2 1 1
5 1 1
10 1 1
11 1 1
snip..
What I want to see is all of the features by centre translation.
Combining the tables gives me:
SELECT
*
FROM
#SmartFeaturesToUpdate new_features
LEFT JOIN Feature feature ON feature.id = new_features.feature_id
ORDER BY
new_features.centre_translation_id,
new_features.feature_id,
new_features.feature_selected
feature_id centre_translation_id feature_selected id name
---------- --------------------- ---------------- -- ----
1 1 1 1 24 Hour Access
2 1 1 2 24 hour CCTV monitoring
5 1 1 5 Business Lounge
10 1 1 10 Double Glazing
11 1 1 11 Elevator
snip..
The result above is missing feature id's 3 and 4, because they are not in the second list.
but the result I need is:
feature_id centre_translation_id feature_selected id name
---------- --------------------- ---------------- -- ----
1 1 1 1 24 Hour Access
2 1 1 2 24 hour CCTV monitoring
3 1 1 3 Airport Location
4 1 1 4 Break-Out Area
5 1 1 5 Business Lounge
snip..
How should I modify the third SELECT statement to acheive this and combine the results from both the features and feature information list?
As the comments alluded, I needed another table which linked Features to centre_translation_ids
First get all of the feature / centre_translation varients
SELECT
[centre_translation_id] = centre_translation.id,
feature.id,
feature.name
INTO #AllTheFeatures
FROM
CentreTranslation centre_translation
CROSS JOIN Feature feature
ORDER BY
centre_translation.id,
feature.id
Now we can simply perform the LEFT JOIN
SELECT
all_features.centre_translation_id,
all_features.id,
all_features.name,
smart_features.feature_selected
FROM
#AllTheFeatures all_features
LEFT JOIN #SmartFeaturesToUpdate smart_features ON smart_features.centre_translation_id = all_features.centre_translation_id AND
smart_features.feature_id = all_features.id
ORDER BY
all_features.centre_translation_id,
all_features.id
This gives the results:
centre_translation_id id name feature_selected
--------------------- -- ---- ----------------
1 1 24 Hour Access 1
1 2 24 hour CCTV monitoring 1
1 3 Airport location NULL
1 4 Break-Out Areas NULL
1 5 Business Lounge 1
Why don't you just put it in one query?
SELECT
centre_translation.id AS centre_translation_id,
feature.id,
feature.name,
smart_features.feature_selected
FROM
CentreTranslation centre_translation
CROSS JOIN Feature feature
LEFT JOIN #SmartFeaturesToUpdate smart_features
ON smart_features.centre_translation_id = all_features.centre_translation_id
AND smart_features.feature_id = all_features.id
ORDER BY
centre_translation.centre_translation_id,
feature.id