Solved

SQL Query syntax help

Posted on 2015-02-13
9
70 Views
Last Modified: 2015-02-18
Hello all,

I have the following query:

select MV.MVDate, DE.DataEntryID,
            CU.ISOCurrencyCode
from [Customer].[snp].[MarketValueSnap] MV
inner join [Customer].[dbo].[cbiCurrency] CU ON MV.CurrencyID = CU.CurrencyID
inner join [Customer].[dbo].[cbiDataEntry] DE ON MV.DataEntryID = DE.DataEntryID

I then have a table called:  docNote

In this table it has the following fields:
NoteID,
EntityID,
InstanceID,
Note

What I need to do somehow is get the Note from this table where EntityID = 9 (DataEntry) and InstanceID = DE.DataEntryID from my query.   I only want to get the 1st note just in case there is more than one to protect myself.   What is the best way to handle this?
0
Comment
Question by:sbornstein2
9 Comments
 
LVL 47

Expert Comment

by:Vitor Montalvão
ID: 40607876
Depends on which note do you want. The first one? The last one?
0
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40607878
with myTable as (
select MV.MVDate, DE.DataEntryID,
            CU.ISOCurrencyCode
from [Customer].[snp].[MarketValueSnap] MV
inner join [Customer].[dbo].[cbiCurrency] CU ON MV.CurrencyID = CU.CurrencyID
inner join [Customer].[dbo].[cbiDataEntry] DE ON MV.DataEntryID = DE.DataEntryID
)
Select top(1) Note
From docNote as D
inner join myTable as M on D.InstanceID = M.DataEntryID
where D.EntityID = 9
0
 
LVL 13

Expert Comment

by:LIONKING
ID: 40607917
Just taking what Phillip did, and switching some things around:

with myTable as (
Select D.EntityID, MAX(Note) AS Note
From docNote as D
where D.EntityID = 9
group by D.EntityID
)
select MV.MVDate, DE.DataEntryID,
             CU.ISOCurrencyCode,
myTable.Note
 from [Customer].[snp].[MarketValueSnap] MV
 inner join [Customer].[dbo].[cbiCurrency] CU ON MV.CurrencyID = CU.CurrencyID
 inner join [Customer].[dbo].[cbiDataEntry] DE ON MV.DataEntryID = DE.DataEntryID
inner join myTable ON DE.DataEntryID = myTable.EntityID

Open in new window


Now you just have to tweak the CTE query to get the note that you really want.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 40608238
select MV.MVDate, DE.DataEntryID,
             CU.ISOCurrencyCode,
             dn.Note
from [Customer].[snp].[MarketValueSnap] MV
inner join [Customer].[dbo].[cbiCurrency] CU ON MV.CurrencyID = CU.CurrencyID
inner join [Customer].[dbo].[cbiDataEntry] DE ON MV.DataEntryID = DE.DataEntryID
cross apply (
    select top (1) Note
    from [Customer].[dbo].[docNote]
    where
         EntityID = 9 and
         InstanceID = DE.DataEntryID
    order by NoteID --desc
) as dn
0
 

Author Comment

by:sbornstein2
ID: 40608365
I like the cross apply but it does not bring back records if there is no note.
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 40608369
OOPS, sorry, quite right, I should have used
OUTER APPLY
rather than CA.
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 500 total points
ID: 40608371
...
outer apply (
     select top (1) Note
     from [Customer].[dbo].[docNote]
     where
          EntityID = 9 and
          InstanceID = DE.DataEntryID
     order by NoteID --desc
 ) as dn
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40609027
perhaps the use of a derived table using row_number() to get the latest note
SELECT
      MV.MVDate
    , DE.DataEntryID
    , CU.ISOCurrencyCode
    , DN.Note
FROM [Customer].[snp].[MarketValueSnap] MV
INNER JOIN [Customer].[dbo].[cbiCurrency] CU ON MV.CurrencyID = CU.CurrencyID
INNER JOIN [Customer].[dbo].[cbiDataEntry] DE ON MV.DataEntryID = DE.DataEntryID
LEFT JOIN (
            SELECT
                  InstanceID
                , Note
                , ROW_NUMBER() OVER (PARTITION BY InstanceID 
                                      ORDER BY NoteID DESC /* for latest */) AS RN
            FROM [Customer].[dbo].[docNote]
            WHERE EntityID = 9
      ) AS DN ON DE.DataEntryID = DN.InstanceID AND DN.RN = 1
;

Open in new window

0
 

Author Closing Comment

by:sbornstein2
ID: 40617752
this worked perfect and nice and clean thanks
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to search for strings inside db views 4 28
Sql Server group by 10 29
replace \ by - in select 4 21
Syntax for query to update table 2 14
I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

803 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