Showing posts with label number. Show all posts
Showing posts with label number. Show all posts

Thursday, March 29, 2012

Easy!! Subquery needs to show all Sales Reps, even when null

I have a subquery that grabs all the sales reps with less then 6 visits. Only problem is, when i have a date when there are any number of sales reps that dont make visits, so the column would be null, they dont show up. I want to display these, because this report is supposed to show the visits made, so if they made none, i want it to show zero, instead of not showing the whole date column, since zero visits were made on that date that were below 6 and more then zero. Here's my stored procedure: (the subquery is highlighted)

Code Snippet

ALTER PROCEDURE [dbo].[Testing_Visits_6]

(@.Region_Key int=null)

AS

BEGIN

SELECT dbo.Qry_Visits.Status,

dbo.Qry_Visits.Customer_code,

Qry_Sales_Group.Name,

dbo.Qry_Sales_Group.SR_Name,

dbo.Qry_Date_Dim.Date_Dimension_Fiscal_Week,

dbo.Qry_Date_Dim.Date_Dimension_Date,

dbo.Qry_Date_Dim.Day_Of_Month,

dbo.Qry_Sales_Group.Region,

dbo.Qry_Visits.period_code,

dbo.Qry_Visits.cycle_day, dbo.Qry_Visits.Visits,

dbo.Qry_Visits.time_log, dbo.Qry_Visits.Mailing_Name,

dbo.Qry_Date_Dim.Date_Dimension_Year,

dbo.Qry_Date_Dim.Date_Dimension_Period,

CONVERT(varchar, dbo.Qry_Visits.time_log, 110) AS Date,

dbo.Qry_Sales_Group.Region_Key, dbo.Qry_Visits.[SR Code],

B.VisitsTotal

FROM dbo.Qry_Visits

INNER JOIN dbo.Qry_Sales_Group

ON dbo.Qry_Visits.[SR Code]

COLLATE SQL_Latin1_General_CP1_CI_AS = dbo.Qry_Sales_Group.SalesPerson_Purchaser_Code

AND dbo.Qry_Visits.[SR Code] = dbo.Qry_Sales_Group.SalesPerson_Purchaser_Code

COLLATE Latin1_General_CI_AS

INNER JOIN dbo.Qry_Date_Dim

ON CONVERT(varchar, dbo.Qry_Date_Dim.Date_Dimension_Date, 110) = CONVERT(varchar, dbo.Qry_Visits.time_log, 110)

INNER JOIN ( Select COUNT(Visits)as VisitsTotal,[Sales Responsible],CONVERT(VARCHAR,(Qry_Visits.time_log),110)TheDate,Qry_Visits.Status

FROM dbo.Qry_Visits

WHERE Qry_Visits.Status=2

GROUP by [Sales Responsible] , CONVERT(VARCHAR,(Qry_Visits.time_log),110),Qry_Visits.Status

HAVING SUM(Visits) < 6)B

ON dbo.Qry_Sales_Group.SR_Name COLLATE Latin1_General_CI_AS = B.[Sales Responsible] COLLATE Latin1_General_CI_AS AND

CONVERT(varchar, dbo.Qry_Date_Dim.Date_Dimension_Date, 110) = B.TheDate

WHERE REGION_KEY=@.Region_Key and Qry_Visits.Status=2

ORDER BY dbo.Qry_Sales_Group.SR_Name, CONVERT(varchar, dbo.Qry_Date_Dim.Date_Dimension_Date, 110)

You'll need to do an outer join to your date dimension

|||

is there a way i can just say, if SUM(visits) is null then 0

or something like that?

|||

either this...

coalesce(sum(visits),0)

or this...

case

when sum(visits) is null then 0

else sum(visits)

end as 'SumVisits'

|||I guess technically this doesnt work because it doesnt change anything. I guess i just need to find a way to Display all dates that have visits, since all dates show up on my other report that displays all visits, no matter how many. Would i do another subquery to grab all dates where Visit is greater then zero?|||

Replace "INNER JOIN" with "LEFT OUTER JOIN", try the following...

ALTER PROCEDURE [dbo].[Testing_Visits_6]

(@.Region_Key int=null)

AS

BEGIN

SELECT dbo.Qry_Visits.Status,

dbo.Qry_Visits.Customer_code,

Qry_Sales_Group.Name,

dbo.Qry_Sales_Group.SR_Name,

dbo.Qry_Date_Dim.Date_Dimension_Fiscal_Week,

dbo.Qry_Date_Dim.Date_Dimension_Date,

dbo.Qry_Date_Dim.Day_Of_Month,

dbo.Qry_Sales_Group.Region,

dbo.Qry_Visits.period_code,

dbo.Qry_Visits.cycle_day, dbo.Qry_Visits.Visits,

dbo.Qry_Visits.time_log, dbo.Qry_Visits.Mailing_Name,

dbo.Qry_Date_Dim.Date_Dimension_Year,

dbo.Qry_Date_Dim.Date_Dimension_Period,

CONVERT(varchar, dbo.Qry_Visits.time_log, 110) AS Date,

dbo.Qry_Sales_Group.Region_Key, dbo.Qry_Visits.[SR Code],

IsNull(B.VisitsTotal,0) VisitsTotal

FROM dbo.Qry_Visits

INNER JOIN dbo.Qry_Sales_Group

ON dbo.Qry_Visits.[SR Code]

COLLATE SQL_Latin1_General_CP1_CI_AS = dbo.Qry_Sales_Group.SalesPerson_Purchaser_Code

AND dbo.Qry_Visits.[SR Code] = dbo.Qry_Sales_Group.SalesPerson_Purchaser_Code

COLLATE Latin1_General_CI_AS

INNER JOIN dbo.Qry_Date_Dim

ON CONVERT(varchar, dbo.Qry_Date_Dim.Date_Dimension_Date, 110) = CONVERT(varchar, dbo.Qry_Visits.time_log, 110)

LEFT OUTER JOIN (

Select COUNT(Visits)as VisitsTotal

, [Sales Responsible]

, CONVERT( VARCHAR,(Qry_Visits.time_log),110) as TheDate

, Qry_Visits.Status

FROM dbo.Qry_Visits

WHERE Qry_Visits.Status=2

GROUP by [Sales Responsible] , CONVERT(VARCHAR,(Qry_Visits.time_log),110),Qry_Visits.Status

HAVING SUM(Visits) < 6

)B

ON dbo.Qry_Sales_Group.SR_Name COLLATE Latin1_General_CI_AS = B.[Sales Responsible] COLLATE Latin1_General_CI_AS AND

CONVERT(varchar, dbo.Qry_Date_Dim.Date_Dimension_Date, 110) = B.TheDate

WHERE REGION_KEY=@.Region_Key and Qry_Visits.Status=2

ORDER BY dbo.Qry_Sales_Group.SR_Name, CONVERT(varchar, dbo.Qry_Date_Dim.Date_Dimension_Date, 110)

END

sql

Tuesday, March 27, 2012

Easy SQL query question

Hi,
I have a table (PartNumberTbl) includes all the part number.
I want to add a part number 'ALL' in the following output so that user
can select "ALL" in the DropDownList control:
SELECT DISTINCT (PartNumber) FROM PartNumberTbl WHERE Customer =
@.Customer
I don't want to create a Part Number "ALL" for each customer.
How to do it without create a temp table?
Or is there a better way to handle this?
Thanks,
BenConsider adding it to the client code so that the general query without part
number 'ALL' can be reused by several other applications. If all
applications require this entry as one of the rows, then consider using the
UNION operator.
Anith|||You can handle this at the client side of your application,
or, you can issue the following SELECT statement.
(Suppose that PartNumber is a char column.)
SELECT PartNumber
FROM PartNumberTbl
WHERE Customer = @.Customer
UNION
SELECT 'ALL' as PartNumber
Martin C K Poon
Senior Analyst Programmer
====================================
"Ben" <wubin_98@.yahoo.com> ?
news:1150146922.304849.52960@.c74g2000cwc.googlegroups.com ?...
> Hi,
> I have a table (PartNumberTbl) includes all the part number.
> I want to add a part number 'ALL' in the following output so that user
> can select "ALL" in the DropDownList control:
> SELECT DISTINCT (PartNumber) FROM PartNumberTbl WHERE Customer =
> @.Customer
> I don't want to create a Part Number "ALL" for each customer.
> How to do it without create a temp table?
> Or is there a better way to handle this?
> Thanks,
> Ben
>

Thursday, March 22, 2012

Easy ....If Else

I completely forgot how to do this. I want one of my groups in the matrix to take its week number (1-7) as Monday-Sunday.

so it would kind of be like this:

