Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Extract Data in Single Record

Extract Data in Single Record

Scheduled Pinned Locked Moved Database
sharepointhelptutorial
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mrfalk
    wrote on last edited by
    #1

    Need to write extract file providing 2 employee emergency contacts with one phone number each in one record. Going to order by emergency contact name when selecting which contact and sort by phone type when selecting phone. I'm at a loss at how to do this and would appreciate any help Fields in Row: EmpNum EmergencyContact1 Relation1 PhType1 Phone1 EmergencyContact2 Relation2 PhType2 Phone2

    EmpNum EmpEmergencyContact Relation PhType Phone
    123456 Rick Grimes SP Cell 9999999999
    123456 Rick Grimes SP Work 8888888888
    123456 Daryl Dixon FR Cell 7777777777
    123456 Daryl Dixon FR Home 3333333333
    123456 Maggie Greene CH Cell 5555555555
    123456 Maggie Greene CH Home 6666666666

    D Richard DeemingR P 3 Replies Last reply
    0
    • M mrfalk

      Need to write extract file providing 2 employee emergency contacts with one phone number each in one record. Going to order by emergency contact name when selecting which contact and sort by phone type when selecting phone. I'm at a loss at how to do this and would appreciate any help Fields in Row: EmpNum EmergencyContact1 Relation1 PhType1 Phone1 EmergencyContact2 Relation2 PhType2 Phone2

      EmpNum EmpEmergencyContact Relation PhType Phone
      123456 Rick Grimes SP Cell 9999999999
      123456 Rick Grimes SP Work 8888888888
      123456 Daryl Dixon FR Cell 7777777777
      123456 Daryl Dixon FR Home 3333333333
      123456 Maggie Greene CH Cell 5555555555
      123456 Maggie Greene CH Home 6666666666

      D Offline
      D Offline
      data modeling guy
      wrote on last edited by
      #2

      SELECT * FROM
      (
      SELECT EMpNum "EmpNum", EmergencyContact1 "Name", Relation1 "Relation", PhType1 "Phone Type", Phone1 "Phone"
      FROM EMERGENCY_CONTACT

      UNION

      SELECT EMpNum "EmpNum", EmergencyContact2 "Name", Relation2 "Relation", PhType2 "Phone Type", Phone2 "Phone"
      FROM EMERGENCY_CONTACT

      ) EMERGENCY_CONTACTS

      ORDER BY "EmpNum", "Name", "Phone Type"

      1 Reply Last reply
      0
      • M mrfalk

        Need to write extract file providing 2 employee emergency contacts with one phone number each in one record. Going to order by emergency contact name when selecting which contact and sort by phone type when selecting phone. I'm at a loss at how to do this and would appreciate any help Fields in Row: EmpNum EmergencyContact1 Relation1 PhType1 Phone1 EmergencyContact2 Relation2 PhType2 Phone2

        EmpNum EmpEmergencyContact Relation PhType Phone
        123456 Rick Grimes SP Cell 9999999999
        123456 Rick Grimes SP Work 8888888888
        123456 Daryl Dixon FR Cell 7777777777
        123456 Daryl Dixon FR Home 3333333333
        123456 Maggie Greene CH Cell 5555555555
        123456 Maggie Greene CH Home 6666666666

        Richard DeemingR Offline
        Richard DeemingR Offline
        Richard Deeming
        wrote on last edited by
        #3

        Something like this should work:

        WITH cteOrderedContacts As
        (
        SELECT
        EmpNum,
        EmpEmergencyContact,
        Relation,
        PhType,
        Phone,

            -- Generate a row number for each employee, ordered by contact name:
            ROW\_NUMBER() OVER (PARTITION BY EmpNum ORDER BY EmpEmergencyContact) As RN
        FROM
            SourceData
        

        )
        SELECT
        C1.EmpNum,
        C1.EmpEmergencyContact As EmergencyContact1,
        C1.Relation As Relation1,
        C1.PhType As PhType1,
        C1.Phone As Phone1,
        C2.EmpEmergencyContact As EmergencyContact2,
        C2.Relation As Relation2,
        C2.PhType As PhType2,
        C2.Phone As Phone2
        FROM
        cteOrderedContacts As C1
        LEFT JOIN cteOrderedContacts As C2
        ON C2.EmpNum = C1.EmpNum -- Record must be for the same employee;
        And C2.RN = C1.RN + 1 -- Get the next record in the sequence;
        WHERE
        -- Only return odd-numbered rows, as the even-numbered rows
        -- will be included in the previous row:
        (C1.RN & 1) = 1
        ORDER BY
        C1.EmpNum,
        C1.RN
        ;

        http://www.sqlfiddle.com/#!3/21e9e/4/0[^]


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

        1 Reply Last reply
        0
        • M mrfalk

          Need to write extract file providing 2 employee emergency contacts with one phone number each in one record. Going to order by emergency contact name when selecting which contact and sort by phone type when selecting phone. I'm at a loss at how to do this and would appreciate any help Fields in Row: EmpNum EmergencyContact1 Relation1 PhType1 Phone1 EmergencyContact2 Relation2 PhType2 Phone2

          EmpNum EmpEmergencyContact Relation PhType Phone
          123456 Rick Grimes SP Cell 9999999999
          123456 Rick Grimes SP Work 8888888888
          123456 Daryl Dixon FR Cell 7777777777
          123456 Daryl Dixon FR Home 3333333333
          123456 Maggie Greene CH Cell 5555555555
          123456 Maggie Greene CH Home 6666666666

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #4

          I should hope that you have translation tables for Relation and PhType -- you can add some sort of prioritization there to better control ordering. RelationshipType ID Abbr Name Priority 1 SP Spouse 1 2 FR Friend 3 3 CH Child 2 PhoneType ID Abbr Name Priority 1 Cell Mobile 1 2 Work Office 3 3 Home Home 2 Such translation table also allow you to use referential integrity and can reduce the amount of space used by the database (not so much a concern these days). Also, it allows your application code to use an enumeration rather than hard-coded string values. Then follow Richard Deeming's advice.

          You'll never get very far if all you do is follow instructions.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups