0

I have Written a query which returns multiple column.Out of which One Column contains repetitive entries.

FAULT_SHORT_NAME

ATM DOWN DUE TO LINK PROBLEM
ATM DOWN DUE TO LINK PROBLEM
ALL CASSETTES FAULTED
ALL CASSETTES FAULTED
ATM IS MARK DOWN
ATM IS MARK DOWN

Now I want to Modify My query in Such a way that it will show me Value Count as

ATM DOWN DUE TO LINK PROBLEM ALL CASSETTES FAULTED  ATM IS MARK DOWN
2                             2                         2

There Can be Different "FAULT_SHORT_NAME" Values so Cant Hard Code them.My Original Query is

      Select * From ATMStatus S Left Join ATM A on S.ATM=A.Code 
          Left Join EventMsg E On S.Fault=E.Code 
          Where A.ATMStatus=0 AND S.TicketBooked <> 0

FAULT_SHORT_NAME is Column of Table "EventMsg"

0

1 Answer 1

4

It looks like you want a PIVOT since you want the values as columns instead of rows. there are two ways to do this either a Static or dynamic pivot.

Static Pivot, you hard-code the values of the columns:

SELECT *
FROM
(
    Select * 
    From ATMStatus S 
    Left Join ATM A 
        on S.ATM=A.Code 
    Left Join EventMsg E 
        On S.Fault=E.Code 
    Where A.ATMStatus=0 
        AND S.TicketBooked <> 0
) x
PIVOT
(
    count(*)
    for FAULT_SHORT_NAME in ([ATM DOWN DUE TO LINK PROBLEM], 
        [ALL CASSETTES FAULTED], [ATM IS MARK DOWN])
) p

Dynamic Pivot, the columns are generated at run-time:

DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX)

select @cols = STUFF((SELECT distinct ',' + QUOTENAME(FAULT_SHORT_NAME) 
                    from EventMsg
                    FOR XML PATH(''), TYPE
                    ).value('.', 'NVARCHAR(MAX)') 
                ,1,1,'')
set @query 
      = 'SELECT ' + @cols + ' from 
         (
            Select * 
            From ATMStatus S 
            Left Join ATM A 
                on S.ATM=A.Code 
            Left Join EventMsg E 
                On S.Fault=E.Code 
            Where A.ATMStatus=0 
                AND S.TicketBooked <> 0
         ) x
         pivot 
         (
            count(*)
            for FAULT_SHORT_NAME in(' + @cols + ')
         ) p '

execute(@query)

Both will produce the same results. If you provide additional details about the tables and some sample data, then I could provide a more exact example.

Sign up to request clarification or add additional context in comments.

2 Comments

I want to Print That Columns in Table So Basically in Format Like Select [.....],[....],[....] From ... This No. Of Column will be Dynamic
Using the dynamic version of this query will get your list and then transform it to columns with the count below it. Here is a working example of a dynamic pivot sqlfiddle.com/#!3/d2fef/1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.