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 Query help

SQL Query help

Scheduled Pinned Locked Moved Database
databasehelp
5 Posts 5 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.
  • P Offline
    P Offline
    picazo
    wrote on last edited by
    #1

    Hi, I have tables:

    Fruits:
    FruitID | FruitName

    1 | Apple
    2 | Orange
    3 | Pear

    Baskets:
    Basket | FruitID | Amount

    Bask1 | 1 | 3
    Bask2 | 1 | 5
    Bask3 | 1 | 2
    Bask1 | 1 | 1
    Bask2 | 1 | 2
    Bask1 | 3 | 2
    Bask3 | 3 | 4

    What query can I use to get the following table:

    Basket | Apple | Orange| Pear

    Bask1 | 3 | 1 | 2
    Bask2 | 5 | 2 | NULL
    Bask3 | 2 | NULL | 4

    Thanks in advance,

    ----------------- Genaro

    A K I E 4 Replies Last reply
    0
    • P picazo

      Hi, I have tables:

      Fruits:
      FruitID | FruitName

      1 | Apple
      2 | Orange
      3 | Pear

      Baskets:
      Basket | FruitID | Amount

      Bask1 | 1 | 3
      Bask2 | 1 | 5
      Bask3 | 1 | 2
      Bask1 | 1 | 1
      Bask2 | 1 | 2
      Bask1 | 3 | 2
      Bask3 | 3 | 4

      What query can I use to get the following table:

      Basket | Apple | Orange| Pear

      Bask1 | 3 | 1 | 2
      Bask2 | 5 | 2 | NULL
      Bask3 | 2 | NULL | 4

      Thanks in advance,

      ----------------- Genaro

      A Offline
      A Offline
      albCode
      wrote on last edited by
      #2

      select fruits.*,baskets.* from fruits,baskets where fruits.FruitID = Baskets.FruitID

      1 Reply Last reply
      0
      • P picazo

        Hi, I have tables:

        Fruits:
        FruitID | FruitName

        1 | Apple
        2 | Orange
        3 | Pear

        Baskets:
        Basket | FruitID | Amount

        Bask1 | 1 | 3
        Bask2 | 1 | 5
        Bask3 | 1 | 2
        Bask1 | 1 | 1
        Bask2 | 1 | 2
        Bask1 | 3 | 2
        Bask3 | 3 | 4

        What query can I use to get the following table:

        Basket | Apple | Orange| Pear

        Bask1 | 3 | 1 | 2
        Bask2 | 5 | 2 | NULL
        Bask3 | 2 | NULL | 4

        Thanks in advance,

        ----------------- Genaro

        K Offline
        K Offline
        Khawar Abbas1
        wrote on last edited by
        #3

        SELECT b.Basket AS Basket, b.Amount AS APPLE FROM Fruits INNER JOIN Baskets b ON Fruits.FruitID = b.FruitID WHERE (Fruits.FruitID = 1) SELECT c.Basket AS Basket, c.Amount AS ORANGE FROM Fruits INNER JOIN Baskets c ON Fruits.FruitID = c.FruitID WHERE (Fruits.FruitID = 2) SELECT d.Basket AS Basket, d.Amount AS PEAR FROM Fruits INNER JOIN Baskets d ON Fruits.FruitID = d.FruitID WHERE (Fruits.FruitID = 3) This will not give u the exact solution but will give you the partial solution.

        Do good and have good.

        1 Reply Last reply
        0
        • P picazo

          Hi, I have tables:

          Fruits:
          FruitID | FruitName

          1 | Apple
          2 | Orange
          3 | Pear

          Baskets:
          Basket | FruitID | Amount

          Bask1 | 1 | 3
          Bask2 | 1 | 5
          Bask3 | 1 | 2
          Bask1 | 1 | 1
          Bask2 | 1 | 2
          Bask1 | 3 | 2
          Bask3 | 3 | 4

          What query can I use to get the following table:

          Basket | Apple | Orange| Pear

          Bask1 | 3 | 1 | 2
          Bask2 | 5 | 2 | NULL
          Bask3 | 2 | NULL | 4

          Thanks in advance,

          ----------------- Genaro

          I Offline
          I Offline
          i j russell
          wrote on last edited by
          #4

          This is the standard way of doing a Crosstab/PIVOT in SQL. SELECT Basket, SUM(CASE FruitID WHEN 1 THEN Amount ELSE 0 END) AS Apple, SUM(CASE FruitID WHEN 2 THEN Amount ELSE 0 END) AS Orange, SUM(CASE FruitID WHEN 3 THEN Amount ELSE 0 END) AS Pear FROM Baskets GROUP BY Basket However, as you can see, you need to know in advance what the columns are. There is a new PIVOT command in SQL Server 2005 that may extend this functionality. The other way is to de-normalise the data using SELECT b.basket, f.FruitName, b.Amount FROM Baskets b INNER JOIN Fruit f on b.FruitID = f.FruitID and import it into Excel where you can use the Pivot tool to do the crosstabbing for you. Ian

          1 Reply Last reply
          0
          • P picazo

            Hi, I have tables:

            Fruits:
            FruitID | FruitName

            1 | Apple
            2 | Orange
            3 | Pear

            Baskets:
            Basket | FruitID | Amount

            Bask1 | 1 | 3
            Bask2 | 1 | 5
            Bask3 | 1 | 2
            Bask1 | 1 | 1
            Bask2 | 1 | 2
            Bask1 | 3 | 2
            Bask3 | 3 | 4

            What query can I use to get the following table:

            Basket | Apple | Orange| Pear

            Bask1 | 3 | 1 | 2
            Bask2 | 5 | 2 | NULL
            Bask3 | 2 | NULL | 4

            Thanks in advance,

            ----------------- Genaro

            E Offline
            E Offline
            Eric Dahlvang
            wrote on last edited by
            #5

            You could make a Stored Procedure that does this:

            DECLARE @iColumns INT, @sql VARCHAR(2500),@cFruitName VARCHAR(10),@cFruitID varchar(10)

            SET @sql = ''

            DECLARE curFruits CURSOR FOR SELECT DISTINCT FruitID,FruitName FROM Fruits
            OPEN curFruits
            FETCH NEXT FROM curFruits INTO @cFruitID,@cFruitName
            WHILE @@FETCH_STATUS = 0
            BEGIN
            SET @sql = @sql + ', SUM(CASE FruitID WHEN ' + @cFruitID + ' THEN Amount ELSE 0 END) as [' + @cFruitName + ']'
            FETCH NEXT FROM curFruits INTO @cFruitID,@cFruitName
            END

            close curFruits
            DEALLOCATE curFruits

            SET @sql = 'SELECT Basket' + @sql + ' FROM Baskets GROUP BY Basket'
            print @sql
            exec(@sql)

            --EricDV Sig--------- Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peters

            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