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.
| Author |
Topic |
|
jung1975
Aged Yak Warrior
503 Posts |
Posted - 2004-07-11 : 14:02:02
|
The following query returns the result looks like:Highest Log-in Volume Day/Hour- Account Holder ------------------------------ 2004-04-23 10:00:00.000Highest Log-in Volume Day/Hour-Holder------------------------------ 2004-03-23 11:00:00.000Highest Log-in Volume Day/Hour -member------------------------------ 2004-07-23 16:00:00.000--Query start from heredeclare @start_date datetimedeclare @end_date datetimedeclare @orgid int select @start_date ='19000630 23:59:59' select @end_date = '20040701 23:59:59' select @orgid = 2345678----- [TopLoginHour-Account Holder]select top 1convert(datetime, convert(varchar(13), lh.login_history_dt, 120) + ':00:00.000') as [Highest Log-in Volume Day/Hour-Account Holder]from login_history lhinner join login l on l.login_id = lh.login_idinner join p_org po on po.p_id = l.p_idinner join p_login pl on pl.login_id = lh.login_idinner join p_account pa on pa.p_id = pl.p_idwhere login_history_dt between @start_date and @end_date and po.org_id in (@orgid) group by convert(datetime, convert(varchar(13), lh.login_history_dt, 120) + ':00:00.000')order by count(*) desc, convert(datetime, convert(varchar(13), lh.login_history_dt, 120) + ':00:00.000')----[TopLoginHour- Holder]select top 1convert(datetime, convert(varchar(13), lh.login_history_dt, 120) + ':00:00.000') as [Highest Log-in Volume Day/Hour- Holder]from login_history lhinner join login l on l.login_id = lh.login_idinner join p_org po on po.p_id = l.p_idinner join p_login pl on pl.login_id = lh.login_idinner join p_account pa on pa.p_id = pl.p_idwhere login_history_dt between @start_date and @end_date and po.org_id in (@orgid) group by convert(datetime, convert(varchar(13), lh.login_history_dt, 120) + ':00:00.000')order by count(*) desc, convert(datetime, convert(varchar(13), lh.login_history_dt, 120) + ':00:00.000')-----[TopLoginHour- Member]select top 1convert(datetime, convert(varchar(13), lh.login_history_dt, 120) + ':00:00.000') as [Highest Log-in Volume Day/ Member]from login_history lhinner join login l on l.login_id = lh.login_idinner join p_org po on po.p_id = l.medicive_p_idinner join p_login pl on pl.login_id = lh.login_idinner join p_account pa on pa.p_id = pl.p_idwhere login_history_dt between @start_date and @end_date and po.org_id in (@orgid) group by convert(datetime, convert(varchar(13), lh.login_history_dt, 120) + ':00:00.000')order by count(*) desc, convert(datetime, convert(varchar(13), lh.login_history_dt, 120) + ':00:00.000')How can i make the output looks like:( put them in one line) Account Holder Holder Member---------------- ------------------- ----------------2004-04-23 10:00am 2004-03-23 11:00am 2004-07-23 4:00 pm |
|
|
ehorn
Master Smack Fu Yak Hacker
1632 Posts |
Posted - 2004-07-11 : 14:14:10
|
The following example show how you could use union along with max to pivot the data into 1 row:create table t ( v int)insert into tselect 1 union allselect 2 union allselect 3select max(a) a, max(b) b,max(c) cfrom(select v a,0 b,0 cfrom twhere v = 1union select 0,v,0from twhere v = 2union select 0,0,vfrom twhere v = 3) ddrop table t The following UDF might be useful for formatting those dates also.http://weblogs.sqlteam.com/leed/archive/2004/06/04/1503.aspx?Pending=true |
 |
|
|
Kristen
Test
22859 Posts |
|
|
|
|
|
|
|