Click here to Skip to main content
15,903,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, i have the following codes. How can i make a sum from the DocStatus?

SQL
SELECT CustomerName,[0] as Pending, [1] as Save_As_Draft, [2] as Spec_Confirmed, [3] as Request_Revision, [4] as Withdraw, [5] as Pending_RND, [6] as Pending_QC, [7] as Manday_Confirmed
FROM
(
    SELECT P.DocStatus,C.CustomerName,P.CustomerID
    FROM dbo.CRF_Project P
    INNER JOIN dbo.CustomerList C
    ON P.CustomerID = C.CustomerID
) p

PIVOT
(
    COUNT (CustomerID) FOR DocStatus IN ([0], [1], [2], [3], [4], [5], [6], [7])

) AS pvt;


i have tried to put the SUM function at different places(eg. inside PIVOT, at the 2 SELECT statement) but none seems can work.
Posted
Comments
Karen Mitchelle 21-May-14 21:48pm    
I'm sorry. Where do you want to use the sum function again?
Jamie888 21-May-14 21:51pm    
i want it to display at the end of the "Manday_Confirmed"
Karen Mitchelle 21-May-14 21:54pm    
What field do you want to get the sum?
Jamie888 21-May-14 22:00pm    
The DocStatus, after i have got the COUNT for each of the DocStatus, i wanna sum them all up according to their CustomerName
Jamie888 21-May-14 22:33pm    
or the CustomerID which will be less complicated

If below values correspond eachother:
[0] - Pending,
[1] - Save_As_Draft
[2] - Spec_Confirmed
[3] - Request_Revision
[4] - Withdraw
[5] - Pending_RND
[6] - Pending_QC
[7] - Manday_Confirmed
your pivot query should looks like:
SQL
SELECT CutomerName, [0] as Pending, [1] as Save_As_Draft, [2] as Spec_Confirmed, [3] as Request_Revision, [4] as Withdraw, [5] as Pending_RND, [6] as Pending_QC, [7] as Manday_Confirmed
FROM (
    SELECT CustomerName, [1], [2], [3], [4], [5], [6], [7]
    FROM
    (
        SELECT P.DocStatus, C.CustomerName,P.CustomerID
        FROM dbo.CRF_Project P
        INNER JOIN dbo.CustomerList C
        ON P.CustomerID = C.CustomerID
    ) dt
    PIVOT
    (
        COUNT (CustomerID) FOR DocStatus IN ([0], [1], [2], [3], [4], [5], [6], [7])
    ) AS pvt
) AS T


Do you see the diffetence?
 
Share this answer
 
Comments
Jamie888 22-May-14 3:51am    
thank you. I have solved it. Would you mind to have a look at my solution?
[no name] 24-May-14 20:20pm    
OP seems to be happy with it's solution....nevertheless my 5
Maciej Los 25-May-14 4:27am    
Thank you, Bruno ;)
Jamie888 25-May-14 21:22pm    
Mr/Madam Los, i dont quite get your solution, would you mind please briefly explain your solution?
i would suggest a better solution but as far as what you want i will give you first then see my solution

SQL
DECLARE @Customer AS TABLE (Customerid INT, CustomerName VARCHAR(15))
DECLARE @Project AS TABLE (Customerid INT, DocStatus INT)

INSERT INTO @Customer
VALUES
(1,'Customer1'),(2,'Customer2'),(3,'Customer3'),(4,'Customer4'),(5,'Customer5'),
(6,'Customer6'),(7,'Customer7'),(8,'Customer8'),(9,'Customer9'),(10,'Customer10'),
(11,'Customer11'),(12,'Customer12'),(13,'Customer13'),(14,'Customer14')

INSERT INTO @Project
VALUES
(1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),
(8,1),(9,2),(10,3),(11,4),(12,5),(13,6),(14,7),
(1,2),(2,3),(3,4),(4,5),(5,6),(6,7),(7,1),
(8,2),(9,3),(10,4),(11,5),(12,6),(13,7),(14,1)

SELECT P.DocStatus, C.CustomerName, P.CustomerID
FROM   @Project P
       INNER JOIN @Customer C
            ON  P.CustomerID = C.CustomerID
UNION ALL
SELECT 8 AS Docstatus, c.CustomerName, COUNT(*)
FROM   @Project P
       INNER JOIN @Customer C
            ON  P.CustomerID = C.CustomerID
GROUP BY
       C.Customerid, c.CustomerName




SELECT CustomerName, [0] AS Pending, [1] AS Save_As_Draft, [2] AS Spec_Confirmed, [3] AS
       Request_Revision, [4] AS Withdraw, [5] AS Pending_RND, [6] AS Pending_QC, [7] AS
       Manday_Confirmed,[8] AS Total
FROM   (
        SELECT P.DocStatus, C.CustomerName, P.CustomerID, 1 AS customercount
        FROM   @Project P
               INNER JOIN @Customer C
                    ON  P.CustomerID = C.CustomerID
        UNION ALL
        select 8 AS Docstatus, c.CustomerName,c.Customerid, COUNT (p.Customerid)
        FROM  @Project P
               INNER JOIN @Customer C
                    ON  P.CustomerID = C.CustomerID
        GROUP BY C.Customerid, c.CustomerName
       ) p

       PIVOT(
        SUM(customercount)  FOR DocStatus IN ([0], [1], [2], [3], [4], [5], [6], [7],[8])
       ) AS pvt;
/* below is my solution see it is read the source only once  hope this one is better readable than the pivot and have a better performance too*/
SELECT C.CustomerName,
       SUM(CASE WHEN docStatus =0 THEN 1 ELSE 0 END) AS Pending,
       SUM(CASE WHEN docStatus =1 THEN 1 ELSE 0 END) AS Save_As_Draft,
       SUM(CASE WHEN docStatus =2 THEN 1 ELSE 0 END) AS Spec_Confirmed,
       SUM(CASE WHEN docStatus =3 THEN 1 ELSE 0 END) AS Request_Revision,
       SUM(CASE WHEN docStatus =4 THEN 1 ELSE 0 END) AS Withdraw,
       SUM(CASE WHEN docStatus =5 THEN 1 ELSE 0 END) AS Pending_RND,
       SUM(CASE WHEN docStatus =6 THEN 1 ELSE 0 END) AS Pending_QC,
       SUM(CASE WHEN docStatus =7 THEN 1 ELSE 0 END) AS Manday_Confirmed,
       SUM(1) AS Total
FROM   @Project P
       INNER JOIN @Customer C
            ON  P.CustomerID = C.CustomerID
GROUP BY c.CustomerName


well this is what you want i think hopt which one is better in looking and performance wise
 
Share this answer
 
v2
Comments
Jamie888 22-May-14 3:51am    
thank you. I have solved it. Would you mind to have a look at my solution?
SQL
SELECT CustomerName,[0] as Pending, [1] as Save_As_Draft, [2] as Spec_Confirmed, [3] as Request_Revision, [4] as Withdraw, [5] as Pending_RND, [6] as Pending_QC, [7] as Manday_Confirmed, [0]+[1]+[2]+[3]+[4]+[5]+[6]+[7] as Total
FROM
(
    SELECT P.DocStatus,C.CustomerName,P.CustomerID
    FROM dbo.CRF_Project P
    INNER JOIN dbo.CustomerList C
    ON P.CustomerID = C.CustomerID
) p

PIVOT
(
    COUNT (CustomerID) FOR DocStatus IN ([0], [1], [2], [3], [4], [5], [6], [7])

) AS pvt;



just add the [0]+[1]+[2]+[3]+[4]+[5]+[6]+[7] as Total at behind
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900