Excel 2010 Pivot Table: Count based on 2 flags - pivot-table

*I'd like to ask that you please do not just say "add a column to your data that..."
*I want to know if the pivot table itself can do this.
My data comes from a sql query. The query is inserted into excel via the Data->From Other Sources path. So, going in to edit the query within excel, or open the query itself, editing, and repasting is not ideal, especially when the excel file needs to be distributed.
My raw data is set-up like this. I've put zeros in for blanks, but my flags are either 1 or blank (empty cell)
id costa costb names ... ... flagA flagB flagC
1 1000 2000 apple ... ... 1 1 1
2 2300 1200 brake ... ... 1 0 1
3 4600 3400 candy ... ... 0 1 1
4 6900 4300 drill ... ... 1 0 0
I want my pivot table to COUNT occurence of items that are flagb and flagc.
Usually with only one flag. Your calculated field will be a SUM with formula ='flagb'
How can I make the field bebased on two flags, not just one?
Again. I don't want to add a new column to my data as certain flags do not correlate easily, and I have to change the orignial SQL file, as well as the query inserted to excel to stay consistent. Also, I feel the pivot should be able to do this. Any solutions?

use a calculated field, and put the formula as = AND(flagB=1,flagC=1)
you can use multiple tests if you need to check for specific flags, just separate each test with a comma within the AND test (and more complex tests can be made with OR, or combinations of them both

Related

Need to add dynamic columns in headers in SSRS report

I need to add column headers in SSRS report but that are dynamic in nature.
For example, sometimes Query will return 5 different named columns with it's data and sometime will return 9 different named columns with it's data and so on.
So how to drag or refresh columns in Dataset and how to show in SSRS report dynamically.
I am totally confused seen many articles but not able to get solution.
How to implement this in SSRS report. I have the query, depending on parameters columns gets generated. Check below sample report preview
its showing date in different columns
In SSRS , the dataset must always return the same number of columns with the same names and datatypes, so you cannot of what you want directly.
You have two options.
Option 1
Normalise the data.
So instead of returning something like
SomeID ColumnA ColumnB ColumnC
1 10 20 30
2 15 25 35
3 100 200 300
You need to return
SomeID ColName Amount
1 'ColumnA' 10
1 'ColumnB' 20
1 'ColumnC' 30
2 'ColumnA' 15
2 'ColumnB' 25
2 'ColumnC' 35
3 'ColumnA' 10
3 'ColumnB' 200
3 'ColumnC' 300
Once you have your data in this format, you can simply use a matrix in your report. Set the rowgroup to SomeID, set the Column Group to ColName and the data value to Amount
This is by far the simplest solution.
Option 2
Deconstruct and rebuild the table in code
There are several drawbaks to this method but if you are interested, read my answer to this question asked a few days ago
SQL Server - SSRS - Display the content of a Table/View directly in the report (and not using table/matrix)

MS Access - Color cells in a query that has the same numbers in a specific column

In my database i have columns with information that are match togheter with a column named MsgNumber this column looks like this:
Where if it´s 1 1 the two rows have some coherent information. 2 and 3 3 3 and 4 4 4 4 and so forh all the rows are aligned that way. My goal is to get the query to order the cells by color as show in the next pictur:
I tried with condition formation but then i needed to do a seperate "report" i need to edit the rows directly in the query. (doing it manually is not an option). Can it be done? Thanks

Reporting Services - Calculating row totals/percentages in a table

I am trying to calculate totals for each row as well as a percentage of the overall total.
Right now I have a table like this:
Blah Col1 Col2 Col3
-----------------------------
ABC 1 1 1
DEF 2 2 3
-----------------------------
Total 3 3 4
And I want it to include totals/percentages like so:
Blah Col1 Col2 Col3 Total %
--------------------------------------------
ABC 1 1 1 3 30%
DEF 2 2 3 7 70%
--------------------------------------------
Total 3 3 4 10 100%
I know I can do the calculations in the SQL query, but the stored procedure is rather complicated so I'd like to avoid that if possible. So I'm wondering if there's a simple way to achieve this in SSRS.
Right now I just have a row group for each Blah which I use to calculate column totals.
I added a Totals Row for my matrix, then I referenced the totals textbox (textbox 8 in my case) for the column and I did:
Sum(Fields.FieldName.Value)/ReportItems!Textbox8.Value
I hope this makes sense!
To calculate the total, just do a simple sum using the + operator. For the percentage, you can refer to the grand total using ReportItems!ItemName.
You can use aggregate functions in Reporting Services like "SUM" and "AVG" to achieve what you are trying to do. The way it works is "Detail" parts of groups in SSRS tables will list all of the data, while non-detail parts (like headers and footers) of groups can be used for aggregates like:
=SUM(Fields!TestValue.Value)
http://msdn.microsoft.com/en-us/library/ms159134%28v=sql.90%29.aspx
Let me know if you need any more help.
Create two groups, one on a column that is the same data for each row, then one on column blah. add a row for the emcompassing group and do a sum there.
you can simply do as following:
Sum(CInt(Fields!TestValue.Value))
or Sum(CInt(Fields!DollarAmountOfCheck.Value),"DataSet1")
sometime when data is coming through WCF, it does not accept Sum() function. but this works fine in that case.

yet another tsql question

i have three tables
documents
attributes
attributevalues
documents can have many attributes
and these atributes have value in attributevalue table
what i want in single query get all documents and assigned atributes of relevant documents in row each row
(i assume every documents have same attributes assigned dont need complexity of diffrent attribues now)
for example
docid attvalue1 attvalue2
1 2 2
2 2 2
3 1 1
how can i do that in single query
Off the top if my head, I don't think you can do this without dynamic SQL.
The crux of the Entity-Attribute-Value (EAV) technique (which is what you are using) is to store columns as rows. What you want to do is convert those rows back to columns for the purpose of this query. Using PIVOT makes this possible. However, PIVOT requires knowing the number of rows that need to be converted to columns at the time the query is written. So assuming you are using EAV because you need flexible attributes/values, you won't know this information when you write the query.
So the solution would be to use dynamic SQL in conjunction with PIVOT. Did a quick search and this looks promising (didn't really read the whole thing):
http://www.simple-talk.com/community/blogs/andras/archive/2007/09/14/37265.aspx
For the record, I am not a fan of dynamic SQL and would recommend finding another approach to the larger problem (e.g. pivoting in application code).
If you know all the attributes (and their IDs) at design-time:
SELECT d.docid,
a1.attvalue AS attvalue1
a2.attvalue AS attvalue2
FROM documents d
JOIN attributevalues a1 ON d.docid = a1.docid
JOIN attributevalues a2 ON d.docid = a2.docid
WHERE a1.attrid = 1
AND a2.attrid = 2
If you don't, things get quite a bit messier and difficult to answer without knowing your schema.
lets make example
documents table's columns
docid,docname,createddate,createduser
and values
1 account.doc 10.10.2010 aeon
2 hr.doc 10.11.2010 aeon
atributes table's columns
attid,name,type
and values
1 subject string
2 recursive int
attributevalues table's columns
attvalueid,docid,attid,attvalue(sql_variant)
and values
1 1 1 "accounting doc"
1 1 2 0
1 2 1 "humen r doc"
1 2 2 1
and I want query result
docid,name,atribvalue1,atribvalue1,atribvalueN
1 account.doc "accounting doc" 0
2 hr.doc "humen r doc" 1

Explaining row and column dependencies

This is a simple and common scenario at work, and I'd appreciate some input.
Say I am generating a report for the owners of a pet show, and they want to know which of their customers have bought how many of each pet. In this scenario my only tools are SQL and something that outputs my query to a spreadsheet.
As the shop owner, I might expect reports in the form:
Customer Dog Cat Rabbit
1 2 3 0
2 0 1 1
3 1 2 0
4 0 0 1
And if one day I decided to stock Goldfish then the report should now come out as.
Customer Dog Cat Rabbit Goldfish
1 2 3 0 0
2 0 1 1 0
3 1 2 0 0
4 0 0 1 0
5 0 0 0 1
But as you probably know, to have a query which works this way would involve some form of dynamic code generation and would be harder to do.
The simplest query would work along the lines of:
Cross join Customers and Pets, Outer join Sales, Group, etc.
and generate:
Customer Pet Quantity
1 Dog 2
1 Cat 3
1 Rabbit 0
1 Goldfish 0
2 Dog 0
2 Cat 1
2 Rabbit 1
...etc
a) How would I explain to the shop owners that the report they want is 'harder' to generate? I'm not trying to say it's harder to read, but it is harder to write.
b) What is the name of the concept I am trying to explain to the customer (to aid with my Googling)?
The name of the concept is 'cross-tab' and can be accomplished in several ways.
MS Access has proprietary extensions to SQL to make this happen. SQL pre-2k5 has a CASE trick and 2k5 and later has PIVOT, but I think you still need to know what the columns will be.
Some databases indeed support some way of creating cross tables, but I think most need to know
the columns in advance, so you'd have to modify the SQL (and get a database that supports such an extension).
Another alternative is to create a program that will postprocess the second "easy" table to get your clients the cross table as output. This is probably easier and more generic than having to modify SQL or dynamically generate it.
And about a way to explain the problem... you could show them in an Excel how many steps are needed to get the desired result:
Source data (your second listing).
Select values from the pets column
Place each pet type found on a new column
Count values per each type per client
Fill the values
and then say that SQL gives you only the source data, so it's of course more work.
This concept is called pivoting
SQL assumes that your data is represented in terms of relations with fixed structure.
Like, equality is a binary relation, "customer has this many pets of this type" is a ternary relation and so on.
When you see this resultset:
Customer Pet Quantity
1 Dog 2
1 Cat 3
1 Rabbit 0
1 Goldfish 0
2 Dog 0
2 Cat 1
2 Rabbit 1
, it's actually a relation defined by all possible combinations of domain values being in this relation.
Like, a customer 1 (domain customers id's) has exactly 2 (domain positive numbers) pets of genus dog (domain pets).
We don't see rows like these in the resultset:
Customer Pet Quantity
1 Dog 3
Pete Wife 0.67
, because the first row is false (customer 1 doesn't have 3 items of dog, but 2), and the second row values are out of their domain scopes.
SQL paradigma implies that your relations are defined when you issue a query and each row returned defines the relation completely.
SQL Server 2005+ can map rows into columns (that is what you want), but you should know the number of columns when designing the query (not running).
As a rule, the reports you are trying to build are built with reporting software which knows how to translate relational SQL resultsets into nice looking human readable reports.
I have always called this pivoting, but that may not be the formal name.
Whatever it's called you can do almost all of this in plain SQL.
SELECT customer, count(*), sum(CASE WHEN pet='dog' THEN 1 ELSE 0 END) as dog, sum(case WHEN pet='cat' THEN 1 ELSE 0 END) as cast FROM customers join pets
Obviously what's missing is the dynamic columns. I don't know if this is possible in straight SQL, but it's certainly possible in a stored procedure to generate the query dynamically after first querying for a list of pets. The query is built into a string then that string is used to create a prepared statement.

Resources