DB2 Convert rows to column - db2

I have to pivot the following data:
From:
SELECT NAMES, SUM(AMT1), SUM(AMT2) FROM MY_TABLE GROUP BY NAMES
NAMES AMT1 AMT2
Mike 6000 5000
Jerry 1000 10
King 500 2000
Mary 400 5000
Harry 100 500
to
Names Mike Jerry King Mary Harry
AMT1 6000 1000 500 400 100
AMT2 5000 10 2000 5000 500
Is it possible to implement this using queries on DB2? Note the NAME field is dynamic and I can have up to 25 rows on my table. Thank you for your help.

I am not sure if there is an already generic solution for this pivot problem. However, it is possible to use the DBMS_SQL to process the metadata and then perform the pivot.
I developed my own generic version of this pivot, and it is published in GitHub: https://github.com/angoca/db2tools
It takes a existing table (session or persistent) and creates a session table with the row as columns and vice versa.

Related

Jasper Report - Format Crosstab

From a query, I get this data (simplified):
Referring Date Name Budget
2017-01 JACK 100
2017-01 JOHN 200
2017-01 SMITH 150
2017-02 JACK 50
2017-02 SMITH 200
2017-03 JOHN 300
2017-03 SMITH 200
2017-03 JENNY 150
I need a crosstab to organize budget on columns and referring-date and name on rows.
Using Jasper Wizard, I got this:
As you see, Referring Date is put in a Rowspan.
Unfortunally, Im requested to build something like this:
I've tried to modify Crosstab but when I try to move Referring Date above Name (companyName in my case) I got the error because Im "invading" company section.
Is there a solution?
Thank you!
Yes its possible. Don't use Cross tab. Learn and use report Group. Create group on "referring date".
For Total create a variable of data type same as $F{budget}.Set the Calculation to 'Sum', the Reset Type to 'Group' (and pick the appropriate group from the dropdown), set the variable expression to $F{budget}.

Only hide duplicated fields in Crystal Reports

I have a table in a SQL Server 2008 R2 database like this:
Num Total Item Item Value
--------------------------------------
1 5000 Cup 2000
1 5000 Brush 3000
2 2500 Kandy 2500
3 1000 Soap 750
3 1000 Rice 250
As you can see there is a duplication in the Num, Total columns, it must be like
Num Total Item Item Value
----------------------------------------
1 5000 Cup 2000
Brush 3000
2 2500 Kandy 2500
3 1000 Soap 750
Rice 250
I've tried to use SQL Group By clause, but that didn't work. I also tried to suppress when duplicated in Crystal Reports, but it removes the entire row
So any help?
In the Group Expert inside of Crystal Reports, group your report on the Num and Total fields.
I found it,
Removing the duplicated fields by suppressing them when they are equal to it's previous record
Num Field ==> Suppress ==> Add this
{Num} = previous({Num})
Total Field ==> Suppress ==> Add this
{Total} = previous({Total})

DB2 9 Fundamentals

Given the following two tables:
NAMES
NAME NUMBER
---------- -------
Wayne Gretzky 99
Jaromir Jagr 68
Bobby Orr 4
Bobby Hull 23
Mario Lemieux 66
POINTS
-----------------------------
NAME POINTS
---------- ------
Wayne Gretzky 244
Bobby Orr 129
Brett Hull 121
Mario Lemieux 189
Joe Sakic 94
How many rows would be returned using the following statement?
SELECT name FROM names, points
Can someone explain why the answer is 25?
Thanks in advance for any help provided
I guess this instruction is equivalent to a cross join in standard SQL. Hence the number of records returned is 5 records in names * 5 records in points = 25.
Also known as the "Cartesian Product"
"The Cartesian product, also referred to as a cross-join, returns all the rows in all the tables listed in the query. Each row in the first table is paired with all the rows in the second table. This happens when there is no relationship defined between the two tables."
from:
http://www.dba-oracle.com/t_garmany_9_sql_cross_join.htm

SQL Server 2008 Provide Value Once for Multiple Instances in Select statement

Thanks in advance for your help.
I'm working with an existing query that is called CustSalesbyRoute. I've made a recent change to it to get a distinct count of Route Stops by Customer and this information is stored in a separate table I created for CustRouteStops. However, the query also includes Product Family so this duplicates my distinct Route-Stop count. I've confirmed that several reports use Product Family from this table to pull various data, otherwise, I would eliminate this column and everything would work great. I also know that this essentially goes against relational database integrity; however, I'm left to work on it, and I'm hoping to have a solution that is equivalent to Excel's FREQUENCY/MATCH functions or SUMPRODUCT, etc.
The question is this: Is there a way to display the Customer Route-Stops value for the first instance of rows that have multiple Product Family values and show 0 for any others for the same Customer/Route combination? For example, below is what the query is doing now if I include Product Family:
CustID ProdFamily Route #ofStops
10001 Dairy 101 14
10001 Dry Groceries 101 14
10001 Meat 101 14
This is what I would like to see happen because I need to leave Product Family in the table:
CustID ProdFamily Route #ofStops
10001 Dairy 101 14 <<< 1st instance will provide the Stops value
10001 Dry Groceries 101 0 <<< remaining rows for same Cust# and Route will show 0
10001 Meat 101 0
Thanks again!

Summarizing each subgroup in Group Footer - Crystal Reports

I'm trying to work on a report for a client. Basically I need something like such
Group 1: Customer ID
Group 2: Truck ID
CustID Vehicle ID Detention Time
------ ---------- --------------
ABX 100 60
35
20
TOTAL: 115
200 80
15
TOTAL: 95
300 10
TOTAL: 10
TOTALS FOR CUSTOMER ABX
100 115
200 95
300 10
Is there anyway to accomplish this without a subreport? I was hoping for a "summary field" that I could summarize more than just a single value.
Thanks!
(FYI using Crystal Reports 2008)
Use a crosstab; place it in the report-footer section.
There might be a better way to do this, but the one that comes to mind is to use two arrays: One to store the truck ID and another to store the corresponding total. In each inner grouping (TruckID), just tack on another array element and store its total time. To display, you could cast the values to strings, attach a newline character after each entry, and set the field to "Can Grow". So altogether, you'd need three formulas: one to initialize the arrays (in GH1), one to update the arrays with sum({truck.time},{truck.ID}) (in GF2), and one to display each entry (in GF1).
With that being said, CR has terrible support for containers... You're limited to 1-dimensional, non-dynamic arrays that are gimped at 1000 items max. It doesn't sound like these would be big problems for what you're trying to do, but you will need to redim preserve the arrays unless you know ahead of time how many trucks you'll have per customer.