Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

T SQL LOOKUP QUERY

Posted on 2009-05-19
9
682 Views
Last Modified: 2012-08-13
Dear Experts,


       I am loading a table from a source which is also a sql server 2008 table and it has following structure with sample rows.

OrderDetailID RecognitionDate         RecognizedAmount
------------- ----------------------- ---------------------
4262695       2010-04-15 00:00:00     -0.0049
4262756       2010-04-15 00:00:00     -0.0049
4263732       2010-04-15 00:00:00     -0.0049
4263793       2010-04-15 00:00:00     -0.0049
4262389       2010-04-15 00:00:00     -0.0023
4215156       2010-04-15 00:00:00     -0.0023
4215157       2010-04-15 00:00:00     -0.0023
4215217       2010-04-15 00:00:00     -0.0023
4215218       2010-04-15 00:00:00     -0.0023
4261839       2010-04-15 00:00:00     -0.0023

i want to load only  the records which is not existing in target that means it should strictly match complete row and see whether it is exisitng in the target or not.If not then load else do not load.

Note : as this is very huge table which consist of 7 million records please provide me with optimized query.

Thanks.
0
Comment
Question by:sas77
  • 4
  • 4
9 Comments
 
LVL 37

Expert Comment

by:momi_sabag
ID: 24424043
insert into newTable
select *
from oldTable t1 left outer join newTable t2 on
t1.OrderDetailID = t2.OrderDetailID
and t1.RecognitionDate        = t2.RecognitionDate        
and t1.RecognizedAmount = t2.RecognizedAmount
0
 
LVL 26

Expert Comment

by:Chris Luttrell
ID: 24424432
The best solution if all the columns match is to use Set Logic which is what relational databases are best at.  Use the EXCEPT operator to identify the records that are not already in the table and inset them like this
INSERT INTO NewTable (OrderDetailID, RecognitionDate, RecognizedAmount)
SELECT OrderDetailID, RecognitionDate, RecognizedAmount
FROM OldTable
EXCEPT
SELECT OrderDetailID, RecognitionDate, RecognizedAmount
FROM NewTable;

Open in new window

0
 

Author Comment

by:sas77
ID: 24425476
Attached code which i am using to load fact table called fact revenue recognition, this is a incremental load and unfortunately i dont have any last_update date in source level where i can load only previous day records which should look on fact table if exisiting dont load if not load. Please provide me appropriate code which i can put in  OLEDB SOURCE IN ssis package.


Thanks.
Target Table:
 
