Showing posts with label created. Show all posts
Showing posts with label created. Show all posts

Wednesday, March 21, 2012

Group Ranking #2

Here is a try. I created a view to do it readable, you can substitute it by
its definition if you one just one #$%^@.&* shot.
use tempdb;
go
create table t(
invoice int not null unique,
paidontime char(3) not null check(paidontime in ('no', 'yes'))
)
insert into t values(1006, 'yes')
insert into t values(1005, 'yes')
insert into t values(1004, 'no')
insert into t values(1003, 'yes')
insert into t values(1002, 'yes')
insert into t values(1001, 'yes')
insert into t values(1000, 'no')
go
create view my_view
as
select
(
select
case
when (select count(distinct d.paidontime) from t as d) > 1 then
coalesce(max(b.invoice), a.invoice - 1)
else 1
end
from
t as b
where
b.invoice < a.invoice
and b.paidontime <> a.paidontime
) as group_id,
a.invoice,
a.paidontime
from
t as a
go
select
t4.rank,
t3.invoice,
t3.paidontime
from
my_view as t3
inner join
(
select
t1.group_id,
count(distinct t2.group_id) as rank
from
my_view as t1
inner join
my_view as t2
on t1.group_id <= t2.group_id
group by
t1.group_id
) as t4
on t3.group_id = t4.group_id
order by
t3.invoice desc
go
drop view my_view
go
drop table t
go
AMB
"mekim" wrote:

