Click here to Skip to main content
15,898,765 members
Home / Discussions / Database
   

Database

 
GeneralRe: I need your help to solve a problem. Pin
David Mujica28-Aug-12 2:27
David Mujica28-Aug-12 2:27 
GeneralRe: I need your help to solve a problem. Pin
uspatel28-Aug-12 23:36
professionaluspatel28-Aug-12 23:36 
GeneralRe: I need your help to solve a problem. Pin
David Mujica29-Aug-12 8:31
David Mujica29-Aug-12 8:31 
GeneralRe: I need your help to solve a problem. Pin
Eddy Vluggen28-Aug-12 2:36
professionalEddy Vluggen28-Aug-12 2:36 
AnswerRe: I need your help to solve a problem. Pin
Shameel28-Aug-12 2:37
professionalShameel28-Aug-12 2:37 
AnswerRe: I need your help to solve a problem. Pin
Karthik J, Coimbatore28-Aug-12 23:28
Karthik J, Coimbatore28-Aug-12 23:28 
QuestionPlease help me for this qusetion Pin
Robymon27-Aug-12 2:57
Robymon27-Aug-12 2:57 
AnswerRe: Please help me for this qusetion Pin
__TR__27-Aug-12 3:31
__TR__27-Aug-12 3:31 
Try the below approach

SQL
CREATE TABLE #CM_Master
(
	ShiftID INT, ShiftName VARCHAR(50)
)

INSERT INTO #CM_Master
SELECT 1, 'Day' UNION
SELECT 2, 'Night'
 


CREATE TABLE #CM_Detail
(
	CM_ID INT, Site_ID INT, Level_ID INT, ShiftID INT, Number INT
)

INSERT INTO #CM_Detail
SELECT 1, 1, 1, 1, 5 UNION
SELECT 2, 1, 2, 1, 4 UNION
SELECT 3, 1, 1, 2, 2 UNION
SELECT 4, 2, 2, 2, 8 



--SELECT * FROM #CM_Detail

SELECT DISTINCT CD.Site_ID, CD.Level_ID, ISNULL(A.Day,0) AS [Day], ISNULL(B.Night,0) AS Night
FROM #CM_Detail CD
LEFT JOIN
(
SELECT Site_ID, Level_ID, 
SUM(Number) [Day]
FROM #CM_Detail
WHERE ShiftID = 1
GROUP BY Site_ID, Level_ID
) A ON CD.Site_ID = A.Site_ID AND CD.Level_ID = A.Level_ID
LEFT JOIN 
(
SELECT Site_ID, Level_ID, 
SUM(Number) AS [Night]
FROM #CM_Detail
WHERE ShiftID = 2
GROUP BY Site_ID, Level_ID
) B ON CD.Site_ID = B.Site_ID AND CD.Level_ID = B.Level_ID


and here is a generic approach where shift names are not hard coded.

SQL
DECLARE @PivotColumnHeader VARCHAR(MAX)
SELECT @PivotColumnHeader = COALESCE(@PivotColumnHeader + ', [' + ShiftName + ']', '[' + ShiftName + ']')
FROM #CM_Master


DECLARE @SQL VARCHAR(MAX)
SET @SQL = N'SELECT * FROM
(
SELECT Site_ID, Level_ID, ShiftName, ISNULL(Number,0) AS Number  FROM #CM_Detail CD
INNER JOIN #CM_Master CM ON CD.ShiftID = CM.ShiftID
) P
PIVOT
(
    SUM(Number) FOR ShiftName IN (' + @PivotColumnHeader + ')
) AS PivotTable'

EXECUTE (@SQL)


modified 28-Aug-12 4:05am.

GeneralRe: Please help me for this qusetion Pin
Robymon27-Aug-12 3:51
Robymon27-Aug-12 3:51 
GeneralRe: Please help me for this qusetion Pin
Eddy Vluggen27-Aug-12 4:50
professionalEddy Vluggen27-Aug-12 4:50 
GeneralRe: Please help me for this qusetion Pin
__TR__27-Aug-12 22:06
__TR__27-Aug-12 22:06 
QuestionJoining 4 Tables Pin
ASPnoob26-Aug-12 10:35
ASPnoob26-Aug-12 10:35 
AnswerRe: Joining 4 Tables Pin
Mycroft Holmes26-Aug-12 13:10
professionalMycroft Holmes26-Aug-12 13:10 
AnswerRe: Joining 4 Tables Pin
Blue_Boy26-Aug-12 22:00
Blue_Boy26-Aug-12 22:00 
GeneralRe: Joining 4 Tables Pin
ASPnoob28-Aug-12 21:45
ASPnoob28-Aug-12 21:45 
GeneralRe: Joining 4 Tables Pin
Mycroft Holmes28-Aug-12 23:15
professionalMycroft Holmes28-Aug-12 23:15 
AnswerRe: Joining 4 Tables Pin
Karthik J, Coimbatore28-Aug-12 23:33
Karthik J, Coimbatore28-Aug-12 23:33 
Questioncannot add or change a record Pin
ASPnoob24-Aug-12 16:52
ASPnoob24-Aug-12 16:52 
AnswerRe: cannot add or change a record Pin
Eddy Vluggen24-Aug-12 22:08
professionalEddy Vluggen24-Aug-12 22:08 
GeneralRe: cannot add or change a record Pin
ASPnoob24-Aug-12 22:16
ASPnoob24-Aug-12 22:16 
GeneralRe: cannot add or change a record Pin
Eddy Vluggen24-Aug-12 22:21
professionalEddy Vluggen24-Aug-12 22:21 
AnswerRe: cannot add or change a record Pin
PIEBALDconsult25-Aug-12 4:57
mvePIEBALDconsult25-Aug-12 4:57 
GeneralRe: cannot add or change a record Pin
ASPnoob26-Aug-12 10:44
ASPnoob26-Aug-12 10:44 
GeneralRe: cannot add or change a record Pin
PIEBALDconsult26-Aug-12 17:10
mvePIEBALDconsult26-Aug-12 17:10 
GeneralRe: cannot add or change a record Pin
Shameel27-Aug-12 23:44
professionalShameel27-Aug-12 23:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.