Cognos SQL multiple rows to one row - cognos-10

The issue I'm having is that the note table allows for multiple entries and I would like it to be in one row for each stock code. I would like this done on the query side. I have tired to several different ways that I know in sql to do it but I keep getting unrecognizable query framework response(example shown blow
stock_code note_sequence stock_note
++++++++++ +++++++++++++ ++++++++++
3051715 1 Sold in sets
3051715 2
From vendor 9999 15; but
3051715 3 will be sold separately
stock_code stock_note
++++++++++ +++++++++++++
3051715 Sold in sets,From vendor 999 15; but will be sold separately

Add three new data items, we'll call them NS 1, NS 2, and NS 3.
NS 1
CASE [note_sequence]
WHEN 1 THEN [stock_note]
ELSE null
END
NS 2
CASE [note_sequence]
WHEN 2 THEN [stock_note]
ELSE null
END
NS 3
CASE [note_sequence]
WHEN 3 THEN [stock_note]
ELSE null
END
Set the Aggregate Function of each new data item to 'Maximum'.
In order to concatenate the results you will have to create a new query and make the existing query the source for the new query. Pull in all of the data items from the original query.
In the new query create a new data item called stock_notes with the following expression:
[NS 1] || ',' || [NS 2] || ',' || [NS 3]
The result should be one row per stock_code and the new data item stock_notes will have all of the appropriate notes for each stock code in a comma-separated list.

There is an object in Report Studio called Repeater. A Repeater is a data container that has to be linked to a query. Inside a Repeater you can place a TextItem linked to the data item stock_note, and another TextItem with the comma.
You can place the Repeater in a column of the List (created for example adding to the edge of the list a TextItem or a Query Calculation). Once you have added this column to the List, click in the padlock icon and place the Repeater inside the column.
The query of the repeater has to be linked to the query of the list through a master-detail relationship (look in the properties of the Repeater query)
(The drawback of master-detail relationships is a loss of performance)

Related

Apex Collection: how to have actual column names instead of C001, C002