> Hi All,
> I have a table that I wish to group by the change or switch in the
> "PaidOnTime" column while sequenced by descending invoice #
> I have read a bunch of gr8 articles that describe this situation...
> i.e.
> http://www.databasejournal.com/feat...10894_2244821_2
> http://support.microsoft.com/defaul...b;EN-US;q186133
> however this is with a twist...I don't want to group or number "Groups" -
I
> want to group the "Switch" in the groups.. (I'm trying my best not to make
> this sound confusing - one could only imagine if I wasn't trying :-)
> Anyway - here's sample data
> Input
> --
> Invoice PaidOnTime
> 1006 Yes
> 1005 Yes
> 1004 No
> 1003 Yes
> 1002 Yes
> 1001 Yes
> 1000 No
> -- RIGHT Output :-D
> Group Invoice PaidOnTime
> 1 1006 Yes
> 1 1005 Yes
> 2 1004 No
> 3 1003 Yes
> 3 1002 Yes
> 3 1001 Yes
> 4 1000 No
>
> The below is the best I can get :-(
> -- Wrong Output Since it Groups by PaidOnTime and not the "Switching" of
> PaidOnTime
> Group Invoice PaidOnTime
> 1 1006 Yes
> 1 1005 Yes
> 2 1004 No
> 1 1003 Yes
> 1 1002 Yes
> 1 1001 Yes
> 2 1000 No
>
> Best Regards,
> Mekim
> p.s. Excuse the dbl post plzCorrection,
use tempdb;
go
create table t(
invoice int not null unique,
paidontime char(3) not null check(paidontime in ('no', 'yes'))
)
insert into t values(1006, 'yes')
insert into t values(1005, 'yes')
insert into t values(1004, 'no')
insert into t values(1003, 'yes')
insert into t values(1002, 'yes')
insert into t values(1001, 'yes')
insert into t values(1000, 'no')
go
create view my_view
as
select
(
select
case
when exists(select * from t as c where c.invoice < a.invoice and
c.paidontime <> a.paidontime) then max(b.invoice)
else -1
end
from
t as b
where
b.invoice < a.invoice
and b.paidontime <> a.paidontime
) as group_id,
a.invoice,
a.paidontime
from
t as a
go
select * from my_view
go
select
t4.rank,
t3.invoice,
t3.paidontime
from
my_view as t3
inner join
(
select
t1.group_id,
count(distinct t2.group_id) as rank
from
my_view as t1
inner join
my_view as t2
on t1.group_id <= t2.group_id
group by
t1.group_id
) as t4
on t3.group_id = t4.group_id
order by
t3.invoice desc
go
drop view my_view
go
drop table t
go
AMB
"Alejandro Mesa" wrote:
> Here is a try. I created a view to do it readable, you can substitute it b
y
> its definition if you one just one #$%^@.&* shot.
> use tempdb;
> go
> create table t(
> invoice int not null unique,
> paidontime char(3) not null check(paidontime in ('no', 'yes'))
> )
> insert into t values(1006, 'yes')
> insert into t values(1005, 'yes')
> insert into t values(1004, 'no')
> insert into t values(1003, 'yes')
> insert into t values(1002, 'yes')
> insert into t values(1001, 'yes')
> insert into t values(1000, 'no')
> go
> create view my_view
> as
> select
> (
> select
> case
> when (select count(distinct d.paidontime) from t as d) > 1 then
> coalesce(max(b.invoice), a.invoice - 1)
> else 1
> end
> from
> t as b
> where
> b.invoice < a.invoice
> and b.paidontime <> a.paidontime
> ) as group_id,
> a.invoice,
> a.paidontime
> from
> t as a
> go
> select
> t4.rank,
> t3.invoice,
> t3.paidontime
> from
> my_view as t3
> inner join
> (
> select
> t1.group_id,
> count(distinct t2.group_id) as rank
> from
> my_view as t1
> inner join
> my_view as t2
> on t1.group_id <= t2.group_id
> group by
> t1.group_id
> ) as t4
> on t3.group_id = t4.group_id
> order by
> t3.invoice desc
> go
> drop view my_view
> go
> drop table t
> go
>
> AMB
> "mekim" wrote:
>|||u r - Alejandro-mazing!!! - it works EXACTLY the way I need to
I know my "posting" was not the clearest - so thx for figuring that out also
Alejandro - I can understand basically "what u did - I'm just not so certain
"how u did" it - kind of intense - but I am certainly putting the effort
into deciphering it
My Very Much Thx!!!
Mekim
"Alejandro Mesa" wrote:
> Correction,
> use tempdb;
> go
> create table t(
> invoice int not null unique,
> paidontime char(3) not null check(paidontime in ('no', 'yes'))
> )
> insert into t values(1006, 'yes')
> insert into t values(1005, 'yes')
> insert into t values(1004, 'no')
> insert into t values(1003, 'yes')
> insert into t values(1002, 'yes')
> insert into t values(1001, 'yes')
> insert into t values(1000, 'no')
> go
> create view my_view
> as
> select
> (
> select
> case
> when exists(select * from t as c where c.invoice < a.invoice and
> c.paidontime <> a.paidontime) then max(b.invoice)
> else -1
> end
> from
> t as b
> where
> b.invoice < a.invoice
> and b.paidontime <> a.paidontime
> ) as group_id,
> a.invoice,
> a.paidontime
> from
> t as a
> go
> select * from my_view
> go
> select
> t4.rank,
> t3.invoice,
> t3.paidontime
> from
> my_view as t3
> inner join
> (
> select
> t1.group_id,
> count(distinct t2.group_id) as rank
> from
> my_view as t1
> inner join
> my_view as t2
> on t1.group_id <= t2.group_id
> group by
> t1.group_id
> ) as t4
> on t3.group_id = t4.group_id
> order by
> t3.invoice desc
> go
> drop view my_view
> go
> drop table t
> go
>
> AMB
> "Alejandro Mesa" wrote:
>|||You are welcome. Here is a new version, I changed the view definition a
little bit.
I am supposing that invoice numbers are unique and greater than zero. Based
on that, I am using the max invoice number that is less than current and
which paidontime value is diff from the current as the group_id (excuse my
english). If you select from the view, you will see:
group_id invoice paidontime
-- -- --
1004 1006 yes
1004 1005 yes
1003 1004 no
1000 1003 yes
1000 1002 yes
1000 1001 yes
-1 1000 no
then you can use the tehcnique describe in KB Q186133, to calculate the rank
:
select
t1.group_id,
count(distinct t2.group_id) as rank
from
my_view as t1
inner join
my_view as t2
on t1.group_id <= t2.group_id
group by
t1.group_id
if you join previous statement with the view by group_id, then you have all
the info needed.
use tempdb;
go
create table t(
invoice int not null unique check (invoice > 0),
paidontime char(3) not null check(paidontime in ('no', 'yes'))
)
insert into t values(1006, 'yes')
insert into t values(1005, 'yes')
insert into t values(1004, 'no')
insert into t values(1003, 'yes')
insert into t values(1002, 'yes')
insert into t values(1001, 'yes')
insert into t values(1000, 'no')
go
create view my_view
as
select
(
select
coalesce(max(b.invoice), -1)
from
t as b
where
b.invoice < a.invoice
and b.paidontime <> a.paidontime
) as group_id,
a.invoice,
a.paidontime
from
t as a
go
select * from my_view
go
select
t4.rank,
t3.invoice,
t3.paidontime
from
my_view as t3
inner join
(
select
t1.group_id,
count(distinct t2.group_id) as rank
from
my_view as t1
inner join
my_view as t2
on t1.group_id <= t2.group_id
group by
t1.group_id
) as t4
on t3.group_id = t4.group_id
order by
t3.invoice desc
go
drop view my_view
go
drop table t
go
AMB
"mekim" wrote:
> u r - Alejandro-mazing!!! - it works EXACTLY the way I need to
> I know my "posting" was not the clearest - so thx for figuring that out al
so
> Alejandro - I can understand basically "what u did - I'm just not so certa
in
> "how u did" it - kind of intense - but I am certainly putting the effort
> into deciphering it
> My Very Much Thx!!!
> Mekim
> "Alejandro Mesa" wrote:
>|||Hi Alejandro,
Hmmm...The 2nd one seems to be more efficent - which I'm not sure why - but
I will try to figure it out - but this brings up an interesting option for m
e
- going the other direction as well
It turns out it's more important to know the "prior" & "Next" invoice out of
the group series then any sort of ranking
Is it possible to get the output to look as follows in one view? I got it
to work separately - but not together w/o a join
group_id2 group_id invoice paidontime
-- -- -- --
-1 1004 1006 yes
-1 1004 1005 yes
1005 1003 1004 no
1004 1000 1003 yes
1004 1000 1002 yes
1004 1000 1001 yes
1001 -1 1000 no
I tried and it works (and again - I have not fully decipered the SQL
Statement so plz excuse the question)
HOWEVER - Is there a way to merge these two in the same statement or does it
need to be a join of some sorts
the follow view gives me these results
group_id invoice paidontime
-- -- --
-1 1006 yes
-1 1005 yes
1005 1004 no
1004 1003 yes
1004 1002 yes
1004 1001 yes
1001 1000 no
drop view my_view
go
create view my_view
as
select
(
select
case
when exists(select * from t as c where c.invoice > a.invoice and
c.paidontime <> a.paidontime) then Min(b.invoice)
else -1
end
from
t as b
where
b.invoice > a.invoice
and b.paidontime <> a.paidontime
) as group_id,
a.invoice,
a.paidontime
from
t as a
The best I can see is I need to join these two views via Invoice - which is
fine - however - greed kicks in to be more efficent
again - I think this is so how u pulled it off - I guess u've probably
figured out that I tried many times w/o success to make it work :-)
Best Regards,
Mekim
"Alejandro Mesa" wrote:
> You are welcome. Here is a new version, I changed the view definition a
> little bit.
> I am supposing that invoice numbers are unique and greater than zero. Base
d
> on that, I am using the max invoice number that is less than current and
> which paidontime value is diff from the current as the group_id (excuse my
> english). If you select from the view, you will see:
> group_id invoice paidontime
> -- -- --
> 1004 1006 yes
> 1004 1005 yes
> 1003 1004 no
> 1000 1003 yes
> 1000 1002 yes
> 1000 1001 yes
> -1 1000 no
> then you can use the tehcnique describe in KB Q186133, to calculate the ra
nk:
> select
> t1.group_id,
> count(distinct t2.group_id) as rank
> from
> my_view as t1
> inner join
> my_view as t2
> on t1.group_id <= t2.group_id
> group by
> t1.group_id
> if you join previous statement with the view by group_id, then you have al
l
> the info needed.
> use tempdb;
> go
> create table t(
> invoice int not null unique check (invoice > 0),
> paidontime char(3) not null check(paidontime in ('no', 'yes'))
> )
> insert into t values(1006, 'yes')
> insert into t values(1005, 'yes')
> insert into t values(1004, 'no')
> insert into t values(1003, 'yes')
> insert into t values(1002, 'yes')
> insert into t values(1001, 'yes')
> insert into t values(1000, 'no')
> go
> create view my_view
> as
> select
> (
> select
> coalesce(max(b.invoice), -1)
> from
> t as b
> where
> b.invoice < a.invoice
> and b.paidontime <> a.paidontime
> ) as group_id,
> a.invoice,
> a.paidontime
> from
> t as a
> go
> select * from my_view
> go
> select
> t4.rank,
> t3.invoice,
> t3.paidontime
> from
> my_view as t3
> inner join
> (
> select
> t1.group_id,
> count(distinct t2.group_id) as rank
> from
> my_view as t1
> inner join
> my_view as t2
> on t1.group_id <= t2.group_id
> group by
> t1.group_id
> ) as t4
> on t3.group_id = t4.group_id
> order by
> t3.invoice desc
> go
> drop view my_view
> go
> drop table t
> go
>
> AMB
> "mekim" wrote:
>

Monday, March 19, 2012

Group members of a derived dimension

Hi, Have created a dimension based on a column in the FACT, called Age as given in the post (http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=199100&SiteID=1) . Now, I need to group these members in custom buckets. The 'aggregate*' property is not allowing for custom buckets. Since this is a derived dimension, it is not appearing in the list of dimensions under the 'Create Custom Member formula' under 'Add BI' feature. How to accomplish this?

Secondly, If I try to create a calculated member on this dimension, why should it be attached under an existing attribute? So, even this route didnt work.

Thanks ina dvance,

If you followed the steps in that post, what you created in the fact table with your case statement is a set of derived surrogate keys. You would then link these keys to a dimension created from a table/view in your dsv. In this dimension table you could then add an extra column for a grouping and specify the group for each member.

Group Headers Not Showing In Excel

Hi,
I have created some reports that I need to export to Excel. I have realised
that when they are exported, the group headers do not appear in Excel, just
the table header and footer, group footers and details group.
Does anyone know why the group headers are ommitted from the Excel files and
how I can get around this problem?
ThanksAre the group headers hidden, or are they missing completely?
"Tabby Cool via SQLMonster.com" wrote:
> Hi,
> I have created some reports that I need to export to Excel. I have realised
> that when they are exported, the group headers do not appear in Excel, just
> the table header and footer, group footers and details group.
> Does anyone know why the group headers are ommitted from the Excel files and
> how I can get around this problem?
> Thanks
>|||They are missing completely.
I hope they fix this in the 2005 version of RS!
daw wrote:
>Are the group headers hidden, or are they missing completely?
>> Hi,
>[quoted text clipped - 6 lines]
>> Thanks
Message posted via SQLMonster.com
http://www.sqlmonster.com/Uwe/Forums.aspx/sql-server-reporting/200509/1

Wednesday, March 7, 2012

Group by Hour

Hello,
I have created a report which gets Clock In and Clock Out time for an
Employee and I would like to group this report by Hour so it would look
something like:
7:00-8:00
A
B
C
8:00-9:00
A
B
9:00-10:00
... AND so on.
So basically, I like to see who was here for each hour starting 7:00 am. My
query that I created is:
SELECT dbo.Employee.FirstName, dbo.Employee.LastName,
dbo.OrganizationUnit.Code AS Dept, dbo.OrganizationUnit.Description AS [Dept
Name],
dbo.EmployeeClocking.WhenCreated AS InClock,
EmployeeClocking_1.WhenCreated AS OutClock, dbo.JobClass.Code AS [Job Code],
dbo.JobClass.Description AS Title
FROM dbo.Employee INNER JOIN
dbo.EmployeeClocking ON dbo.Employee.ID = dbo.EmployeeClocking.EmployeeID INNER JOIN
dbo.OrganizationUnit ON
dbo.EmployeeClocking.OrganizationUnitID = dbo.OrganizationUnit.ID INNER JOIN
dbo.JobClass ON dbo.EmployeeClocking.JobClassID = dbo.JobClass.ID INNER JOIN
dbo.EmployeeClocking EmployeeClocking_1 ON
dbo.EmployeeClocking.OutClockingGuid = EmployeeClocking_1.Guid
WHERE (dbo.OrganizationUnit.Code = @.Department) AND
(dbo.EmployeeClocking.WhenCreated BETWEEN @.BeginDate AND @.EndDate)
And I need help to group it hourly and show employees who worked between
those hours. Thanks in advance for any assistance.kalindi,
Add a column to your dataset for the hour, and group by that field.
Here is a sample query.
select cast((cast(dateField as float) - cast(dateField as int)) * 24 as
int) as hour from temp4
Casting dateField as float gets you the julian date with time. Example:
right now it is 38973.4013944444.
Casting dateField as int gets you the julian date without time.
Example: right now it is 38973.
Subtracting the 2 gets you 0.4013944444. Multiply that result by 24 and
you get 9.6334666656. Just for fun, multiply 0.6334666656 by 60, and
you get 38. That means it is 9:38am.
Casting 9.6334666656 as int gets you 9.
Do whatever you need to do to get the group header to display the time
in the format that you mentioned (7:00-8:00).
Hope this helps!
-Josh
kalindi05 wrote:
> Hello,
> I have created a report which gets Clock In and Clock Out time for an
> Employee and I would like to group this report by Hour so it would look
> something like:
> 7:00-8:00
> A
> B
> C
> 8:00-9:00
> A
> B
> 9:00-10:00
> ... AND so on.
> So basically, I like to see who was here for each hour starting 7:00 am. My
> query that I created is:
> SELECT dbo.Employee.FirstName, dbo.Employee.LastName,
> dbo.OrganizationUnit.Code AS Dept, dbo.OrganizationUnit.Description AS [Dept
> Name],
> dbo.EmployeeClocking.WhenCreated AS InClock,
> EmployeeClocking_1.WhenCreated AS OutClock, dbo.JobClass.Code AS [Job Code],
> dbo.JobClass.Description AS Title
> FROM dbo.Employee INNER JOIN
> dbo.EmployeeClocking ON dbo.Employee.ID => dbo.EmployeeClocking.EmployeeID INNER JOIN
> dbo.OrganizationUnit ON
> dbo.EmployeeClocking.OrganizationUnitID = dbo.OrganizationUnit.ID INNER JOIN
> dbo.JobClass ON dbo.EmployeeClocking.JobClassID => dbo.JobClass.ID INNER JOIN
> dbo.EmployeeClocking EmployeeClocking_1 ON
> dbo.EmployeeClocking.OutClockingGuid = EmployeeClocking_1.Guid
> WHERE (dbo.OrganizationUnit.Code = @.Department) AND
> (dbo.EmployeeClocking.WhenCreated BETWEEN @.BeginDate AND @.EndDate)
> And I need help to group it hourly and show employees who worked between
> those hours. Thanks in advance for any assistance.|||Couldn't you just group by =Datepart(h,InClock) ? It seems like that
should work so long as your field is datetime datatype.
bell.joshua@.gmail.com wrote:
> kalindi,
> Add a column to your dataset for the hour, and group by that field.
> Here is a sample query.
> select cast((cast(dateField as float) - cast(dateField as int)) * 24 as
> int) as hour from temp4
> Casting dateField as float gets you the julian date with time. Example:
> right now it is 38973.4013944444.
> Casting dateField as int gets you the julian date without time.
> Example: right now it is 38973.
> Subtracting the 2 gets you 0.4013944444. Multiply that result by 24 and
> you get 9.6334666656. Just for fun, multiply 0.6334666656 by 60, and
> you get 38. That means it is 9:38am.
> Casting 9.6334666656 as int gets you 9.
> Do whatever you need to do to get the group header to display the time
> in the format that you mentioned (7:00-8:00).
> Hope this helps!
> -Josh
>
> kalindi05 wrote:
> > Hello,
> >
> > I have created a report which gets Clock In and Clock Out time for an
> > Employee and I would like to group this report by Hour so it would look
> > something like:
> >
> > 7:00-8:00
> > A
> > B
> > C
> > 8:00-9:00
> > A
> > B
> > 9:00-10:00
> > ... AND so on.
> >
> > So basically, I like to see who was here for each hour starting 7:00 am. My
> > query that I created is:
> > SELECT dbo.Employee.FirstName, dbo.Employee.LastName,
> > dbo.OrganizationUnit.Code AS Dept, dbo.OrganizationUnit.Description AS [Dept
> > Name],
> > dbo.EmployeeClocking.WhenCreated AS InClock,
> > EmployeeClocking_1.WhenCreated AS OutClock, dbo.JobClass.Code AS [Job Code],
> > dbo.JobClass.Description AS Title
> > FROM dbo.Employee INNER JOIN
> > dbo.EmployeeClocking ON dbo.Employee.ID => > dbo.EmployeeClocking.EmployeeID INNER JOIN
> > dbo.OrganizationUnit ON
> > dbo.EmployeeClocking.OrganizationUnitID = dbo.OrganizationUnit.ID INNER JOIN
> > dbo.JobClass ON dbo.EmployeeClocking.JobClassID => > dbo.JobClass.ID INNER JOIN
> > dbo.EmployeeClocking EmployeeClocking_1 ON
> > dbo.EmployeeClocking.OutClockingGuid = EmployeeClocking_1.Guid
> > WHERE (dbo.OrganizationUnit.Code = @.Department) AND
> > (dbo.EmployeeClocking.WhenCreated BETWEEN @.BeginDate AND @.EndDate)
> >
> > And I need help to group it hourly and show employees who worked between
> > those hours. Thanks in advance for any assistance.|||kalindi,
I always hate finding out that I took the long way. =)
The datepart function is a much better approach. I prefer to do stuff
like that in my SQL, and the SQL Server 2005 syntax for the hour
datepart looks like this:
select datepart(hh,dateField) as theHour from temp4
So, whether or not you add this calculation to the datasource or just
use it as the group by expression is up to you.
-Josh
toolman wrote:
> Couldn't you just group by =Datepart(h,InClock) ? It seems like that
> should work so long as your field is datetime datatype.
> bell.joshua@.gmail.com wrote:
> > kalindi,
> >
> > Add a column to your dataset for the hour, and group by that field.
> > Here is a sample query.
> >
> > select cast((cast(dateField as float) - cast(dateField as int)) * 24 as
> > int) as hour from temp4
> >
> > Casting dateField as float gets you the julian date with time. Example:
> > right now it is 38973.4013944444.
> >
> > Casting dateField as int gets you the julian date without time.
> > Example: right now it is 38973.
> >
> > Subtracting the 2 gets you 0.4013944444. Multiply that result by 24 and
> > you get 9.6334666656. Just for fun, multiply 0.6334666656 by 60, and
> > you get 38. That means it is 9:38am.
> >
> > Casting 9.6334666656 as int gets you 9.
> >
> > Do whatever you need to do to get the group header to display the time
> > in the format that you mentioned (7:00-8:00).
> >
> > Hope this helps!
> >
> > -Josh
> >
> >
> > kalindi05 wrote:
> > > Hello,
> > >
> > > I have created a report which gets Clock In and Clock Out time for an
> > > Employee and I would like to group this report by Hour so it would look
> > > something like:
> > >
> > > 7:00-8:00
> > > A
> > > B
> > > C
> > > 8:00-9:00
> > > A
> > > B
> > > 9:00-10:00
> > > ... AND so on.
> > >
> > > So basically, I like to see who was here for each hour starting 7:00 am. My
> > > query that I created is:
> > > SELECT dbo.Employee.FirstName, dbo.Employee.LastName,
> > > dbo.OrganizationUnit.Code AS Dept, dbo.OrganizationUnit.Description AS [Dept
> > > Name],
> > > dbo.EmployeeClocking.WhenCreated AS InClock,
> > > EmployeeClocking_1.WhenCreated AS OutClock, dbo.JobClass.Code AS [Job Code],
> > > dbo.JobClass.Description AS Title
> > > FROM dbo.Employee INNER JOIN
> > > dbo.EmployeeClocking ON dbo.Employee.ID => > > dbo.EmployeeClocking.EmployeeID INNER JOIN
> > > dbo.OrganizationUnit ON
> > > dbo.EmployeeClocking.OrganizationUnitID = dbo.OrganizationUnit.ID INNER JOIN
> > > dbo.JobClass ON dbo.EmployeeClocking.JobClassID => > > dbo.JobClass.ID INNER JOIN
> > > dbo.EmployeeClocking EmployeeClocking_1 ON
> > > dbo.EmployeeClocking.OutClockingGuid = EmployeeClocking_1.Guid
> > > WHERE (dbo.OrganizationUnit.Code = @.Department) AND
> > > (dbo.EmployeeClocking.WhenCreated BETWEEN @.BeginDate AND @.EndDate)
> > >
> > > And I need help to group it hourly and show employees who worked between
> > > those hours. Thanks in advance for any assistance.|||If you are not going over multiple days, the datepart is definitely the
easiest way to go.
Bruce Loehle-Conger
MVP SQL Server Reporting Services
<bell.joshua@.gmail.com> wrote in message
news:1158341470.346786.90070@.i3g2000cwc.googlegroups.com...
> kalindi,
> I always hate finding out that I took the long way. =)
> The datepart function is a much better approach. I prefer to do stuff
> like that in my SQL, and the SQL Server 2005 syntax for the hour
> datepart looks like this:
> select datepart(hh,dateField) as theHour from temp4
> So, whether or not you add this calculation to the datasource or just
> use it as the group by expression is up to you.
> -Josh
>
> toolman wrote:
>> Couldn't you just group by =Datepart(h,InClock) ? It seems like that
>> should work so long as your field is datetime datatype.
>> bell.joshua@.gmail.com wrote:
>> > kalindi,
>> >
>> > Add a column to your dataset for the hour, and group by that field.
>> > Here is a sample query.
>> >
>> > select cast((cast(dateField as float) - cast(dateField as int)) * 24 as
>> > int) as hour from temp4
>> >
>> > Casting dateField as float gets you the julian date with time. Example:
>> > right now it is 38973.4013944444.
>> >
>> > Casting dateField as int gets you the julian date without time.
>> > Example: right now it is 38973.
>> >
>> > Subtracting the 2 gets you 0.4013944444. Multiply that result by 24 and
>> > you get 9.6334666656. Just for fun, multiply 0.6334666656 by 60, and
>> > you get 38. That means it is 9:38am.
>> >
>> > Casting 9.6334666656 as int gets you 9.
>> >
>> > Do whatever you need to do to get the group header to display the time
>> > in the format that you mentioned (7:00-8:00).
>> >
>> > Hope this helps!
>> >
>> > -Josh
>> >
>> >
>> > kalindi05 wrote:
>> > > Hello,
>> > >
>> > > I have created a report which gets Clock In and Clock Out time for an
>> > > Employee and I would like to group this report by Hour so it would
>> > > look
>> > > something like:
>> > >
>> > > 7:00-8:00
>> > > A
>> > > B
>> > > C
>> > > 8:00-9:00
>> > > A
>> > > B
>> > > 9:00-10:00
>> > > ... AND so on.
>> > >
>> > > So basically, I like to see who was here for each hour starting 7:00
>> > > am. My
>> > > query that I created is:
>> > > SELECT dbo.Employee.FirstName, dbo.Employee.LastName,
>> > > dbo.OrganizationUnit.Code AS Dept, dbo.OrganizationUnit.Description
>> > > AS [Dept
>> > > Name],
>> > > dbo.EmployeeClocking.WhenCreated AS InClock,
>> > > EmployeeClocking_1.WhenCreated AS OutClock, dbo.JobClass.Code AS [Job
>> > > Code],
>> > > dbo.JobClass.Description AS Title
>> > > FROM dbo.Employee INNER JOIN
>> > > dbo.EmployeeClocking ON dbo.Employee.ID =>> > > dbo.EmployeeClocking.EmployeeID INNER JOIN
>> > > dbo.OrganizationUnit ON
>> > > dbo.EmployeeClocking.OrganizationUnitID = dbo.OrganizationUnit.ID
>> > > INNER JOIN
>> > > dbo.JobClass ON dbo.EmployeeClocking.JobClassID
>> > > =>> > > dbo.JobClass.ID INNER JOIN
>> > > dbo.EmployeeClocking EmployeeClocking_1 ON
>> > > dbo.EmployeeClocking.OutClockingGuid = EmployeeClocking_1.Guid
>> > > WHERE (dbo.OrganizationUnit.Code = @.Department) AND
>> > > (dbo.EmployeeClocking.WhenCreated BETWEEN @.BeginDate AND @.EndDate)
>> > >
>> > > And I need help to group it hourly and show employees who worked
>> > > between
>> > > those hours. Thanks in advance for any assistance.
>

