Click here to Skip to main content
15,920,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, i have the following SQL codes which will return me the following output:

TargetDate ActualDate Percentage Month
======================================
    2          1          80       8
    1          1         100       9

below are my SQL codes:

SQL
CREATE TABLE #TargetDateTblx(IssueID VARCHAR(100), 
              TargetDate VARCHAR(100))
INSERT [#TargetDateTblx]
SELECT issueid, 
       [fielddata] AS TargetDate
FROM rndbug.dbo.gemini_customfielddata
WHERE customfieldid = 215
 
CREATE TABLE #ActualDateTblx(IssueID VARCHAR(100), 
              ActualDate VARCHAR(100))
INSERT [#ActualDateTblx]
SELECT issueid, 
       [fielddata] AS ActualDate
FROM rndbug.dbo.gemini_customfielddata
WHERE customfieldid = 217
 
CREATE TABLE #CompleteRateTblx(IssueID VARCHAR(100), 
              CompleteRate VARCHAR(100))
INSERT [#CompleteRateTblx]
SELECT issueid, 
       [fielddata] AS CompleteRate
FROM rndbug.dbo.gemini_customfielddata
WHERE customfieldid = 234
 
CREATE TABLE #ALLTB(IssueID VARCHAR(100), 
              UserID VARCHAR(100), 
              UserName VARCHAR(100), 
              TargetDate VARCHAR(100), 
              ActualDate VARCHAR(100), 
              CompleteRate VARCHAR(100), 
              Months VARCHAR(100))
INSERT [#ALLTB]
SELECT a.issueid, 
       a.userid,
       b.firstname,
       c.TargetDate,
       d.ActualDate,
       e.CompleteRate,
       SUBSTRING(c.TargetDate, 5, 1) AS Month
FROM dbo.gemini_issueresources A, 
     dbo.gemini_users B, 
     #TargetDateTblx c, 
     #ActualDateTblx d, 
     #CompleteRateTblx e
WHERE a.userid = b.userid 
  AND a.issueid = c.IssueID 
  AND  a.issueid = d.IssueID 
  AND  a.issueid = e.IssueID
 
SELECT SUM(CONVERT(INT, TargetDate)) AS TargetDate, 
       SUM(CONVERT(INT, ActualDate)) AS ActualDate, 
       SUM(CONVERT(INT, CompleteRate))/SUM(CONVERT(INT, TargetDate)) 
    AS CompletePercentage, Months
FROM #FORMULA
GROUP BY Months

how can i transfer the output which is in 2 rows into a table. FIY, the output is correct and i want to make the 2 rows into ONE table not ROW. Any help would be appreciated.
Posted
Comments
Jamie888 1-Jul-14 0:22am    
the output after beging transfer into a table should be the same as the output above.
syed shanu 1-Jul-14 0:25am    
Your question is not clear ,Improve your question and what exactly you need what is 2 rows int a table ,rows will be inside table only "i want to make the 2 rows into ONE table not ROW." what this mean

 
Share this answer
 
TRY LIKE THIS

SQL
CREATE TABLE #ALLTB(
              TargetDate VARCHAR(100), 
              ActualDate VARCHAR(100), 
              CompleteRate VARCHAR(100), 
    INSERT [#ALLTB]
SELECT
	CASE WHEN C.customfieldid = 215 THEN  C.fielddata END AS TargetDate,
	CASE WHEN C.customfieldid = 217 THEN C.fielddata END AS ActualDate,
	CASE WHEN C.customfieldid = 234 THEN  C.fielddata END AS CompleteRate,
 FROM
	dbo.gemini_issueresources A 
	INNER JOIN dbo.gemini_users B ON  a.userid = b.userid
	INNER JOIN rndbug.dbo.gemini_customfielddata ON a.issueid = c.IssueID
	
	SELECT SUM(CONVERT(INT, TargetDate)) AS TargetDate, 
       SUM(CONVERT(INT, ActualDate)) AS ActualDate, 
       SUM(CONVERT(INT, CompleteRate))/SUM(CONVERT(INT, TargetDate)) 
    AS CompletePercentage,
    SUBSTRING(c.TargetDate, 5, 1) AS Months
FROM #ALLTB
GROUP BY SUBSTRING(c.TargetDate, 5, 1)
 
Share this answer
 
Add this line at the beginning of your code

SQL
CREATE TABLE #OutPutTBL(TARGETDATE INT, ACTUALDATE INT, PERCENTAGE DECIMAL(10,2),
              MONTH INT(100))


At the end modify the SQL like below

SQL
SELECT SUM(CONVERT(INT, TargetDate)) AS TargetDate,
       SUM(CONVERT(INT, ActualDate)) AS ActualDate,
       SUM(CONVERT(INT, CompleteRate))/SUM(CONVERT(INT, TargetDate))
    AS CompletePercentage, Months
      INTO #OutputTBL
FROM #FORMULA
GROUP BY Months
 
Share this answer
 
v2

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