Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Crosstab query in sql server 2000

 
   Database Help (Home) -> Programming RSS
Next:  Query across servers with SQL 2000  
Author Message
rguarnieri

External


Since: Sep 01, 2006
Posts: 6



(Msg. 1) Posted: Wed Feb 07, 2007 6:45 am
Post subject: Crosstab query in sql server 2000
Archived from groups: microsoft>public>sqlserver>programming (more info?)

Hi!, I'm trying to make a cross tab query in sql server, I know how
to
do it, but I need to improve the performance of my query, I'm working
with vb.net 2005 and sql server 2000

I need to visualize the information of a different way:

My Query:

Date Standard Actual Output
2006/10 5 8 5
2006/11 7 3 7
2006/12 9 2 4

I need to visualize it this way:

2006/10 2006/11 2006/12
Standard 5 7 9
Actual 8 3 2
Output 5 7 4

This is my first query:

SELECT CONVERT(varchar, Year(Dte)) + ' M' + RIGHT('0' +
CONVERT(varchar, MONTH(Dte)), 2) As [Date Range],
(Round(((CASE WHEN Sum(TotStdMin) <= 0 THEN 0 ELSE (Sum(GrossQty) /
Sum(TotStdMin)) END) * Sum(TotMin)) , 0) / 1000) as [Output],
((Sum(ProdQty - (CASE WHEN QaRej IS NULL THEN 0 ELSE QaRej END) )) /
1000) as [Actual]
FROM dbo.ViwOEE_Source LEFT OUTER JOIN (SELECT SeqNum as Seq, MachNum
as Mach, SUM(QaRej) AS QaRej
FROM dbo.ViwOEE_Rej3
WHERE (DateCode BETWEEN
CAST('20060713' AS smalldatetime)
AND CAST('20061013' AS
smalldatetime))
GROUP BY SeqNum, MachNum)
Rej3
ON dbo.ViwOEE_Source.SeqNum = Rej3.Seq
AND dbo.ViwOEE_Source.MachNum = Rej3.Mach
WHERE Dte BETWEEN CAST('20060713' AS smalldatetime)
AND CAST('20061013' AS smalldatetime)
AND MachNum in (Select PkSubGrp
From dbo.ViwP_MchGrpDetail
Where((McTypNum = 2)
AND PKMcGrp in (14, 15, 16, 17, 18, 19, 21)))
GROUP BY CONVERT(varchar, Year(Dte)) + ' M' + RIGHT('0' +
CONVERT(varchar, MONTH(Dte)), 2)
ORDER BY [Date Range]

this is the result of my first query:

Date Output Actual
2006 M07 1265.4 955.90
2006 M08 2512.2 1878.36
2006 M09 2413.9 1837.58
2006 M10 975.49 748.17

the only way that I found is to make one query for each column
(Output, Actual, etc) and make a sum for each date, but I think that
there is another way to do this.