Friday, February 24, 2012

group by

I've created a table containing columns date, cost, orders etc. I want to
write a query group by the month part of the date. Is it possible to write
it. if yes how. Any suggestion would be greatly appreciated.
regards
shineSelect col1,col2,col3,MONTH(datecol)
From SomeTable
Group by col1,col2,col3,MONTH(datecol)
HTH, Jens Suessmeyer.
http://www.sqlserver2005.de
--
"shine" <shine@.discussions.microsoft.com> schrieb im Newsbeitrag
news:3FA1DE3D-DD75-442B-B969-2367E7406E48@.microsoft.com...
> I've created a table containing columns date, cost, orders etc. I want to
> write a query group by the month part of the date. Is it possible to write
> it. if yes how. Any suggestion would be greatly appreciated.
> regards
> shine|||u can get the month as
select datepart(m,getdate()) This_Month
best Regards,
Chandra
http://chanduas.blogspot.com/
http://groups.msn.com/SQLResource/
---
"shine" wrote:

> I've created a table containing columns date, cost, orders etc. I want to
> write a query group by the month part of the date. Is it possible to write
> it. if yes how. Any suggestion would be greatly appreciated.
> regards
> shine|||Two problems with the code:
(1) all the months get grouped together, regardless of the year, so the
data is meaningless
(2) You are not allowed to have a function in a GROUP BY in Standard
SQL, so this does not port.
I would use a Calendar table to get a year/month value, do a JOIN and
gorup by that.|||On 19 May 2005 08:44:25 -0700, --CELKO-- wrote:

> Two problems with the code:
> (1) all the months get grouped together, regardless of the year, so the
> data is meaningless
> (2) You are not allowed to have a function in a GROUP BY in Standard
> SQL, so this does not port.
> I would use a Calendar table to get a year/month value, do a JOIN and
> gorup by that.
A Calendar table could be real overkill in this case; it didn't sound as
though there are any data attached to specific dates, like there are in the
traditional holiday calendar table.
To get around both the problems identified above, and to show a possible
meaningful use for the grouping, use this construction:
SELECT col1, col2, col3, theYear, theMonth,
COUNT(*) "OrderCount", SUM(cost) "TotalCost"
FROM (
SELECT col1,col2,col3,YEAR(datecol) "theYear", MONTH(datecol) "theMonth"
FROM SomeTable
)
GROUP BY col1, col2, col3, theYear, theMonth|||Comments inline.
http://www.sqlserver2005.de
--
"--CELKO--" <jcelko212@.earthlink.net> schrieb im Newsbeitrag
news:1116517465.190810.201240@.o13g2000cwo.googlegroups.com...
> Two problems with the code:
> (1) all the months get grouped together, regardless of the year, so the
> data is meaningless
Even this was only a example, to show the teamwork of group functions with
the selection of oher columns. Due to the lack of DDL by the original poster
there was no clue what he wants to do. Sytanx exmaples doesnt alway make
sense since they are only syntax examples (due to the lack of DDL).

