I need to show grouped id and only the max order value for each unique id

select distinct 
Table1.id,
Table1.id +' - '+ Table1.VisitNumber +' : '+ Table1.Priority as UidVisitKey,
Table1.VisitNumber,
DATEDIFF(d, [dob],[Visite_dte])/365.25 as Age_On_Visit,
Table1.Priority,
Table1.OrderOfVisit,
Table1.OrderOfVisit + ' - ' + Table1.Notes AS VisitNote, 
Table1.Visitor_FName,
Table1.Visitor_SName,
Table2.dob,
Table2.sex,
Table1.Visit_dte,
into #Temp1
FROM         Table1 INNER JOIN
            Table2 ON Table1.id = Table2.id
WHERE Table1.LeaveDate IS NOT NULL 
and Table1.LeaveDate  between DATEADD(mm,-1,DATEADD(mm,DATEDIFF(mm,0,GETDATE()),0)) 
and DATEADD(ms,-3,DATEADD(mm,0,DATEADD(mm,DATEDIFF(mm,0,GETDATE()),0))) 
select #Temp1.id, max(#Temp1.[OrderOfVisit]), #Temp1.VisitNote 
from #Temp1
group by #Temp1.id, #Temp1.OrderOfVisit, #Temp1.[VisitNote]
ORDER BY #Temp1.id
drop table #Temp1
---I need to show grouped id and only the max OrderOfVisit for each unique id, and the VisitNote for each OrderOfVisit
----------------need help-------------

Sounds like this
select distinct
Table1.id,
Table1.id +' - '+ Table1.VisitNumber +' : '+ Table1.Priority as UidVisitKey,
Table1.VisitNumber,
DATEDIFF(d, [dob],[Visite_dte])/365.25 as Age_On_Visit,
Table1.Priority,
Table1.OrderOfVisit,
Table1.OrderOfVisit + ' - ' + Table1.Notes AS VisitNote,
Table1.Visitor_FName,
Table1.Visitor_SName,
Table2.dob,
Table2.sex,
Table1.Visit_dte,
into #Temp1
FROM Table1 INNER JOIN
Table2 ON Table1.id = Table2.id
WHERE Table1.LeaveDate IS NOT NULL
and Table1.LeaveDate between DATEADD(mm,-1,DATEADD(mm,DATEDIFF(mm,0,GETDATE()),0))
and DATEADD(ms,-3,DATEADD(mm,0,DATEADD(mm,DATEDIFF(mm,0,GETDATE()),0)))
select id,OrderOfVisit,VisitNote
from
select #Temp1.id, #Temp1.[OrderOfVisit], #Temp1.VisitNote,ROW_NUMBER() OVER (PARTITION BY #Temp1.id ORDER BY #Temp1.[OrderOfVisit] DESC) AS Seq
from #Temp1
)t
WHERE Seq = 1
ORDER BY id
drop table #Temp1
Please Mark This As Answer if it helps to solve the issue Visakh ---------------------------- http://visakhm.blogspot.com/ https://www.facebook.com/VmBlogs