created a page to allow users to input SQL statement, when user press button, it should Execute this query and another region with Classic Report will display results.
on Button a Dynamic Action execute PL/SQL code below:
on Click event:
begin
IF :P8_YOURSQL IS NOT NULL THEN
APEX_COLLECTION.CREATE_COLLECTION_FROM_QUERY (
p_collection_name => 'SOMECOLLECTION',
p_query => :P8_YOURSQL,
p_truncate_if_exists => 'YES'
);
end if;
end;
Item to Submit: P8_YOURSQL
second Action refreshes CR region and CR region is based on:
SELECT * FROM APEX_COLLECTIONS
WHERE COLLECTION_NAME = 'SOMECOLLECTION';
resulted columns are not limited to the query results and column headers are C001, C002, C003 etc.
(a) how i can limit the columns to the SQL statement contained?
(b) how to change header to the actual column names?
(c) how to check for a valid SQL statement?
please help with sample code How To.
APEX_COLLECTIONS is a generic table so it has a couple of columns for the most common data types with names like C001, N001 etc. As with any table columns can be aliased in a query.
Example:
Create page process before header to populate the collection
APEX_COLLECTION.CREATE_COLLECTION_FROM_QUERY (
p_collection_name => 'EMP_COLLECTION',
p_query => q'!SELECT * FROM emp!',
p_truncate_if_exists => 'YES'
);
Run the page and open the "Session" window from the developer toolbar. Select "Collections" in the "View" dropdown and click "Set". This will list the data in the APEX_COLLECTIONS table for any collections that exist for the session. Take note of the data and the columns the data is in.
Leave the "Sessions" window open and create a sql query on the APEX_COLLECTIONS view with appropriate aliases.
SELECT
c001 as EMPNO,
c002 as ENAME,
c003 as JOB,
c004 as MGR,
<rest_of_columns>...
FROM
APEX_COLLECTIONS WHERE collection_name = 'EMP_COLLECTION'
It's not possible to alias columns when doing a SELECT * FROM ...
If the SELECT * is important then there is another possibility. Create a view on top of APEX_COLLECTIONS with relevant column names. Example:
CREATE OR REPLACE view EMP_COLLECTION_V
AS
SELECT
c001 as EMPNO,
c002 as ENAME,
c003 as JOB,
c004 as MGR
FROM
APEX_COLLECTIONS WHERE collection_name = 'EMP_COLLECTION'
and then use SELECT * FROM EMP_COLLECTION_V as SQL source for the classic report.
To get the columns names for a SELECT * FROM ... have a look at [DBMS_SQL.DESCRIBE_COLUMNS3][https://docs.oracle.com/en/database/oracle/oracle-database/18/arpls/DBMS_SQL.html#GUID-00AB5DE3-C428-4E60-9398-FD4892F32402]. There is an example in the doc that shows how to print the column names.
This can then be implemented in a classic report
Create a page item per column: P1_C1, P1_C2, etc
Set the column header for each column to the corresponding page item: col1 has header &P1_C1, col2 has header &P1_C2, etc
Create a page process to set the column headers based on the sql query using DBMS_SQL.DESCRIBE_COLUMNS3 - OR - use a
Note that it is strongly advised to assert the sql query to avoid unwanted sql (like a delete or drop table command).

how to bind a repeater inside a repeater on the basis of values from the first repeater

student code| student.Name |Region Name|Location Name| Assets Details |
I have to display asset details on the basis of each student. There can be more than one asset details for one student, how can i use this in repeater or gridview.
Table which i have in SQL.
Student Code | student name | Asset Details
1 Ray laptop
1 Ray Bed
2 Raj Phone
2 Raj charger\
....
I want to display asset details for each unique student code like this on my page.
Student Code | student name | Asset Details
1 Ray laptop
Bed
2 Raj Phone
Charger
How do i perform the same with only one hit from sql.?
You can do the following:
Use a stored procedure to return two results set for the following queries
SELECT DISTINCT StudentCode, studentname FROM StudentInfo
SELECT StudentCode, student name, Asset Details
Create a DataSet for the results with 2 tables. You can name the first table "Student" and the second table "StudentInfo"
Create a relationship for the tables (please use the intellisense for syntax)
DataSet.Relationships.Add("RelationshipName",Table1.StudentCode,Table2.StudentCode)
Bind the GridView with the DataSet
Create a Template column for Asset Details and insert a repeater or Gridview for that column.
On the ItemDataBound event of the GridView.
DataRowView drv = (DataRowView)e.ItemData;
Repeater.DataSource = dv.CreateChildView();
Instead of using two repeater control, try to make a sql query to get data in this format
use such query
select studentID,
stuff((select ',' + AssetDetails
from Tbl_Student t2
where t2.studentID= t1.studentID
order by studentID
for xml path('')),1,1,'') as AssetDetailsString
from Tbl_Student t1
group by studentID
this will return single row for every studentID with Assestdetails in CSV format in same row

Using a Repeater with a dynamically generated table, ie, so unknown field names

I'm trying to produce a repeater showing amounts of money taken by various payment types into a table.
Payment types available come from a global settings file as an array, I am creating a dataTable by looping this list and extracting sales reports (there might be a more efficient way than this loop, but this is not my concern at the minute).
My question: How do I bind this to a repeater and display it when I dont necessarily know the table column names?
I've tried various methods to give the table a header row and give the columns numerical names from a for > next loop, but am either getting no results, or
System.Data.DataRowView' does not contain a property with the name '7'. < or whatever number
This is where I currently am:
EDIT: JUST REALISED MY CODE WAS AWFUL, SO UPDATED:
Dim paymentTable As New DataTable("paymentTable")
For j = 0 To UBound(paymentTypes)
Dim Type = Trim(paymentTypes(j))
Dim headers As DataColumn = New DataColumn(j.ToString)
paymentTable.Columns.Add(headers)
Next
Dim titleRow As DataRow = paymentTable.NewRow()
For k = 0 To UBound(paymentTypes)
Dim Type = Trim(paymentTypes(k))
titleRow.Item(k) = Type
Next
paymentTable.Rows.Add(titleRow)
Dim newRow As DataRow = paymentTable.NewRow()
For i = 0 To UBound(paymentTypes)
Dim Type = Trim(paymentTypes(i))
Try
newRow.Item(i) = '' GO OFF AND GET STUFF FROM DB
Catch
newRow.Item(i) = "0 "
End Try
Next
paymentTable.Rows.Add(newRow)
THIS EDITED CODE WORKS BUT I ONLY GET ONE ITEM
What I was hoping for would look something like:
card | cash | paypal ... etc (headings row)
£250 | £54 | £78 ... etc (values row)
Obviously there're a million ways this can be done, but this makes sense for my application, which has to be expandable and contractable depending on payment types available and this whole table needs to be repeated for multiple locations (also variable depending on who's viewing, and the number of locations in the system)
No, dont give up but just dont name columns by absolute number with no string before try
Dim headers As DataColumn = New DataColumn("col"+ j.ToString)