> (2) You are not allowed to have a function in a GROUP BY in Standard
> SQL, so this does not port.
Actually you are allowed, unless you mention the function also in your Group
by clause, if you are working on TSQL and you dont wanna port your code,
why should you bother about Standard SQL ?

> I would use a Calendar table to get a year/month value, do a JOIN and
> gorup by that.

group by

I've created a table containing columns date, cost, orders etc. I want to
write a query group by the month part of the date. Is it possible to write
it. if yes how. Any suggestion would be greatly appreciated.
regards
shineSELECT AVG(cost), DATEPART(mm, datecol)
FROM tbl
GROUP BY DATEPART(mm, datecol)
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"shine" <shine@.discussions.microsoft.com> wrote in message
news:F792B5DB-98C6-48C3-B85C-EE43E2915567@.microsoft.com...
> I've created a table containing columns date, cost, orders etc. I want to
> write a query group by the month part of the date. Is it possible to write
> it. if yes how. Any suggestion would be greatly appreciated.
> regards
> shine|||Example:
use northwind
go
select month(orderdate) as month_number, count(*) as month_cnt
from dbo.orders
group by month(orderdate);
AMB
"shine" wrote:

> I've created a table containing columns date, cost, orders etc. I want to
> write a query group by the month part of the date. Is it possible to write
> it. if yes how. Any suggestion would be greatly appreciated.
> regards
> shine

