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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Database & SysAdmin
  3. Database
  4. please help me with sql query (fine tune the sql query)

please help me with sql query (fine tune the sql query)

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

    please go through the below query. SELECT DR.id , case PC.id when 4 then sum(amount) end , case PC.id when 5 then sum(amount) end , case PC.id when 3 then sum(amount) end , case PC.id when 6 then sum(amount) end , case when pc.id = 2 and DP.id = 211 then sum(amount) end , case when pc.id = 2 and DP.id = 210 then sum(amount) end , case when pc.id = 2 and DP.id = 212 then sum(amount) end , case when pc.id = 2 and DP.id = 213 then sum(amount) end , case when pc.id = 2 and DP.id = 214 then sum(amount) end , case when pc.id = 2 and DP.id = 197 then sum(amount) end , case when PC.id = 1 and DP.id3 = 45 then sum(amount) end FROM table1 DR LEFT JOIN table2 DP ON DP.id1 = DR.id1 LEFT JOIN table3 PL ON DP.id2 = PL.id LEFT JOIN table4 PC ON PL.id1 = PC.id WHERE DR.date = GETDATE() group by DR.id, PC.id, DP.id, DP.id3 hope you understood my requirement. i didn't think this is the right way to do. please help any one can do this in a proper manner. thanks in advance.

    P I 2 Replies Last reply
    0
    • P poornas

      please go through the below query. SELECT DR.id , case PC.id when 4 then sum(amount) end , case PC.id when 5 then sum(amount) end , case PC.id when 3 then sum(amount) end , case PC.id when 6 then sum(amount) end , case when pc.id = 2 and DP.id = 211 then sum(amount) end , case when pc.id = 2 and DP.id = 210 then sum(amount) end , case when pc.id = 2 and DP.id = 212 then sum(amount) end , case when pc.id = 2 and DP.id = 213 then sum(amount) end , case when pc.id = 2 and DP.id = 214 then sum(amount) end , case when pc.id = 2 and DP.id = 197 then sum(amount) end , case when PC.id = 1 and DP.id3 = 45 then sum(amount) end FROM table1 DR LEFT JOIN table2 DP ON DP.id1 = DR.id1 LEFT JOIN table3 PL ON DP.id2 = PL.id LEFT JOIN table4 PC ON PL.id1 = PC.id WHERE DR.date = GETDATE() group by DR.id, PC.id, DP.id, DP.id3 hope you understood my requirement. i didn't think this is the right way to do. please help any one can do this in a proper manner. thanks in advance.

      P Offline
      P Offline
      PSK_
      wrote on last edited by
      #2

      poornas wrote:

      hope you understood my requirement.

      What is your requirement??

      Regards, Prakash Kalakoti

      1 Reply Last reply
      0
      • P poornas

        please go through the below query. SELECT DR.id , case PC.id when 4 then sum(amount) end , case PC.id when 5 then sum(amount) end , case PC.id when 3 then sum(amount) end , case PC.id when 6 then sum(amount) end , case when pc.id = 2 and DP.id = 211 then sum(amount) end , case when pc.id = 2 and DP.id = 210 then sum(amount) end , case when pc.id = 2 and DP.id = 212 then sum(amount) end , case when pc.id = 2 and DP.id = 213 then sum(amount) end , case when pc.id = 2 and DP.id = 214 then sum(amount) end , case when pc.id = 2 and DP.id = 197 then sum(amount) end , case when PC.id = 1 and DP.id3 = 45 then sum(amount) end FROM table1 DR LEFT JOIN table2 DP ON DP.id1 = DR.id1 LEFT JOIN table3 PL ON DP.id2 = PL.id LEFT JOIN table4 PC ON PL.id1 = PC.id WHERE DR.date = GETDATE() group by DR.id, PC.id, DP.id, DP.id3 hope you understood my requirement. i didn't think this is the right way to do. please help any one can do this in a proper manner. thanks in advance.

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

        SELECT DR.id,
        SUM(case PC.id when 4 then amount else 0 end) AS PCID4,
        SUM(case PC.id when 5 then amount else 0 end) AS PCID5,
        SUM(case PC.id when 3 then amount else 0 end) AS PCID3,
        SUM(case PC.id when 6 then amount else 0 end) AS PCID6,
        SUM(case when pc.id = 2 and DP.id = 211 then amount else 0 end) AS DPID211,
        SUM(case when pc.id = 2 and DP.id = 210 then amount else 0 end) AS DPID210,
        SUM(case when pc.id = 2 and DP.id = 212 then amount else 0 end) AS DPID212,
        SUM(case when pc.id = 2 and DP.id = 213 then amount else 0 end) AS DPID213,
        SUM(case when pc.id = 2 and DP.id = 214 then amount else 0 end) AS DPID214,
        SUM(case when pc.id = 2 and DP.id = 197 then amount else 0 end) AS DPID197,
        SUM(case when PC.id = 1 and DP.id3 = 45 then amount else 0 end) AS DPID45
        FROM
        table1 DR
        LEFT JOIN table2 DP
        ON DP.id1 = DR.id1
        LEFT JOIN table3 PL
        ON DP.id2 = PL.id
        LEFT JOIN table4 PC
        ON PL.id1 = PC.id
        WHERE
        DR.date = GETDATE()
        group by
        DR.id

        modified on Tuesday, March 23, 2010 11:24 AM

        C 1 Reply Last reply
        0
        • I i j russell

          SELECT DR.id,
          SUM(case PC.id when 4 then amount else 0 end) AS PCID4,
          SUM(case PC.id when 5 then amount else 0 end) AS PCID5,
          SUM(case PC.id when 3 then amount else 0 end) AS PCID3,
          SUM(case PC.id when 6 then amount else 0 end) AS PCID6,
          SUM(case when pc.id = 2 and DP.id = 211 then amount else 0 end) AS DPID211,
          SUM(case when pc.id = 2 and DP.id = 210 then amount else 0 end) AS DPID210,
          SUM(case when pc.id = 2 and DP.id = 212 then amount else 0 end) AS DPID212,
          SUM(case when pc.id = 2 and DP.id = 213 then amount else 0 end) AS DPID213,
          SUM(case when pc.id = 2 and DP.id = 214 then amount else 0 end) AS DPID214,
          SUM(case when pc.id = 2 and DP.id = 197 then amount else 0 end) AS DPID197,
          SUM(case when PC.id = 1 and DP.id3 = 45 then amount else 0 end) AS DPID45
          FROM
          table1 DR
          LEFT JOIN table2 DP
          ON DP.id1 = DR.id1
          LEFT JOIN table3 PL
          ON DP.id2 = PL.id
          LEFT JOIN table4 PC
          ON PL.id1 = PC.id
          WHERE
          DR.date = GETDATE()
          group by
          DR.id

          modified on Tuesday, March 23, 2010 11:24 AM

          C Offline
          C Offline
          Corporal Agarn
          wrote on last edited by
          #4

          I had thought about answering this as I knew something was wrong but could not think what was. i.j.russell you have hit it on the head.

          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