CREATE TABLE [dbo].[FactRevenueRecognition](
	[RevenueRecognitionUID] [int] IDENTITY(1,1) NOT NULL,
	[OrderDetailKey] [int] NOT NULL,
	[OrderKey] [int] NOT NULL,
	[ProductKey] [int] NOT NULL,
	[AddressKey] [int] NOT NULL,
	[RecognitionDateKey] [int] NOT NULL,
	[RevenueRecognitionType] [char](2) NOT NULL,
	[RecognizedAmount] [smallmoney] NOT NULL,
 CONSTRAINT [RevenueRecognition_Fact_pk] PRIMARY KEY CLUSTERED 
(
	[RevenueRecognitionUID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
 
GO
 
 
Source Query:
 
SELECT 
ISNULL(RR.ORDERDETAILID,'0') AS OrderDetailID,
ISNULL(O.OrderID,'0') AS OrderID,
ISNULL(P.PRODUCTID,'0') AS ProductID,
ISNULL(A.AddressID,'0') AS AddressID,
ISNULL(Production.dbo.TruncToDay(RR.RECOGNITIONDATE),'1990-01-01')AS RecognitionDate,
ISNULL(RRT.REVENUERECOGNITIONTYPE,'Unknown') AS RevenueRecognitionType,
ISNULL(RR.RECOGNIZEDAMOUNT,'0.00')AS RecognizedAmount
FROM
Production.dbo.REVENUERECOGNITION AS RR LEFT JOIN Production.dbo.ORDERDETAIL OD
ON RR.ORDERDETAILID = OD.ORDERDETAILID
LEFT JOIN 
Production.dbo.PRODUCT AS P ON OD.PRODUCTID = P.PRODUCTID
LEFT JOIN
Production.dbo.Orders AS O  ON OD.ORDERID = O.ORDERID
LEFT JOIN 
Production.dbo.OrderAddress AS OA ON O.ORDERID = OA.ORDERID
LEFT JOIN
Production.dbo.ADDRESS  AS A ON  OA.ADDRESSID = A.ADDRESSID
LEFT JOIN
Production.dbo.SEMINARINFO AS SI ON P.PRODUCTID = SI.PRODUCTID
LEFT JOIN 
Production.dbo.REVENUERECOGNITIONTYPE RRT  ON P.REVENUERECOGNITIONTYPE = RRT.REVENUERECOGNITIONTYPE

Open in new window

0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
LVL 26

Expert Comment

by:Chris Luttrell
ID: 24425793
This should work logically but it would be much better if you could that last update date into your source table to avoid having to do this.
INSERT INTO dbo.FactRevenueRecognition
SELECT 
ISNULL(RR.ORDERDETAILID,'0') AS OrderDetailID,
ISNULL(O.OrderID,'0') AS OrderID,
ISNULL(P.PRODUCTID,'0') AS ProductID,
ISNULL(A.AddressID,'0') AS AddressID,
ISNULL(Production.dbo.TruncToDay(RR.RECOGNITIONDATE),'1990-01-01')AS RecognitionDate,
ISNULL(RRT.REVENUERECOGNITIONTYPE,'Unknown') AS RevenueRecognitionType,
ISNULL(RR.RECOGNIZEDAMOUNT,'0.00')AS RecognizedAmount
FROM
Production.dbo.REVENUERECOGNITION AS RR LEFT JOIN Production.dbo.ORDERDETAIL OD
ON RR.ORDERDETAILID = OD.ORDERDETAILID
LEFT JOIN 
Production.dbo.PRODUCT AS P ON OD.PRODUCTID = P.PRODUCTID
LEFT JOIN
Production.dbo.Orders AS O  ON OD.ORDERID = O.ORDERID
LEFT JOIN 
Production.dbo.OrderAddress AS OA ON O.ORDERID = OA.ORDERID
LEFT JOIN
Production.dbo.ADDRESS  AS A ON  OA.ADDRESSID = A.ADDRESSID
LEFT JOIN
Production.dbo.SEMINARINFO AS SI ON P.PRODUCTID = SI.PRODUCTID
LEFT JOIN 
Production.dbo.REVENUERECOGNITIONTYPE RRT  ON P.REVENUERECOGNITIONTYPE = RRT.REVENUERECOGNITIONTYPE
EXCEPT
SELECT 	OrderDetailKey,
	OrderKey,
	ProductKey,
	AddressKey,
	RecognitionDateKey,
	RevenueRecognitionType,
	RecognizedAmount
FROM dbo.FactRevenueRecognition

Open in new window

0
 

Author Comment

by:sas77
ID: 24425992
Dear CGLuttrell:

 That query is taking long time as it should look up on 7 million records can you please provide me any other way of doing this?
0
 
LVL 26

Expert Comment

by:Chris Luttrell
ID: 24426406
If RR.RECOGNITIONDATE in your Production.dbo.REVENUERECOGNITION table is always increasing (you would never insert an older date or update existing data) they you could try something like below for the selection of new values to insert in leu of having a last_updated_date column.  You have not given exact names of the RecognitionDateKey Dimension table or columns so those are my best guesses.
SELECT 
ISNULL(RR.ORDERDETAILID,'0') AS OrderDetailID,
ISNULL(O.OrderID,'0') AS OrderID,
ISNULL(P.PRODUCTID,'0') AS ProductID,
ISNULL(A.AddressID,'0') AS AddressID,
ISNULL(Production.dbo.TruncToDay(RR.RECOGNITIONDATE),'1990-01-01')AS RecognitionDate,
ISNULL(RRT.REVENUERECOGNITIONTYPE,'Unknown') AS RevenueRecognitionType,
ISNULL(RR.RECOGNIZEDAMOUNT,'0.00')AS RecognizedAmount
FROM
Production.dbo.REVENUERECOGNITION AS RR LEFT JOIN Production.dbo.ORDERDETAIL OD
ON RR.ORDERDETAILID = OD.ORDERDETAILID
LEFT JOIN 
Production.dbo.PRODUCT AS P ON OD.PRODUCTID = P.PRODUCTID
LEFT JOIN
Production.dbo.Orders AS O  ON OD.ORDERID = O.ORDERID
LEFT JOIN 
Production.dbo.OrderAddress AS OA ON O.ORDERID = OA.ORDERID
LEFT JOIN
Production.dbo.ADDRESS  AS A ON  OA.ADDRESSID = A.ADDRESSID
LEFT JOIN
Production.dbo.SEMINARINFO AS SI ON P.PRODUCTID = SI.PRODUCTID
LEFT JOIN 
Production.dbo.REVENUERECOGNITIONTYPE RRT  ON P.REVENUERECOGNITIONTYPE = RRT.REVENUERECOGNITIONTYPE
WHERE RR.RECOGNITIONDATE > (SELECT max(RecongnitionDateKeyValue) from RecognitionDateKeyDimensionTable)

Open in new window

0
 

Author Comment

by:sas77
ID: 24426610
Attached is the schema diagram  and the query i am using to populate fact table revenue recognition, recognized date will look up on dimdate and get datekey load as recognitiondatekey.

Please let me know if you need anything
RR-SCHEMA.pdf
0
 
LVL 26

Accepted Solution

by:
Chris Luttrell earned 500 total points
ID: 24427455
ok, with those tables and columns, try this for the where clause to limit the select in the insert statement.
First try just this select to make sure it returns fast with what you would expect to be the greatest date in the Fact table right now.
SELECT max(DD.Date) from FactRevenueRecognition RR inner join DimDate DD on RR.ReccongitionDateKey = DD.DateKey
SELECT 
ISNULL(RR.ORDERDETAILID,'0') AS OrderDetailID,
ISNULL(O.OrderID,'0') AS OrderID,
ISNULL(P.PRODUCTID,'0') AS ProductID,
ISNULL(A.AddressID,'0') AS AddressID,
ISNULL(Production.dbo.TruncToDay(RR.RECOGNITIONDATE),'1990-01-01')AS RecognitionDate,
ISNULL(RRT.REVENUERECOGNITIONTYPE,'Unknown') AS RevenueRecognitionType,
ISNULL(RR.RECOGNIZEDAMOUNT,'0.00')AS RecognizedAmount
FROM
Production.dbo.REVENUERECOGNITION AS RR LEFT JOIN Production.dbo.ORDERDETAIL OD
ON RR.ORDERDETAILID = OD.ORDERDETAILID
LEFT JOIN 
Production.dbo.PRODUCT AS P ON OD.PRODUCTID = P.PRODUCTID
LEFT JOIN
Production.dbo.Orders AS O  ON OD.ORDERID = O.ORDERID
LEFT JOIN 
Production.dbo.OrderAddress AS OA ON O.ORDERID = OA.ORDERID
LEFT JOIN
Production.dbo.ADDRESS  AS A ON  OA.ADDRESSID = A.ADDRESSID
LEFT JOIN
Production.dbo.SEMINARINFO AS SI ON P.PRODUCTID = SI.PRODUCTID
LEFT JOIN 
Production.dbo.REVENUERECOGNITIONTYPE RRT  ON P.REVENUERECOGNITIONTYPE = RRT.REVENUERECOGNITIONTYPE
WHERE RR.RECOGNITIONDATE > (SELECT max(DD.Date) from FactRevenueRecognition RR inner join DimDate DD on RR.ReccongitionDateKey = DD.DateKey )

Open in new window

0
 

Author Comment

by:sas77
ID: 24496255
Thanks
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Query to return total 6 21
Challenging SQL Update 5 49
MS SQL 2008 Divide by zero error encountered. Error 3 45
(sql serv16)ssis 2016 question/check 1 68
There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question