Sunday, February 19, 2012

GridView wont delete or update

I have had this problem before but it turned out to be dodgy SQL created by the wizard. Doesn't seem to be the case this time.

The following does a postback but makes no changes.

1<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ehlConnectionString %>"2DeleteCommand="DELETE FROM [tblSubRegions] WHERE [SubRegionID] = ?"3InsertCommand="INSERT INTO [tblSubRegions] ([SubRegionID], [RegionID], [SubRegionName]) VALUES (?, ?, ?)"4ProviderName="<%$ ConnectionStrings:ehlConnectionString.ProviderName %>"5SelectCommand="SELECT tblSubRegions.SubRegionID, tblSubRegions.RegionID, tblSubRegions.SubRegionName, tblRegions.RegionName FROM (tblSubRegions INNER JOIN tblRegions ON tblSubRegions.RegionID = tblRegions.RegionID) WHERE (tblSubRegions.RegionID = ?) ORDER BY tblSubRegions.SubRegionName"6UpdateCommand="UPDATE [tblSubRegions] SET [RegionID] = ?, [SubRegionName] = ? WHERE [SubRegionID] = ?">78<DeleteParameters>9 <asp:Parameter Name="SubRegionID" Type="Int32" />10</DeleteParameters>1112<UpdateParameters>13<asp:Parameter Name="RegionID" Type="Int32" />14<asp:Parameter Name="SubRegionName" Type="String" />15<asp:Parameter Name="SubRegionID" Type="Int32" />16</UpdateParameters>1718<SelectParameters>19<asp:ControlParameter ControlID="dropRegions" Name="RegionID" PropertyName="SelectedValue" Type="Int32" />20</SelectParameters>2122<InsertParameters>23<asp:Parameter Name="SubRegionID" Type="Int32" />24<asp:Parameter Name="RegionID" Type="Int32" />25<asp:Parameter Name="SubRegionName" Type="String" />26</InsertParameters>2728</asp:SqlDataSource>29303132<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ehlConnectionString %>"33ProviderName="<%$ ConnectionStrings:ehlConnectionString.ProviderName %>"34SelectCommand="SELECT [RegionID], [RegionName] FROM [tblRegions]">3536</asp:SqlDataSource>37383940<asp:DropDownList id="dropStates" runat="server" OnSelectedIndexChanged="dropStates_SelectedIndexChanged" AutoPostBack="True">41</asp:DropDownList>4243<asp:DropDownList id="dropRegions" runat="server" OnSelectedIndexChanged="dropRegions_SelectedIndexChanged" AutoPostBack="True">44</asp:DropDownList>45464748 <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"49 AutoGenerateColumns="False" EnableViewState=false Width="100%" DataSourceID="SqlDataSource1">50 <Columns>51 <asp:TemplateField HeaderText="SubRegionName" SortExpression="SubRegionName">52 <EditItemTemplate>53 <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource2"54 DataTextField="RegionName" DataValueField="RegionID" SelectedValue='<%# Bind("RegionID") %>'>55 </asp:DropDownList>56 </EditItemTemplate>57 <ItemTemplate>58 <asp:Label ID="Label1" runat="server" Text='<%# Bind("SubRegionName") %>'></asp:Label>59 </ItemTemplate>60 </asp:TemplateField>61 <asp:BoundField DataField="RegionName" HeaderText="RegionName" SortExpression="RegionName" />62 <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />63 </Columns>64 </asp:GridView>

