Click here to Skip to main content
15,897,371 members
Home / Discussions / Database
   

Database

 
GeneralRe: Delta between records in one column Pin
Member 377323026-Mar-11 23:16
Member 377323026-Mar-11 23:16 
GeneralRe: Delta between records in one column Pin
Wendelius26-Mar-11 23:38
mentorWendelius26-Mar-11 23:38 
AnswerRe: Delta between records in one column Pin
Jörgen Andersson27-Mar-11 8:26
professionalJörgen Andersson27-Mar-11 8:26 
GeneralRe: Delta between records in one column [modified] Pin
Member 377323027-Mar-11 22:34
Member 377323027-Mar-11 22:34 
QuestionPivot? Pin
Andy_L_J26-Mar-11 14:56
Andy_L_J26-Mar-11 14:56 
AnswerRe: Pivot? Pin
Mycroft Holmes26-Mar-11 19:56
professionalMycroft Holmes26-Mar-11 19:56 
GeneralRe: Pivot? Pin
Andy_L_J27-Mar-11 18:18
Andy_L_J27-Mar-11 18:18 
GeneralRe: Pivot? Pin
Andy_L_J27-Mar-11 23:47
Andy_L_J27-Mar-11 23:47 
Tada...

SQL
-- Copy Article stuff
DECLARE @List VARCHAR(1000), @Sql VarChar(5000)
SET @List = ''
SET @Sql = ''

IF EXISTS (SELECT * FROM tempdb.sys.objects
		WHERE name LIKE '#tblX%' AND type IN (N'U'))
	DROP TABLE [dbo].#tblX
	
CREATE TABLE #tblX(MixDate DateTime, NoMixes Int, MixPlant_Name VarChar(50) )
INSERT [#tblX]
SELECT DISTINCT(mp.MixDate), SUM(mp.NoMixes) As NoMixes, Mixplant.MixPlant_Name
	FROM MixProduction mp
	  INNER	JOIN MixPlant
	    ON mp.MixPlant_ID = Mixplant.ID And MixPlant.IsActive =1
	WHERE mp.MixDate BETWEEN '2/1/2011' AND '2/28/2011'
	Group By Mixplant.MixPlant_Name, mp.MixDate	
	
	
	
DECLARE @tblY TABLE(MixPlant_Name VarChar(50))
INSERT @tblY
  SELECT DISTINCT MixPlant_Name
    FROM #tblX
      ORDER BY MixPlant_Name
		
 SELECT @List = ISNULL(@List, '') +
		 CASE WHEN ISNULL(@List,'') = ''
		 THEN '[' + MixPlant_Name + ']'
		 ELSE ',[' + MixPlant_Name + ']' END
FROM @tblY
ORDER BY MixPlant_Name
		
SET @Sql = 'SELECT MixDate, ' + @List + CHAR(13)
SET @Sql = @Sql + 'FROM (SELECT MixDate, NoMixes, MixPlant_Name ' + CHAR(13)
SET @Sql = @Sql + 'FROM #tblX ) As P ' + CHAR(13)
SET @Sql = @Sql + 'PIVOT (SUM(NoMixes) FOR MixPlant_Name IN (' + @List + ')) As Pvt' + CHAR(13)
	
-- test output
--print @sql
--SELECT * FROM #tblX
	
-- exucute dynamic sql
Exec (@Sql)
	
-- Drop Table
DROP TABLE #tblX
GO


Thanks for prompting me to persist. Now to add a couple more columns to the pivot Big Grin | :-D
I don't speak Idiot - please talk slowly and clearly

'This space for rent'

Driven to the arms of Heineken by the wife

QuestionWhat are solutions for a way-too-big database? Pin
bobbi200426-Mar-11 7:23
bobbi200426-Mar-11 7:23 
AnswerRe: What are solutions for a way-too-big database? Pin
Wendelius26-Mar-11 7:41
mentorWendelius26-Mar-11 7:41 
GeneralRe: What are solutions for a way-too-big database? Pin
bobbi200426-Mar-11 16:20
bobbi200426-Mar-11 16:20 
AnswerRe: What are solutions for a way-too-big database? Pin
PIEBALDconsult26-Mar-11 17:21
mvePIEBALDconsult26-Mar-11 17:21 
GeneralRe: What are solutions for a way-too-big database? Pin
bobbi200427-Mar-11 6:53
bobbi200427-Mar-11 6:53 
AnswerRe: What are solutions for a way-too-big database? Pin
Klaus-Werner Konrad31-Mar-11 8:42
Klaus-Werner Konrad31-Mar-11 8:42 
QuestionMultiple selection from the same table Pin
CodingLover25-Mar-11 19:43
CodingLover25-Mar-11 19:43 
AnswerRe: Multiple selection from the same table Pin
Аslam Iqbal26-Mar-11 0:14
professionalАslam Iqbal26-Mar-11 0:14 
AnswerRe: Multiple selection from the same table Pin
Wendelius26-Mar-11 0:53
mentorWendelius26-Mar-11 0:53 
AnswerRe: Multiple selection from the same table Pin
Luc Pattyn26-Mar-11 7:27
sitebuilderLuc Pattyn26-Mar-11 7:27 
GeneralRe: Multiple selection from the same table Pin
Wendelius26-Mar-11 7:45
mentorWendelius26-Mar-11 7:45 
GeneralRe: Multiple selection from the same table Pin
Luc Pattyn26-Mar-11 8:05
sitebuilderLuc Pattyn26-Mar-11 8:05 
GeneralRe: Multiple selection from the same table Pin
Wendelius26-Mar-11 8:07
mentorWendelius26-Mar-11 8:07 
Questiondynamically change column name Pin
amit sahu2025-Mar-11 1:16
amit sahu2025-Mar-11 1:16 
AnswerRe: dynamically change column name Pin
Simon_Whale25-Mar-11 1:31
Simon_Whale25-Mar-11 1:31 
AnswerRe: dynamically change column name Pin
Eddy Vluggen25-Mar-11 1:32
professionalEddy Vluggen25-Mar-11 1:32 
GeneralRe: dynamically change column name Pin
PIEBALDconsult25-Mar-11 3:03
mvePIEBALDconsult25-Mar-11 3:03 

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.