SELECT 'Output At Standard' as 'Values',
Sum(CASE WHEN [Date Range] = '2006 M07' THEN [Output] ELSE NULL END)
as [2006 M07],
Sum(CASE WHEN [Date Range] = '2006 M08' THEN [Output] ELSE NULL END)
as [2006 M08],
Sum(CASE WHEN [Date Range] = '2006 M09' THEN [Output] ELSE NULL END)
as [2006 M09],
Sum(CASE WHEN [Date Range] = '2006 M10' THEN [Output] ELSE NULL END)
as [2006 M10]
FROM (SELECT CONVERT(varchar, Year(Dte)) + ' M' + RIGHT('0' +
CONVERT(varchar, MONTH(Dte)), 2) As [Date Range], (Round(((CASE WHEN
Sum(TotStdMin) <= 0 THEN 0 ELSE (Sum(GrossQty) / Sum(TotStdMin))
END)
* Sum(TotMin)) , 0) / 1000) as [Output]
FROM dbo.ViwOEE_Source LEFT OUTER JOIN (SELECT SeqNum as Seq, MachNum
as Mach, SUM(QaRej) AS QaRej FROM dbo.ViwOEE_Rej3 WHERE (DateCode
BETWEEN CAST('20060713' AS smalldatetime) AND CAST('20061013' AS
smalldatetime)) GROUP BY SeqNum, MachNum) Rej3 ON
dbo.ViwOEE_Source.SeqNum = Rej3.Seq AND dbo.ViwOEE_Source.MachNum =
Rej3.Mach
WHERE Dte BETWEEN CAST('20060713' AS smalldatetime) AND
CAST('20061013' AS smalldatetime) AND MachNum in (Select PkSubGrp
From
dbo.ViwP_MchGrpDetail Where((McTypNum = 2) AND PKMcGrp in (14, 15,
16,
17, 18, 19, 21))) GROUP BY CONVERT(varchar, Year(Dte)) + ' M' +
RIGHT('0' + CONVERT(varchar, MONTH(Dte)), 2)) as OEECrossTab
UNION
SELECT 'Output Actual' as 'Values',
Sum(CASE WHEN [Date Range] = '2006 M07' THEN [Actual] ELSE NULL END)
as [2006 M07],
Sum(CASE WHEN [Date Range] = '2006 M08' THEN [Actual] ELSE NULL END)
as [2006 M08],
Sum(CASE WHEN [Date Range] = '2006 M09' THEN [Actual] ELSE NULL END)
as [2006 M09],
Sum(CASE WHEN [Date Range] = '2006 M10' THEN [Actual] ELSE NULL END)
as [2006 M10]
FROM (SELECT CONVERT(varchar, Year(Dte)) + ' M' + RIGHT('0' +
CONVERT(varchar, MONTH(Dte)), 2) As [Date Range], ((Sum(ProdQty -
(CASE WHEN QaRej IS NULL THEN 0 ELSE QaRej END) )) / 1000) as
[Actual]
FROM dbo.ViwOEE_Source LEFT OUTER JOIN (SELECT SeqNum as Seq, MachNum
as Mach, SUM(QaRej) AS QaRej FROM dbo.ViwOEE_Rej3 WHERE (DateCode
BETWEEN CAST('20060713' AS smalldatetime) AND CAST('20061013' AS
smalldatetime)) GROUP BY SeqNum, MachNum) Rej3 ON
dbo.ViwOEE_Source.SeqNum = Rej3.Seq AND dbo.ViwOEE_Source.MachNum =
Rej3.Mach WHERE Dte BETWEEN CAST('20060713' AS smalldatetime) AND
CAST('20061013' AS smalldatetime) AND MachNum in (Select PkSubGrp
From
dbo.ViwP_MchGrpDetail Where((McTypNum = 2) AND PKMcGrp in (14, 15,
16,
17, 18, 19, 21))) GROUP BY CONVERT(varchar, Year(Dte)) + ' M' +
RIGHT('0' + CONVERT(varchar, MONTH(Dte)), 2)) as OEECrossTab

 >> Stay informed about: Crosstab query in sql server 2000 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Can you edit the result set from Studio Manager Sql Server.. - Can you edit the result set from Studio Manager Sql Server 2005 like you could in EnterpriseManager sql server 2000? I loved being able to go directly into a table in sql server 2000 enterprise manager, return a result set and just edit inline right..

Difference in result set from sql server 2000 and sql serv.. - The following query returns only about 16,000 rows under 2000 but when I run it against the same data it returns over 300,000 with many duplicate entries. Would anyone be able to tell me if I'm doing something wrong? Thanks Select tblCountryCode.Npa...

how to use sql server 2005 management tools to connect to .. - Hello, After installing SQL Server 2005, I get a message stating "You must use SQL Server 2005 management tools to connect to this server", while trying to connect to my localhost using SQL Server 2000 Enterprise Manager. Regards, junior

restoring SQL Server 2005 backup in sql server 2000 - Hi! Can it is possible to restore backup of a database from sql server 2005 to sql server 2000 or i need to use DTS for this transformayion. Regrads, Jami *** Sent via Developersdex http://www.developersdex.com ***

Restore Sql Server 2005 db to Sql Server 2000 - I have a database that was backed up on a SQL Server 2005 system. Is it possible to restore the database to a SQL Server 2000 system?
   Database Help (Home) -> Programming All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]