Friday, March 30, 2012
grouping similar data
Thank you,
Bil
From http://www.developmentnow.com/g/115_0_0_0_0_0/sql-server-reporting-services.ht
Posted via DevelopmentNow.com Group
http://www.developmentnow.comIf I am understanding correctly you want to have the booth size and then
show all companies under that that match that description. Have you tried
grouping it by the booth size and then put your companies in the detail?
"Bkana" <nospam@.developmentnow.com> wrote in message
news:97f68ac4-a6e2-429d-a4e5-3b98fed63020@.developmentnow.com...
> In layout view: how do I group all the company names together who have the
> same value for a certain field? For instance, 10 companies all have the
> same booth size, but in Preview mode, it lists each company seprately with
> the booth size on each line. I need to have the booth size listed once
> with all the companies who share that common size. I have tried adding a
> group and using the expression for the company name as well as the booth
> size and it does not work. Can someone provide some detailed instructions?
> Thank you,
> Bill
> From
> http://www.developmentnow.com/g/115_0_0_0_0_0/sql-server-reporting-services.htm
> Posted via DevelopmentNow.com Groups
> http://www.developmentnow.com
Grouping Row Border
I'm trying to achieve the following in a table. Have border after/before the
grouping changes to the next value.
Nov 1 v1 v2 v3 v4
v1 v2 v3 v4
_____________________
Nov 3 v1 v2 v3 v4
_____________________
Is there an easy way to do this?Yes. Add either a top or bottom border to group footer row in the table. The
report at end of this posting demonstrates how to accomplish this task.
--
Bruce Johnson [MSFT]
Microsoft SQL Server Reporting Services
This posting is provided "AS IS" with no warranties, and confers no rights.
"Reg" <reg@.dsl.za.org> wrote in message
news:O%23n$LYymEHA.3684@.TK2MSFTNGP10.phx.gbl...
> Hi,
> I'm trying to achieve the following in a table. Have border after/before
the
> grouping changes to the next value.
> Nov 1 v1 v2 v3 v4
> v1 v2 v3 v4
> _____________________
> Nov 3 v1 v2 v3 v4
> _____________________
> Is there an easy way to do this?
>
BorderBetweenGroups.rdl
<?xml version="1.0" encoding="utf-8"?>
<Report
xmlns="http://schemas.microsoft.com/sqlserver/reporting/2003/10/reportdefini
tion"
xmlns:rd="">http://schemas.microsoft.com/SQLServer/reporting/reportdesigner">
<RightMargin>1in</RightMargin>
<Body>
<ReportItems>
<Table Name="table1">
<Height>1.125in</Height>
<Style />
<Header>
<TableRows>
<TableRow>
<Height>0.25in</Height>
<TableCells>
<TableCell>
<ReportItems>
<Textbox Name="textbox1">
<Style>
<PaddingLeft>2pt</PaddingLeft>
<PaddingBottom>2pt</PaddingBottom>
<PaddingTop>2pt</PaddingTop>
<PaddingRight>2pt</PaddingRight>
</Style>
<ZIndex>14</ZIndex>
<rd:DefaultName>textbox1</rd:DefaultName>
<CanGrow>true</CanGrow>
<Value />
</Textbox>
</ReportItems>
</TableCell>
<TableCell>
<ReportItems>
<Textbox Name="textbox2">
<Style>
<PaddingLeft>2pt</PaddingLeft>
<PaddingBottom>2pt</PaddingBottom>
<PaddingTop>2pt</PaddingTop>
<PaddingRight>2pt</PaddingRight>
</Style>
<ZIndex>13</ZIndex>
<rd:DefaultName>textbox2</rd:DefaultName>
<CanGrow>true</CanGrow>
<Value />
</Textbox>
</ReportItems>
</TableCell>
<TableCell>
<ReportItems>
<Textbox Name="textbox3">
<Style>
<PaddingLeft>2pt</PaddingLeft>
<PaddingBottom>2pt</PaddingBottom>
<PaddingTop>2pt</PaddingTop>
<PaddingRight>2pt</PaddingRight>
</Style>
<ZIndex>12</ZIndex>
<rd:DefaultName>textbox3</rd:DefaultName>
<CanGrow>true</CanGrow>
<Value />
</Textbox>
</ReportItems>
</TableCell>
</TableCells>
</TableRow>
</TableRows>
</Header>
<Details>
<TableRows>
<TableRow>
<Height>0.25in</Height>
<TableCells>
<TableCell>
<ReportItems>
<Textbox Name="CompanyName">
<Style>
<PaddingLeft>2pt</PaddingLeft>
<PaddingBottom>2pt</PaddingBottom>
<PaddingTop>2pt</PaddingTop>
<PaddingRight>2pt</PaddingRight>
</Style>
<ZIndex>2</ZIndex>
<rd:DefaultName>CompanyName</rd:DefaultName>
<CanGrow>true</CanGrow>
<Value>=Fields!CompanyName.Value</Value>
</Textbox>
</ReportItems>
</TableCell>
<TableCell>
<ReportItems>
<Textbox Name="City">
<Style>
<PaddingLeft>2pt</PaddingLeft>
<PaddingBottom>2pt</PaddingBottom>
<PaddingTop>2pt</PaddingTop>
<PaddingRight>2pt</PaddingRight>
</Style>
<ZIndex>1</ZIndex>
<rd:DefaultName>City</rd:DefaultName>
<CanGrow>true</CanGrow>
<Value>=Fields!City.Value</Value>
</Textbox>
</ReportItems>
</TableCell>
<TableCell>
<ReportItems>
<Textbox Name="textbox6">
<Style>
<PaddingLeft>2pt</PaddingLeft>
<PaddingBottom>2pt</PaddingBottom>
<PaddingTop>2pt</PaddingTop>
<PaddingRight>2pt</PaddingRight>
</Style>
<rd:DefaultName>textbox6</rd:DefaultName>
<CanGrow>true</CanGrow>
<Value />
</Textbox>
</ReportItems>
</TableCell>
</TableCells>
</TableRow>
</TableRows>
</Details>
<DataSetName>Northwind</DataSetName>
<TableGroups>
<TableGroup>
<Header>
<TableRows>
<TableRow>
<Height>0.25in</Height>
<TableCells>
<TableCell>
<ReportItems>
<Textbox Name="Country">
<Style>
<PaddingLeft>2pt</PaddingLeft>
<PaddingBottom>2pt</PaddingBottom>
<PaddingTop>2pt</PaddingTop>
<PaddingRight>2pt</PaddingRight>
</Style>
<ZIndex>11</ZIndex>
<rd:DefaultName>Country</rd:DefaultName>
<CanGrow>true</CanGrow>
<Value>=Fields!Country.Value</Value>
</Textbox>
</ReportItems>
</TableCell>
<TableCell>
<ReportItems>
<Textbox Name="textbox11">
<Style>
<PaddingLeft>2pt</PaddingLeft>
<PaddingBottom>2pt</PaddingBottom>
<PaddingTop>2pt</PaddingTop>
<PaddingRight>2pt</PaddingRight>
</Style>
<ZIndex>10</ZIndex>
<rd:DefaultName>textbox11</rd:DefaultName>
<CanGrow>true</CanGrow>
<Value />
</Textbox>
</ReportItems>
</TableCell>
<TableCell>
<ReportItems>
<Textbox Name="textbox12">
<Style>
<PaddingLeft>2pt</PaddingLeft>
<PaddingBottom>2pt</PaddingBottom>
<PaddingTop>2pt</PaddingTop>
<PaddingRight>2pt</PaddingRight>
</Style>
<ZIndex>9</ZIndex>
<rd:DefaultName>textbox12</rd:DefaultName>
<CanGrow>true</CanGrow>
<Value />
</Textbox>
</ReportItems>
</TableCell>
</TableCells>
</TableRow>
</TableRows>
</Header>
<Grouping Name="table1_Group1">
<GroupExpressions>
<GroupExpression>=Fields!Country.Value</GroupExpression>
</GroupExpressions>
</Grouping>
<Footer>
<TableRows>
<TableRow>
<Height>0.125in</Height>
<TableCells>
<TableCell>
<ReportItems>
<Textbox Name="textbox13">
<Style>
<PaddingLeft>2pt</PaddingLeft>
<BorderWidth>
<Default>2pt</Default>
</BorderWidth>
<BorderStyle>
<Bottom>Solid</Bottom>
</BorderStyle>
<PaddingBottom>2pt</PaddingBottom>
<PaddingTop>2pt</PaddingTop>
<PaddingRight>2pt</PaddingRight>
</Style>
<ZIndex>5</ZIndex>
<rd:DefaultName>textbox13</rd:DefaultName>
<CanGrow>true</CanGrow>
<Value />
</Textbox>
</ReportItems>
</TableCell>
<TableCell>
<ReportItems>
<Textbox Name="textbox14">
<Style>
<PaddingLeft>2pt</PaddingLeft>
<BorderWidth>
<Default>2pt</Default>
</BorderWidth>
<BorderStyle>
<Bottom>Solid</Bottom>
</BorderStyle>
<PaddingBottom>2pt</PaddingBottom>
<PaddingTop>2pt</PaddingTop>
<PaddingRight>2pt</PaddingRight>
</Style>
<ZIndex>4</ZIndex>
<rd:DefaultName>textbox14</rd:DefaultName>
<CanGrow>true</CanGrow>
<Value />
</Textbox>
</ReportItems>
</TableCell>
<TableCell>
<ReportItems>
<Textbox Name="textbox15">
<Style>
<PaddingLeft>2pt</PaddingLeft>
<BorderWidth>
<Default>2pt</Default>
</BorderWidth>
<BorderStyle>
<Bottom>Solid</Bottom>
</BorderStyle>
<PaddingBottom>2pt</PaddingBottom>
<PaddingTop>2pt</PaddingTop>
<PaddingRight>2pt</PaddingRight>
</Style>
<ZIndex>3</ZIndex>
<rd:DefaultName>textbox15</rd:DefaultName>
<CanGrow>true</CanGrow>
<Value />
</Textbox>
</ReportItems>
</TableCell>
</TableCells>
</TableRow>
</TableRows>
</Footer>
</TableGroup>
</TableGroups>
<Footer>
<TableRows>
<TableRow>
<Height>0.25in</Height>
<TableCells>
<TableCell>
<ReportItems>
<Textbox Name="textbox7">
<Style>
<PaddingLeft>2pt</PaddingLeft>
<PaddingBottom>2pt</PaddingBottom>
<PaddingTop>2pt</PaddingTop>
<PaddingRight>2pt</PaddingRight>
</Style>
<ZIndex>8</ZIndex>
<rd:DefaultName>textbox7</rd:DefaultName>
<CanGrow>true</CanGrow>
<Value />
</Textbox>
</ReportItems>
</TableCell>
<TableCell>
<ReportItems>
<Textbox Name="textbox8">
<Style>
<PaddingLeft>2pt</PaddingLeft>
<PaddingBottom>2pt</PaddingBottom>
<PaddingTop>2pt</PaddingTop>
<PaddingRight>2pt</PaddingRight>
</Style>
<ZIndex>7</ZIndex>
<rd:DefaultName>textbox8</rd:DefaultName>
<CanGrow>true</CanGrow>
<Value />
</Textbox>
</ReportItems>
</TableCell>
<TableCell>
<ReportItems>
<Textbox Name="textbox9">
<Style>
<PaddingLeft>2pt</PaddingLeft>
<PaddingBottom>2pt</PaddingBottom>
<PaddingTop>2pt</PaddingTop>
<PaddingRight>2pt</PaddingRight>
</Style>
<ZIndex>6</ZIndex>
<rd:DefaultName>textbox9</rd:DefaultName>
<CanGrow>true</CanGrow>
<Value />
</Textbox>
</ReportItems>
</TableCell>
</TableCells>
</TableRow>
</TableRows>
</Footer>
<TableColumns>
<TableColumn>
<Width>2.16667in</Width>
</TableColumn>
<TableColumn>
<Width>2.16667in</Width>
</TableColumn>
<TableColumn>
<Width>2.16667in</Width>
</TableColumn>
</TableColumns>
</Table>
</ReportItems>
<Style />
<Height>2in</Height>
</Body>
<TopMargin>1in</TopMargin>
<DataSources>
<DataSource Name="Northwind">
<rd:DataSourceID>76d8d0cd-7eef-42fe-aa1e-81920ad6bfed</rd:DataSourceID>
<ConnectionProperties>
<DataProvider>SQL</DataProvider>
<ConnectString>data source=localhost;initial
catalog=Northwind</ConnectString>
<IntegratedSecurity>true</IntegratedSecurity>
</ConnectionProperties>
</DataSource>
</DataSources>
<Width>6.5in</Width>
<DataSets>
<DataSet Name="Northwind">
<Fields>
<Field Name="CustomerID">
<DataField>CustomerID</DataField>
<rd:TypeName>System.String</rd:TypeName>
</Field>
<Field Name="CompanyName">
<DataField>CompanyName</DataField>
<rd:TypeName>System.String</rd:TypeName>
</Field>
<Field Name="ContactName">
<DataField>ContactName</DataField>
<rd:TypeName>System.String</rd:TypeName>
</Field>
<Field Name="ContactTitle">
<DataField>ContactTitle</DataField>
<rd:TypeName>System.String</rd:TypeName>
</Field>
<Field Name="Address">
<DataField>Address</DataField>
<rd:TypeName>System.String</rd:TypeName>
</Field>
<Field Name="City">
<DataField>City</DataField>
<rd:TypeName>System.String</rd:TypeName>
</Field>
<Field Name="Region">
<DataField>Region</DataField>
<rd:TypeName>System.String</rd:TypeName>
</Field>
<Field Name="PostalCode">
<DataField>PostalCode</DataField>
<rd:TypeName>System.String</rd:TypeName>
</Field>
<Field Name="Country">
<DataField>Country</DataField>
<rd:TypeName>System.String</rd:TypeName>
</Field>
<Field Name="Phone">
<DataField>Phone</DataField>
<rd:TypeName>System.String</rd:TypeName>
</Field>
<Field Name="Fax">
<DataField>Fax</DataField>
<rd:TypeName>System.String</rd:TypeName>
</Field>
</Fields>
<Query>
<DataSourceName>Northwind</DataSourceName>
<CommandText>select * from customers</CommandText>
<rd:UseGenericDesigner>true</rd:UseGenericDesigner>
</Query>
</DataSet>
</DataSets>
<LeftMargin>1in</LeftMargin>
<rd:SnapToGrid>true</rd:SnapToGrid>
<rd:DrawGrid>true</rd:DrawGrid>
<rd:ReportID>2fd0260c-7640-49fb-9128-9a9647b7cdcf</rd:ReportID>
<BottomMargin>1in</BottomMargin>
<Language>en-US</Language>
</Report>
Wednesday, March 28, 2012
Grouping parameter value
I'm working on a stored procedure that works fine. I just want to make it possible for the user to be able to have a drop down list in reporting services to display the "question codes" grouped by whatever the first two digits are. for example.
VT01
VT02
VT03
VN01
VN02
VN03
ST01
ST02
ST03
instead of listing everything, i want the viewers to see this
VT
VN
ST
or an alias for each of these like this:
Vet Tasks
Vet National
Survey Tasks
Survey National
any ideas, here's my current code, which is pullin up anything with the added substring part
Code Snippet
ALTER PROCEDURE [dbo].[Testing_Questions]
(@.Region_Key int=null,@.QuestionCode char(5))
AS
BEGIN
SELECT dbo.Qry_Questions.Territory,
dbo.Qry_Questions.SalesResponsible,
dbo.Qry_Questions.Customer,
dbo.Qry_Questions.Date,
dbo.Qry_Questions.StoreName,
dbo.Qry_Questions.PostCode,
dbo.Qry_Questions.Address2,
dbo.Qry_Questions.[Question Code],
dbo.Qry_Questions.Question,
dbo.Qry_Questions.[Response Type],
dbo.Qry_Questions.response,
dbo.Qry_Questions.sales_person_code,
dbo.Qry_Sales_Group.Region_Key,
dbo.Qry_Sales_Group.Region
FROM dbo.Qry_Questions
INNER JOIN dbo.Qry_Sales_Group
ON dbo.Qry_Questions.sales_person_code COLLATE SQL_Latin1_General_CP1_CI_AS = dbo.Qry_Sales_Group.SalesPerson_Purchaser_Code
WHERE REGION_KEY=@.Region_Key
AND SUBSTRING(dbo.Qry_Questions.[Question Code],0,3)=@.QuestionCode
END
SET NOCOUNT OFF
You might try using the follwing as the grouping expression:
Code Snippet
=Left(Fields!<your_field>.Value, 2)From there you could either set up a CASE statement or code for your aliases.
Hope this helps!
Scott
|||I have the report working, i just want to be able to group the choices into 6 different choices. I know there is a way to do this in the report parameters properties box. I created my own non queried values that look like this:
Label Value
Survey national =IIF(Left(Fields!Question_Code.Value, 2)="SN",Fields!Question_Code.Value,nothing)
Survey vet =IIF(Left(Fields!Question_Code.Value, 2)="SV",Fields!Question_Code.Value,nothing)
Survey independent =IIF(Left(Fields!Question_Code.Value, 2)="SI",Fields!Question_Code.Value,nothing)
and so on...
But i keep getting an error :
A Value expression used for the report parameter ��QuestionCode�� refers to a field. Fields cannot be used in report parameter expressions.
[rsFieldInReportParameterExpression] A Value expression used for the report parameter ��QuestionCode�� refers to a field. Fields cannot be used in report parameter expressions.
[rsFieldInReportParameterExpression] A Value expression used for the report parameter ��QuestionCode�� refers to a field.
what am i doing wrong?
|||I believe that if you are populating values for a parameter, you can't use the same dataset used in the report. I vaguely remember running into the same problem when I fist began using parameters. We use separate datasets for the parameters in our reports.
|||Create a second dataset with the following query:
Select Distinct Left(Question_Code, 2)
FROM dbo.Qry_Questions
Group by Question_Code
Order by Question_Code
Change your parameter to query and point it at this new dataset. Then in your main dataset query add the following to your where statement:
Where Question_Code IN(@.question_code_parm)
|||Thanks that worked beautifully!! And i was able to hard code and rename the Question codes that were group for report parameters.Grouping parameter value
I'm working on a stored procedure that works fine. I just want to make it possible for the user to be able to have a drop down list in reporting services to display the "question codes" grouped by whatever the first two digits are. for example.
VT01
VT02
VT03
VN01
VN02
VN03
ST01
ST02
ST03
instead of listing everything, i want the viewers to see this
VT
VN
ST
or an alias for each of these like this:
Vet Tasks
Vet National
Survey Tasks
Survey National
any ideas, here's my current code, which is pullin up anything with the added substring part
Code Snippet
ALTER PROCEDURE [dbo].[Testing_Questions]
(@.Region_Key int=null,@.QuestionCode char(5))
AS
BEGIN
SELECT dbo.Qry_Questions.Territory,
dbo.Qry_Questions.SalesResponsible,
dbo.Qry_Questions.Customer,
dbo.Qry_Questions.Date,
dbo.Qry_Questions.StoreName,
dbo.Qry_Questions.PostCode,
dbo.Qry_Questions.Address2,
dbo.Qry_Questions.[Question Code],
dbo.Qry_Questions.Question,
dbo.Qry_Questions.[Response Type],
dbo.Qry_Questions.response,
dbo.Qry_Questions.sales_person_code,
dbo.Qry_Sales_Group.Region_Key,
dbo.Qry_Sales_Group.Region
FROM dbo.Qry_Questions
INNER JOIN dbo.Qry_Sales_Group
ON dbo.Qry_Questions.sales_person_code COLLATE SQL_Latin1_General_CP1_CI_AS = dbo.Qry_Sales_Group.SalesPerson_Purchaser_Code
WHERE REGION_KEY=@.Region_Key
AND SUBSTRING(dbo.Qry_Questions.[Question Code],0,3)=@.QuestionCode
END
SET NOCOUNT OFF
You might try using the follwing as the grouping expression:
Code Snippet
=Left(Fields!<your_field>.Value, 2)From there you could either set up a CASE statement or code for your aliases.
Hope this helps!
Scott
|||I have the report working, i just want to be able to group the choices into 6 different choices. I know there is a way to do this in the report parameters properties box. I created my own non queried values that look like this:
Label Value
Survey national =IIF(Left(Fields!Question_Code.Value, 2)="SN",Fields!Question_Code.Value,nothing)
Survey vet =IIF(Left(Fields!Question_Code.Value, 2)="SV",Fields!Question_Code.Value,nothing)
Survey independent =IIF(Left(Fields!Question_Code.Value, 2)="SI",Fields!Question_Code.Value,nothing)
and so on...
But i keep getting an error :
A Value expression used for the report parameter ��QuestionCode�� refers to a field. Fields cannot be used in report parameter expressions.
[rsFieldInReportParameterExpression] A Value expression used for the report parameter ��QuestionCode�� refers to a field. Fields cannot be used in report parameter expressions.
[rsFieldInReportParameterExpression] A Value expression used for the report parameter ��QuestionCode�� refers to a field.
what am i doing wrong?
|||I believe that if you are populating values for a parameter, you can't use the same dataset used in the report. I vaguely remember running into the same problem when I fist began using parameters. We use separate datasets for the parameters in our reports.
|||Create a second dataset with the following query:
Select Distinct Left(Question_Code, 2)
FROM dbo.Qry_Questions
Group by Question_Code
Order by Question_Code
Change your parameter to query and point it at this new dataset. Then in your main dataset query add the following to your where statement:
Where Question_Code IN(@.question_code_parm)
|||Thanks that worked beautifully!! And i was able to hard code and rename the Question codes that were group for report parameters.grouping or formatting issue
hey there
I have a date time field
Fields!CreatedDate.Value which I am grouping on and Counting on an ID Field
result of grouping is this
7/13/2006 12:00:00 AM 1
7/21/2006 12:00:00 AM 11
8/7/2006 12:00:00 AM 3
8/8/2006 12:00:00 AM 2
I know if I put (Month(Fields!CreatedDate.Value)
I get this result
7 1
7 11
8 3
8 2
What I really want is
July 12
August 5
how do I do this please
thanks
Hi there!I dont know if this will resolve your issue but maybe if you try the following:
Format((Month(Fields!CreatedDate.Value)), "MMM")
If this works like I think it might then it should return:
Jul 12
Aug 5......etc
I cant test right now so I cant be sure...!?|||
Try
Monthname(Datepart("m", Fields!CreatedDate.Value))
This report function will return the "January, Feburary, March and etc...
Ham
|||
ok thanks for the replies
unfortunately not quite complete
Ham what I get then is
July 1
July 11
August 1
August 3
etc
so I need
total for July
total for August
etc
cheers
|||thought I should put the sql
SELECT TicketNumber, CreatedDate
FROM Ticket
ORDER BY CreatedDate
grouped on created date - associated to parent group created date
count on ticketnumber
cheers
Dianne
|||jewel,
Try
Placing the expression Monthname(Datepart("m", Fields!CreatedDate.Value)) into your group expression (edit group), this will cause the group to only consider the month when summating the values.
Ham
|||
thanks Ham
I had to put this in the group expression and the field expression to get it to work? I presume this is ok?
But yes it does work now thanks heaps for your help
jewel
Grouping Issue
Output is coming this way
customer ID Product name
111 Key board
111 Mouse
111 Mother Board
Output is suppose to come this way
customer ID Product name
111 Key board
Mouse
Mother Board
Can anyone help me to figure out why the value of field is repeating in row even though i have put that field in Group Section???
Thank in AdvacneIs your output
Preview View
<GH>111
<D>111 Key board
<D>111 Mouse
<D>111 Mother Board
Design View
Group Header: Group #1 name
Details: <Customer_id><Product_name>
<GH> = Group Header <D> = Details
If so you left the <customer_ID> field in the Details section
Let me know if Im off.
Thanks
Monday, March 26, 2012
grouping data
I had a table with these columns.
Table(Id int,Name varchar,Value Varchar).
I have to group them by ID and each Name becomes column name of the new table
ex:-
Id Name Value
-------
1 x a1
2 x a2
3 x a3
1 y b1
2 y b2
3 y b3
1 z c1
2 z c2
3 z c3
I need it in this way
x y z
----
a1 b1 c1
a2 b2 c2
a3 b3 c3
(no of columns in the new table can't be pre determined)
and which one would be better option to do this
in VB.Net code or in a Storedprocedure?You can do this in a stored procedure using CASE statements. You can find a fine explanation of the method by searching for "Cross-Tab Reports" in Books online.|||Normally I'd recommend that you do this on the client, but it can (usually) be done on the server. Just to prove that, I wrote a snippet of code that seems to work, although it doesn't deal well with ill-behaved data. FWIW, my code is:CREATE TABLE tPivot (
id INT NOT NULL
, name VARCHAR(20) NOT NULL
, value VARCHAR(20) NOT NULL
)
INSERT tPivot (id, name, value) VALUES (1, 'x', 'A1')
INSERT tPivot (id, name, value) VALUES (2, 'x', 'A2')
INSERT tPivot (id, name, value) VALUES (3, 'x', 'A3')
INSERT tPivot (id, name, value) VALUES (1, 'y', 'B1')
INSERT tPivot (id, name, value) VALUES (2, 'y', 'B2')
INSERT tPivot (id, name, value) VALUES (3, 'y', 'B3')
INSERT tPivot (id, name, value) VALUES (1, 'z', 'C1')
INSERT tPivot (id, name, value) VALUES (2, 'z', 'C2')
INSERT tPivot (id, name, value) VALUES (3, 'z', 'C3')
DECLARE @.cmd NVARCHAR(4000)
DECLARE @.cName SYSNAME
SELECT @.cmd = 'SELECT DISTINCT id'
DECLARE zName CURSOR FOR SELECT DISTINCT
name
FROM tPivot
OPEN zName
FETCH zName INTO @.cName
WHILE 0 = @.@.fetch_status
BEGIN
SELECT @.cmd = @.cmd +
', (SELECT Min(value) FROM tPivot AS b'
+ ' WHERE b.id = a.id'
+ ' AND b.name = ''' + @.cName
+ ''') AS [' + @.cName + ']'
FETCH zName INTO @.cName
END
CLOSE zName
DEALLOCATE zName
SELECT @.cmd = @.cmd + ' FROM tPivot AS a'
SELECT @.cmd
EXECUTE (@.cmd)Note that even though this CAN be done on the server, it would be better handled on the client side in most cases.
-PatP|||Hey Pat, you just love cursors, don't you?
declare @.tbl table (
ID int not null,
Name char(1) not null,
Value char(2) not null)
insert @.tbl values(1, 'x', 'a1')
insert @.tbl values(2, 'x', 'a2')
insert @.tbl values(3, 'x', 'a3')
insert @.tbl values(1, 'y', 'b1')
insert @.tbl values(2, 'y', 'b2')
insert @.tbl values(3, 'y', 'b3')
insert @.tbl values(1, 'z', 'c1')
insert @.tbl values(2, 'z', 'c2')
insert @.tbl values(3, 'z', 'c3')
select [x], [y], [z] from (
select distinct ID from @.tbl) t1
left outer join (
select ID, [x] = Value from @.tbl where Name = 'x') t2
on t1.ID = t2.ID
left outer join (
select ID, [y] = Value from @.tbl where Name = 'y') t3
on t1.ID = t3.ID
left outer join (
select ID, [z] = Value from @.tbl where Name = 'z') t4
on t1.ID = t4.ID|||Originally posted by rdjabarov
Hey Pat, you just love cursors, don't you? Not hardly, hate 'em with a passion. Unfortunately when I read the specs, I couldn't think of another way to deal with unknown column names.
-PatP|||Who loves left-joins and subqueries?
declare @.tbl table (
ID int not null,
Name char(1) not null,
Value char(2) not null)
insert @.tbl values(1, 'x', 'a1')
insert @.tbl values(2, 'x', 'a2')
insert @.tbl values(3, 'x', 'a3')
insert @.tbl values(1, 'y', 'b1')
insert @.tbl values(2, 'y', 'b2')
insert @.tbl values(3, 'y', 'b3')
insert @.tbl values(1, 'z', 'c1')
insert @.tbl values(2, 'z', 'c2')
insert @.tbl values(3, 'z', 'c3')
select max(case when Name = 'x' then Value end) as x,
max(case when Name = 'y' then Value end) as y,
max(case when Name = 'z' then Value end) as z
from @.tbl
group by ID|||Originally posted by theguru
(no of columns in the new table can't be pre determined)
Was this part of the spec optional ?
-PatP|||Originally posted by Pat Phelan
Not hardly, hate 'em with a passion. Unfortunately when I read the specs, I couldn't think of another way to deal with unknown column names.
-PatP
I saw the spec and stayed away...
Blenderized data anyone?
Salt or No Salt?|||Originally posted by Brett Kaiser
Salt or No Salt? Stayed away ? Don't you mean you're wasting away... oops, wait a sec, you're headed there anyway!
-PatP|||Not until 5:00 Pat...
Hey, look at that...it's 5:00!
See ya....|||"(no of columns in the new table can't be pre determined)"
Crap. Always read the fine print...
theguru, I've seen posts for fully dynamic SQL code that will do on-demand cross tabs, though I haven't tested them. rdjabarov, didn't you have one? It is definitely advanced SQL programming, so if one of these other gentlemen cannot refer you to some prewritten code, I suggest you try to accomplish this in VB.Net, or wait for SQL Server Yukon to be released.|||Whenever I see something like "the number of columns cannot be pre..." I just can't believe there is a developer that can actually buy it! You mean to say, that the number of columns can be 432347656? Or even more realistic, like 2972? Isn't it indicative of both poor app and poor database design? But I'd just stress the first one, - who in their sober mind would design an application that would produce such output?|||Originally posted by blindman
It is definitely advanced SQL programming, so if one of these other gentlemen cannot refer you to some prewritten code, I suggest you try to accomplish this in VB.Net, or wait for SQL Server Yukon to be released. At least I think that is what my code sample does. That is exactly why I had to resort to a cursor to build dynamic code, even though both of those go against my better judgement!
-PatP|||Whoa! Please go get your cig!
People ask this question because
A) They are trying to format the data for reporting
and
2) They were weaned on MS Access and its wonderfully convenient and fully dynamic cross-tab functionality.
Problem is, theguru, that when you don't know the number of columns or the names of the columns, most reporting applications (such as Crystal or even MS Access' reports) will choke on the output.
Perhaps your best bet would be to load the data into a pivot table in flat-file format, and then slice-and-dice however you want. How pretty does the output need to be?
Pat Phelan, I like your idea, though I haven't tried it out. I'd call it semi-dynamic, since you are working with a defined table format. A fully-dynamic method applicable to any dataset is the Holy Grail of cross-tab reporting.|||Originally posted by rdjabarov
Isn't it indicative of both poor app and poor database design? I'm not prepared to argue that point. We'd both be "preaching to the choir" on this one!
Originally posted by rdjabarov
But I'd just stress the first one, - who in their sober mind would design an application that would produce such output? What makes you think that the designer was in their sober mind ? ;)
-PatP|||Hey, at least my problems can be answered with a cig! You guys are sitting on a poor design and kicking this horse with "non-determined" number of legs wondering if it's gonna ever run again (tsk, it's been dead for a while...)|||See what happens when we both start typing really fast?|||Originally posted by rdjabarov
See what happens when we both start typing really fast? Yeah, but it's fun to watch!
-PatP|||Thank you all for u r replies.
Well "the number of columns cannot be pre..." doesn't mean that no of columns may exceed a 2 digit number atmost 20 columns.
ok I will try this out in stored procedure with my actual data.
thanks once again..keep sending u r suggestions...
Originally posted by rdjabarov
Whenever I see something like "the number of columns cannot be pre..." I just can't believe there is a developer that can actually buy it! You mean to say, that the number of columns can be 432347656? Or even more realistic, like 2972? Isn't it indicative of both poor app and poor database design? But I'd just stress the first one, - who in their sober mind would design an application that would produce such output?|||sorry, I think I might be missing some thing here.
This will work when I am sure of occurance x,y,z names exactly in the table
but it is not case here x,y,z may be reffered with some other names like A,B,C or O,P,Q which can't be presumed.
Originally posted by blindman
Who loves left-joins and subqueries?
declare @.tbl table (
ID int not null,
Name char(1) not null,
Value char(2) not null)
insert @.tbl values(1, 'x', 'a1')
insert @.tbl values(2, 'x', 'a2')
insert @.tbl values(3, 'x', 'a3')
insert @.tbl values(1, 'y', 'b1')
insert @.tbl values(2, 'y', 'b2')
insert @.tbl values(3, 'y', 'b3')
insert @.tbl values(1, 'z', 'c1')
insert @.tbl values(2, 'z', 'c2')
insert @.tbl values(3, 'z', 'c3')
select max(case when Name = 'x' then Value end) as x,
max(case when Name = 'y' then Value end) as y,
max(case when Name = 'z' then Value end) as z
from @.tbl
group by ID|||Great...I go out and slam some 'ritas...get called back in...yeah there;s a concept..to fix a prod problem and everyone is going nuts...
The point is mute...
It's still bender data...
No?|||Just curious at this point, but have you tried my code with your data?
-PatP|||yup,
thank u.but I am waiting for some more options.
any way I will use this one for the time being.
thank u.
Originally posted by Pat Phelan
Just curious at this point, but have you tried my code with your data?
-PatP|||Just a note...
1. I'm exhausted...
2. What's the point of your result set? It makes no sense.
Is this homework?|||This is what i need to show to my client.
I can't change the DB design at this point.I have to do this at any cost, performance is an exception for this.
Originally posted by Brett Kaiser
Just a note...
1. I'm exhausted...
2. What's the point of your result set? It makes no sense.
Is this homework?
Friday, March 23, 2012
Grouping by $10 Dollar Increments in SQL
dollar increments. I can get it to work by hard coding each group and union
each group, but it is slow. Wondering if anyone knows how to do this with
just one group by
This is what I got so far:
Select Sum(SubTotal), Count(OrderNumber)
From Sales
Group By OrderNumber
Having Sum(SubTotal) <= 10
Union
Select Sum(SubTotal), Count(OrderNumber)
From Sales
Group By OrderNumber
Having 10 < Sum(SubTotal) and Sum(SubTotal) <= 20
Union
Select Sum(SubTotal), Count(OrderNumber)
From Sales
Group By OrderNumber
Having 20 < Sum(SubTotal) and Sum(SubTotal) <= 30
....
Union
Select Sum(SubTotal), Count(OrderNumber)
From Sales
Group By OrderNumber
Having 500 < Sum(SubTotal)
Thanks In Advance,
Tom WingertOn Fri, 9 Sep 2005 13:12:02 -0700, Tom Wingert wrote:
>Been trying to find a way to do group orders by their dollar value in 10
>dollar increments. I can get it to work by hard coding each group and union
>each group, but it is slow. Wondering if anyone knows how to do this with
>just one group by
Hi Tom,
Call me paranoid, but this smells like a homework assignment...
Hint 1: You can use expressions in the GROUP BY clause.
Hint 2: In integer division, the remainder is lost.
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)|||1) Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, data types, etc. in
your schema are. Sample data is also a good idea, along with clear
specifications. It is very hard to debug code when you do not let us
see it.
2) Quit thinking like a procedural programmer and think is sets and
tables. Inparticular, auxiliaryt tables like this:
CREATE TABLE ReportRangeAmts
(range_name CHAR(15) NOT NULL,
start_amt DECIMAL (8,2) NOT NULL,
end_amt DECIMAL (8,2) NOT NULL,
CHECK (start_amt < end_amt)),
PRIMARY KEY(start_amt, end_amt)),
INSERT INTO ReportRangeAmts VALUES ('< $10', 0.00, 9.99);
INSERT INTO ReportRangeAmts VALUES ('$10 - $19.99', 10.00, 19.99);
etc.
Since you did not bother to post any DDL to help us, I will make a
guess that you do not have a subtotal column, since that would be
computed, and thus not an attribute of a base table. But you should
have the total amount for each sale.
SELECT R.range_name, SUM(sales_amt), COUNT(order_nbr)
FROM Sales AS S, ReportRangeAmts AS R
WHERE S.sales_amt BETWEEN R.start_amt AND R.end_amt
GROUP BY range_name;|||LOL. Yeah, it's "homework", but I get paid for my homework :).
Not sure what you are trying to get at. Can you give me an example?
Thanks in Advance,
Tom Wingert, Software Engineer
Motorcycle-Superstore.com
"Hugo Kornelis" wrote:
> On Fri, 9 Sep 2005 13:12:02 -0700, Tom Wingert wrote:
>
> Hi Tom,
> Call me paranoid, but this smells like a homework assignment...
> Hint 1: You can use expressions in the GROUP BY clause.
> Hint 2: In integer division, the remainder is lost.
> Best, Hugo
> --
> (Remove _NO_ and _SPAM_ to get my e-mail address)
>|||one more comment:
always use union all unless you really really need union|||one way...
select
convert(int,total)/10 as TenDollarRange,
Count(*) as OrdersInThisRange
from (
select OrderNumber, Sum(SubTotal) as Total
from @.Sales
Group By OrderNumber
) OrderGroup
group by convert(int,total)/10
Tom Wingert wrote:
> Been trying to find a way to do group orders by their dollar value in 10
> dollar increments. I can get it to work by hard coding each group and unio
n
> each group, but it is slow. Wondering if anyone knows how to do this with
> just one group by
> This is what I got so far:
> Select Sum(SubTotal), Count(OrderNumber)
> From Sales
> Group By OrderNumber
> Having Sum(SubTotal) <= 10
> Union
> Select Sum(SubTotal), Count(OrderNumber)
> From Sales
> Group By OrderNumber
> Having 10 < Sum(SubTotal) and Sum(SubTotal) <= 20
> Union
> Select Sum(SubTotal), Count(OrderNumber)
> From Sales
> Group By OrderNumber
> Having 20 < Sum(SubTotal) and Sum(SubTotal) <= 30
> ....
> Union
> Select Sum(SubTotal), Count(OrderNumber)
> From Sales
> Group By OrderNumber
> Having 500 < Sum(SubTotal)
> Thanks In Advance,
> Tom Wingert
>|||OK. My Bad. Newbie mistake. Should have posted the rest of the info.
Thanks for the info,
Tom Wingert
PS:
Like your book. Been helpful plenty of times.
"--CELKO--" wrote:
> 1) Please post DDL, so that people do not have to guess what the keys,
> constraints, Declarative Referential Integrity, data types, etc. in
> your schema are. Sample data is also a good idea, along with clear
> specifications. It is very hard to debug code when you do not let us
> see it.
> 2) Quit thinking like a procedural programmer and think is sets and
> tables. Inparticular, auxiliaryt tables like this:
> CREATE TABLE ReportRangeAmts
> (range_name CHAR(15) NOT NULL,
> start_amt DECIMAL (8,2) NOT NULL,
> end_amt DECIMAL (8,2) NOT NULL,
> CHECK (start_amt < end_amt)),
> PRIMARY KEY(start_amt, end_amt)),
> INSERT INTO ReportRangeAmts VALUES ('< $10', 0.00, 9.99);
> INSERT INTO ReportRangeAmts VALUES ('$10 - $19.99', 10.00, 19.99);
> etc.
> Since you did not bother to post any DDL to help us, I will make a
> guess that you do not have a subtotal column, since that would be
> computed, and thus not an attribute of a base table. But you should
> have the total amount for each sale.
> SELECT R.range_name, SUM(sales_amt), COUNT(order_nbr)
> FROM Sales AS S, ReportRangeAmts AS R
> WHERE S.sales_amt BETWEEN R.start_amt AND R.end_amt
> GROUP BY range_name;
>|||On Fri, 9 Sep 2005 13:40:01 -0700, Tom Wingert wrote:
>LOL. Yeah, it's "homework", but I get paid for my homework :).
Hi Tom,
Oh. Sorry for being paranoid then.
>Not sure what you are trying to get at. Can you give me an example?
I could, but Trey Walpole beat me to it. Check his post in this thread
to see what I had in mind.
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)|||Thank you, but which book? I have several and there is now a third
editionof SQL FOR SMARTIES. Oh, my mortgage also says "thank you" ...sql
Grouping and Average Question
My problem is as follows:
I would like to take the average value of count column grouping by drive
letter and date.
Sample table
DriveLetter Date Count
K: 2005-07-05 06:00:00:000 33.555
K: 2005-07-05 06:30:00:000 35.555
K: 2005-07-05 07:00:00:000 48.555
K: 2005-07-05 07:30:00:000 52.555
h: 2005-07-05 06:00:00:000 33.555
h: 2005-07-05 06:30:00:000 35.555
h: 2005-07-05 07:00:00:000 48.555
h: 2005-07-05 07:30:00:000 52.555
i: 2005-07-05 06:00:00:000 33.555
i: 2005-07-05 06:30:00:000 35.555
i: 2005-07-05 07:00:00:000 48.555
i: 2005-07-05 07:30:00:000 52.555
Thanks
MikeDragon9994 wrote:
> I would like to take the average value of count column grouping by
> drive letter and date.
> Sample table
> DriveLetter Date Count
> K: 2005-07-05 06:00:00:000 33.555
> K: 2005-07-05 06:30:00:000 35.555
> K: 2005-07-05 07:00:00:000 48.555
> K: 2005-07-05 07:30:00:000 52.555
select DriveLetter, CAST(CONVERT(char(8), [date], 112) AS DATETIME) as
[Date], avg([Count]) as AvgCount
from SampleTable
group by DriveLetter, CAST(CONVERT(char(8), [date], 112) AS DATETIME)
HTH,
Stijn Verrept.|||>> .. average value of count column grouping by drive
letter and date. <<
DATE and COUNT are reserved words in SQL.
SELECT drive_letter, foobar_date, AVG(foobar_count)
FROM Foobar
GROUP BY drive_letter, foobar_date;
I have no Stijn wants to CAST() temporal data into strings. I also
have no idea why he is also using CONVERT() unless he likes proprietary
code.|||--CELKO-- wrote:
> DATE and COUNT are reserved words in SQL.
Very true that date and count reserved words are, it's indeed better
not to use them, that's also why I put them between brackets.
> I have no Stijn wants to CAST() temporal data into strings. I also
> have no idea why he is also using CONVERT() unless he likes
> proprietary code.
Well if you check the original message you'll see that his sample date
column also contains hours and he wanted to group by date.
It maybe is a little confusing since the OP also uses Date as the
column name so it's not sure if he wants to sort by date or by the
column date (which also contains the time). HOWEVER, if you look at
the sample data you'll see that every hour only occurs once per drive
letter so it would be useless to get an average value, that why we can
be pretty sure that the OP means date (in date without time).
That's why your query is pretty much useless. If you run that query on
the data he has supplied you'll get exactly that same data back.
The CAST(CONVERT(char(8), [date], 112) AS DATETIME) is used to get rid
of the time and only look at the date.
Hope this clears things up,
Stijn Verrept.|||Thanks for your help. It is what I needed to do.
Sorry for the confussion on the Date Column.
Mike
"Stijn Verrept" wrote:
> --CELKO-- wrote:
>
> Very true that date and count reserved words are, it's indeed better
> not to use them, that's also why I put them between brackets.
>
>
> Well if you check the original message you'll see that his sample date
> column also contains hours and he wanted to group by date.
> It maybe is a little confusing since the OP also uses Date as the
> column name so it's not sure if he wants to sort by date or by the
> column date (which also contains the time). HOWEVER, if you look at
> the sample data you'll see that every hour only occurs once per drive
> letter so it would be useless to get an average value, that why we can
> be pretty sure that the OP means date (in date without time).
> That's why your query is pretty much useless. If you run that query on
> the data he has supplied you'll get exactly that same data back.
> The CAST(CONVERT(char(8), [date], 112) AS DATETIME) is used to get rid
> of the time and only look at the date.
> --
> Hope this clears things up,
> Stijn Verrept.
>
Wednesday, March 21, 2012
Group Totals
Reporting Services report? Here is a simplified version of what I am
struggling with:
Let's say I have an SQL statement that returns the flowing set
RoomID Max Occupants Name
1 3 Bill Clinton
1 3 Hilary Clinton
1 3 Chelsea Clinton
2 4 George W Bush
2 4 Barbara Bush
Total 7 5
I have a report with a group that groups rooms, shows the max occupants
on the header of each group and lists the current occupants underneath
each group header. On the bottom I want to show the total number of
occupants the rooms could have and the number of spots currently
occupied, 7 and 5 in my example. What do I need to do to show 7 on the
bottom of the report? It looks simple and I have done this many times
on different report writers but for some reason I have trouble figuring
out what to do on RS report.
Any help is appreciated.
Tim.Group by room ID and then add the First(Fields!occupants.value) from each
group.
"Tim." wrote:
> Can some one help me with totaling a group value on SQL Server
> Reporting Services report? Here is a simplified version of what I am
> struggling with:
> Let's say I have an SQL statement that returns the flowing set
> RoomID Max Occupants Name
> 1 3 Bill Clinton
> 1 3 Hilary Clinton
> 1 3 Chelsea Clinton
> 2 4 George W Bush
> 2 4 Barbara Bush
> Total 7 5
> I have a report with a group that groups rooms, shows the max occupants
> on the header of each group and lists the current occupants underneath
> each group header. On the bottom I want to show the total number of
> occupants the rooms could have and the number of spots currently
> occupied, 7 and 5 in my example. What do I need to do to show 7 on the
> bottom of the report? It looks simple and I have done this many times
> on different report writers but for some reason I have trouble figuring
> out what to do on RS report.
> Any help is appreciated.
> Tim.
>sql
Group Total..
is calculated from a formula by passing the row's record id and commission
rate. (the formula is inside a custom dll). The values are correctly
computed. Now, the footer should display the total of all the rows in the
group.
for instance:
"Unit" "BrandName" "XYZ Total" "Comments"
Sodas
Pepsi $361,000 gfyeefyefffee
Coca Cola $475,250 djfdfjdfddddd
RCola $28,757 re8reruejreerr
fdfsfnfsfssf
_________________________________________
Total: $ 865,007
Each of the "XYZ Total" in the above example, uses an expression as = FindTotal(recID!value, comm_rate!value)
In this case, how do I get the total in the footer? How to recursively add
the FindTotal expression when it contains the row's unique record id?
Thanks
P.S. The above data is a sample data. The actual report contains 3 different
levels of grouping - Grouping1: Unit, Grouping2: Brand, Grouping 3:
Transaction TitleI recently came across a new software, that I think you might want to look into.
www.simx.com/simx/home_report%20manager.htm
Works with SQL Server, and I was able to do reporting much like what you are describing.
"newmem" <"" wrote:
> I'm working on a Financial Report which contains a column "XYZ" , its value
> is calculated from a formula by passing the row's record id and commission
> rate. (the formula is inside a custom dll). The values are correctly
> computed. Now, the footer should display the total of all the rows in the
> group.
> for instance:
> "Unit" "BrandName" "XYZ Total" "Comments"
> Sodas
> Pepsi $361,000 gfyeefyefffee
> Coca Cola $475,250 djfdfjdfddddd
> RCola $28,757 re8reruejreerr
> fdfsfnfsfssf
> _________________________________________
> Total: $ 865,007
> Each of the "XYZ Total" in the above example, uses an expression as => FindTotal(recID!value, comm_rate!value)
> In this case, how do I get the total in the footer? How to recursively add
> the FindTotal expression when it contains the row's unique record id?
> Thanks
> P.S. The above data is a sample data. The actual report contains 3 different
> levels of grouping - Grouping1: Unit, Grouping2: Brand, Grouping 3:
> Transaction Title
>
>|||While I appreciate your eagerness to help, I think that most people would
prefer that you refrain from advertising other products in a forum dedicated
to SQL Server Reporting Services. If you start a SIMX newsgroup, I promise
not to post there. :)
That being said, you should be able to define a custom field that does the
calculation and then referce the custom field in a sum in the group footer.
Presumably, you only need to add values from the inner group as the outer
group is just summary. If you want it to do parent / child hierarcy
aggregates, you need to use the recursive keyword.
--
Brian Welcker
Group Program Manager
SQL Server Reporting Services
This posting is provided "AS IS" with no warranties, and confers no rights.
"Brian" <Brian@.discussions.microsoft.com> wrote in message
news:C6115A01-132A-429D-9AFC-99E46F75A2FC@.microsoft.com...
>I recently came across a new software, that I think you might want to look
>into.
> www.simx.com/simx/home_report%20manager.htm
> Works with SQL Server, and I was able to do reporting much like what you
> are describing.
> "newmem" <"" wrote:
>> I'm working on a Financial Report which contains a column "XYZ" , its
>> value
>> is calculated from a formula by passing the row's record id and
>> commission
>> rate. (the formula is inside a custom dll). The values are correctly
>> computed. Now, the footer should display the total of all the rows in the
>> group.
>> for instance:
>> "Unit" "BrandName" "XYZ Total" "Comments"
>> Sodas
>> Pepsi $361,000 gfyeefyefffee
>> Coca Cola $475,250 djfdfjdfddddd
>> RCola $28,757 re8reruejreerr
>> fdfsfnfsfssf
>> _________________________________________
>> Total: $ 865,007
>> Each of the "XYZ Total" in the above example, uses an expression as =>> FindTotal(recID!value, comm_rate!value)
>> In this case, how do I get the total in the footer? How to recursively
>> add
>> the FindTotal expression when it contains the row's unique record id?
>> Thanks
>> P.S. The above data is a sample data. The actual report contains 3
>> different
>> levels of grouping - Grouping1: Unit, Grouping2: Brand, Grouping 3:
>> Transaction Title
>>|||Thanks Brian.
Can you give me an example of using a custom field and using the Recusrive
keyword? If there is a sample in BOL, then pls provide any reference/links
(I wasn't able to locate any help on this topic)
appreciate it.
"Brian Welcker [MSFT]" <bwelcker@.online.microsoft.com> wrote in message
news:OxzDQD1WEHA.3972@.TK2MSFTNGP12.phx.gbl...
> While I appreciate your eagerness to help, I think that most people would
> prefer that you refrain from advertising other products in a forum
dedicated
> to SQL Server Reporting Services. If you start a SIMX newsgroup, I promise
> not to post there. :)
> That being said, you should be able to define a custom field that does the
> calculation and then referce the custom field in a sum in the group
footer.
> Presumably, you only need to add values from the inner group as the outer
> group is just summary. If you want it to do parent / child hierarcy
> aggregates, you need to use the recursive keyword.
> --
> Brian Welcker
> Group Program Manager
> SQL Server Reporting Services
> This posting is provided "AS IS" with no warranties, and confers no
rights.
> "Brian" <Brian@.discussions.microsoft.com> wrote in message
> news:C6115A01-132A-429D-9AFC-99E46F75A2FC@.microsoft.com...
> >I recently came across a new software, that I think you might want to
look
> >into.
> >
> > www.simx.com/simx/home_report%20manager.htm
> >
> > Works with SQL Server, and I was able to do reporting much like what you
> > are describing.
> >
> > "newmem" <"" wrote:
> >
> >> I'm working on a Financial Report which contains a column "XYZ" , its
> >> value
> >> is calculated from a formula by passing the row's record id and
> >> commission
> >> rate. (the formula is inside a custom dll). The values are correctly
> >> computed. Now, the footer should display the total of all the rows in
the
> >> group.
> >> for instance:
> >>
> >> "Unit" "BrandName" "XYZ Total" "Comments"
> >> Sodas
> >> Pepsi $361,000 gfyeefyefffee
> >> Coca Cola $475,250 djfdfjdfddddd
> >> RCola $28,757 re8reruejreerr
> >>
> >> fdfsfnfsfssf
> >> _________________________________________
> >> Total: $ 865,007
> >>
> >> Each of the "XYZ Total" in the above example, uses an expression as => >> FindTotal(recID!value, comm_rate!value)
> >> In this case, how do I get the total in the footer? How to recursively
> >> add
> >> the FindTotal expression when it contains the row's unique record id?
> >>
> >> Thanks
> >>
> >> P.S. The above data is a sample data. The actual report contains 3
> >> different
> >> levels of grouping - Grouping1: Unit, Grouping2: Brand, Grouping 3:
> >> Transaction Title
> >>
> >>
> >>
>
Group Sum over multiple pages with IIF
than 1 page. The sum looks like this:
Sum(IIf(Fields!MySwitch.Value = 'A', Fields!MyValue.Value,0))
Im doing the sum in the table1_group1 footer and it works fine if all the
data in the group remains on the same page, but once it is expanded to 2 or
more pages I get #Error. Any suggestions?
Michael CIf you can bring from a query then it is really good instead of summing two
pages rows.
Amarnath
"Michael C" wrote:
> I am using an IIF statement to sum a group of detail records that spans more
> than 1 page. The sum looks like this:
> Sum(IIf(Fields!MySwitch.Value = 'A', Fields!MyValue.Value,0))
> Im doing the sum in the table1_group1 footer and it works fine if all the
> data in the group remains on the same page, but once it is expanded to 2 or
> more pages I get #Error. Any suggestions?
> Michael C
Group sum outside group?
=SUM(Fields!BF_Child.Value, "table1_Details_Group"). When I try to run it I
get the following error.
[rsInvalidAggregateScope] The Value expression for the textbox â'textbox7â'
has a scope parameter that is not valid for an aggregate function. The scope
parameter must be set to a string constant that is equal to either the name
of a containing group, the name of a containing data region, or the name of a
data set.
Build complete -- 1 errors, 0 warnings
Why can't I sum group values?Eric,
Try this:
=RunningValue(Fields!BF_Child.Value,Sum,"table1_Details_Group")
Michael C
"eric" wrote:
> I'm trying to get a sum of values from a group with the following expression.
> =SUM(Fields!BF_Child.Value, "table1_Details_Group"). When I try to run it I
> get the following error.
> [rsInvalidAggregateScope] The Value expression for the textbox â'textbox7â'
> has a scope parameter that is not valid for an aggregate function. The scope
> parameter must be set to a string constant that is equal to either the name
> of a containing group, the name of a containing data region, or the name of a
> data set.
> Build complete -- 1 errors, 0 warnings
>
> Why can't I sum group values?|||Hi Michael,
Thank you for the response. Unfortunately I get the same error. I have 2
groups, one to group by parent and the second group to reduce duplicate
records. The Sum I would like to total the second group values, but only
the visible records in the group. Any other ideas? Thanks
Eric
"Michael C" wrote:
> Eric,
> Try this:
> =RunningValue(Fields!BF_Child.Value,Sum,"table1_Details_Group")
> Michael C
> "eric" wrote:
> > I'm trying to get a sum of values from a group with the following expression.
> > =SUM(Fields!BF_Child.Value, "table1_Details_Group"). When I try to run it I
> > get the following error.
> >
> > [rsInvalidAggregateScope] The Value expression for the textbox â'textbox7â'
> > has a scope parameter that is not valid for an aggregate function. The scope
> > parameter must be set to a string constant that is equal to either the name
> > of a containing group, the name of a containing data region, or the name of a
> > data set.
> > Build complete -- 1 errors, 0 warnings
> >
> >
> > Why can't I sum group values?|||Hi Eric,
Did you find the solution for this?
I have the same problem and I would like to know if there´s anyway of doing
it.
Thanks,
Mónica
"eric" <eric@.discussions.microsoft.com> escribió en el mensaje
news:2DD05D4D-4B91-4088-B1A9-EA5BF35592B4@.microsoft.com...
> Hi Michael,
> Thank you for the response. Unfortunately I get the same error. I have 2
> groups, one to group by parent and the second group to reduce duplicate
> records. The Sum I would like to total the second group values, but only
> the visible records in the group. Any other ideas? Thanks
> Eric
> "Michael C" wrote:
>> Eric,
>> Try this:
>> =RunningValue(Fields!BF_Child.Value,Sum,"table1_Details_Group")
>> Michael C
>> "eric" wrote:
>> > I'm trying to get a sum of values from a group with the following
>> > expression.
>> > =SUM(Fields!BF_Child.Value, "table1_Details_Group"). When I try to
>> > run it I
>> > get the following error.
>> >
>> > [rsInvalidAggregateScope] The Value expression for the textbox
>> > 'textbox7'
>> > has a scope parameter that is not valid for an aggregate function. The
>> > scope
>> > parameter must be set to a string constant that is equal to either the
>> > name
>> > of a containing group, the name of a containing data region, or the
>> > name of a
>> > data set.
>> > Build complete -- 1 errors, 0 warnings
>> >
>> >
>> > Why can't I sum group values?sql
group sum
following expression:
=iif(Fields!endDate.Value = Nothing, 1, iif(Fields!endDate.Value <
dateadd("d", 14, Parameters!startDate.Value), 0, 1))
I need to have a field in the group footer sum this column. How do I do
this? If I do =Sum(ReportItems!textbox66.value) in a group footer field, I
get the following errors:
Aggregate functions can be used only on report items contained in page
headers and footers.
AND
Report Item expressions can only refer to other report items within the same
grouping scope or a containing grouping scope.Just use this expression for the group footer:
=Sum(iif(IsNothing(Fields!endDate.Value), 1, iif(Fields!endDate.Value <
dateadd("d", 14, Parameters!startDate.Value), 0, 1)))
-- Robert
This posting is provided "AS IS" with no warranties, and confers no rights.
"SharinDenver" <SharinDenver@.discussions.microsoft.com> wrote in message
news:CC1EE200-443F-45B8-95CA-08FEFF2941C6@.microsoft.com...
>I have a table with groups. I have a detail field (textbox66) that has the
> following expression:
> =iif(Fields!endDate.Value = Nothing, 1, iif(Fields!endDate.Value <
> dateadd("d", 14, Parameters!startDate.Value), 0, 1))
> I need to have a field in the group footer sum this column. How do I do
> this? If I do =Sum(ReportItems!textbox66.value) in a group footer field,
> I
> get the following errors:
> Aggregate functions can be used only on report items contained in page
> headers and footers.
> AND
> Report Item expressions can only refer to other report items within the
> same
> grouping scope or a containing grouping scope.
group subtotal and grand total
anybody know how to do it?
Make sure you are in the Group Footer
FOR Subtotal:
="Display Subtotal Text" & "" & Fields!txtColumn.Value
=Sum(Fields!Balance.Value, "
=Sum(Fields!Balance.Value, "grpNameofGroup")
")
FOR Grandtotal:
=Sum(Fields!Balance.Value)
|||why the subtotal have many equal sigh? shoulnt be just one?I put sum(Field!Balance.value,"Grpname") in group folder, it return same subtotall one each page in same group category, can the subtotal just sum up the total on textfield withinn the same group in a page?
|||
From the question that you have posted and the example that you have given I think it can be achieved by having 2 groups in 2 different group header. In the detail section put the value of the 2 nd group that you want to display. and at the 2 group footer just put the expression Sum(Field!Net_Weight.Value). For every group you have to set 'Page Break at End'.
I think it will work.
Monday, March 19, 2012
Group newbie: question on parsing a field value
dynamic tree on a web page. For example:
"1.1"
"1.2"
"1.2.1"
"1.2.2"
--
--
'1.2.10"
--
etc..
What I need to do is compare this field "value" to another value in a query.
(I'm using ASP and VBScript to create the statement). For example:
sql = "SELECT SomeField FROM MyTable WHERE TreeNode>=' " & MyNode & " ' "
The problem I run into is when the TreeNode value is say "1.2.10", and it's
being compared against "1.2.1" and "1.2.2". It should be greater than both
of these (in my implementation of this), but it actually falls between the
two, since it is a text comparison.
All I'm really interested in is the last value. But I can't use the RIGHT
function, because the last value may be one or more digits, and there could
be any number of levels (periods).
Is there an SQL function I could use that would strip away everything but
the text following the last period? I could then easily do the same in the
ASP script and compare integers. Something like:
sql = "SELECT SomeField FROM MyTable WHERE GetLastValueSQL(TreeNode) >= " &
GetLastValueASP(MyTreeNode)
This also needs to work in MS Access BTW :-)
Thanks in advance for any help!
Calan
AxMaster Guitar Software
www.jcsautomation.com
www.jcsautomation.com/music.asp
Music software and web design/hosting
"Reality exists only in the minds of the extremely deranged""Calan" <calan_svcREMOVE@.yaNOSPAMhoo.com> wrote in message news:<P3Jcc.643$FB1.182@.fe25.usenetserver.com>...
> I have a text field (called TreeNode) that contains node identifiers for a
> dynamic tree on a web page. For example:
> "1.1"
> "1.2"
> "1.2.1"
> "1.2.2"
> --
> --
> '1.2.10"
> --
> etc..
> What I need to do is compare this field "value" to another value in a query.
> (I'm using ASP and VBScript to create the statement). For example:
> sql = "SELECT SomeField FROM MyTable WHERE TreeNode>=' " & MyNode & " ' "
> The problem I run into is when the TreeNode value is say "1.2.10", and it's
> being compared against "1.2.1" and "1.2.2". It should be greater than both
> of these (in my implementation of this), but it actually falls between the
> two, since it is a text comparison.
> All I'm really interested in is the last value. But I can't use the RIGHT
> function, because the last value may be one or more digits, and there could
> be any number of levels (periods).
> Is there an SQL function I could use that would strip away everything but
> the text following the last period? I could then easily do the same in the
> ASP script and compare integers. Something like:
> sql = "SELECT SomeField FROM MyTable WHERE GetLastValueSQL(TreeNode) >= " &
> GetLastValueASP(MyTreeNode)
> This also needs to work in MS Access BTW :-)
> Thanks in advance for any help!
> Calan
> AxMaster Guitar Software
> www.jcsautomation.com
> www.jcsautomation.com/music.asp
> Music software and web design/hosting
> "Reality exists only in the minds of the extremely deranged"
This is one way:
declare @.node varchar(50)
set @.node = '1.2.1.10'
select substring(
@.node,
len(@.node) - charindex('.', reverse(@.node))+2,
charindex('.', reverse(@.node))
)
Or this may be easier to read:
select reverse(left(reverse(@.node), charindex('.', reverse(@.node))-1))
You could put this into a function (in SQL2000), but it would be
invoked once per row in queries, so using a stored procedure is
probably a better approach.
Simon|||> Is there an SQL function I could use that would strip away everything but
> the text following the last period? I could then easily do the same in the
> ASP script and compare integers. Something like:
> This also needs to work in MS Access BTW :-)
Hi,
I don't know anything about Access. You can use a table of numbers
trick to parse your "node" into individual nodes by using the period
as a delimiter. Erland has documented it in his site. In the code
below, I called my table of numbers TALLY which has one column ID with
values from 1,2,3,... to 8000.
declare @.node varchar (50)
set @.node='1.2.10'
SELECT
substring(phrase,s,(e-s-1)) as NODES
FROM
(
SELECT
id,
phrase,
charindex('.','.'+phrase+'.',id) as s,
charindex('.','.'+phrase+'.',id+1) as e
FROM tally,(select phrase=@.node) A
WHERE charindex('.','.'+phrase+'.',id) <
charindex('.','.'+phrase+'.',id+1)
) B
OUTPUTS:
NODES
----------------
1
2
10
Further modifying it to output the last NODE piece:
SELECT
substring(phrase,s,(e-s-1)) as NODES, identity(int,1,1) as i
INTO #T
FROM
(
SELECT
id,
phrase,
charindex('.','.'+phrase+'.',id) as s,
charindex('.','.'+phrase+'.',id+1) as e
FROM tally,(select phrase=@.node) A
WHERE charindex('.','.'+phrase+'.',id) <
charindex('.','.'+phrase+'.',id+1)
) B
SELECT NODES FROM #T WHERE i=(SELECT max(i) as i FROM #T)
OUTPUTS:
NODES
----------------
10
Group Name in Page Header
in the page header (I'm also inserting a page break at the end of each
group). Is this possible?Yes this is possible in your case. Assuming you have a textbox called
GroupName in the grouping header which shows the current value of the
grouping, you can just add another textbox in the page header which
references the value of the GroupName textbox:
=ReportItems!GroupName.Value
Note: only the ReportItems collection is accessible in the page
headers/footers, but not the Fields collection.
--
This posting is provided "AS IS" with no warranties, and confers no rights.
"J Chang" <julia.chang@.maritz.com> wrote in message
news:946c3e64.0410221341.3018ec18@.posting.google.com...
> When using Grouping, I need to display the value of the grouped field
> in the page header (I'm also inserting a page break at the end of each
> group). Is this possible?
Group Min Record
A small problem here, I have the below table and I need to group and display the record that has the minimum value in the table (this table is derived from a query that permutates some records to give me this result).
F1 F2 F3
QQQ C 2
QQQ B 1
QQQ A 3
expected result:
QQQ B 1
my result:
when I group by F1, First(F2) and MIN(F3):
QQQ C 1
when I group by F1, MIN(F2) and MIN(F3):
QQQ A 1
when I group by F1, F2 and MIN(F3):
QQQ C 2
QQQ B 1
QQQ A 3
Any help would be very much appreciated..
CyherusYou don't GROUP BY the aggregate for one thing. What you need is something like this in this particular example:
SELECT
t1.F1,
t1.F2,
t1.F3
FROM
table t1
INNER JOIN (
SELECT MIN(F3) AS F3 FROM table) t2 ON t1.F3 = t2.F3
This is assuming you're using SQL Server, which you probably aren't.
Group ID and ID Column
Hi,
I want to get the value from a row that contains the minimum value of a field AND for which I group by a 3rd field. For example:
The table tbl1 has:
X Quantity Location
1 20 slot 1
1 34 slot 1
3 17 slot 1
42 12 slot 5
5 65 slot 5
If I just want the quantities and location I can do:
Select Location, Min(Quantity) from tbl1 group by Location
However if I want X (or any other field in the table) I cannot do:
Select X, Location, Min(Quantity) from tbl1 group by Location
because X is not in the group by clause.
And putting X in the group by clause causes incorrect results.
Does anyone know the correct select statement?
jerry
It might help if you provided sample output from the query you are trying to write.|||In the case of a group by, any non grouped columns in the select must be contained in an aggregate function.
Think about your example, If you group by Location which value of x should you get for location = 'slot 1' 1 or 3.
so, use MIN(X) or MAX(X) in your select and it should work
|||Can you explain what you are trying to accomplish?
;with cte
as
(
select X, Quantity, Location, row_number() over(partition by Location order by Quantity) as rn
from dbo.t1
)
select *
from cte
where rn = 1;
AMB
|||If you use sql server 2000,
Code Snippet
Create Table #qty (
[X] Varchar(100) ,
[Quantity] Varchar(100) ,
[Location] Varchar(100)
);
Insert Into #qty Values('1','20','slot1');
Insert Into #qty Values('1','34','slot1');
Insert Into #qty Values('3','17','slot1');
Insert Into #qty Values('42','12','slot5');
Insert Into #qty Values('5','65','slot5');
Select * From #qty [Main]
Join (
Select
[Location]
,Min([Quantity])[Quantity]
From
#qty
Group By
[Location]
) as [Data] On [Data].[Quantity] = [Main].[Quantity]
And [Data].[Location] = [Main].[Location]
Sunday, February 26, 2012
Group By and Update
I need to do the following. Create a subtotal value from a table and then
populate another table with that value.
Table 1 consists of PO Orders.
I need to group the subtotals by Item Number.
Table 2 is our Item Master File. I would like to populate this table with
the subtotal derived above based on Item Number (the common field).
I have been successful running a group by Query on Table 1 to return the
results of Item Number with the subtotal value. However, I cannot figure out
the methodology to get that information into the second table.
Thanks.
AnnTry this:
UPDATE Table2
SET total = (SELECT SUM(x)
FROM Table1
WHERE Table1.item_num = Table2.item_num)
Are you sure you want to store the total? It's normally inefficient and
undesirable to store any derived calculations in the database. Have you
considered using a view instead? You have the option of an indexed view
to achieve much the same thing.
--
David Portas
SQL Server MVP
--|||I need to be able to access the data from Infopath. Therefore it must be in
a table. I believe is only for reference and is not consider a table. Am I
correct?
"David Portas" wrote:
> Try this:
> UPDATE Table2
> SET total => (SELECT SUM(x)
> FROM Table1
> WHERE Table1.item_num = Table2.item_num)
> Are you sure you want to store the total? It's normally inefficient and
> undesirable to store any derived calculations in the database. Have you
> considered using a view instead? You have the option of an indexed view
> to achieve much the same thing.
> --
> David Portas
> SQL Server MVP
> --
>|||You should be able to create a VIEW which will utilize the calculations and
have InfoPath pull data from the view.
Rick Sawtell
MCT, MCSD, MCDBA