Thanks in advance.

Shaun

You need to set DataKeyNames="yourprimarykey"

GridView - SqlDataSource

I have created a GridView that uses a SqlDataSource. When I run the page it does not pull back any data. However when I test the query in the SqlDataSource dialog box it pulls back data.

Here is my GridView and SqlDataSource:

<

asp:GridViewID="Results"runat="server"AllowPaging="True"AllowSorting="True"CellPadding="2"EmptyDataText="No records found."AutoGenerateColumns="False"Width="100%"CssClass="tableResults"PageSize="20"DataSourceID="SqlResults"><Columns><asp:BoundFieldDataField="DaCode"HeaderText="Sub-Station"SortExpression="DaCode"><ItemStyleHorizontalAlign="Center"CssClass="tdResults"/><HeaderStyleHorizontalAlign="Center"CssClass="tdHeaderResults"/></asp:BoundField><asp:BoundFieldDataField="DpInfo"HeaderText="Delivery Point"SortExpression="DpInfo"><HeaderStyleHorizontalAlign="Left"CssClass="tdHeaderResults"/><ItemStyleCssClass="tdResults"/></asp:BoundField><asp:HyperLinkFieldDataNavigateUrlFields="CuCode,OrderID"DataNavigateUrlFormatString="TCCustDetail.asp?CuCode={0}&OrderID={1}"DataTextField="OrderID"HeaderText="Order No"SortExpression="OrderID"><ItemStyleCssClass="tdResults"HorizontalAlign="Center"/><HeaderStyleCssClass="tdHeaderResults"HorizontalAlign="Center"/></asp:HyperLinkField><asp:BoundFieldHeaderText="Order Date"SortExpression="OrderDate"DataField="OrderDate"><ItemStyleHorizontalAlign="Center"CssClass="tdResults"/><HeaderStyleHorizontalAlign="Center"CssClass="tdHeaderResults"/></asp:BoundField><asp:BoundFieldDataField="ReqDeliveryDate"HeaderText="Req Delivery Date"SortExpression="ReqDeliveryDate"><ItemStyleHorizontalAlign="Center"CssClass="tdResults"/><HeaderStyleHorizontalAlign="Center"CssClass="tdHeaderResults"/></asp:BoundField><asp:BoundFieldDataField="StatusDate"HeaderText="Status Date"SortExpression="StatusDate"><ItemStyleHorizontalAlign="Center"CssClass="tdResults"/><HeaderStyleHorizontalAlign="Center"CssClass="tdHeaderResults"/></asp:BoundField><asp:BoundFieldDataField="ManifestNo"HeaderText="Manifest No"SortExpression="ManifestNo"><ItemStyleHorizontalAlign="Center"CssClass="tdResults"/><HeaderStyleHorizontalAlign="Center"CssClass="tdHeaderResults"/></asp:BoundField><asp:BoundFieldDataField="CustomerPO"HeaderText="P.O. No"SortExpression="CustomerPO"><ItemStyleHorizontalAlign="Center"CssClass="tdResults"/><HeaderStyleHorizontalAlign="Center"CssClass="tdHeaderResults"/></asp:BoundField><asp:BoundFieldDataField="Class"HeaderText="Class"SortExpression="Class"><ItemStyleHorizontalAlign="Left"CssClass="tdResults"/><HeaderStyleHorizontalAlign="Left"CssClass="tdHeaderResults"/></asp:BoundField><asp:BoundFieldDataField="OrderStatus"HeaderText="Order Status"SortExpression="StatusSort"><ItemStyleHorizontalAlign="Left"CssClass="tdResults"/><HeaderStyleHorizontalAlign="Left"CssClass="tdHeaderResults"/></asp:BoundField></Columns><HeaderStyleForeColor="White"HorizontalAlign="Left"/><AlternatingRowStyleCssClass="tdResultsAltRowColor"/></asp:GridView><asp:SqlDataSourceID="SqlResults"runat="server"ConnectionString="<%$ ConnectionStrings:TransportationConnectionString %>"SelectCommand="GetOrderSummaryResults"SelectCommandType="StoredProcedure"><SelectParameters><asp:ParameterDefaultValue="10681"Name="CuCode"Type="String"/><asp:ParameterDefaultValue=""Name="DaCode"Type="String"/><asp:ParameterDefaultValue=""Name="DpCode"Type="String"/><asp:ParameterDefaultValue=""Name="OrderID"Type="String"/><asp:ParameterDefaultValue=""Name="ManifestNo"Type="String"/><asp:ParameterDefaultValue=""Name="PONo"Type="String"/></SelectParameters></asp:SqlDataSource>

I can get it to fill with data by manually filling the GridView without using a SqlDataSource but then I cannot get the sorting to work when I do it that way. Actually not sure if the sorting will work this way either as I cannot get it to fill with data. Any ideas would be much appreciated.

It doesn't appear as though your parameters are collecting any data in SqlDataSource. For instance, if you were storing your parameters in the querystring, you would have in your <asp:QueryParameter /> tags something such as QueryString="", or similar...