Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 Other Development Tools
 Need help to build a query

Author  Topic 

powersakthi
Starting Member

9 Posts

Posted - 2007-01-03 : 03:35:38
Hi All,
I need to build a query from MS-SQL 2000 to ASP to shows the count of hourly data for 24 hours. Please check take a look at the table in the below link



Here i need to segregate the data as hourly and then count of that hourly data has to displayed in asp like



based on the date selection of the user as below

.

In the output column line1 to line8 is nothing but station id which means stationid 47 is line 1 e.t.c.

Below is my query for line 1 which grows to very big tree for 8 lines.

countSQL = " select "
countSQL = countSQL & " count(case when stationid=49 and unitstateid= 305 and entertime >= ('"& curdate &" 00:00:00.000') and exittime <= ('"& curdate &" 00:59:59.000') then 1 end) as 01"
..................................................
countSQL = countSQL & " from ffhistory with (nolock)"

I need to reduce the query size or put some stored proc or create view. Help me because the server has more data so it takes 3 minutes to generate data!

Thanks!


sakthi

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-01-03 : 04:39:30
Try this for speed. Make sure the needed columns are indexed.
SELECT		d.Line,
SUM(CASE WHEN d.Hour = 0 THEN 1 ELSE 0 END) AS '00',
SUM(CASE WHEN d.Hour = 1 THEN 1 ELSE 0 END) AS '01',
SUM(CASE WHEN d.Hour = 2 THEN 1 ELSE 0 END) AS '02',
SUM(CASE WHEN d.Hour = 3 THEN 1 ELSE 0 END) AS '03',
SUM(CASE WHEN d.Hour = 4 THEN 1 ELSE 0 END) AS '04',
SUM(CASE WHEN d.Hour = 5 THEN 1 ELSE 0 END) AS '05',
SUM(CASE WHEN d.Hour = 6 THEN 1 ELSE 0 END) AS '06',
SUM(CASE WHEN d.Hour = 7 THEN 1 ELSE 0 END) AS '07',
SUM(CASE WHEN d.Hour = 8 THEN 1 ELSE 0 END) AS '08',
SUM(CASE WHEN d.Hour = 9 THEN 1 ELSE 0 END) AS '09',
SUM(CASE WHEN d.Hour = 10 THEN 1 ELSE 0 END) AS '10',
SUM(CASE WHEN d.Hour = 11 THEN 1 ELSE 0 END) AS '11',
SUM(CASE WHEN d.Hour = 12 THEN 1 ELSE 0 END) AS '12',
SUM(CASE WHEN d.Hour = 13 THEN 1 ELSE 0 END) AS '13',
SUM(CASE WHEN d.Hour = 14 THEN 1 ELSE 0 END) AS '14',
SUM(CASE WHEN d.Hour = 15 THEN 1 ELSE 0 END) AS '15',
SUM(CASE WHEN d.Hour = 16 THEN 1 ELSE 0 END) AS '16',
SUM(CASE WHEN d.Hour = 17 THEN 1 ELSE 0 END) AS '17',
SUM(CASE WHEN d.Hour = 18 THEN 1 ELSE 0 END) AS '18',
SUM(CASE WHEN d.Hour = 19 THEN 1 ELSE 0 END) AS '19',
SUM(CASE WHEN d.Hour = 20 THEN 1 ELSE 0 END) AS '20',
SUM(CASE WHEN d.Hour = 21 THEN 1 ELSE 0 END) AS '21',
SUM(CASE WHEN d.Hour = 22 THEN 1 ELSE 0 END) AS '22',
SUM(CASE WHEN d.Hour = 23 THEN 1 ELSE 0 END) AS '23'
FROM (
SELECT 'Line1' AS 'Line',
DATEPART(hour, EnterTime) AS 'Hour'
FROM ffhistory
WHERE StationID = 49
AND UnitStateID = 305
AND EnterTime >= DATEADD(day, DATEDIFF(day, 0, @UserSelectedDateTimeValue), 0)
AND EnterTime < DATEADD(day, DATEDIFF(day, 0, @UserSelectedDateTimeValue), 1)
UNION ALL
SELECT 'Line2',
DATEPART(hour, EnterTime)
FROM ffhistory
WHERE StationID = <x2>
AND UnitStateID = <y2>
AND EnterTime >= DATEADD(day, DATEDIFF(day, 0, @UserSelectedDateTimeValue), 0)
AND EnterTime < DATEADD(day, DATEDIFF(day, 0, @UserSelectedDateTimeValue), 1)
UNION ALL
SELECT 'Line3',
DATEPART(hour, EnterTime)
FROM ffhistory
WHERE StationID = <x3>
AND UnitStateID = <y3>
AND EnterTime >= DATEADD(day, DATEDIFF(day, 0, @UserSelectedDateTimeValue), 0)
AND EnterTime < DATEADD(day, DATEDIFF(day, 0, @UserSelectedDateTimeValue), 1)
UNION ALL
SELECT 'Line4',
DATEPART(hour, EnterTime)
FROM ffhistory
WHERE StationID = <x4>
AND UnitStateID = <y4>
AND EnterTime >= DATEADD(day, DATEDIFF(day, 0, @UserSelectedDateTimeValue), 0)
AND EnterTime < DATEADD(day, DATEDIFF(day, 0, @UserSelectedDateTimeValue), 1)
UNION ALL
SELECT 'Line5',
DATEPART(hour, EnterTime)
FROM ffhistory
WHERE StationID = <x5>
AND UnitStateID = <y5>
AND EnterTime >= DATEADD(day, DATEDIFF(day, 0, @UserSelectedDateTimeValue), 0)
AND EnterTime < DATEADD(day, DATEDIFF(day, 0, @UserSelectedDateTimeValue), 1)
UNION ALL
SELECT 'Line6',
DATEPART(hour, EnterTime)
FROM ffhistory
WHERE StationID = <x6>
AND UnitStateID = <y6>
AND EnterTime >= DATEADD(day, DATEDIFF(day, 0, @UserSelectedDateTimeValue), 0)
AND EnterTime < DATEADD(day, DATEDIFF(day, 0, @UserSelectedDateTimeValue), 1)
UNION ALL
SELECT 'Line7',
DATEPART(hour, EnterTime)
FROM ffhistory
WHERE StationID = <x7>
AND UnitStateID = <y7>
AND EnterTime >= DATEADD(day, DATEDIFF(day, 0, @UserSelectedDateTimeValue), 0)
AND EnterTime < DATEADD(day, DATEDIFF(day, 0, @UserSelectedDateTimeValue), 1)
) d
GROUP BY d.Line
ORDER BY d.Line

Peter Larsson
Helsingborg, Sweden
Go to Top of Page

powersakthi
Starting Member

9 Posts

Posted - 2007-01-03 : 06:07:45
Hi Peter Larsson,
I took your query and modified as per your instructions and as you told is working fine and producing output in just a span of 7 seconds! That's great! Thank you so much!

Regards,

sakthi
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-01-03 : 11:04:18
From 3 minutes (180 seconds) down to 7 seconds?
That's good enough..


Peter Larsson
Helsingborg, Sweden
Go to Top of Page
   

- Advertisement -