=IIF(Fields!DayOfWeek.Value=2,"Mon",

ELSE(Fields!DayOfWeek.Value=3,"Tues",

Else(Fields!DayOfWeek.Value=4,"Wed",

etc etc

Am i doing this right, or am i off?

I think you are almost right, but is RS you must use it like this :

=IIF(Fields!DayOfWeek.Value=2,"Mon",

iif(Fields!DayOfWeek.Value=3,"Tues",

iif(Fields!DayOfWeek.Value=4,"Wed", "other")))

Hope this helps

Vinnie

|||You could use the Switch function i.e =Switch(Fields!DayOfWeek.Value=2,"Mon", Fields!DayOfWeek.Value=3,"Tues"... and so on|||thanks for the help, I should have known the "(" didnt close till the end .sql

Early User Questions for developing large number of reports

Hi All,
Firstly, let me say who we are.....a group of us are developing a BI
product where we have decided to use Report Services as the reporting
tool. We are long term BI experts and IT consultants......We are
experienced in all manner of reporting and query tools we are just new
to RS. We settled on RS as our 'lower end' reporting tool for our
product and we will give our clients the 'option' of selecting other
reporting tools but the 'out of the box' reports will be in RS.
We have been dilligent and done our reading (WROX RS book) and testing
and figured out most of what we want to do. We have our first report up
and running and we are learning what the limits of RS seem to be to us.
So hopefully I will not ask too many 'dumb' questions... :-)
We also hope by asking questions here that other users of RS might also
benefit from the public discussion.
Q1. We are also working on our 'templates' for reports and looking at
how we develop a large number (200-300) of reports according to a
'template'. In the WROX Book they talk about codesmith and a way of
handling templates. But we didn't 'get' it...Also, when we try using
the VS.NET report wizard it seems that it insists on a query rather
than a stored procedure for it's data for the wizard to work.
So we are thinking we need to write each report by copying the rdl xml,
editting it by hand and then putting it into vs.net to further edit.
We have no real problem with that but we were thinking someone must
have come up with a better idea than editing the XML by now!!!
Has anyone published papers/ideas/tools (fee or free) to manage the
development and deployment of large numbers of reports?
Q2. We see MSFT has been out talking up RS2005/SQL Server 2005 and
talking about the pricing of SQL Server 2005. But we have not seen any
comment on the possible cost of RS2005 if it is NOT purchased with SQL
Server 2005. We would like to have the option of deploying our product
onto SQL Server with RS running either on that machine or elsewhere. Is
there any public comment from MSFT as to the possible pricing of RS2005
when not purchased with SQL Server?
Q3. Is there any published information on 'best practice' or 'best
examples of usage' of report services? For example MicroStrategy,
Business Objects etc post demos of their products to showcase the 'best
possible use' of the product. Dundas have also put out some really
great demos. But the RS demos from MSFTs web site look a little
'plain' especially the graphics. Are there any really great examples
of RS 'out there' or are the demos on the MSFT web site the best there
is? We have decided to go with DNN for the portal and we are currently
testing RS/DNN integration.
Q4. Layout of prompts on a report.
We have reports with quite a few prompts and they are 'grouped'. For
example we have a set of 4 prompts for the user to select the levels in
the company reporting structure. ANd then we have prompts for the
client to select year and month. Then we might have campaign structure
selection which is 3 levels....so we would like to lay out
Line 1: 4 prompts
Line 2: 2 Prompts
Line 3: 3 prompts
We are told this is not supported...but it also seems like it only
ever places two prompts on one line? If we cannot group prompts can we
at least get it to put more than 2 prompts per line?
Thanks in Advance
Peter Nolan
www.peternolan.comRS is like most of the other MS products: a "back-end" tool with high
custimization available. You can extend it from many way.
Q2:
The licensing is the same as 2000. RS is part of SQL 2005 and if you want
report capabilities only, you have to buy a license of SQL 2005. But the RS
repository required SQL Server.
So for your answer, yes RS can be purchased "separatly" but at the full
price of SQL 2005.
Q3:
On the MS web site, there is some samples reports for different usage:
CRM, Exchange, Axapta, SQL Logs, RS logs.
Best pratices & usages depend on your needs.
Designing printing reports is different from web based usage only reports.
Today there is nothing except simple samples. But these small samples don't
limit me to use RS.
Generally I start from an overall report, then I link to detailed reports.
Q4:
Create you own prompt page if you want to create a specific layout.
Remember that RS is designed for developpers to include reporting capability
into their applications.
But there is a lack of sample and helps to create front end tools around RS.
"Peter Nolan" <peter@.peternolan.com> wrote in message
news:1120988013.636226.99630@.o13g2000cwo.googlegroups.com...
> Hi All,
> Firstly, let me say who we are.....a group of us are developing a BI
> product where we have decided to use Report Services as the reporting
> tool. We are long term BI experts and IT consultants......We are
> experienced in all manner of reporting and query tools we are just new
> to RS. We settled on RS as our 'lower end' reporting tool for our
> product and we will give our clients the 'option' of selecting other
> reporting tools but the 'out of the box' reports will be in RS.
> We have been dilligent and done our reading (WROX RS book) and testing
> and figured out most of what we want to do. We have our first report up
> and running and we are learning what the limits of RS seem to be to us.
> So hopefully I will not ask too many 'dumb' questions... :-)
> We also hope by asking questions here that other users of RS might also
> benefit from the public discussion.
>
> Q1. We are also working on our 'templates' for reports and looking at
> how we develop a large number (200-300) of reports according to a
> 'template'. In the WROX Book they talk about codesmith and a way of
> handling templates. But we didn't 'get' it...Also, when we try using
> the VS.NET report wizard it seems that it insists on a query rather
> than a stored procedure for it's data for the wizard to work.
> So we are thinking we need to write each report by copying the rdl xml,
> editting it by hand and then putting it into vs.net to further edit.
> We have no real problem with that but we were thinking someone must
> have come up with a better idea than editing the XML by now!!!
> Has anyone published papers/ideas/tools (fee or free) to manage the
> development and deployment of large numbers of reports?
>
> Q2. We see MSFT has been out talking up RS2005/SQL Server 2005 and
> talking about the pricing of SQL Server 2005. But we have not seen any
> comment on the possible cost of RS2005 if it is NOT purchased with SQL
> Server 2005. We would like to have the option of deploying our product
> onto SQL Server with RS running either on that machine or elsewhere. Is
> there any public comment from MSFT as to the possible pricing of RS2005
> when not purchased with SQL Server?
>
> Q3. Is there any published information on 'best practice' or 'best
> examples of usage' of report services? For example MicroStrategy,
> Business Objects etc post demos of their products to showcase the 'best
> possible use' of the product. Dundas have also put out some really
> great demos. But the RS demos from MSFTs web site look a little
> 'plain' especially the graphics. Are there any really great examples
> of RS 'out there' or are the demos on the MSFT web site the best there
> is? We have decided to go with DNN for the portal and we are currently
> testing RS/DNN integration.
>
> Q4. Layout of prompts on a report.
> We have reports with quite a few prompts and they are 'grouped'. For
> example we have a set of 4 prompts for the user to select the levels in
> the company reporting structure. ANd then we have prompts for the
> client to select year and month. Then we might have campaign structure
> selection which is 3 levels....so we would like to lay out
> Line 1: 4 prompts
> Line 2: 2 Prompts
> Line 3: 3 prompts
> We are told this is not supported...but it also seems like it only
> ever places two prompts on one line? If we cannot group prompts can we
> at least get it to put more than 2 prompts per line?
>
> Thanks in Advance
>
> Peter Nolan
> www.peternolan.com
>|||Hi Jerome,
Q2. In licensing MSFT talks about buying RS when it needs to be
deployed on a server other than the SQL Server...and the WROX book
talks about this as being a 'medium' sized deployment.....if that
means that we need to buy a full SQL Server license (I guess workgroup)
to run RS on a separate server that's ok...we just wanted to
know....but that seems a bit pricey....
Q4. I found examples on odetocode web site but most of the examples I
am finding are 'fragments' intended for developers to include into
already designed/built apps.....that's ok..if there are no other
examples out there we can start from where we are at...just wanted to
check there was not something more publicly available before we spent
our time and effort...it's kind of suprising MSFT has not published
some more things to give developers a better base to work from to go up
against the likes of Crystal/Business Objects/Cognos...maybe more is
coming'
Best Regards
Peter|||Q1, Q3, and Q4--answered in depth in Peter Blackburn and William Vaughn's
Hitchhiker's Guide!
Amazon: http://shrinkster.com/6km
"Peter Nolan" <peter@.peternolan.com> wrote in message
news:1120988013.636226.99630@.o13g2000cwo.googlegroups.com...
> Hi All,
> Firstly, let me say who we are.....a group of us are developing a BI
> product where we have decided to use Report Services as the reporting
> tool. We are long term BI experts and IT consultants......We are
> experienced in all manner of reporting and query tools we are just new
> to RS. We settled on RS as our 'lower end' reporting tool for our
> product and we will give our clients the 'option' of selecting other
> reporting tools but the 'out of the box' reports will be in RS.
> We have been dilligent and done our reading (WROX RS book) and testing
> and figured out most of what we want to do. We have our first report up
> and running and we are learning what the limits of RS seem to be to us.
> So hopefully I will not ask too many 'dumb' questions... :-)
> We also hope by asking questions here that other users of RS might also
> benefit from the public discussion.
>
> Q1. We are also working on our 'templates' for reports and looking at
> how we develop a large number (200-300) of reports according to a
> 'template'. In the WROX Book they talk about codesmith and a way of
> handling templates. But we didn't 'get' it...Also, when we try using
> the VS.NET report wizard it seems that it insists on a query rather
> than a stored procedure for it's data for the wizard to work.
> So we are thinking we need to write each report by copying the rdl xml,
> editting it by hand and then putting it into vs.net to further edit.
> We have no real problem with that but we were thinking someone must
> have come up with a better idea than editing the XML by now!!!
> Has anyone published papers/ideas/tools (fee or free) to manage the
> development and deployment of large numbers of reports?
>
> Q2. We see MSFT has been out talking up RS2005/SQL Server 2005 and
> talking about the pricing of SQL Server 2005. But we have not seen any
> comment on the possible cost of RS2005 if it is NOT purchased with SQL
> Server 2005. We would like to have the option of deploying our product
> onto SQL Server with RS running either on that machine or elsewhere. Is
> there any public comment from MSFT as to the possible pricing of RS2005
> when not purchased with SQL Server?
>
> Q3. Is there any published information on 'best practice' or 'best
> examples of usage' of report services? For example MicroStrategy,
> Business Objects etc post demos of their products to showcase the 'best
> possible use' of the product. Dundas have also put out some really
> great demos. But the RS demos from MSFTs web site look a little
> 'plain' especially the graphics. Are there any really great examples
> of RS 'out there' or are the demos on the MSFT web site the best there
> is? We have decided to go with DNN for the portal and we are currently
> testing RS/DNN integration.
>
> Q4. Layout of prompts on a report.
> We have reports with quite a few prompts and they are 'grouped'. For
> example we have a set of 4 prompts for the user to select the levels in
> the company reporting structure. ANd then we have prompts for the
> client to select year and month. Then we might have campaign structure
> selection which is 3 levels....so we would like to lay out
> Line 1: 4 prompts
> Line 2: 2 Prompts
> Line 3: 3 prompts
> We are told this is not supported...but it also seems like it only
> ever places two prompts on one line? If we cannot group prompts can we
> at least get it to put more than 2 prompts per line?
>
> Thanks in Advance
>
> Peter Nolan
> www.peternolan.com
>|||thanks for the pointer goodman 93...I'll see if I can get a copy of
that too.....
Peter

Wednesday, March 21, 2012

dynmica use @db

Hi There,
I've number of databases, each one is having invoice table and a amount
fields in it.
I want to use somethig like this in a dynamic way using loop, i can generate
all database what all i need in a cursor, but i can not use like this
use @.db_name in a loop
use db1
select @.amt = sum(amount) from invoice
use db2
select @.amt = sum(amount) from invoice
@.tot = @.tot + @.amt
print @.tot
Thanks
GaneshYou can use 3-part naming like :
SELECT @.tot = SUM( amount )
FROM ( SELECT SUM( amount ) FROM db1.dbo.invoice
UNION ALL
SELECT SUM( amount ) FROM db2.dbo.invoice
UNION ALL
... ) D ( amount ) ;
PRINT @.tot
Anith|||If you really want to do it in a loop...and already have the Cursor logic
setup, you need to do something like this:
DECLARE db_cursor CURSOR FOR
SELECT dbname FROM yourtable
--Get first name
OPEN db_cursor
FETCH NEXT FROM db_cursor INTO @.dbname
WHILE (@.@.fetch_status <> -1)
BEGIN
SELECT @.cmd = 'use ' + @.dbname
EXEC (@.cmd)
select @.amt = sum(amount) from invoice
set @.tot = @.tot + @.amt
FETCH NEXT FROM db_cursor INTO @.dbname
END
"Ganesh" <gsganesh@.yahoo.com> wrote in message
news:22D76D45-8A91-4DFD-84DC-1BB2D15DEA97@.microsoft.com...
> Hi There,
> I've number of databases, each one is having invoice table and a amount
> fields in it.
> I want to use somethig like this in a dynamic way using loop, i can
> generate
> all database what all i need in a cursor, but i can not use like this
> use @.db_name in a loop
> use db1
> select @.amt = sum(amount) from invoice
> use db2
> select @.amt = sum(amount) from invoice
> @.tot = @.tot + @.amt
> print @.tot
>
> --
> Thanks
> Ganesh|||I got error message like this
Server: Msg 208, Level 16, State 1, Line 1
Invalid object name 'Invoice'.
Thanks
Ganesh
"VC" wrote:

> If you really want to do it in a loop...and already have the Cursor logic
> setup, you need to do something like this:
> DECLARE db_cursor CURSOR FOR
> SELECT dbname FROM yourtable
> --Get first name
> OPEN db_cursor
> FETCH NEXT FROM db_cursor INTO @.dbname
> WHILE (@.@.fetch_status <> -1)
> BEGIN
> SELECT @.cmd = 'use ' + @.dbname
> EXEC (@.cmd)
> select @.amt = sum(amount) from invoice
> set @.tot = @.tot + @.amt
> FETCH NEXT FROM db_cursor INTO @.dbname
> END
>
> "Ganesh" <gsganesh@.yahoo.com> wrote in message
> news:22D76D45-8A91-4DFD-84DC-1BB2D15DEA97@.microsoft.com...
>
>|||Well, then the table Invoice must not exist in the particular db that you
are in when the error occurs.
I would add a statement "print @.cmd" before the "select @.amt..." statement
to see which db you are currently using when the error occurs.
"Ganesh" <gsganesh@.yahoo.com> wrote in message
news:8C338237-CD72-4553-BEBF-2FE48E6CF804@.microsoft.com...
>I got error message like this
> Server: Msg 208, Level 16, State 1, Line 1
> Invalid object name 'Invoice'.
>
> --
> Thanks
> Ganesh
>
> "VC" wrote:
>|||No Invoice exists, But the Exec is a kind of connection, once we exected
after that it closes the connection, That's why i could not execute next
statment using exec
then i changed the select statement into the same exec now it works
Thanks for your help
Thanks
Ganesh
"VC" wrote:

> Well, then the table Invoice must not exist in the particular db that you
> are in when the error occurs.
> I would add a statement "print @.cmd" before the "select @.amt..." statement
> to see which db you are currently using when the error occurs.
>
> "Ganesh" <gsganesh@.yahoo.com> wrote in message
> news:8C338237-CD72-4553-BEBF-2FE48E6CF804@.microsoft.com...
>
>

Dynamicaly add charts to report

I need display some processes (lines in a chart) in the report. One process
per chart. The number of processes is not known at the design time and is
defined by the result of the underlying report query, so I do not know how
many chart objects to place on the report at design time. Is it possible to
add charts dynamically to the report during the run time?
Any hint would be greatly appreciated.
Thanks.Maybe this will help!
I create a group (detail) line and I add a chart to this group by defining a
chart in one of the text boxes...
hen do the normal things with a chart - but the chart is automatically
GROUPED for you based upon the results of your query (in line or T-SQL)
"Simon Gold" wrote:
> I need display some processes (lines in a chart) in the report. One process
> per chart. The number of processes is not known at the design time and is
> defined by the result of the underlying report query, so I do not know how
> many chart objects to place on the report at design time. Is it possible to
> add charts dynamically to the report during the run time?
> Any hint would be greatly appreciated.
> Thanks.
>

Monday, March 19, 2012

Dynamically set different font weight for each text in the textbox

Hi friends,

I have a text box with n number of text.

I want to set the font weight of each text in the textbox dynamically..

For eg.. suppose the text of the textbox is "Hello Friends", then i need "Hello Friends" as output.

Is there any way to accomplish this in SQL Reporting Service.

Any help will be appreciated. Its critical.

Please help me out ASAP.

No, the font settings are per textbox, so everything in the textbox will have the same font weight. You can use different textboxes, but that ends up pretty messy.

|||

Yes it is possible. However, it is not easy and you will need Visual Studio 2005 to do it.

It sounds like you are basically wanting to change the layout of the report at runtime.

Here is how to do it:

http://msdn2.microsoft.com/en-us/library/ms170667.aspx

After you are able to change the layout of the report at runtime, you will want to deploy the report from your application using SetReportDefinition.

|||

Greg, are you thinking that he would create a separate textbox for each wordin the original textbox to handle this requirement, so that each one would have its own formatting? How would that work, for different instances of the same textbox in the report (for example, the first one reads "this is my text" and "is" should be bold, but the second instance reads "this is not really my text" and the word "my" should be bold?

Also I think the positioning/kerning would be a nightmare...

>L<

|||

The only way I know how to do this successfully in Reporting Services (although Greg may have another approach, I'm not seeing it!), is to do some custom rendering. IOW, if you render the information yourself, you are free to set the formatting for each word in the text. The result becomes a small graphical "piece" in the report, though, not really text. So it wouldn't be searchable text.

If this solution appeals to you at all, I can give you more details.

>L<

|||

Lisa Nicholls wrote:

The only way I know how to do this successfully in Reporting Services (although Greg may have another approach, I'm not seeing it!), is to do some custom rendering. IOW, if you render the information yourself, you are free to set the formatting for each word in the text. The result becomes a small graphical "piece" in the report, though, not really text. So it wouldn't be searchable text.

If this solution appeals to you at all, I can give you more details.

>L<

This sounds like a better approach. I suppose I was thinking that you could change the font weight per word.

|||

Thanks Lisa and Greg for the answers .. I am sorry to reply late.. I was trying out lisa's solution.

But my scenario is different. I have a text bos and the expression of my textbox is as follows:

="Dear"+" "+Ucase("Robert")+space(2)+vbcrlf+"How are you"

where "vbclrf" is for new line and "space(2)" is for leaving 2 spaces between the text and "Ucase" stands for upper case.

Similarly I need a way to display the text "Robert" in bold letters.

Do you have any suggestion for this.

|||

Lisa Nicholls wrote:

...do some custom rendering. IOW, if you render the information yourself, you are free to set the formatting for each word in the text. The result becomes a small graphical "piece" in the report, though, not really text. So it wouldn't be searchable text.

If this solution appeals to you at all, I can give you more details.

>L<

It still sounds like this is what you need.

|||


>>But my scenario is different.

No, really, it's exactly what I thought. If you tried what I suggested... what exactly did you try?

In the current version of RS, what you need to do is build a function that parses your markup in a

CustomReportItem, and renders the content appropriately for your markup.

In the next version of RS (Katmai -- there is a thread post about rich text) you might have a

better choice, from the point of view of rendering. IOW, you would not have to use a

CustomReportItem for this. However, given your custom markup, you would still have to create a

code function to parse the markup and put it in a more standard form, such as HTML markup or RTF

or whatever Katmai supports, so that the standard rendering could deal with it.

For this reason, I strongly suggest you think about switching to a standard markup format that

standard renderers, whether in RS or elsewhere, could read! Your users and designers will also

thank you for it.

>L<

Dynamically number of parameters

I have 3 paramemters. How i can dynamically show/hide other two parameter based on the value as 1 or 2 input from the user from the first parameter? (for example: if the user enter 1, i will show the second parameter, if tyhe user enter 2, i will sho the thirs parameter on the report so the user can enter other value in these dynamical parameters?)Thats not possible. What is possible to still display them , but to clean them to show no value. That can be done using a query in the parameter definition.


HTH, Jens Suessmeyer.

http://www.sqlserver2005.de

Sunday, March 11, 2012

Dynamically creating SSIS package for each flat file

Trying to figure out the best method of reading in a number of flat files, all with different number of columns and data types and outputting them to a database.

Here's the problem: They are EBCDIC encoded and some of the columns are packed decimal. I've set up one package that takes the flat file, unpacks the decimal (Using UnpackDecimal component) and then sending the rest through a second component to go from EBCDIC -> ASCII.

What I need is a way to do this for every flat file based on the schema for that flat file. One current solution is to write a script/app to create the .dtsx XML file and then execute that for each flat file. It appears like this may be possible, but I haven't gotten far enough to know for sure. So my questions are this:

1) Is there an easier way to do this (ie somehow feed the schema to the package and use it to dynamically set up the column makers and determine which columns get fed to the unpack decimal component.

2) If there isn't a better way, will dynamically creating the .dtsx XML file based on the necessary input/output columns for each flat file work? If so, what is a good source of information on this (information about how the .dtsx XML file is set up, what needs to be changed/what doesn't, etc).

Thanks,

Travis

Trav2003 wrote:

1) Is there an easier way to do this (ie somehow feed the schema to the package and use it to dynamically set up the column makers and determine which columns get fed to the unpack decimal component.

2) If there isn't a better way, will dynamically creating the .dtsx XML file based on the necessary input/output columns for each flat file work? If so, what is a good source of information on this (information about how the .dtsx XML file is set up, what needs to be changed/what doesn't, etc).

Thanks,

Travis

1) No. SSIS can't handle dynamic columns. The best it can do it dynamically create a child package, which is no different than #2.
2) Yes, it will work. You probably don't want to create XML directly, but instead use the API to generate the package. The updated samples contain one showing how to create a basic package. You may also find this tool helpful for reverse engineering packages.
|||you might also check out http://www.aminosoftware.com they have a custom source component that will read in many forms of ebcdic (including packed, zoned, etc) and output it into ASCII with only a single pass through the data file.

Friday, March 9, 2012

Dynamically changing position of Textbox

Hi,

In my report, I've to print the address of the customer in a form which is in a pre-defined format. So while printing, the line number and position should be specified by the user. Is there any ways in SSRS to change the position of a textbox/any control dynamically by inputing the x and y coordinates? If there is a way please let me know that.

Thanks in Advance,

Leks

What does the surrounding content in the layout look like? Do you have a "clear" region in which you could place a very large textbox, representing the all the page area in which the textbox could potentially be placed?

If so, you might be able to do this with padding. IOW, you would place this very large textbox in the layout and then you would pad the top and left to "push" the content of the text box into the customer's preferred position, at runtime.

This is possible because you can express the padding values as expressions. Think of the textbox's true top and left as an offset from 0,0, which you will subtract from the customer's input values (which will be parameters) to get the padded top and left at runtime.

HTH,

>L<

|||

Hi Lisa,

Thanks for your quick reply. But unfortunately my layout is not clear. It contains other data too that I need to position according to the input for them. For each item in the report we have to enter the position. I know this process is a bit tedious. but there is no other ways. They actualy need the report data such that it can be printed in many forms. Please reply as soon as possible.

Thank you dear,

Leks

|||

Hi Leks,

If you have to enter the position of each item in the report, then I think your best option is to edit the RDL on the fly, put your coordinates right in the report definition.

Are you comfortable with doing this? It may be quite straightward. Some things that would enter into it and might change what you have to do would include:

* -- What type of data region are you using (list/table) to contain the dynamic items?

* -- Are there additional items in the report that are *not* dynamically positioned, for example is this an order header you are trying to position so that it fits in a window envelope with a table of order lines that is relatively static?

>L<

|||

Hi Lisa,

Here all the data should change position dynamically. So Your solution shoud work. But I didnt get it. How we can change the rdl on the fly?

Thanking you,

Leks

|||

You can write a whole RDL on the fly if you want to. Look for an example called something like "generate RDL dynamically" on http://gotreportviewer.com/ and there is another example there that will show you how to LoadReportDefinition for a local report from a stream. If the report is a serverreport, you could use the SOAP API to upload the adjusted report definition instead.

In your case, I don't think that it will be necessary to write the whole RDL from scratch (although you will still have to load or upload your revised version). You can have a "template" RDL that you adjust, for each run or each customer, or as needed.

There are two ways to do an RDL template revision, the way I would do it and the way most developers would prefer to do it <g>. I prefer XSLT; in this case I might have a parameter in the XSLT for each positionable item in the report, or pass the entire set of adjustments in the form of a lookup document. If you prefer to load up the RDL in the DOM and select the textboxes as nodes to adjust each of them, this is pretty easy too.

Do you need more help than this, or is this what you're looking for?

>L<

|||

Hi Lisa,

Yes. This was what I was looking for. Now I know that using the XSLT we can position the textbox or any control dynamically.

Thank you so much Lisa,

Leks

|||

Great -- if you get stuck, holler. I am very willing to go the extra mile for other folks who like XSLT <s>.

>L<

Dynamically changing position of Textbox

Hi,

In my report, I've to print the address of the customer in a form which is in a pre-defined format. So while printing, the line number and position should be specified by the user. Is there any ways in SSRS to change the position of a textbox/any control dynamically by inputing the x and y coordinates? If there is a way please let me know that.

Thanks in Advance,

Leks

What does the surrounding content in the layout look like? Do you have a "clear" region in which you could place a very large textbox, representing the all the page area in which the textbox could potentially be placed?

If so, you might be able to do this with padding. IOW, you would place this very large textbox in the layout and then you would pad the top and left to "push" the content of the text box into the customer's preferred position, at runtime.

This is possible because you can express the padding values as expressions. Think of the textbox's true top and left as an offset from 0,0, which you will subtract from the customer's input values (which will be parameters) to get the padded top and left at runtime.

HTH,

>L<

|||

Hi Lisa,

Thanks for your quick reply. But unfortunately my layout is not clear. It contains other data too that I need to position according to the input for them. For each item in the report we have to enter the position. I know this process is a bit tedious. but there is no other ways. They actualy need the report data such that it can be printed in many forms. Please reply as soon as possible.

Thank you dear,

Leks

|||

Hi Leks,

If you have to enter the position of each item in the report, then I think your best option is to edit the RDL on the fly, put your coordinates right in the report definition.

Are you comfortable with doing this? It may be quite straightward. Some things that would enter into it and might change what you have to do would include:

* -- What type of data region are you using (list/table) to contain the dynamic items?

* -- Are there additional items in the report that are *not* dynamically positioned, for example is this an order header you are trying to position so that it fits in a window envelope with a table of order lines that is relatively static?

>L<

|||

Hi Lisa,

Here all the data should change position dynamically. So Your solution shoud work. But I didnt get it. How we can change the rdl on the fly?

Thanking you,

Leks

|||

You can write a whole RDL on the fly if you want to. Look for an example called something like "generate RDL dynamically" on http://gotreportviewer.com/ and there is another example there that will show you how to LoadReportDefinition for a local report from a stream. If the report is a serverreport, you could use the SOAP API to upload the adjusted report definition instead.

In your case, I don't think that it will be necessary to write the whole RDL from scratch (although you will still have to load or upload your revised version). You can have a "template" RDL that you adjust, for each run or each customer, or as needed.

There are two ways to do an RDL template revision, the way I would do it and the way most developers would prefer to do it <g>. I prefer XSLT; in this case I might have a parameter in the XSLT for each positionable item in the report, or pass the entire set of adjustments in the form of a lookup document. If you prefer to load up the RDL in the DOM and select the textboxes as nodes to adjust each of them, this is pretty easy too.

Do you need more help than this, or is this what you're looking for?

>L<

|||

Hi Lisa,

Yes. This was what I was looking for. Now I know that using the XSLT we can position the textbox or any control dynamically.

Thank you so much Lisa,

Leks

|||

Great -- if you get stuck, holler. I am very willing to go the extra mile for other folks who like XSLT <s>.

>L<

Dynamically Changing Images

Hi All:
I have a report that will be shared by a number of sales agents across a
number of different communities. The agents select a set of parameters and
run their report. Currently Community is one of the parameters they can
select.
I'd like to know if there is any way to dynamically change a graphic based
upon what Community is selected. We'd like to have only one report where the
logo can change rather than 12 different reports.
Thanks
BrennanWe have a property report that shows an image dynamically, our properties
have an ID number assigned to them.
ID number is one of the parameters of the report. On our webserver, we have
an image file (named the id number).
For example the user can choose building number 00818, and on the web server
is a corresponding image 00818.jpg.
We set the image type to external, and the value is an expression -
http://<yourserver>/BuildingImages/ & Parameters!ID.value & ".jpg"
This returns an image on the report based on the building the user has
chosen.
Chris
"Brennan" <Brennan@.discussions.microsoft.com> wrote in message
news:A3E076C0-EF3A-4903-B565-0DFA39A5A13A@.microsoft.com...
> Hi All:
> I have a report that will be shared by a number of sales agents across a
> number of different communities. The agents select a set of parameters
> and
> run their report. Currently Community is one of the parameters they can
> select.
> I'd like to know if there is any way to dynamically change a graphic based
> upon what Community is selected. We'd like to have only one report where
> the
> logo can change rather than 12 different reports.
> Thanks
> Brennan|||"Chris" wrote:
> We have a property report that shows an image dynamically, our properties
> have an ID number assigned to them.
> ID number is one of the parameters of the report. On our webserver, we have
> an image file (named the id number).
> For example the user can choose building number 00818, and on the web server
> is a corresponding image 00818.jpg.
> We set the image type to external, and the value is an expression -
> http://<yourserver>/BuildingImages/ & Parameters!ID.value & ".jpg"
> This returns an image on the report based on the building the user has
> chosen.
> Chris
> "Brennan" <Brennan@.discussions.microsoft.com> wrote in message
> news:A3E076C0-EF3A-4903-B565-0DFA39A5A13A@.microsoft.com...
> > Hi All:
> >
> > I have a report that will be shared by a number of sales agents across a
> > number of different communities. The agents select a set of parameters
> > and
> > run their report. Currently Community is one of the parameters they can
> > select.
> >
> > I'd like to know if there is any way to dynamically change a graphic based
> > upon what Community is selected. We'd like to have only one report where
> > the
> > logo can change rather than 12 different reports.
> >
> > Thanks
> > Brennan
>
>
Thanks Chris|||You can also use a "switch" expression in your value field for the
image:
switch(parameters!community.value = 'x', image1,
parameters!community.value = 'y', image2, ...)
Brennan wrote:
> "Chris" wrote:
> > We have a property report that shows an image dynamically, our properties
> > have an ID number assigned to them.
> > ID number is one of the parameters of the report. On our webserver, we have
> > an image file (named the id number).
> > For example the user can choose building number 00818, and on the web server
> > is a corresponding image 00818.jpg.
> > We set the image type to external, and the value is an expression -
> > http://<yourserver>/BuildingImages/ & Parameters!ID.value & ".jpg"
> >
> > This returns an image on the report based on the building the user has
> > chosen.
> >
> > Chris
> >
> > "Brennan" <Brennan@.discussions.microsoft.com> wrote in message
> > news:A3E076C0-EF3A-4903-B565-0DFA39A5A13A@.microsoft.com...
> > > Hi All:
> > >
> > > I have a report that will be shared by a number of sales agents across a
> > > number of different communities. The agents select a set of parameters
> > > and
> > > run their report. Currently Community is one of the parameters they can
> > > select.
> > >
> > > I'd like to know if there is any way to dynamically change a graphic based
> > > upon what Community is selected. We'd like to have only one report where
> > > the
> > > logo can change rather than 12 different reports.
> > >
> > > Thanks
> > > Brennan
> >
> >
> >
>
> Thanks Chris

Wednesday, March 7, 2012

Dynamically adding values to line chart

I have a report with a line chart and I need to add a number of values to
the chart from the data in my dataset. The problem is that the number of
values to be drawn on the chart may change each time a report is generated
and so I cannot add the values manually in the report designer. So for
example one report may have 5 values to be drawn on the chart Value1,
Value2, Values3... but then another report may only contain 2 values in the
dataset that need to be drawn on the chart.
Each value (Value1...) in the dataset belongs to the same row and so it is
the number of columns in each row which can vary each time a report is
generated.
First report generated (5 columns):
Value1 Value2 Value3 Value4 Value5
10 20 54 32 67
Second Report generated (2 columns):
Value1 Value2
45 2
Does anybody know how I can add values dynamically to a line chart?Hi Holmes,
Welcome to use MSDN Managed Newsgroup!
From your descriptions, I understood you would like to generate a Chart
with dynamic columns and its values. However, I am not sure what's the data
for your X-axis and Y-axis. Would you please show me a detailed
information? (For example, the chart will be has such points (1,10), (2,
20), (3,54), (4,32), (5, 67) )
In the Chart properties, we may notice that you will have to add Fields /
Expressions that based on Fields for your Values / Category Group / Series
Group, we cannot add columns dynamically. You will have to convert the data
in the same row into same column.
Thank you for your patience and cooperation. If you have any questions or
concerns, don't hesitate to let me know. We are always here to be of
assistance!
Sincerely yours,
Michael Cheng
Microsoft Online Partner Support
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
=====================================================This posting is provided "AS IS" with no warranties, and confers no rights.|||Hi
Thanks for the reply. From what I understand you are suggesting I should
convert the dynamic values into just one column.
Here is a more detailed description of the data I have. Each query returns a
number of rows which have an ID, Value (Integer), and then a dynamic number
of Bands (Band1, Band2 ... which are Integers). However the number of bands
is constant for all rows from a single query but is does change each time
the query is executed.
So I may have
ID Value Band1 Band2
1033 25 30 60
1034 35 30 60
The bands need to be represented as straight lines drawn horizontally across
the line chart.
The Values need to be plotted as points and then a line drawn connected them
all.
So I can put the Band values into a single column but how will this help?
How can I get the Band values back from a single column?
Kind Regards
Lewis Holmes
"Michael Cheng [MSFT]" <v-mingqc@.online.microsoft.com> wrote in message
news:ZiTlmYovFHA.780@.TK2MSFTNGXA01.phx.gbl...
> Hi Holmes,
> Welcome to use MSDN Managed Newsgroup!
> From your descriptions, I understood you would like to generate a Chart
> with dynamic columns and its values. However, I am not sure what's the
> data
> for your X-axis and Y-axis. Would you please show me a detailed
> information? (For example, the chart will be has such points (1,10), (2,
> 20), (3,54), (4,32), (5, 67) )
> In the Chart properties, we may notice that you will have to add Fields /
> Expressions that based on Fields for your Values / Category Group / Series
> Group, we cannot add columns dynamically. You will have to convert the
> data
> in the same row into same column.
> Thank you for your patience and cooperation. If you have any questions or
> concerns, don't hesitate to let me know. We are always here to be of
> assistance!
>
> Sincerely yours,
> Michael Cheng
> Microsoft Online Partner Support
> When responding to posts, please "Reply to Group" via your newsreader so
> that others may learn and benefit from your issue.
> =====================================================> This posting is provided "AS IS" with no warranties, and confers no
> rights.
>|||Hi Lewis,
If the Bands number are not sure, I am afraid you cannot add these values
dynamically. As I have said, you must convert the tables like below
Line1: (for Band1)
X-axis Y-axis
1033 30
1033 60
Line2: (for Band2)
X-axis Y-axis
1034 30
1034 60
Make sure what was the X-axis and Y-axis value you want in the table and
then compose a line.
If you have any more tables or pictures to demonstrate this scenario more
clearly, you are welcome to send the files here or send them to me directly
if you have security concerns. my direct email address is
v-mingqc@.microsoft.com, you may send the file to me directly and I will
keep secure.
Sincerely yours,
Michael Cheng
Microsoft Online Partner Support
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
=====================================================This posting is provided "AS IS" with no warranties, and confers no rights.

Sunday, February 26, 2012

Dynamic Transformations

Hi

I have a dts that is creating a table with not a fixed number of columns. The number of colums depend on a couple of factors based on the data that I'm pulling from other tables.

After some processing I need to dump all the data in the "dynamic" table into an excel doc. My problem is with the transformations within the transform data task. I don't know how many fields I will have in my table and this needs to be mapped to columns within the excel doc. Is it possible to programmatically define the transformations within an activeX script or what can I do.

Thanks

Johnnie

SSIS does not support dynamic metadata. The best that could be done is to have a package for each dynamic type (if this is feasible, i.e. there are a finite number of dynamic types) and then have a master package that picks which of these "dynamic" packages to execute.

Matt

Friday, February 24, 2012

dynamic table in report

hi,

i have a dataset which returns me a dynamic set of data ie. number of columns can be different....say 14 columns for some dates...and just 12 for some other...(pivot table is being used in the backend)...how can i present this in the table...also my column names are best suited to be the column headers in the table.....is it possible to create a table at runtime...?

Are your field names and the max # of fields returned in the dataset fixed? If so, you can define a table with N (N=max# of fields) columns (one field per column). Then set the visibility property on the table column to be based on the IsMissing property on the field http://msdn2.microsoft.com/en-us/library/ms157274.aspx|||it was..but the client wants it dynamic...columns r the active schemes..they can vary with time...though i am getting the number of schemes and their names..... its like...select activeschemes where date between report start date and end date ........pivot it and get other details..and return the dataset....|||i got it, made the columns into rows and used matrix in report....|||Just create a dataset and Mark visiability of column by =IIf(Fields!Collection.IsMissing=True,True,False) so ur report becomes dynamic.Column which is not there will gets hidden

dynamic table in report

hi,

i have a dataset which returns me a dynamic set of data ie. number of columns can be different....say 14 columns for some dates...and just 12 for some other...(pivot table is being used in the backend)...how can i present this in the table...also my column names are best suited to be the column headers in the table.....is it possible to create a table at runtime...?

Are your field names and the max # of fields returned in the dataset fixed? If so, you can define a table with N (N=max# of fields) columns (one field per column). Then set the visibility property on the table column to be based on the IsMissing property on the field http://msdn2.microsoft.com/en-us/library/ms157274.aspx|||it was..but the client wants it dynamic...columns r the active schemes..they can vary with time...though i am getting the number of schemes and their names..... its like...select activeschemes where date between report start date and end date ........pivot it and get other details..and return the dataset....|||i got it, made the columns into rows and used matrix in report....|||Just create a dataset and Mark visiability of column by =IIf(Fields!Collection.IsMissing=True,True,False) so ur report becomes dynamic.Column which is not there will gets hidden

Dynamic subreports

Hi all!

I need to create a report with a number of subreports. The problem is that those subreports might be different for each datarow.

Is it possible to set the report name for the subreport in some dynamic manner, e.g. extract it from the data?

Hello Joric,

At the present time it is not possible to achieve.

I have opened a case at connect.mcrsf some time ago. You can vote for it to be on the next release.

http://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=286068

Best Regards,

Wednesday, February 15, 2012

Dynamic SQL Column Total Help

am trying to dynamically determine the number of Active Projects in a
given month dynamically for a give ProjectType.
(e.g. For June 2005, I would like to see all projects created on June
30, 2005 or before that have not yet closed grouped by Project Type.)
**Simplified DDL **
Projects
(
ProjectID
OpenDate,
ClosedDate,
ProjectType
)
SQL I already have that works that gives the number of projects opened
in a given month
======================
SELECT ProjectType,
cast(datename(month,opendate) as varchar(15)) [DateMonth],
--Guarantees Months of Diferent Years are not grouped together
Convert(datetime,datename(month,opendate
) + ' 1 ' +
datename(year,opendate),110) [OrderByDate],
count(*) ProjectsOpened
INTO ##temp
FROM Projects P
WHERE OpenDate >= @.BeginDate and OpenDate <= @.EndDate --Year
to retrieve Projects for
GROUP BY ProjectName,
cast(datename(month,opendate) as varchar(15)),
Convert(datetime,datename(month,opendate
) + ' 1 ' +
datename(year,opendate),110)
ORDER BY Convert(datetime,datename(month,opendat
e) + ' 1 ' +
datename(year,opendate),110),
ProjectType
I have a second query to retrieve the number of active projects at the
end of a particular month
==========================
SELECT ProjectType,Count(*) OpenProjectCount
FROM Projects P2
WHERE OpenDate <= dbo.EndOfMonth('01-May-2005')
AND (ClosedDate Is Null Or ClosedDate >
dbo.EndOfMonth('01-May-2005'))
GROUP BY ProjectType
What I can't Figure Out is how to join the two queries so that where
you see '01-May-2005', I can pass the
" Convert(datetime,datename(month,opendate
) + ' 1 ' +
datename(year,opendate),110)" value and return the Active Projects for
each particular month in the year span.
RESULTSET SHOULD BE
ProjectType DateMonth OrderByDate Projec
tsOpened ActiveProjects
1 Jan 01-Jan-2005 5 9
1 Feb 01-Feb-2005 3 7
NOTE
-====
I have simplified the DDL for the example, but the gist of what I need
remains the same
Sorry about the google post and any formatting issues, but the company
I work for doesn't allow outlook express to be used.
L Anthony JohnsonYou need to use derived to combine them within one query. Since the join
happens prior to the group, I am going to join 2 derived tables. I made it
a
bit more robust by doing a full outer join between the two tables, this will
handle cases for months that have active but no open and vice versa. This
results in all the ISNULLs in the SELECT.
I am going to modify the second query slightly to use @.EndDate instead of
the function, since it seems like you have this. Also, this query needs to
return its month to join by. Hopefully this will give you a rough idea of
what you can do.
Something like this would work (although I didn't test it out)(Sorry about
the formatting)
DECLARE @.BeginDate as DateTime
DECLARE @.EndDate as DateTime
SET @.BeginDate = '6/1/2005'
SET @.EndDate = '6/30/2005'
SELECT ISNULL(derived_table1.ProjectType, derived_table2.ProjectType) as
ProjectType
ISNULL(derived_table1.GroupByDate, derived_table2.GroupByDate) as
OrderByDate,
ISNULL(derived_table1.OpenProjects, 0) as ProjectsOpened,
ISNULL(derived_table.ActiveProjects,0) as ActiveProjects
FROM (
SELECT ProjectType,
Convert(datetime,datename(month,opendate
) + ' 1 '
+datename
(year,opendate),110) as GroupByDate,
count(*) as OpenProjects,
FROM Projects P
WHERE OpenDate >= @.BeginDate and OpenDate <= @.EndDate
GROUP BY ProjectType,
Convert(datetime,datename(month,opendate
) + ' 1 ' +
datename
(year,opendate),110)
) derived_table1 -- table that gives us project type and open count per mont
h
FULL OUTER JOIN
( SELECT ProjectType,
@.BeginDate as GroupByDate,
count(*) as ActiveProjects,
FROM Projects P
WHERE ClosedDate IS NULL OR ClosedDate > @.EndDate
GROUP BY ProjectType
) derived_table2 -- table that gives us project type and active as of end of
month
-- join derived tables together on both type and date (which is always first
of month)
ON derived_table1.ProjectType = derived_table2.ProjectType
AND derived_table1.GroupByDate = derived_table2.GroupByDate
ORDER BY
derived_table1.GroupByDate,
derived_table1.ProjectType
HTH
--
Ryan Powers
Clarity Consulting
http://www.claritycon.com
"LJohnsonNG@.gmail.com" wrote:

