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. querying all items... the best method? [modified]

querying all items... the best method? [modified]

Scheduled Pinned Locked Moved Database
questiondatabasehelp
8 Posts 3 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.
  • G Offline
    G Offline
    gethomast
    wrote on last edited by
    #1

    I have problems on quering or better numerating all items from a specified table. Here are my tables (Third normal form): MailTable: Id, Created, Subject AddressTable: Id, Name SenderTable: MailTableId, AddressTableId RecipientTable: MailTableId, AddressTableId, State Now i want to make a query to numerate alle senders and recipients for one mail. My query: select m.Id, m.Created, m.Subject, sender.Name, recipient.Name from MailTable as m join SenderTable as s on s.MailTableId = m.Id join AddressTable as sender on sender.Id = s.AddressTableId join RecipientTable as r on s.MailTableId = m.Id join AddressTable as recipient on recipient.Id = r.AddressTableId The result: 1 | 2008-01-01 | Test | sender1@domain.tld | recipient1@domain.tld 1 | 2008-01-01 | Test | sender1@domain.tld | recipient2@domain.tld 2 | 2008-01-02 | Test | sender3@domain.tld | recipient3@domain.tld etc. My target: 1 | 2008-01-01 | Test | sender1@domain.tld | recipient1@domain.tld,recipient2@domain.tld 2 | 2008-01-02 | Test | sender3@domain.tld | recipient3@domain.tld How do I achive this, whithout using time-consuming queries? The bad way is to lookup for every mail all recipients, but this is not the best way! Should I compare all records and extract my desired information? Please Help me. Thanks!

    modified on Tuesday, July 22, 2008 5:09 AM

    M L 2 Replies Last reply
    0
    • G gethomast

      I have problems on quering or better numerating all items from a specified table. Here are my tables (Third normal form): MailTable: Id, Created, Subject AddressTable: Id, Name SenderTable: MailTableId, AddressTableId RecipientTable: MailTableId, AddressTableId, State Now i want to make a query to numerate alle senders and recipients for one mail. My query: select m.Id, m.Created, m.Subject, sender.Name, recipient.Name from MailTable as m join SenderTable as s on s.MailTableId = m.Id join AddressTable as sender on sender.Id = s.AddressTableId join RecipientTable as r on s.MailTableId = m.Id join AddressTable as recipient on recipient.Id = r.AddressTableId The result: 1 | 2008-01-01 | Test | sender1@domain.tld | recipient1@domain.tld 1 | 2008-01-01 | Test | sender1@domain.tld | recipient2@domain.tld 2 | 2008-01-02 | Test | sender3@domain.tld | recipient3@domain.tld etc. My target: 1 | 2008-01-01 | Test | sender1@domain.tld | recipient1@domain.tld,recipient2@domain.tld 2 | 2008-01-02 | Test | sender3@domain.tld | recipient3@domain.tld How do I achive this, whithout using time-consuming queries? The bad way is to lookup for every mail all recipients, but this is not the best way! Should I compare all records and extract my desired information? Please Help me. Thanks!

      modified on Tuesday, July 22, 2008 5:09 AM

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      shouteye wrote:

      whithout using time-consuming queries?

      You can't You are attempting to concat multiple records into 1, it will take at least 2 queries to achieve this.

      Never underestimate the power of human stupidity RAH

      G 1 Reply Last reply
      0
      • M Mycroft Holmes

        shouteye wrote:

        whithout using time-consuming queries?

        You can't You are attempting to concat multiple records into 1, it will take at least 2 queries to achieve this.

        Never underestimate the power of human stupidity RAH

        G Offline
        G Offline
        gethomast
        wrote on last edited by
        #3

        Thank you! It's a major problem, or my database design? I've decided to use this but limiting to 100 records select top(100) m.Id, m.Created, m.Subject, ( SELECT ai.Name + ';' AS [text()] FROM SenderTable as s JOIN AddressTable AS at ON at.Id = s.AddressTableId WHERE s.MailTableId = m.Id FOR XML PATH('') ) as senders, ( SELECT ai.Name + ';' AS [text()] FROM RecipientTable as r JOIN AddressTable AS ai ON ai.Id = r.AddressTableId WHERE r.MailTableId = m.Id FOR XML PATH('') ) AS recipients FROM MailTable AS m Is this method better as using cursors?

        modified on Tuesday, July 22, 2008 8:24 AM

        1 Reply Last reply
        0
        • G gethomast

          I have problems on quering or better numerating all items from a specified table. Here are my tables (Third normal form): MailTable: Id, Created, Subject AddressTable: Id, Name SenderTable: MailTableId, AddressTableId RecipientTable: MailTableId, AddressTableId, State Now i want to make a query to numerate alle senders and recipients for one mail. My query: select m.Id, m.Created, m.Subject, sender.Name, recipient.Name from MailTable as m join SenderTable as s on s.MailTableId = m.Id join AddressTable as sender on sender.Id = s.AddressTableId join RecipientTable as r on s.MailTableId = m.Id join AddressTable as recipient on recipient.Id = r.AddressTableId The result: 1 | 2008-01-01 | Test | sender1@domain.tld | recipient1@domain.tld 1 | 2008-01-01 | Test | sender1@domain.tld | recipient2@domain.tld 2 | 2008-01-02 | Test | sender3@domain.tld | recipient3@domain.tld etc. My target: 1 | 2008-01-01 | Test | sender1@domain.tld | recipient1@domain.tld,recipient2@domain.tld 2 | 2008-01-02 | Test | sender3@domain.tld | recipient3@domain.tld How do I achive this, whithout using time-consuming queries? The bad way is to lookup for every mail all recipients, but this is not the best way! Should I compare all records and extract my desired information? Please Help me. Thanks!

          modified on Tuesday, July 22, 2008 5:09 AM

          L Offline
          L Offline
          leoinfo
          wrote on last edited by
          #4

          ( You are using SQL2005, right? :) ) Try this:

          ;WITH RecipientsByMail (MailId, RecipientList) AS (
          select mx.Id AS MailId ,
          ( select AddressTable.Name+','
          from MailTable
          join RecipientTable on RecipientTable.MailTableId = MailTable.Id
          join AddressTable on AddressTable.Id = RecipientTable.AddressTableId
          WHERE MailTable.Id = mx.Id
          FOR XML PATH('')
          ) AS RecipientList
          from MailTable as mx
          )
          select m.Id, m.Created, m.Subject, sender.Name, r.RecipientList
          from MailTable as m
          join SenderTable as s on s.MailTableId = m.Id
          join AddressTable as sender on sender.Id = s.AddressTableId
          join RecipientsByMail as r on r.MailId = m.Id ;

          Please... SAVE my time by rating the posts that you read!


          There are 10 kinds of people in the world: those who understand binary and those who don't.

          M 2 Replies Last reply
          0
          • L leoinfo

            ( You are using SQL2005, right? :) ) Try this:

            ;WITH RecipientsByMail (MailId, RecipientList) AS (
            select mx.Id AS MailId ,
            ( select AddressTable.Name+','
            from MailTable
            join RecipientTable on RecipientTable.MailTableId = MailTable.Id
            join AddressTable on AddressTable.Id = RecipientTable.AddressTableId
            WHERE MailTable.Id = mx.Id
            FOR XML PATH('')
            ) AS RecipientList
            from MailTable as mx
            )
            select m.Id, m.Created, m.Subject, sender.Name, r.RecipientList
            from MailTable as m
            join SenderTable as s on s.MailTableId = m.Id
            join AddressTable as sender on sender.Id = s.AddressTableId
            join RecipientsByMail as r on r.MailId = m.Id ;

            Please... SAVE my time by rating the posts that you read!


            There are 10 kinds of people in the world: those who understand binary and those who don't.

            M Offline
            M Offline
            Mycroft Holmes
            wrote on last edited by
            #5

            Cute - you may be able to drop the xml, I often concat fields into CSV strings as variables, I had not thought of using a subselect. I will have to play with htis in the morning.

            Never underestimate the power of human stupidity RAH

            L 1 Reply Last reply
            0
            • M Mycroft Holmes

              Cute - you may be able to drop the xml, I often concat fields into CSV strings as variables, I had not thought of using a subselect. I will have to play with htis in the morning.

              Never underestimate the power of human stupidity RAH

              L Offline
              L Offline
              leoinfo
              wrote on last edited by
              #6

              I wonder why everyone runs away from XML... :) Let me know if you find a more elegant way. Maybe you have also time to run some performance tests ;)

              Please... SAVE my time by rating the posts that you read!


              There are 10 kinds of people in the world: those who understand binary and those who don't.

              M 1 Reply Last reply
              0
              • L leoinfo

                I wonder why everyone runs away from XML... :) Let me know if you find a more elegant way. Maybe you have also time to run some performance tests ;)

                Please... SAVE my time by rating the posts that you read!


                There are 10 kinds of people in the world: those who understand binary and those who don't.

                M Offline
                M Offline
                Mycroft Holmes
                wrote on last edited by
                #7

                I don't object to xml - just the removal/simplification of any script. I ran across an article using XML to pass recordsets into a function - what an excellent use of a technology. So for mae at least it is whatever does the job.

                Never underestimate the power of human stupidity RAH

                1 Reply Last reply
                0
                • L leoinfo

                  ( You are using SQL2005, right? :) ) Try this:

                  ;WITH RecipientsByMail (MailId, RecipientList) AS (
                  select mx.Id AS MailId ,
                  ( select AddressTable.Name+','
                  from MailTable
                  join RecipientTable on RecipientTable.MailTableId = MailTable.Id
                  join AddressTable on AddressTable.Id = RecipientTable.AddressTableId
                  WHERE MailTable.Id = mx.Id
                  FOR XML PATH('')
                  ) AS RecipientList
                  from MailTable as mx
                  )
                  select m.Id, m.Created, m.Subject, sender.Name, r.RecipientList
                  from MailTable as m
                  join SenderTable as s on s.MailTableId = m.Id
                  join AddressTable as sender on sender.Id = s.AddressTableId
                  join RecipientsByMail as r on r.MailId = m.Id ;

                  Please... SAVE my time by rating the posts that you read!


                  There are 10 kinds of people in the world: those who understand binary and those who don't.

                  M Offline
                  M Offline
                  Mycroft Holmes
                  wrote on last edited by
                  #8

                  Nope - the idea I had didn't work, the only other way I could get at this was to use a function which is even uglier!

                  Never underestimate the power of human stupidity RAH

                  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