sql - Suppress Columns with Null Values in Pivot Table -


The Hare people combine 3 tables in a pivot table to display the following queries basically in the form of the Staffrerence Code, Headers and WipAmount. . This code has been exhausted because it runs properly, though I want to suppress any column in which all fields are taped, it will be easy to read the report

Example of the current output

  -------- ejh ----- aea-- --- firm as ---- tap ---- 10.00 ---- 6.00 fm --- null ----- 5.00 ----- Tap AIA --- Null ----- Tap- --- 7.00   

For example, what do I want:

  ------- aea ----- fum ejh - 10.00- --- 6.00 fm --- 5.00 ---- blank AIA --- blank ----- 7.00 < / Code>  

Actually, if any If the value is filled with zero values, then leaving that column I have a query below. This query works and displays all the correct data

  declar @ sql varchar (MAX) DECLARE @ Insert the column table (col varchar (50)) @columnscsv varchar (MAX) in @columns; select separately from StaffCode: tblStaff @columnscsv Selection = COALESCE (@columnscsv + '], [', '') + @columns Colonel set @columnscsv = '[' + @columnscsv + ']' @sql set = 'from' StaffReference '+ @ columnscsv +' (select ocr.staffcode, em.StaffReference, WipAmount from tblTranWip w.crink.exe = w Join tblengagement e at .contindex ocr.staffindex = tblstaf at e.clientpartner Join f ocr tblstaff them at em.staffindex Join them = e ClientManager WHERE (w.WIPDate & gt; = 'ts' '2013-04-01 00:00:00' '} and W.IPDT  {ts' 2014-03-31 00:00:01 '' }) And W.TransTypeIndex = 1 and w.ContIndex & LT; 900000) in an axis ('+ @columnscsv +') (for the WipAmount StaffCode) as a Private Order by StaffReference 'EXEC (as @sql)    

You can use isnull to get rid of the null v alues ​​and just specify what you want instead of just below I I use empty space, but you can return 0 for any numerical values.

  Select from ISNULL (ocr.staffcode, ''), isnull (em.StaffReference, ''), isnull (WipAmount, '') to tblTranWip   

Edit for the requested explanation - View the comments below

then ISNULL will allow you to get rid of zero values, but it will change those values Will give

I am going to explain that the code you are using is doing it. First of all you are using dynamic SQL and it can be useful when there are some safety implications. Tithis link] is one of your favorite explanations of the curiosity and blessing of dynamic SQL. It is certainly good that you have not used it before.

This code is taking you to the columns you have identified in the table and to fill it in a string that is delimited with a comma, which will be used later in your selection statement.

  announcement @ sql varchar (MAX) DECLARE @columns table (col varchar (50)) @columnscsv varchar (MAX) @column Please select Staphkoda from tblStaff @columnscsv = COALESCE ( @columnscsv + '), [', ''] + COL to set @columns @columnscsv = '[' + @columnscsv + ']'   

Select this line The column is about to install. When you use the pivot function, you have to specify the columns you want to smoke on, and you can allow them to variable using the dynamic SQL here:

  SET @sql = 'StaffReference Select' callcive + '  

However what I'm looking for is because you have these values ​​set up steadily before you even That let your inner query run Which I am here to list, which is bound to use all of you at e.contindex = w.contindex

  select ocr.staffcode, em.StaffReference, WipAmount from tblTranWip w Join tblengagement e tblstaff OCR on OCR: regardless of those pillar names if they come back null. staffindex = e.clientpartner em.staffindex = add to tblstaff em (w.WIPDate & gt; = 'ts'' 2013-04-01 00:00:00 ''} and w.WIPDate & lt; { ts' 2014 -03-31 00:00:01 ''}) and w.TransTypeIndex = 1 and w.ContIndex & lt; 900000   

Therefore, it is as follows that if you really want to exclude empty columns, you want to be able to populate the @columns tab Le dynamically grab values ​​with only columns, which should be able to use your initial internal query which I have pasted above. If you are not getting your column value from that internal statement, we need more information to understand where they are coming from.

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -