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. SQL For Smarties

SQL For Smarties

Scheduled Pinned Locked Moved Database
databasegame-devquestion
5 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.
  • A Offline
    A Offline
    afronaut
    wrote on last edited by
    #1

    create table TestLoan( loanid int identity(1,1), custid int, loanvalue varchar(50) ) GO INSERT INTO TestLoan values(2,'First Loan') INSERT INTO TestLoan values(2,'Second Loan') INSERT INTO TestLoan values(2,'Third Loan') INSERT INTO TestLoan values(2,'Fourth Loan') INSERT INTO TestLoan values(2,'Fifth Loan') GO Does anyone know a way I could return the 3 most recent loans in the same row? The trick would be making it always be the 3 most recent loans, no matter what else was added. *->>Always working on my game, teach me *->>something new. cout << "dav1d\n";

    E B 2 Replies Last reply
    0
    • A afronaut

      create table TestLoan( loanid int identity(1,1), custid int, loanvalue varchar(50) ) GO INSERT INTO TestLoan values(2,'First Loan') INSERT INTO TestLoan values(2,'Second Loan') INSERT INTO TestLoan values(2,'Third Loan') INSERT INTO TestLoan values(2,'Fourth Loan') INSERT INTO TestLoan values(2,'Fifth Loan') GO Does anyone know a way I could return the 3 most recent loans in the same row? The trick would be making it always be the 3 most recent loans, no matter what else was added. *->>Always working on my game, teach me *->>something new. cout << "dav1d\n";

      E Offline
      E Offline
      Edbert P
      wrote on last edited by
      #2

      Here is one that you might be able to use: DECLARE @TotalLoanValue varchar(150) SELECT TOP 3 @TotalLoanValue = COALESCE(@TotalLoanValue + ', ', '') + loanvalue FROM TestLoan ORDER BY loanid DESC SELECT @TotalLoanValue This will work with SQL 2K. I haven't tested it with any other DBs. It will return something like this: Fifth Loan, Fourth Loan, Third Loan I hope this helps :) Edbert P. Sydney, Australia.

      A 1 Reply Last reply
      0
      • E Edbert P

        Here is one that you might be able to use: DECLARE @TotalLoanValue varchar(150) SELECT TOP 3 @TotalLoanValue = COALESCE(@TotalLoanValue + ', ', '') + loanvalue FROM TestLoan ORDER BY loanid DESC SELECT @TotalLoanValue This will work with SQL 2K. I haven't tested it with any other DBs. It will return something like this: Fifth Loan, Fourth Loan, Third Loan I hope this helps :) Edbert P. Sydney, Australia.

        A Offline
        A Offline
        afronaut
        wrote on last edited by
        #3

        Wow Edbert - I'm not even sure how this works. Isn't COALESCE meant to return the first non null value? Can you explain the mechanics a bit? *->>Always working on my game, teach me *->>something new. cout << "dav1d\n";

        E 1 Reply Last reply
        0
        • A afronaut

          Wow Edbert - I'm not even sure how this works. Isn't COALESCE meant to return the first non null value? Can you explain the mechanics a bit? *->>Always working on my game, teach me *->>something new. cout << "dav1d\n";

          E Offline
          E Offline
          Edbert P
          wrote on last edited by
          #4

          Sorry, it was 9AM in the morning and I haven't had my coffee ;P Yes, COALESCE is meant to return the first non-null value from the values passed. Have a look at this code: SELECT TOP 3 @TotalLoanValue = COALESCE(@TotalLoanValue + ', ', '') + loanvalue The first time the @TotalLoanValue variable is processed, it will return NULL as the value. I use COALESCE to return empty string ('') instead, so when I concatenate the next rows, they will not result in null. You can also use ISNULL(@TotalLoanValue + ', ', '') instead of COALESCE(@TotalLoanValue + ', ', '') as they're basically doing the same thing, except that ISNULL only accepts two values. What the whole SQL statement does is just to concatenate the result of the query into one comma-delimited string. Just change the delimiter if you need to. Good luck! :-D PS: Use WHERE loanvalue IS NOT NULL if you want to always return 3 records. Edbert P. Sydney, Australia.

          1 Reply Last reply
          0
          • A afronaut

            create table TestLoan( loanid int identity(1,1), custid int, loanvalue varchar(50) ) GO INSERT INTO TestLoan values(2,'First Loan') INSERT INTO TestLoan values(2,'Second Loan') INSERT INTO TestLoan values(2,'Third Loan') INSERT INTO TestLoan values(2,'Fourth Loan') INSERT INTO TestLoan values(2,'Fifth Loan') GO Does anyone know a way I could return the 3 most recent loans in the same row? The trick would be making it always be the 3 most recent loans, no matter what else was added. *->>Always working on my game, teach me *->>something new. cout << "dav1d\n";

            B Offline
            B Offline
            Bill Dean
            wrote on last edited by
            #5

            Self joins! There are a beautiful thing when some Neanderthal wants a denormalized report from you nice orderly normalized data structure.:wtf: Try this (works on sql server. Access is ???):select top 1 * from testloan t1, testloan t2, testloan t3 where t1.loanid<>t2.loanid and t2.loanid<>t3.loanid and t1.loanid<>t3.loanid order by t1.loanid desc, t2.loanid desc, t3.loanid desc

            Bill

            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