Edit the code of a GridView column asp.net

I've been looking for a way to control the text that appears in a particular column of a GridView.
For example consider a database with two tables Student and Class.
I want to add a column to the GridView which print out all the students in the Database, the column will show the student's class name, how can do it? (I can normally print the ClassId since its a FK in the student table)
I want to add a column to the GridView which print all the classes, the column will count the number of students in each class, how can I do it?
1- You can do that simply with inner join or a stored procedure to get the class name beside all the data you need in your query.
2- More than one way to do what you want:
For example:
you can add a column to your data table (empty column ), and fill it later through using Sum() aggregate function in a query.
DataTable result_dt = DAL_Helper.Return_DataTable(sqlSelect);//your original query
result_dt.Columns.Add("NumberOfStudent");
result_dt.Columns["NumberOfStudent"].DataType = typeof(string);
result_dt.Columns["NumberOfStudent"].MaxLength = 255;
if (result_dt.Rows.Count > 0)
{
for (int i = 0; i < result_dt.Rows.Count; i++)
{
//Here u can fill your new empty column.
}
}
result_dt.AcceptChanges();
After you return your customized data table(as a data source), you can bind it to the grid view.
Another solution : add an empty column to the grid view and in the RowDataBound event of the grid view you can fill this column through some loop and you can use LINQ to help you in getting the summation.

How to insert multiple asp.net checkboxes values to MSSQl database as comma seperaed string 1,2,3,4,5 using vb.net?

How to insert and retrive multiple asp.net checkboxes values to MSSQl database as comma seperaed string 1,2,3,4,5 using vb.net ?
and retrieve the inserted checkbox chekched in disabled form ..using vb.net
example of this is :
http://www.redbus.in/Booking/SeatSelection.aspx?rt=4017230&doj=31-Dec-2010&dep=04:55%20PM&showSpInst=false
I want this type of whole layout in vb.net ... means if user registered selected seats then then next the same page load for todays date the selected seats will be disabled ...
pleas provide me the code snippet for that // insert and retrieve funda... in checkbox
Try with such a loop
string str = "";
for (int i = 0; i <= 29; i++)
{
if (CheckBoxList1.Items[i].Selected)
{
string b = CheckBoxList1.Items[i].Value;
str += b + " , ";
}
}
than make an insert statement and you are putting all the checkbox values into a column.
than just call select statement call the column
You are doing a very bad thing storing lists of values in a single MSSQL column. I know it may be convenient now, but it is always eventually bad news. Why not store booked seats in their own table, one seat per row? When you store a list of values in a column, you restrict access to the information stored in that column to only the code that can split the list. It isn't available to database queries or to other code as a list, merely as a string. You will end up replicating the code to split this string to other places that need the list data.

Resources