Remove columns from SQL output where all values are 0

Source Matters

SO, I'm not that great with PIVOT's in SQL (as I usually do this in Excel with "flat" data), but managed to cobble together the following output:

CONTID  FULLNAME    %!  %%  %3
001     Store 1      0   0   0
002     Store 2      0   0   0
003     Store 3      0   0   0
004     Store 4      0   0   0
005     Store 5      0   0   0

(hope that displays properly)

Here is my SQL:

USE mydb 

go 

WITH basequery 
     AS (SELECT c.contid, 
                p.fullname, 
                h.keyalm 
         FROM   customer c 
                LEFT JOIN clogs h 
                       ON c.serialno = h.serialno 
                LEFT JOIN contact P 
                       ON c.serialno = P.serialno 
         WHERE  evtype = 1 
                AND p.conttype = 1) 
SELECT * 
FROM   basequery 
       PIVOT(Count(keyalm) 
            FOR keyalm IN ("%!", 
                           "%%", 
                           "%3", 
                           "%4", 
                           "%6", 
                           "%8", 
                           "%9", 
                           "%A", 
                           "%B", 
                           "%C", 
                           "%D", 
                           "%E", 
                           "%F", 
                           "%G", 
                           "%H", 
                           "%I", 
                           "%K", 
                           "%L", 
                           "%M", 
                           "%O", 
                           "%P", 
                           "%Q", 
                           "%R", 
                           "%S", 
                           "%T", 
                           "%U", 
                           "%V", 
                           "%W", 
                           "%X")) AS pvt --I truncated some of this

What I'd like to is somehow not display the columns that have all 0's for their totals, like the ones displayed here. Attempted to do a "WHERE" after the pivot, but I got yelled at for that.

I've truncated the columns for SO purposes, but my output actually has like 100 columns, and majority are all 0's. Would like to eliminate those from the displayed pivot if possible. Is there a right way to do this?

Mohammed

Try this query for dynamic columns:

USE mydb 

go 
declare @str as nvarchar(max),@query as nvarchar(max);
set @str = stuff((SELECT distinct ',['+keyalm+']' FROM   customer c 
                LEFT JOIN clogs h 
                       ON c.serialno = h.serialno 
                LEFT JOIN contact P 
                       ON c.serialno = P.serialno 
         WHERE  evtype = 1 
                AND p.conttype = 1 for xml path('')),1,1,'')
set @query = 
'WITH basequery 
     AS (SELECT c.contid, 
                p.fullname, 
                h.keyalm 
         FROM   customer c 
                LEFT JOIN clogs h 
                       ON c.serialno = h.serialno 
                LEFT JOIN contact P 
                       ON c.serialno = P.serialno 
         WHERE  evtype = 1 
                AND p.conttype = 1) 
SELECT * 
FROM   basequery 
       PIVOT(Count(keyalm) 
            FOR keyalm IN ('+ @str +')) AS pvt'
execute sp_executesql @query

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

remove all empty values from url string

분류에서Dev

sqlite select all rows where some columns are in set of possible values

분류에서Dev

Get all columns from an SQL Table without Computed Columns

분류에서Dev

Remove all unused rows and columns from Google Sheet

분류에서Dev

Select distinct rows where all values in a column are the same SQL Server

분류에서Dev

SQL Server Display only lines with all values not equal to 0

분류에서Dev

SQL Server select * from table where columns in list

분류에서Dev

Remove rows based on columns values

분류에서Dev

SQL query to return maximum values from multiple columns

분류에서Dev

SQL Server : how to pivot where new columns have multiple unique values

분류에서Dev

Remove quote from the JSONArray output

분류에서Dev

output of replace SQL as a where clause in another SQL

분류에서Dev

Find rows where all columns are equal in R

분류에서Dev

LINQ Remove all Users where UserId is in the list

분류에서Dev

MongoDB linq C # select document where subdocumnets array contains all values from string array

분류에서Dev

remove lines from an output file from diff

분류에서Dev

선택 * FROM WHERE 0 <0

분류에서Dev

Remove all hyperlinks from a spreadsheet

분류에서Dev

Remove all characters from the string

분류에서Dev

How do I rewrite my SQL query to remove redundant values from a GROUP BY column?

분류에서Dev

How to remove the filename from wc -l output?

분류에서Dev

Count all rows from a colum, and get 2 different count columns on the result (One for each possible value 1 or 0)

분류에서Dev

Calculate average of values between 2 columns sql

분류에서Dev

SQL rows to columns (Pivot with just bit values)

분류에서Dev

SQL Server 2008 : how to remove char(0)

분류에서Dev

PL/SQL to select all and more columns

분류에서Dev

r column values in sql where statement

분류에서Dev

Remove duplicate values in a cell SQL Server

분류에서Dev

Get all columns per id where the column is equal to a value

Related 관련 기사

  1. 1

    remove all empty values from url string

  2. 2

    sqlite select all rows where some columns are in set of possible values

  3. 3

    Get all columns from an SQL Table without Computed Columns

  4. 4

    Remove all unused rows and columns from Google Sheet

  5. 5

    Select distinct rows where all values in a column are the same SQL Server

  6. 6

    SQL Server Display only lines with all values not equal to 0

  7. 7

    SQL Server select * from table where columns in list

  8. 8

    Remove rows based on columns values

  9. 9

    SQL query to return maximum values from multiple columns

  10. 10

    SQL Server : how to pivot where new columns have multiple unique values

  11. 11

    Remove quote from the JSONArray output

  12. 12

    output of replace SQL as a where clause in another SQL

  13. 13

    Find rows where all columns are equal in R

  14. 14

    LINQ Remove all Users where UserId is in the list

  15. 15

    MongoDB linq C # select document where subdocumnets array contains all values from string array

  16. 16

    remove lines from an output file from diff

  17. 17

    선택 * FROM WHERE 0 <0

  18. 18

    Remove all hyperlinks from a spreadsheet

  19. 19

    Remove all characters from the string

  20. 20

    How do I rewrite my SQL query to remove redundant values from a GROUP BY column?

  21. 21

    How to remove the filename from wc -l output?

  22. 22

    Count all rows from a colum, and get 2 different count columns on the result (One for each possible value 1 or 0)

  23. 23

    Calculate average of values between 2 columns sql

  24. 24

    SQL rows to columns (Pivot with just bit values)

  25. 25

    SQL Server 2008 : how to remove char(0)

  26. 26

    PL/SQL to select all and more columns

  27. 27

    r column values in sql where statement

  28. 28

    Remove duplicate values in a cell SQL Server

  29. 29

    Get all columns per id where the column is equal to a value

뜨겁다태그

보관