Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Dear All,

Thank you for reading this...

I have query
SQL
SELECT  CONVERT(DECIMAL(18,3), ISNULL(CAST(60 AS DECIMAL(18,3))/CAST( 30 AS DECIMAL(18,3)),0))* 100 AS Efficiency
 FROM ProductionResultDisplyMst PRDM
 JOIN (SELECT PRDMID, SUM(Actual) Actual, SUM(Plans) Plans FROM ProductionResultDisplyDtls
   WHERE PRDMID IN (SELECT ID FROM ProductionResultDisplyMst WHERE PlantID=2
   AND StageID IN(2 ,3, 4, 5) AND shift=2)
   GROUP BY PRDMID ) PRDD
   ON PRDD.PRDMID = PRDM.ID
 JOIN StageMaster SM ON SM.StageID = PRDM.StageID
 WHERE PRDM.STAGEID IN (2,3, 4, 5)
 AND PRDM.PlantID = 2 AND PRDM.Shift = 2
 AND CONVERT(VARCHAR(10), PRDM.ProductionDate, 120) = CONVERT(VARCHAR(10), GETDATE(), 120)


For this Query I am getting the record as below


Efficiency
80
126
109
91


I want to change the Efficiency value as 100 if Efficiency >100
if Efficiency value less than 100, the same value will display.

ie,
Expected Result

Efficiency
80
100
100
91


How can i reach in this answer?...
Expecting your reply.
Posted
Updated 7-Mar-11 2:18am
v3

Try out below solution with CASE statement in SQL.

SQL
SELECT CASE WHEN CONVERT(DECIMAL(18,3), ISNULL(CAST(60 AS DECIMAL(18,3))/CAST( 30 AS DECIMAL(18,3)),0))* 100 AS E1 > 100 THEN 100 ELSE CONVERT(DECIMAL(18,3), ISNULL(CAST(60 AS DECIMAL(18,3))/CAST( 30 AS DECIMAL(18,3)),0))* 100 AS E2 END AS Efficiency
 FROM ProductionResultDisplyMst PRDM
 JOIN (SELECT PRDMID, SUM(Actual) Actual, SUM(Plans) Plans FROM ProductionResultDisplyDtls
   WHERE PRDMID IN (SELECT ID FROM ProductionResultDisplyMst WHERE PlantID=2
   AND StageID IN(2 ,3, 4, 5) AND shift=2)
   GROUP BY PRDMID ) PRDD
   ON PRDD.PRDMID = PRDM.ID
 JOIN StageMaster SM ON SM.StageID = PRDM.StageID
 WHERE PRDM.STAGEID IN (2,3, 4, 5)
 AND PRDM.PlantID = 2 AND PRDM.Shift = 2
 AND CONVERT(VARCHAR(10), PRDM.ProductionDate, 120) = CONVERT(VARCHAR(10), GETDATE(), 120)


Hope it helps.
 
Share this answer
 
Thank you Aragon<code>,
A small difference from the above query
Remove "As E1" and "As E2"
ie,
SQL
SELECT CASE WHEN CONVERT(DECIMAL(18,3), ISNULL(CAST(60 AS DECIMAL(18,3))/CAST( 30 AS DECIMAL(18,3)),0))* 100  > 100 THEN 100 ELSE CONVERT(DECIMAL(18,3), ISNULL(CAST(60 AS DECIMAL(18,3))/CAST( 30 AS DECIMAL(18,3)),0))* 100  END AS Efficiency
 FROM ProductionResultDisplyMst PRDM
 JOIN (SELECT PRDMID, SUM(Actual) Actual, SUM(Plans) Plans FROM ProductionResultDisplyDtls
   WHERE PRDMID IN (SELECT ID FROM ProductionResultDisplyMst WHERE PlantID=2
   AND StageID IN(2 ,3, 4, 5) AND shift=2)
   GROUP BY PRDMID ) PRDD
   ON PRDD.PRDMID = PRDM.ID
 JOIN StageMaster SM ON SM.StageID = PRDM.StageID
 WHERE PRDM.STAGEID IN (2,3, 4, 5)
 AND PRDM.PlantID = 2 AND PRDM.Shift = 2
 AND CONVERT(VARCHAR(10), PRDM.ProductionDate, 120) = CONVERT(VARCHAR(10), GETDATE(), 120)




Thank you guys.........
 
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