简体   繁体   中英

List mysql row values horizontally without pivoting

In my table I have following columns

--loannumber--disbid--username--docs--
--------------------------------------
---1234567  -- 430  -- jhon   -- NCN --
-- same   ---- 425  -- sam    -- NPC --
-- same     -- 455  -- clin   -- Mis --
-- 4567891  -- 666  -- some1  -- NCN --
-- same     -- 777  -- some2  -- NPC --

and also I have a auto incremented primary key

What I want to do is filter it like this and show in datagridview

++ loannumber ++  NCN  ++  NPC  ++  MIS  ++
--   1234567  ++  430  ++  --   ++  --   ++
--    same    ++  --   ++  425  ++  --   ++
--    same    ++  --   ++  --   ++  455  ++

I tried with following sql

SELECT loannumber, 

      CASE WHEN docs='NCN'THEN 1 ELSE 0 END 'NCN',      
      CASE WHEN docs='NPC'THEN 1 ELSE 0 END 'NPC',
      CASE WHEN docs='Missing'THEN 1 ELSE 0 END 'Missing'

FROM claimloans 

but this only counts number of disbld's . I want to show them instead of count. Loannumber field is not compulsory. I'm planning to write a sproc and call it form winforms. someone please help me ?

  ++ NCN  ++  NPC   ++  MIS ++
  ++  430  ++  --   ++  --   ++
  ++  --   ++  425  ++  --   ++
  ++  --   ++  --   ++  455  ++

(this also okay !!)

This is working perfectly fine. but how can I eliminate null

SELECT loannumber, 

      CASE WHEN docs='NCN'THEN disblid END 'NCN',
      CASE WHEN docs='Missing'THEN disblid END 'Missing',
      CASE WHEN docs='NF'THEN disblid END 'NF'


FROM claimloans 

Not sure i understand you correctly, but try this:

SELECT loannumber, 
      SUM(CASE WHEN docs='NCN'THEN 1 ELSE 0 END) 'NCN',
      SUM(CASE WHEN docs='Missing'THEN 1 ELSE 0 END) 'Missing',
      SUM(CASE WHEN docs='NF'THEN 1 ELSE 0 END) 'NF'
FROM claimloans 
GROUP BY loannumber

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM