Showing posts with label input. Show all posts
Showing posts with label input. Show all posts

Wednesday, March 21, 2012

help with report

Hi everybody!

Need to create report according to example below

User will input start & end date

From Date:______ ToDate:________

Then the report should create output like this:

CustomerID: Number of orders:

John 5

Jim 12

Aileen 21

I tried to accomplish that with Northwind / Orders, had no success.

Thank you in advance.

Alex

Is your question about how to write the SELECT statement to retrive the data, or how to use Reporting Services to create a formatted report?

It would help to tell us what you've tried; that might give us a clue why you've not been successful.

Don|||

Hi Don,

my question is about how to write the SELECT statement to retrive the data

Thanks for the reply

|||You're going to keep what you've tried a close secret, eh? Heh.

This does what you want, except that it doesn't include the join to get the customer name:

SELECT [CustomerID]
,COUNT(*)AS [Numberof orders]
FROM [Orders]
WHERE OrderDateBETWEEN'6/1/1997'AND'6/30/1997'GROUP BY CustomerID
Don|||

Thanks again Don,

I did tried to use that Select string, I have no errors but also no results.

I am using datagrid to display the result of the query.

This is the code that I use as ASP.NET, please take a pick.

I must to admit that I'm very green in SQL area.

<%@. Page Language="VB" %><%@. import Namespace="System.Data.SqlClient" %><%@. import Namespace="System.Data.SqlTypes" %><script runat="server"> shared public dim startDate as date shared public dim endDate as date sub load_page() startDate = "6/1/1997" endDate = "6/30/1997" dgOrders.DataSource = orders(startDate, endDate ) dgOrders.DataBind() end sub Function orders(ByVal fromDate As Date, ByVal toDate As Date) As System.Data.IDataReader Dim connectionString As String = "server='(local)'; trusted_connection=true; database='Northwind'" Dim dbConnection As System.Data.IDbConnection = New System.Data.SqlClient.SqlConnection(connectionString) Dim orderNumb as integer Dim queryString As String queryString = "SELECT [Orders].[CustomerID] ,COUNT(*) AS [orderNumb] FROM [Orders] WHERE OrderDate BETWEEN startDate AND endDate GROUP BY CustomerID" Dim dbCommand As System.Data.IDbCommand = New System.Data.SqlClient.SqlCommand dbCommand.CommandText = queryString dbCommand.Connection = dbConnection Dim dbParam_fromDate As System.Data.IDataParameter = New System.Data.SqlClient.SqlParameter dbParam_fromDate.ParameterName = "@.fromDate" dbParam_fromDate.Value = fromDate dbParam_fromDate.DbType = System.Data.DbType.DateTime dbCommand.Parameters.Add(dbParam_fromDate) Dim dbParam_toDate As System.Data.IDataParameter = New System.Data.SqlClient.SqlParameter dbParam_toDate.ParameterName = "@.toDate" dbParam_toDate.Value = toDate dbParam_toDate.DbType = System.Data.DbType.DateTime dbCommand.Parameters.Add(dbParam_toDate) dbConnection.Open Dim dataReader As System.Data.IDataReader = dbCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection) Return dataReader End Function</script><html><head></head><body> <form runat="server"> <p> </p> <p> <asp:DataGrid id="dgOrders" runat="server" AutoGenerateColumns="False"> <Columns> <asp:BoundColumn DataField="orderNumb" HeaderText="CustomerID"></asp:BoundColumn> <asp:BoundColumn DataField="Orders" HeaderText="Number of Orders"></asp:BoundColumn> </Columns> </asp:DataGrid> </p> </form></body></html>
|||

try this:

BETWEEN @.startDate AND @.endDate

|||Actually, it would be
BETWEEN @.fromDateAND @.toDate
based on what are apparently the parameter names.

Don|||

Thank you guys for helping me here,

i tried that:BETWEEN @.fromDateAND @.toDate

still get empti web page, no errors though.

I probably missing somthing here, but i don't where to look cause there is no compilation error of any kind.

Thanks again

|||

Can you try this:

<asp:DataGrid id="dgOrders" runat="server" AutoGenerateColumns="True"> </asp:DataGrid>

You don't have the orders column in your data source.


|||

Thank Limno

did tried that - get the same result - blank web page.

may be using the datagrid isn't such a good idea for this case?

just don't know how to display the query result in any other way.

Alex

|||

The datareader for the datagrid looks fine to me. Can you confirm that you can run the sql query in your database with the results you are expecting? (hard code your dates in the query).

If you can get something from your database, we will be close to get something in the code.

|||

Hi Limno,

I did try this Select statement in SQL Query Analiser:

SELECT [CustomerID]
,COUNT(*)AS [Numberof orders]
FROM [Orders]
WHERE OrderDateBETWEEN'6/1/1997'AND'6/30/1997'
GROUP BY CustomerID
and did get the following result:
ANTON 1
AROUT 1
BERGS 1
BLAUS 1
BLONP 3
BSBEV 1
ERNSH 1
FAMIA 1
FOLKO 1
...
I went through my code again and again but dont find the problem.
Please advice

Alex

|||

Here you go:

<%@. Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

Public Shared startDate As Date
Public Shared endDate As Date

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

startDate = "6/1/1997"
endDate = "6/30/1997"
dgOrders.DataSource = orders(startDate, endDate)
dgOrders.DataBind()
End Sub




Function orders(ByVal fromDate As Date, ByVal toDate As Date) As System.Data.IDataReader
Dim connectionString As String = System.Configuration.ConfigurationManager.ConnectionStrings("Personal").ConnectionString
Dim dbConnection As System.Data.IDbConnection = New System.Data.SqlClient.SqlConnection(connectionString)


Dim queryString As String
queryString = "SELECT [Orders].[CustomerID] ,COUNT(*) AS [orderNumb] FROM [Orders] WHERE OrderDate BETWEEN @.startDate AND @.endDate GROUP BY CustomerID"
Dim dbCommand As System.Data.IDbCommand = New System.Data.SqlClient.SqlCommand
dbCommand.CommandText = queryString
dbCommand.Connection = dbConnection

Dim dbParam_fromDate As System.Data.IDataParameter = New System.Data.SqlClient.SqlParameter
dbParam_fromDate.ParameterName = "@.startDate"
dbParam_fromDate.Value = fromDate
dbParam_fromDate.DbType = System.Data.DbType.DateTime
dbCommand.Parameters.Add(dbParam_fromDate)

Dim dbParam_toDate As System.Data.IDataParameter = New System.Data.SqlClient.SqlParameter
dbParam_toDate.ParameterName = "@.endDate"
dbParam_toDate.Value = toDate
dbParam_toDate.DbType = System.Data.DbType.DateTime
dbCommand.Parameters.Add(dbParam_toDate)

dbConnection.Open()
Dim dataReader As System.Data.IDataReader = dbCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection)

Return dataReader
End Function

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataGrid id="dgOrders" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundColumn DataField="CustomerID" HeaderText="CustomerID"></asp:BoundColumn>
<asp:BoundColumn DataField="orderNumb" HeaderText="Number of Orders"></asp:BoundColumn>
</Columns>
</asp:DataGrid>


</div>
</form>
</body>
</html>

|||

You are great Limno!

That works right from the beginning.

Didn't had the chance to examine the code yet, I'm curies how did you find the problem?

Oh…, is this two lines Must have in my code?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

Thank a lotLimno

Alex

|||

Hi Alex:

Actually, your page load event syntax is not right. It would save you a lot of headaches to use the IDE's intellisence. Pick the Page from left dropdown list , then find the event you want from right dropdown list.

Not the two lines you think. you can test them out.

Monday, March 19, 2012

Help with query field parameters

Hi everyone,

within one of my reports I would like to take an input parameter, feed this into a dataset which is then used to populate the dataset of the query used in the main body of the report. It won't allow me todo this, could you guys offer any advice on how this may be possible?

E.g

Sub Query:

@.personIds = SELECT personid FROM people WHERE name = @.name;

Main Report Query

SELECT * FROM orders WHERE personId IN (@.personIds)

I cannot change the "main report query" as this is actually a stored procedure from an external application vendor. Please help.

Kind regards

Taz

Is the @.Name the parameter in your report?

If so, then set up the population of that parameter up with the SQL:

Select PersonID, Name from People

with the value = PersonID and the label = Name

and then in your main query, the personid is returned as the parameter value.

Hope that helps.

BobP

|||

Hi Bobp, thanks for the response.

The problem I have is that the SQL (SELECT * FROM people) will return 100s of rows. I dont want to populate the drop down with all of these, instead I want the drop down to have generic options (i.e. "David", "James") which when selected perform the query to get ther relvant IDs and then populate the query that the main report is based on.

Does that clarify at all?

Thanks again for your suggestion, do you have any further ideas?

Kind regards
Taz

|||

Yes, actually... one more idea...

Create parameter named @.Name. This is a string, user type in.

Then create another parameter named @.personIDs. This should be a string, with hidden and multi value selected.

Create a dataset, using the following SQL:

Select personid from people where name like '%' + @.name + '%'

Use this new data set as the Default Values Query for the @.PersonIDs parameter.

Then you can pass personid to your main query like this:

SELECT * FROM orders WHERE personId IN (@.personIds)

Make sure that in the parameter list, the @.Name parameter is first in the list.

Let me know if that works for you. I have tested, and am using that approach in several reports where the select list is too long.

Another way to do this would be to make the @.PersonIDs NOT hidden, and also use the new dataset to populate the available values of it, and allow the user to select an individual. This way, if the user types in DAVID, a second parameters asks the user to select from a list of '%David%'

BobP

|||

Bobp,

fantastic, this worked perfectly! many thanks for your help here.

One further question in regards to thw SQL command IN. i.e.

SELECT * FROM people WHERE peopleid IN(1,2,3,4);

is it possible to easily negate the IN within reporting services filters?

I have something like

=Fields!Name.value IN =Parameters!Names.value

How can I negate this?

Any help appreciated, however what you have done so far is fantastic enough! :)

Kind regards
Taz

|||

No problem at all...

I am not sure what you are trying to do with the filter... Could you give some more detail?

by default, the names that come back should be like the names parameter.

Thanks

BobP

|||

BobP - BIM wrote:

No problem at all...

I am not sure what you are trying to do with the filter... Could you give some more detail?

by default, the names that come back should be like the names parameter.

Thanks

BobP

Well, I am finding that I am overloading the input parameter for my stored procedure. It has a limit of 4000 characters, and my dynamic SQL is along the region of 5200 characters.

I can generate the SQL for the stored proc in 2 ways, either get the clients who have bought something (small list) or get the clients who haven't bought something (very long list). The former works fine (small list) however when i try to send in the big list it exceeds the limit of the stored proc and thus falls over.

Thus, I thought maybe I could instead return everything and then create a filter on my dataset where I do something like

Expression:
=Fields!ClientName.Value

Operator:
IN

Value:

=Parameters!ResultsOfTheSQLQueryWeCreatedBefore.Value

This works, i.e. show all the rows where the ClientName appears in the Parameter list. I was wonder if there was a simple way of making it show all the rows where the ClientName does NOT appear in the Parameter list without loading the huge list instad (as this would be slow).

Man this is a difficult one to explain. I hope I was succesful. Thanks for your time Bob!

Taz

Help with Query Field Parameters

Hi everyone,
within one of my reports I would like to take an input parameter, feed this
into a dataset which is then used to populate the dataset of the query used
in the main body of the report. It won't allow me todo this, could you guys
offer any advice on how this may be possible?
E.g
Sub Query:
@.personIds = SELECT personid FROM people WHERE name = @.name;
Main Report Query
SELECT * FROM orders WHERE personId IN (@.personIds)
I cannot change the "main report query" as this is actually a stored
procedure from an external application vendor. Please help.
Kind regards
TazWhat is your exact problem? Can you run the stored procedure from Query
Analyzer?
As I said, I don't really understand the issue but if you are trying to do a
master detail report you should be using subreports.
Bruce Loehle-Conger
MVP SQL Server Reporting Services
"Tarun Mistry" <nospam@.nospam.com> wrote in message
news:%23TPfRN95GHA.4112@.TK2MSFTNGP04.phx.gbl...
> Hi everyone,
> within one of my reports I would like to take an input parameter, feed
> this into a dataset which is then used to populate the dataset of the
> query used in the main body of the report. It won't allow me todo this,
> could you guys offer any advice on how this may be possible?
> E.g
> Sub Query:
> @.personIds = SELECT personid FROM people WHERE name = @.name;
> Main Report Query
> SELECT * FROM orders WHERE personId IN (@.personIds)
> I cannot change the "main report query" as this is actually a stored
> procedure from an external application vendor. Please help.
> Kind regards
> Taz
>|||Sorry, let me try again.
In my main report I have a drop down parameter with 2 values, lets say its
called "Locations" with 2 posible values
Locations:
Leeds
Wakefield
Based on this I need to run a query that will return all the customers
within that region, i.e.
SELECT customerids FROM customer WHERE location=@.location
I would then like to feed the results of this query into the query used in
the dataset that populates the report (which is actually a stored procedure
not a query). i wanted todo this by setting the Parameter within thr dataset
= to the results of the above query (which I tried to create as a seperate
dataset).
You see, the stored procedure is hard coded with a ... "WHERE IN(@.params)" ,
and I would like my above query to populate the @.params parameter for me.
I hope this clarifies in some way, if not, i can have another bash. Finally,
the sceanrio i gave above is not the one I have, however it represents the
same problem. Such, i cant change it.
Thanks
Taz
"Bruce L-C [MVP]" <bruce_lcNOSPAM@.hotmail.com> wrote in message
news:u7Jffe95GHA.3952@.TK2MSFTNGP04.phx.gbl...
> What is your exact problem? Can you run the stored procedure from Query
> Analyzer?
> As I said, I don't really understand the issue but if you are trying to do
> a master detail report you should be using subreports.
>
> --
> Bruce Loehle-Conger
> MVP SQL Server Reporting Services
> "Tarun Mistry" <nospam@.nospam.com> wrote in message
> news:%23TPfRN95GHA.4112@.TK2MSFTNGP04.phx.gbl...
>> Hi everyone,
>> within one of my reports I would like to take an input parameter, feed
>> this into a dataset which is then used to populate the dataset of the
>> query used in the main body of the report. It won't allow me todo this,
>> could you guys offer any advice on how this may be possible?
>> E.g
>> Sub Query:
>> @.personIds = SELECT personid FROM people WHERE name = @.name;
>> Main Report Query
>> SELECT * FROM orders WHERE personId IN (@.personIds)
>> I cannot change the "main report query" as this is actually a stored
>> procedure from an external application vendor. Please help.
>> Kind regards
>> Taz
>