> am trying to dynamically determine the number of Active Projects in a
> given month dynamically for a give ProjectType.
> (e.g. For June 2005, I would like to see all projects created on June
> 30, 2005 or before that have not yet closed grouped by Project Type.)
> **Simplified DDL **
> Projects
> (
> ProjectID
> OpenDate,
> ClosedDate,
> ProjectType
> )
> SQL I already have that works that gives the number of projects opened
> in a given month
> ======================
> SELECT ProjectType,
> cast(datename(month,opendate) as varchar(15)) [DateMonth],
> --Guarantees Months of Diferent Years are not grouped together
> Convert(datetime,datename(month,open
date) + ' 1 ' +
> datename(year,opendate),110) [OrderByDate],
> count(*) ProjectsOpened
> INTO ##temp
> FROM Projects P
> WHERE OpenDate >= @.BeginDate and OpenDate <= @.EndDate --Year
> to retrieve Projects for
> GROUP BY ProjectName,
> cast(datename(month,opendate) as varchar(15)),
> Convert(datetime,datename(month,open
date) + ' 1 ' +
> datename(year,opendate),110)
> ORDER BY Convert(datetime,datename(month,opendat
e) + ' 1 ' +
> datename(year,opendate),110),
> ProjectType
>
> I have a second query to retrieve the number of active projects at the
> end of a particular month
> ==========================
> SELECT ProjectType,Count(*) OpenProjectCount
> FROM Projects P2
> WHERE OpenDate <= dbo.EndOfMonth('01-May-2005')
> AND (ClosedDate Is Null Or ClosedDate >
> dbo.EndOfMonth('01-May-2005'))
> GROUP BY ProjectType
> What I can't Figure Out is how to join the two queries so that where
> you see '01-May-2005', I can pass the
> " Convert(datetime,datename(month,opendate
) + ' 1 ' +
> datename(year,opendate),110)" value and return the Active Projects for
> each particular month in the year span.
>
> RESULTSET SHOULD BE
>
> ProjectType DateMonth OrderByDate Projec
tsOpened ActiveProjects
> 1 Jan 01-Jan-2005 5 9
> 1 Feb 01-Feb-2005 3 7
> NOTE
> -====
> I have simplified the DDL for the example, but the gist of what I need
> remains the same
> Sorry about the google post and any formatting issues, but the company
> I work for doesn't allow outlook express to be used.
> L Anthony Johnson
>|||(LJohnsonNG@.gmail.com) writes:
> What I can't Figure Out is how to join the two queries so that where
> you see '01-May-2005', I can pass the
> " Convert(datetime,datename(month,opendate
) + ' 1 ' +
> datename(year,opendate),110)" value and return the Active Projects for
> each particular month in the year span.
I think this query takes you closer to the final goal:
SELECT A.ProjectType, A.YearMonth, A.ProjectsOpened,
(SELECT COUNT(*)
FROM Projects B
WHERE B.OpenDate < dateadd(MONTH, 1, A.YearMonth + '01')
AND (B.CloseDate IS NULL OR
B.CloseDate >= dateadd(MONTH, 1, A.YearMonth + '01')))
AS ActiveProjects
FROM (SELECT ProjectType, YearMonth = convert(char(6), OpenDate, 112),
count(*) ProjectsOpened
FROM Projects
WHERE OpenDate >= @.BeginDate and OpenDate <= @.EndDate
GROUP BY ProjectType, convert(char(6), OpenDate, 112)) AS A
ORDER BY A.ProjectType, A.YearMonth
I'm using a derived table, to encapsulate the inner query. A derived
table is sort of a temptable within the query, but is never materialised.
This is a very powerful tool in SQL.
I took the liberty to change the date formatting a bit, to not get
drowned in those issues. By nesting more derived tables, you can
get the format you want.
Since you did not supply sample data, this is untested.
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx|||Erland's solution is better than mine.
I realized that I failed to provide something that would work for more than
1 month. The challenge was that your original query2 really needs to be
reexecuted for each month returned by query1. So, my solution does not
satisify that.
Erland's solution utilized a sub-select for query 2 in order to accomplish
this. That would do the trick. You could also use the new CROSS APPLY in
SQL 2005 to accomplish this type of thing.
The one problem with Erland's solution is that I think his solution would
not correctly group all projects for the month into one row for that month.
I think if you change his use of convert(char(6), OpenDate, 112) back into
something like you were originally doing to change all entries from the same
month into the first of that month prior to grouping.
Sorry if I misled you. I did not quite grasp the whole problem originally.
--
Ryan Powers
Clarity Consulting
http://www.claritycon.com
"Erland Sommarskog" wrote:

> (LJohnsonNG@.gmail.com) writes:
> I think this query takes you closer to the final goal:
> SELECT A.ProjectType, A.YearMonth, A.ProjectsOpened,
> (SELECT COUNT(*)
> FROM Projects B
> WHERE B.OpenDate < dateadd(MONTH, 1, A.YearMonth + '01')
> AND (B.CloseDate IS NULL OR
> B.CloseDate >= dateadd(MONTH, 1, A.YearMonth + '01'))
)
> AS ActiveProjects
> FROM (SELECT ProjectType, YearMonth = convert(char(6), OpenDate, 112
),
> count(*) ProjectsOpened
> FROM Projects
> WHERE OpenDate >= @.BeginDate and OpenDate <= @.EndDate
> GROUP BY ProjectType, convert(char(6), OpenDate, 112)) AS A
> ORDER BY A.ProjectType, A.YearMonth
> I'm using a derived table, to encapsulate the inner query. A derived
> table is sort of a temptable within the query, but is never materialised.
> This is a very powerful tool in SQL.
> I took the liberty to change the date formatting a bit, to not get
> drowned in those issues. By nesting more derived tables, you can
> get the format you want.
> Since you did not supply sample data, this is untested.
>
> --
> Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
> Books Online for SQL Server 2005 at
> http://www.microsoft.com/technet/pr...oads/books.mspx
> Books Online for SQL Server 2000 at
> http://www.microsoft.com/sql/prodin...ions/books.mspx
>|||Why did you not post REAL DDL instead of making other people do it and
have to create their own sample data to help you? Is this what you
would have done, if you cared about us?
CREATE TABLE Projects
(project_id INTEGER NOT NULL PRIMARY KEY,
open_date DATETIME NOT NULL,
close_date DATETIME, -- null is still open
CHECK (open_date < close_date),
project_type CHAR(1) NOT NULL
CHECK (project_type IN ('a', 'b', ..))
);
dynamically for a give project_type.<<
Create a Calendar table; this is a standard SQL programming that you can
Google. The best way to do this is with a procedure. You need a
month-year which you want to keep in a temproal datatype -- I picked the
first day of a month -- so you can do math on it.
CREATE TABLE Calendar
(cal_date DATETIME NOT NULL PRIMARY KEY,
cal_year_month DATETIME NOT NULL,-- first day of month
.);
CREATE PROCEDURE MonthlyProjectTypeTally
(@.my_cal_year_month_open DATETIME,
@.my_cal_year_month_close DATETIME,
@.my_project_type CHAR(1))
AS
SELECT C1.cal_year_month,
@.my_project_type,
COUNT(DISTINCT project_id) AS project_tally
FROM Calendar AS C1, Projects AS P1
WHERE P1.project_type = @.my_project_type
AND C1.cal_date BETWEEN P1.open_date
AND COALESCE (P1.close_date, CURRENT_TIMESTAMP)
GROUP BY P1.project_type, C1.cal_year_month;
Avoid proprietary and expensive temporal computations with auxiliary
tables that will be useful in other queries.
--CELKO--
Please post DDL in a human-readable format and not a machine-generated
one. This way people do not have to guess what the keys, constraints,
DRI, datatypes, etc. in your schema are. Sample data is also a good
idea, along with clear specifications.
*** Sent via Developersdex http://www.examnotes.net ***|||> WHERE OpenDate >= @.BeginDate and OpenDate <= @.EndDate
Erland, I like your solution, but I have some reservations.
What if a projected opened on 15-jun-2005 and closed on 15-nov-2005,
and @.BeginDate=20050801 and @.EndDate=20050831? The project will surely
be open in Agust 2005, but will it mett the criteria OpenDate >=
@.BeginDate and OpenDate <= @.EndDate ? I don't think so.
I would suggest this criteria (assuming CLoseDate is not null):
( OpenDate >= @.BeginDate and OpenDate <= @.EndDate)
or
(@.BeginDate between OpenDate and CloseDate)
or
(@.EndDate between OpenDate and CloseDate)
What do you think?|||--CELKO-- (remove.jcelko212@.earthlink.net) writes:
> Avoid proprietary and expensive temporal computations with auxiliary
> tables that will be useful in other queries.
Yeah, that's good. Avoid anything that can be useful. Go with Celko's
useless solutions instead.
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx|||Alexander Kuznetsov (AK_TIREDOFSPAM@.hotmail.COM) writes:
> Erland, I like your solution, but I have some reservations.
> What if a projected opened on 15-jun-2005 and closed on 15-nov-2005,
> and @.BeginDate=20050801 and @.EndDate=20050831? The project will surely
> be open in Agust 2005, but will it mett the criteria OpenDate >=
> @.BeginDate and OpenDate <= @.EndDate ? I don't think so.
> I would suggest this criteria (assuming CLoseDate is not null):
> ( OpenDate >= @.BeginDate and OpenDate <= @.EndDate)
> or
> (@.BeginDate between OpenDate and CloseDate)
> or
> (@.EndDate between OpenDate and CloseDate)
> What do you think?
That criteria was from the original post, and that query was to display
projects *opened* during the period. Thus your example project were not to
to be listed as opened.
But I now realise that there is a flaw in my query: if there are no
projects at opened at all during a month, then that month would not
be displayed. Note that since project still may have been closed
during that month, the number of active projects could change from
the month before.
The best way to handle this, would be to use a month table (or some
sort of calendar table). We'll see if LJohnson comes back add says
what he thinks of the suggested solution. Who knows, maybe we even
get some test data to work with. :-)
--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx|||I'm coming into this thread a little late, but give this a try. This is a
stored procedure that takes a project type as an input and produces a
calendar list of the number of projects opened in that month, along with the
active projects.
I made the assumption that the datatype for ProjectType is int; the
following procedure would need to be modified if it wasn't.
CREATE PROCEDURE showProjects @.ProjectType int
AS
DECLARE @.minmonth int
DECLARE @.maxopen int
DECLARE @.maxclosed int
DECLARE @.maxmonth int
DECLARE @.numActiveProjects int
DECLARE @.ActiveProjects table (ProjectMonth datetime, NumberActiveProjects
int)
/* Get the month that the earliest project was opened in. */
SELECT @.minmonth = MIN(DATEDIFF(mm, 0, OpenDate)) FROM Projects
/* Since ClosedDate can be null, it's possible that
the latest date in the table will be an OpenDate. Check to see whether
OpenDate or Closed date has the latest date */
SELECT @.maxopen = MAX(DATEDIFF(mm, 0, OpenDate)) FROM Projects
SELECT @.maxclosed = MAX(DATEDIFF(mm, 0, ClosedDate)) FROM Projects
SELECT @.maxmonth = CASE WHEN @.maxopen >= @.maxclosed THEN @.maxopen ELSE
@.maxclosed END
WHILE (@.minmonth <= @.maxmonth)
BEGIN
SELECT @.numActiveProjects = Count(*)
FROM Projects P2
WHERE OpenDate <= dbo.EndOfMonth(DATEADD(mm, @.minmonth, 0))
AND (ClosedDate Is Null Or ClosedDate > dbo.EndOfMonth(DATEADD(mm,
@.minmonth, 0)))
AND ProjectType = @.ProjectType
INSERT INTO @.ActiveProjects VALUES(DATEADD(mm, @.minmonth, 0),
@.numActiveProjects)
SET @.minmonth = @.minmonth + 1
END
/* Join the results together to get both number of projects started in month
and number of active projects during that month */
SELECT A.ProjectMonth, ISNULL(t1.NumProjectsOpened, 0) AS "ProjectsOpened",
A.NumberActiveProjects
FROM @.ActiveProjects A
LEFT JOIN (SELECT DATEADD(mm, DATEDIFF(mm, 0, OpenDate), 0) AS ProjectMonth,
COUNT(*) AS NumProjectsOpened
FROM [Projects]
WHERE ProjectType = @.ProjectType
GROUP BY DATEADD(mm, DATEDIFF(mm, 0, OpenDate), 0)
) t1
ON A.ProjectMonth = t1.ProjectMonth
The results will be in the following format
ProjectMonth ProjectsOpened NumberActiveProjects
2005-01-01 3 3
2005-02-01 2 5
You can play around with the formatting of the date, but this way is pretty
readable as well.
"LJohnsonNG@.gmail.com" wrote:

> am trying to dynamically determine the number of Active Projects in a
> given month dynamically for a give ProjectType.
> (e.g. For June 2005, I would like to see all projects created on June
> 30, 2005 or before that have not yet closed grouped by Project Type.)
> **Simplified DDL **
> Projects
> (
> ProjectID
> OpenDate,
> ClosedDate,
> ProjectType
> )
> SQL I already have that works that gives the number of projects opened
> in a given month
> ======================
> SELECT ProjectType,
> cast(datename(month,opendate) as varchar(15)) [DateMonth],
> --Guarantees Months of Diferent Years are not grouped together
> Convert(datetime,datename(month,open
date) + ' 1 ' +
> datename(year,opendate),110) [OrderByDate],
> count(*) ProjectsOpened
> INTO ##temp
> FROM Projects P
> WHERE OpenDate >= @.BeginDate and OpenDate <= @.EndDate --Year
> to retrieve Projects for
> GROUP BY ProjectName,
> cast(datename(month,opendate) as varchar(15)),
> Convert(datetime,datename(month,open
date) + ' 1 ' +
> datename(year,opendate),110)
> ORDER BY Convert(datetime,datename(month,opendat
e) + ' 1 ' +
> datename(year,opendate),110),
> ProjectType
>
> I have a second query to retrieve the number of active projects at the
> end of a particular month
> ==========================
> SELECT ProjectType,Count(*) OpenProjectCount
> FROM Projects P2
> WHERE OpenDate <= dbo.EndOfMonth('01-May-2005')
> AND (ClosedDate Is Null Or ClosedDate >
> dbo.EndOfMonth('01-May-2005'))
> GROUP BY ProjectType
> What I can't Figure Out is how to join the two queries so that where
> you see '01-May-2005', I can pass the
> " Convert(datetime,datename(month,opendate
) + ' 1 ' +
> datename(year,opendate),110)" value and return the Active Projects for
> each particular month in the year span.
>
> RESULTSET SHOULD BE
>
> ProjectType DateMonth OrderByDate Projec
tsOpened ActiveProjects
> 1 Jan 01-Jan-2005 5 9
> 1 Feb 01-Feb-2005 3 7
> NOTE
> -====
> I have simplified the DDL for the example, but the gist of what I need
> remains the same
> Sorry about the google post and any formatting issues, but the company
> I work for doesn't allow outlook express to be used.
> L Anthony Johnson
>|||On 5 Jan 2006 19:33:49 -0800, Alexander Kuznetsov wrote:

>Erland, I like your solution, but I have some reservations.
>What if a projected opened on 15-jun-2005 and closed on 15-nov-2005,
>and @.BeginDate=20050801 and @.EndDate=20050831? The project will surely
>be open in Agust 2005, but will it mett the criteria OpenDate >=
>@.BeginDate and OpenDate <= @.EndDate ? I don't think so.
Hi Alexander,
I understand that it's not really relevant to the question originally
posed in this thread, but since situations like the one you describe
come up quite common, I'll chime in anyway.

>I would suggest this criteria (assuming CLoseDate is not null):
>( OpenDate >= @.BeginDate and OpenDate <= @.EndDate)
>or
>(@.BeginDate between OpenDate and CloseDate)
>or
>(@.EndDate between OpenDate and CloseDate)
>What do you think?
A better way to test for any overlap between two periods (OpenDate to
CloseDate vs @.BeginDate to @.EndDate) is
WHERE OpenDate < @.EndDate
AND CloseDate > @.BeginDate
This is a lot shorter, and it's completely sargable (which means that
SQL Server can use index s if an index on either or both of these
columns is defined).
Hugo Kornelis, SQL Server MVP