Similar Messages

  • Group by and take the Max year Record

    Hi,
    I have the data as below:
    A     B     C     D     E
    1     A     ACC     AAA     2008
    1     A     BCC     BBB     2007
    1     A     CCC     CCC     2009
    2     B     ATT     TTT     2004
    2     B     BYY     YYY     2009
    2     B     CZZ     ZZZ     2006
    Here I have to eliminate duplicates using A,B and take the record with max(E)
    So output should be:
    A     B     C     D     E
    1     A     CCC     CCC     2009
    2     B     BYY     YYY     2009
    Please help me how i can do this.
    Thanks in advance
    Mahesh

    Slightly shorter:
    SQL> with t as (
      2   select 1 a, 'A' b, 'ACC' c, 'AAA' d, '2008' e from dual union all
      3   select 1, 'A', 'BCC', 'BBB', '2007' from dual union all
      4   select 1, 'A', 'CCC', 'CCC', '2009' from dual union all
      5   select 2, 'B', 'ATT', 'TTT', '2004' from dual union all
      6   select 2, 'B', 'BYY', 'YYY', '2009' from dual union all
      7   select 2, 'B', 'CZZ', 'ZZZ', '2006 ' from dual
      8  )
      9  --
    10  --
    11  select a,
    12         max(b) keep (dense_rank last order by e) b,
    13         max(c) keep (dense_rank last order by e) c,
    14         max(d) keep (dense_rank last order by e) d,
    15         max(e) e
    16    from t
    17  group by a
    18  /
             A B C   D   E
             1 A CCC CCC 2009
             2 B BYY YYY 2009
    2 rijen zijn geselecteerd.Regards,
    Rob.

  • I need to send group email and forward group email?

    I need to send group emails and forward group emails and dont find that capability with ipad or iphone

    There are quite a few group emailing apps available, but most will not allow you to forward an email, as they can only send new emails written within their own app.
    MailShot Pro, uniquely, adds group email to your device so you can send to your groups directly from the standard Mail app. This allows you to forward an email, with any attachments intact, right from Mail.
    Find out why quarter of a milllion users are now using our MailShot apps at jo.my/mailshot (iTunes link). A free version is also available to try, limited to a few small groups of contacts.
    Peter
    Soluble
    Disclosure: I am the developer of MailShot, and may receive benefit from any sales.

  • How do I open photoshop? I have it downloaded and only the Adobe Application manager shows with the

    How do I open photoshop? I have it downloaded and only the Adobe Application manager shows with the list of other software. I clicked on photoshop, but not responding.

    Nobody can know. Your information is far too vague - no system info, no other details. Just saying that it doesn't work is not conducive to providing assistance...
    Mylenium

  • Whenevenr I call a person sometimes the phone gives me an option of callback and end when I press the callback button the whole screen goes blank and only the upper Taskbar where it shows network provider , time and battery percentage is shown

    whenevenr I call a person sometimes the phone gives me an option of callback and end when I press the callback button the whole screen goes blank and only the upper Taskbar where it shows network provider , time and battery percentage is shown

    Standard troubleshooting...
    Try a reset by pressing the home and sleep buttons until you see the Apple logo, ignoring the slider if, it comes up. Takes about 5-15 secs of button holding and you won't lose any data or settings.
    Remove all apps from Recently Used list...
    - From any Home Screen, double tap the home button to bring up the Recents List
    - Tap and hold any icon in this list until they wiggle
    - Press the red (-) to delete all apps from this list.
    - Press the home button twice when done.
    If still a problem restore with your backup.
    If still a problem restore as new, i.e. without your backup. See how it runs with nothing synced to it.
    If still a problem, it's likely a hardware issue.

  • Grouping, Pegging and Distribution (GPD) when run MRP for PS

    Dear all,
    As you know, MRP is a core of Planning Process. So when you run MRP for PS, SAP can't check stock in general stock and project stock at the same time. Now, my client have accept to run MRP base on project stock. But there are some problem when they give me requirement and actually situation in their company. One of them as below:
    They have 3 WBS element (WBS 1, WBS 2, WBS 3) and they use the same material A. Their requirement is they want to run MRP to plan material for 3 WBS.
    Solution 1: use MD51 and run MRP for Project definition and each of WBS element have respective project stock, the system will generate some of planned order for each oder reservation. After that, I will convert all of planned order to PR => PO => Good Receipt for each WBS element. When run MD04, I can see the stock for each project stock for respective WBS element.
    Ok, I will talk about the limited of this solution. I suppose after complete technical for all of order reservation in WBS 1 and in project stock of WBS 1 have 3 material A not be use. But in WBS 2 is missing 3 material A. So when I run MRP again for WBS 2 by tcode MD51, the system will generate new planned order for this missing material A. This is not logic because I have enough material A for reservation in WBS 1. So I suggest my client that you should transfer material A from WBS 2 to WBS 1, they agree with me if they don't have many WBS element. But they have about 300 WBS element and they can't do transfer posting between many many WBS element. This solution 1 is not be accept.
    Solution 2: I use GPD for this requirement. Firstly I will group WBS 1, WBS 2 and WBS 3 into WBS group. So use tcode MD04, I can see only WBS group and all of order reservation in each WBS 1, 2, 3 be showed on MD04 table. When I run MRP, the system will generate planned order for them. After that, I will conver all to PR => PO => Good receipt to WBS group. And in MD04 table, I can see project stock for WBS group. I begin transfer material from WBS group to each WBS 1, 2, 3. When all of order reservation in WBS 1 is technical complete, the material in WBS 1 have 3 material A not be use. And in WBS 2, I have a new order reservation and it be assigned 4 material A. At this time, project stock on WBS group is 0 but in WBS 1 is 3. But when I run MRP by tcode MD51, the system generate planned order with amount is 4. I wonder that why SAP does not check all material on all WBS element in WBS group to suggest a planned order with amount is 1. If SAP can solve this problem, my client will be accept this solution.
    Hoping you support me.
    Thanks, Regards
    Hoai Vu

    Hi Ahmed Rifaee
    Thanks for your reply, I will talk about process in SAP.
    So, I have 2 WBS element, that's simple because my project is have about 200 WBS element.
    WBS1 have 2 order reservation for each actvity and material A0001 be use in 2 order reservation.
    Order Reservation 1 needs 3  A0001
    Order Reservation 2 needs 5  A0001
    WBS2 have 1 order reservation for each activity and material A0001 be use in it
    Order Reservation 3 needs 4  A0001
    1.)     If I don't group 2 WBS1 and WBS2 to one WBS group, SAP will have 2 project stock for each WBS. and when I run MRP, WBS1 and WBS2 run independent base on current project stock at each WBS. So, although project stock WBS1 is run out of A0001 while A0001 still exist A0001 in project stock WBS2, SAP still generat planned order for missing part A0001 in project stock WBS1.
    2.)     If I use GPD by group WBS1 and WBS2, I can solve this problem easy because they will use only one project stock for WBS group. Ok, after I good receipt A0001 into project stock, I'm sure that I have enough A0001 for 2 WBS1 and WBS2. But I can trace which reservation be ordered to delivery to them exactly. I try to use tcode MD09 to trace from Purchase order to Order reservation but I have just to trace in case PO have not been good receipted. I wonder that Pegging can help me in this case (PO have been good receipted).
    Can you give me any idea for these problem
    Thanks, Regards
    Hoai Vu

  • The quantity received/required for each material (only sum of Dep and Foreq

    Hi all
    I have a report and I need it to show the <b>quantity received/required</b> for each material (<b>only sum of Depreq and ForReq</b>) in a plant due to MRP execution.
    Basically something like MD04 showing each MRP element entry (only sum of Depreq and ForReq)
    Do anyone knows a function, bapi, or other that can helps calculate this?
    Thanks in advance

    Hi srivalli,
    U mean that for every material u need sum of recieved quntity and required qunatity? if this is your requirement.
    Try the synax below.
    Example.
    itab - internal table of any sturcture. suppose itab  consits of materil and quantity fiedls.
    sort itab by matnr.
    loop at itab.
    At end of matnr.
    Sum.
    Write: itab-matnr, itab-menge.
    ENDAT.
    endloop.
    This syntax will display the sum of quantity for each material.
    <b>Reward if useful</b>
    Any thing please reply back if you have any other doubt.
    Regards,
    sunil kairam.

  • How to send a group email and have the recipients hidden?

    How do I send a group email and have the recipients hidden?

    I have a new OSX 27" iMac that is running Maverick. 
    RE: group emails, I am trying to omit individual recients and have tried the following options without success.
    1) Mail- Composing- checked "Automatically- Bcc"
    2) Mail- Composing- unchecked "when sending to a group, show all member addresses"
    The result is that all names continue to show up with email addresses for group emails.
    HELP!  HELP!  HELP!
    Lynn 

  • Problem to calculate the coherence (using NetworkFunction-VI) with only 1 row of data for each, the stimulus and response input

    Hello,
    I am trying to calculate the coherence of a stimulus and response
    signal using the Network Functions (avg) VI's. The problem is that I
    always get a coherence of "1" at all frequencies. This problem is
    already known (see KnowledgeBase document: Why is the Network Functions (avg) VI's Coherence Function Output "1"?).
    My trouble is that the described solution (-> the stimulus and response input matrices need to have at least two rows to get non-unity coherence values) doesn't help me much, because I only have one array of stimulus data and one array of response values.
    Thus, how can I fullfil the 'coherence-criteria' to input at least two rows of data each when I just have one row of data each?
    Any hint or idea is very much appreciated. Thanks!
    Horst

    With this weird board layout, I'm not sure whether you were asking me, but, on the assumption that you were, here goes:
    I found no need to use the cross-power spectrum and power spectrum blocks
    1... I was looking for speed.
    2... I already had the component spectral data there, for other purposes. From that, it's nothing but addition and multiplication.
    3... The "easy" VIs, assume a time wave input, as I recall. Which means they would take the same spectrum of the same timewave several times, where I only do it once.
    I have attached PNGs of my code.
    The PROCESS CHANNEL vi accepts the time wave and:
    1... Removes DC value.
    2... Integrates (optional, used for certain sensors).
    3... Windows (Hanning, etc. - optional)
    4... Finds spectrum.
    5... Removes spectral mirrors.
    6... Scales into Eng. units.
    7... From there, you COULD use COMPLEX-TO-POLAR, but I don't care about the phase data, and I need the MAG^2 data anyway, so I rolled my own COMPLEX-TO-MAG code.
    The above is done on each channel. The PROCESS DATA vi calls the above with data for each channel. The 1st channel in the list is required to be the reference (stimulus) channel.
    After looping over each channel, we have the Sxx, Syy, and Sxy terms. This code contains some averaging and peak-picking stuff that's not relevant.
    From there, it's straightforward to ger XFER = Sxy/Sxx and COHERENCE = |Sxy|^2 / (Sxx * Syy)
    Note that it uses the MAGNITUDE SQUARED of Sxy. Again, if you use the "easy" stuff, it will do a square-root operation that you just have to reverse - it is obtained faster by the sum of the squares of the real and imag parts.
    Hope this helps.
    Steve Bird
    Culverson Software - Elegant software that is a pleasure to use.
    Culverson.com
    Blog for (mostly LabVIEW) programmers: Tips And Tricks
    Attachments:
    ProcessChannel.png ‏25 KB

  • Apparently I inadverdently clicked on something that changed my address book listings to showing only the first three letters of each name -

    the column showing my address book now shows only the first three characters of each name
    it's like they have all been abbreviated
    I have to try and decipher the name from those three characters before clicking
    to see the
    person's address window with the full information
    I would like to be able to read the entire name- first & last like before
    Thank you in advance

    try dragging the column heading to make the column wider

  • How do you download all chapters of a video at once?  I purchased a yoga video with multiple chapters and only the first one loaded.  It says I can't download another chapter for 90 days.

    I purchased a yoga video with multiple chapters and only the first one loaded.  It says I can't download another chapter for 90 days.  How do I get all chapters to download?

    Once a device or computer is associated with your Apple ID, you cannot associate that device or computer with another Apple ID for 90 days.
    http://support.apple.com/kb/ht4627

  • Why does my apple id in app store settings show one thing and in the apps it shows a different id (ccl43) no email address)

    why does my apple id in app store settings show one thing and in the apps it shows a different id (ccl43) no email address) 
    I am unsure what to do I cannot get into my apps account in order to upgrade it.  HELP PLEASE!!!!!!

    It's the Apple ID that app was purchased with.

  • How do I find bad music files?  I have thousands of files, but some are bad.  When I try to load them into iTunes, it just spins.  I need a routine to identify and remove the bad files.  Any ideas?

    How do I find bad music files?  I have thousands of files, but some are bad.  When I try to load them into iTunes, it just spins.  I need a routine to identify and remove the bad files.  Any ideas?

    Hi ajoddo20,
    Welcome to the Apple Support Communities!
    You can use Home Sharing to import music and other media from one computer to another. Please use the instructions and information located in the attached article for information on how to complete the process. 
    iTunes 11 for Mac: Use Home Sharing to import items from another iTunes library
    Cheers,
    Joe 

  • TS1292 I bought a 4 pack of $25 Itunes gift cards and only 2 will activate.  For the second two it just keeps asking for my login. No error message just keeps repeating the login

    I bought a 4 pack of $25 Itunes gift cards and only 2 will activate.  For the second two it just keeps asking for my login. No error message just keeps repeating the login.  Is there any way to fix this or did I just lose $50

    Report this here:
    iTunes Store Support
    http://www.apple.com/emea/support/itunes/contact.html

  • My iPhone 5 is not charging properly and only the left speaker in any headphones is working

    Hi folks my iPhone 5 is not chagrin properly and only the left speaker on any set of headphones is working how do I report to apple

    Will not turn on, will not turn on unless connected to power, or unexpected power off
    No sound through stereo headset

Maybe you are looking for

  • Slow GDM - different issues than other posts

    Hi all, GDM is slow to start (compare to what it use to be) the issue is I can't pin point why, as I have made a lot of updates lately including moving to Grub2 and changing quite a few .pacnew today (I know I should do it right away...) I seems howe

  • Can anyone recommend a good Firewire PCi card for G5??????

    I want to expand my number of Firewire ports on my G5 dual 1.8ghz. Anyone know which is meant to be the best in terms of Compatibility, quality and price. I would like a couple of 800 and 1 more 400 preferably.

  • Oracle database normalisation and logical design documnetation

    Hi , Am a begginer on DB design , can anyone help me find Oracle database normalisation and logical design documnetation for reference. Thanks, Swaroop

  • Error in script which create a table!

    i publish a cript which create a table. at the first sync,the table is created and everything is ok. however,at the second sync,the error appear and the sync stoped! i have to delete the table created by the script and re-sync ,it's ok! it seems that

  • Photoshop Elements 9 Stopped Working - tried fresh install

    Problem signature:   Problem Event Name:          APPCRASH   Application Name:          PhotoshopElementsEditor.exe   Application Version:          9.0.0.0   Application Timestamp:          4ca3884b   Fault Module Name:          ScCore.dll   Fault Mo