Showing posts with label containing. Show all posts
Showing posts with label containing. Show all posts

Monday, March 19, 2012

Aggregation on monthly base

I need to create a report containing the headcounts of the employees for each month. The data I can retrieve from the database looks like this:

Name

StartEmp

EndEmp

tom

1/jan/05

15/mrt/05

dirk

1/jan/05

31/mrt/06

jan

1/feb/05

NULL

In order to get the right information for the report the information should be represented in the following way:

January 2005

February 2005

March 2005

"April" 2005

Tom

1

1

0,5

1

Dirk

1

1

1

1

Jan

0

1

1

1

HeadCount

2

3

2,5

3


(there should be more data in the 2nd table but it's a sliced representation of the data. Sliced by 2 report parameters: startdate (=1 January 2005) and enddate (=30 April 2005) )

I need some help about building up the query to aggregate the info from table1 to the format of table2.

Hi,

Are you using SQL Server 2005? If so, check out the PIVOT function in the Books Online.

HTH

|||

Yes I use sql server 2005. I looked at the pivot function and it can help me I guess (I don't know the function that well yet). But I still think that just using this function will not give me the wanted result.

My goal is to have a report with 2 parameters: a startdate and an enddate. For example 1 January 2005 as startdate, 31 December 2005 as enddate. Then the report should show a total headcount per month. So each column is a month, each row is an employee. (It should be shown in a bar-chart afterwards but I first need to retrieve/ manipulate the data)

|||

Hmmm... this is not really the strong point of databases.
It is possible but it will be a quite complicated procedure and I'm not sure it belongs on the database tier.

If you really feel it belongs there, it can all be done with standard syntax and a lot of creativity :-) But I would implement this as an application procedure.

|||

with application procedure; do you mean the CLR stored procedures which can be written in .NET code? And will that type of stored procedure be less complex?

I tried making a stored procedure, and I used the following article a bit (it's about pivot tables for MS SQL Server): http://www.sqlservercentral.com/columnists/plarsson/pivottableformicrosoftsqlserver.asp But I'm stuck at the point where each month is a column. I really don't know how to start adding the headcounts

Please take a look at the results (3 tables almost on top of page) the sp. already creates: http://users.telenet.be/master/ict/uspHeadCount.html

I was thinking: In the headcount table I have a column for each month, so ... I iterate through the employees table, and add a row to the headcount table for each employee. But how do I add an aggregation to the month rows? Because they are dynamic.

Each row in the headcount table should contain an Employee and per employee row the columns should contain this logic:
IF
([end of employment] > Month OR [end of employment] is null)
AND [start of employment] < Month
THEN headcount = 1
ELSE
...(lets leave the else out of it for now )
(Month is the date per column)

Everything that can help me is welcome!!!! Feedback, comments, guidance on the working method, tips, suggestions, ...

Sunday, March 11, 2012

Aggregates containing subqueries...

So I already no this can't be done... but I need a suitable alternative (if one exists) so I don't have to competely re-write this already too huge query.

Anyways, in my select I have something like this:

sum( case when code in (1,2,3,4) then 0 else 1 end ) as total

which has now increase from four static values to a more dynamic format, that being a table with 47 values which may shrink or grow over time. Attempting the following fails:

sum( case when code in (select code_id from ExcludedCodes) then 0 else 1 end ) as total

because apparently you can't use selects or aggregates within an aggregate.

So any ideas on how I can get this working... is there no Array or List type variable I could just substitute in? I've already tried using a Table Variable, but that failed as well.

Please keep in mind, this is one line of a very large select containing many other fields and aggregates (on a fair amount of joins) which is used in at least four differerent reporting queries. If there is no quick and easy replacement trick I can do just let me know so I can start re-writing all of them (which is seriously going to make my head hurt).

Try this

SELECT COUNT(t1.code) AS TOTAL FROM TABLE1 t1 WHERE t1.code NOT IN (SELECT t2.code FROM TABLE2 t2)

|||

Do a LEFT JOIN with the table ExcludedCodes and then use CASE to return 0 or 1 appropriately. Then you can SUM around the CASE.

|||

The left join works perfectly, I'm not sure why I didn't think of that earlier. Just set my case to check if current_code.code_id = exclude_code.code_id then 0 else 1 and the rest of the query didn't have to change at all thanks.

|||

Well sure, use the solution from the guy who has 150 times as many points as meWink

Sunday, February 19, 2012

After server rename, xp_cmdshell problem

I have a job which runs a SP containing a xp_cmdshell call to
wzunzip.exe to unzip a file. This has worked for over a year without
a glitch. I recently renamed the server, following the proper steps I
think, and now this SP hangs at the unzip command. I can't even
cancel the job, when I try I see that the server is doing a rollback,
but it never finishes, and I have no idea what it would be rolling
back. I end up stopping the agent and restarting. I am probably
missing something easy but I can't figure it out. This same script
has always worked and continues to work on other servers, the only
change being the server rename. Any ideas on what I am missing?
Thanks,
Charles
Are you using the command-line versions of winzip (separate download)?
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"Charles Sands" <z666z@.yahoo.com> wrote in message news:1f9df651.0408140754.3047b6a@.posting.google.co m...
> I have a job which runs a SP containing a xp_cmdshell call to
> wzunzip.exe to unzip a file. This has worked for over a year without
> a glitch. I recently renamed the server, following the proper steps I
> think, and now this SP hangs at the unzip command. I can't even
> cancel the job, when I try I see that the server is doing a rollback,
> but it never finishes, and I have no idea what it would be rolling
> back. I end up stopping the agent and restarting. I am probably
> missing something easy but I can't figure it out. This same script
> has always worked and continues to work on other servers, the only
> change being the server rename. Any ideas on what I am missing?
> Thanks,
> Charles
|||"Tibor Karaszi" <tibor_please.no.email_karaszi@.hotmail.nomail.com> wrote in message news:<O2t6qnigEHA.2928@.TK2MSFTNGP10.phx.gbl>...
> Are you using the command-line versions of winzip (separate download)?
Yes I am using the command-line version. I just uninstalled and
reinstalled winzip and the command-line add-on. It is working fine
from the command line. But when I try to run it from QA it just
hangs, no indication of anything. I can see from taskmanager that
wzunzip.exe is running, but nothing happens.
[vbcol=seagreen]
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
>
> "Charles Sands" <z666z@.yahoo.com> wrote in message news:1f9df651.0408140754.3047b6a@.posting.google.co m...
|||Perhaps it is waiting form some type of input? I believe that there are silent switches and stuff for the
command-line versions. Might be worth trying out? I haven't unzipped from xp_cmdshell, but I have zipped...
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"Charles Sands" <z666z@.yahoo.com> wrote in message news:1f9df651.0408151256.29d1aded@.posting.google.c om...
> "Tibor Karaszi" <tibor_please.no.email_karaszi@.hotmail.nomail.com> wrote in message
news:<O2t6qnigEHA.2928@.TK2MSFTNGP10.phx.gbl>...[vbcol=seagreen]
> Yes I am using the command-line version. I just uninstalled and
> reinstalled winzip and the command-line add-on. It is working fine
> from the command line. But when I try to run it from QA it just
> hangs, no indication of anything. I can see from taskmanager that
> wzunzip.exe is running, but nothing happens.
>

After server rename, xp_cmdshell problem

I have a job which runs a SP containing a xp_cmdshell call to
wzunzip.exe to unzip a file. This has worked for over a year without
a glitch. I recently renamed the server, following the proper steps I
think, and now this SP hangs at the unzip command. I can't even
cancel the job, when I try I see that the server is doing a rollback,
but it never finishes, and I have no idea what it would be rolling
back. I end up stopping the agent and restarting. I am probably
missing something easy but I can't figure it out. This same script
has always worked and continues to work on other servers, the only
change being the server rename. Any ideas on what I am missing?
Thanks,
CharlesAre you using the command-line versions of winzip (separate download)?
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"Charles Sands" <z666z@.yahoo.com> wrote in message news:1f9df651.0408140754.3047b6a@.posting.
google.com...
> I have a job which runs a SP containing a xp_cmdshell call to
> wzunzip.exe to unzip a file. This has worked for over a year without
> a glitch. I recently renamed the server, following the proper steps I
> think, and now this SP hangs at the unzip command. I can't even
> cancel the job, when I try I see that the server is doing a rollback,
> but it never finishes, and I have no idea what it would be rolling
> back. I end up stopping the agent and restarting. I am probably
> missing something easy but I can't figure it out. This same script
> has always worked and continues to work on other servers, the only
> change being the server rename. Any ideas on what I am missing?
> Thanks,
> Charles|||"Tibor Karaszi" <tibor_please.no.email_karaszi@.hotmail.nomail.com> wrote in message news:<O2
t6qnigEHA.2928@.TK2MSFTNGP10.phx.gbl>...
> Are you using the command-line versions of winzip (separate download)?
Yes I am using the command-line version. I just uninstalled and
reinstalled winzip and the command-line add-on. It is working fine
from the command line. But when I try to run it from QA it just
hangs, no indication of anything. I can see from taskmanager that
wzunzip.exe is running, but nothing happens.
[vbcol=seagreen]
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
>
> "Charles Sands" <z666z@.yahoo.com> wrote in message news:1f9df651.040814075
4.3047b6a@.posting.google.com...|||Perhaps it is waiting form some type of input? I believe that there are sile
nt switches and stuff for the
command-line versions. Might be worth trying out? I haven't unzipped from xp
_cmdshell, but I have zipped...
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"Charles Sands" <z666z@.yahoo.com> wrote in message news:1f9df651.0408151256.29d1aded@.posting
.google.com...
> "Tibor Karaszi" <tibor_please.no.email_karaszi@.hotmail.nomail.com> wrote in messag
e
news:<O2t6qnigEHA.2928@.TK2MSFTNGP10.phx.gbl>...[vbcol=seagreen]
> Yes I am using the command-line version. I just uninstalled and
> reinstalled winzip and the command-line add-on. It is working fine
> from the command line. But when I try to run it from QA it just
> hangs, no indication of anything. I can see from taskmanager that
> wzunzip.exe is running, but nothing happens.
>

After server rename, xp_cmdshell problem

I have a job which runs a SP containing a xp_cmdshell call to
wzunzip.exe to unzip a file. This has worked for over a year without
a glitch. I recently renamed the server, following the proper steps I
think, and now this SP hangs at the unzip command. I can't even
cancel the job, when I try I see that the server is doing a rollback,
but it never finishes, and I have no idea what it would be rolling
back. I end up stopping the agent and restarting. I am probably
missing something easy but I can't figure it out. This same script
has always worked and continues to work on other servers, the only
change being the server rename. Any ideas on what I am missing?
Thanks,
CharlesAre you using the command-line versions of winzip (separate download)?
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"Charles Sands" <z666z@.yahoo.com> wrote in message news:1f9df651.0408140754.3047b6a@.posting.google.com...
> I have a job which runs a SP containing a xp_cmdshell call to
> wzunzip.exe to unzip a file. This has worked for over a year without
> a glitch. I recently renamed the server, following the proper steps I
> think, and now this SP hangs at the unzip command. I can't even
> cancel the job, when I try I see that the server is doing a rollback,
> but it never finishes, and I have no idea what it would be rolling
> back. I end up stopping the agent and restarting. I am probably
> missing something easy but I can't figure it out. This same script
> has always worked and continues to work on other servers, the only
> change being the server rename. Any ideas on what I am missing?
> Thanks,
> Charles|||"Tibor Karaszi" <tibor_please.no.email_karaszi@.hotmail.nomail.com> wrote in message news:<O2t6qnigEHA.2928@.TK2MSFTNGP10.phx.gbl>...
> Are you using the command-line versions of winzip (separate download)?
Yes I am using the command-line version. I just uninstalled and
reinstalled winzip and the command-line add-on. It is working fine
from the command line. But when I try to run it from QA it just
hangs, no indication of anything. I can see from taskmanager that
wzunzip.exe is running, but nothing happens.
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
>
> "Charles Sands" <z666z@.yahoo.com> wrote in message news:1f9df651.0408140754.3047b6a@.posting.google.com...
> > I have a job which runs a SP containing a xp_cmdshell call to
> > wzunzip.exe to unzip a file. This has worked for over a year without
> > a glitch. I recently renamed the server, following the proper steps I
> > think, and now this SP hangs at the unzip command. I can't even
> > cancel the job, when I try I see that the server is doing a rollback,
> > but it never finishes, and I have no idea what it would be rolling
> > back. I end up stopping the agent and restarting. I am probably
> > missing something easy but I can't figure it out. This same script
> > has always worked and continues to work on other servers, the only
> > change being the server rename. Any ideas on what I am missing?
> >
> > Thanks,
> > Charles|||Perhaps it is waiting form some type of input? I believe that there are silent switches and stuff for the
command-line versions. Might be worth trying out? I haven't unzipped from xp_cmdshell, but I have zipped...
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"Charles Sands" <z666z@.yahoo.com> wrote in message news:1f9df651.0408151256.29d1aded@.posting.google.com...
> "Tibor Karaszi" <tibor_please.no.email_karaszi@.hotmail.nomail.com> wrote in message
news:<O2t6qnigEHA.2928@.TK2MSFTNGP10.phx.gbl>...
> > Are you using the command-line versions of winzip (separate download)?
> Yes I am using the command-line version. I just uninstalled and
> reinstalled winzip and the command-line add-on. It is working fine
> from the command line. But when I try to run it from QA it just
> hangs, no indication of anything. I can see from taskmanager that
> wzunzip.exe is running, but nothing happens.
>
> > --
> > Tibor Karaszi, SQL Server MVP
> > http://www.karaszi.com/sqlserver/default.asp
> > http://www.solidqualitylearning.com/
> >
> >
> > "Charles Sands" <z666z@.yahoo.com> wrote in message news:1f9df651.0408140754.3047b6a@.posting.google.com...
> > > I have a job which runs a SP containing a xp_cmdshell call to
> > > wzunzip.exe to unzip a file. This has worked for over a year without
> > > a glitch. I recently renamed the server, following the proper steps I
> > > think, and now this SP hangs at the unzip command. I can't even
> > > cancel the job, when I try I see that the server is doing a rollback,
> > > but it never finishes, and I have no idea what it would be rolling
> > > back. I end up stopping the agent and restarting. I am probably
> > > missing something easy but I can't figure it out. This same script
> > > has always worked and continues to work on other servers, the only
> > > change being the server rename. Any ideas on what I am missing?
> > >
> > > Thanks,
> > > Charles