SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


count number of rows per hour per client and in the end total


count number of rows per hour per client and in the end total

Author
Message
bang.prashant
bang.prashant
SSC Journeyman
SSC Journeyman (86 reputation)SSC Journeyman (86 reputation)SSC Journeyman (86 reputation)SSC Journeyman (86 reputation)SSC Journeyman (86 reputation)SSC Journeyman (86 reputation)SSC Journeyman (86 reputation)SSC Journeyman (86 reputation)

Group: General Forum Members
Points: 86 Visits: 221
how to count number of rows per hour per client and in the end total

here is something like needed output

datetime no. of rows client
9/16/2008 7:00:00 AM 10 abc
9/16/2008 8:00:00 AM 20 abc
9/16/2008 9:00:00 AM 30 abc
9/16/2008 7:00:00 AM 20 xyz
9/16/2008 8:00:00 AM 10 xyz
9/16/2008 9:00:00 AM 40 xyz
9/16/2008 7:00:00 AM 10 tqm
9/16/2008 8:00:00 AM 20 tqm
9/16/2008 9:00:00 AM 60 tqm
Total 220
Garadin
Garadin
Hall of Fame
Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)

Group: General Forum Members
Points: 3719 Visits: 4107
You're looking for something like this?

Declare @StartTime   datetime,
@EndTime datetime,
@Total int

SELECT Client, datepart(hh, YourDate) HourNumber, SUM([NumRows]) VRowCount
INTO #1
FROM YourTable
WHERE YourDate BETWEEN @StartTime and @EndTime
GROUP BY Client, datepart(hh,YourDate)

SET @Total = (SELECT SUM(VRowCount)
FROM #1)

SELECT Client, HourNumber, VRowCount, @Total TotalRows
FROM #1



Seth Phelabaum
Consistency is only a virtue if you're not a screwup. ;-)

Links: How to Post Sample Data :: Running Totals :: Tally Table :: Cross Tabs/Pivots :: String Concatenation
Adrian Nichols-360275
Adrian Nichols-360275
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1284 Visits: 915
This will give you client sub-totals without the need for a temporary table.


SELECT
CASE WHEN client IS NOT NULL AND hourSegment IS NULL THEN 'Client Total' ELSE CAST(hourSegment AS VARCHAR) END,
[No of Rows],
CASE WHEN client IS NULL AND hourSegment IS NULL THEN 'Grand Total' ELSE client END AS client
FROM (
SELECT LEFT(CONVERT(VARCHAR(30), [datetime], 20), 13) AS hourSegment, client, SUM([no. of rows]) AS [No of Rows]
FROM myTable
GROUP BY LEFT(CONVERT(VARCHAR(30), [datetime], 20), 13), client WITH ROLLUP
)x



The above should give you something like:

hourSegment No of Rows Client
9/16/2008 7 10 abc
9/16/2008 8 20 abc
9/16/2008 9 30 abc
Client Total 60 abc
9/16/2008 7 20 xyz
9/16/2008 8 10 xyz
9/16/2008 9 40 xyz
Client Total 70 xyz
9/16/2008 7 10 tqm
9/16/2008 8 20 tqm
9/16/2008 9 60 tqm
Client Total 90 tqm
220 Grand Total



Ade

A Freudian Slip is when you say one thing and mean your mother.
For detail-enriched answers, ask detail-enriched questions...
bang.prashant
bang.prashant
SSC Journeyman
SSC Journeyman (86 reputation)SSC Journeyman (86 reputation)SSC Journeyman (86 reputation)SSC Journeyman (86 reputation)SSC Journeyman (86 reputation)SSC Journeyman (86 reputation)SSC Journeyman (86 reputation)SSC Journeyman (86 reputation)

Group: General Forum Members
Points: 86 Visits: 221
thanks , this wht i needed
bang.prashant
bang.prashant
SSC Journeyman
SSC Journeyman (86 reputation)SSC Journeyman (86 reputation)SSC Journeyman (86 reputation)SSC Journeyman (86 reputation)SSC Journeyman (86 reputation)SSC Journeyman (86 reputation)SSC Journeyman (86 reputation)SSC Journeyman (86 reputation)

Group: General Forum Members
Points: 86 Visits: 221
i don't want grant total row. can you help me in that?
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search