Cross tabbing in SELECT statement??

Hi,
The following SQL statement:
SELECT
T.LASTFIRST, C.COURSE_NAME, CC.COURSE_NUMBER, CC.SECTION_NUMBER, CC.EXPRESSION, ST.GENDER, COUNT(CC.STUDENTID) STUDENTCOUNT
FROM TEACHERS T
JOIN CC
ON CC.TEACHERID = T.DCID
JOIN COURSES C
ON C.COURSE_NUMBER=CC.COURSE_NUMBER
JOIN STUDENTS ST
ON CC.STUDENTID=ST.ID
WHERE CC.SCHOOLID = 100 AND TERMID>=1900
GROUP BY T.LASTFIRST, C.COURSE_NAME, CC.COURSE_NUMBER, CC.SECTION_NUMBER, CC.EXPRESSION, ST.GENDER
ORDER BY T.LASTFIRST, CC.COURSE_NUMBER, CC.SECTION_NUMBER, CC.EXPRESSION, ST.GENDER
returns the following dataset:
Abram, Michael     U.S. History     SOC1000     2     1(A)     F     7
Abram, Michael     U.S. History     SOC1000     2     1(A)     M     12
Is there a way to make the gender count for each sex show up as an individual column in the SELECT statement so that their is only ONE row returned per Course Number with column headings like FEMALECOUNT and MALECOUNT??
I've tried using a nested select count statement inside the original SELECT statement but with no luck.
Jeff

Nimish Garg wrote:
try this one
SELECT
     T.LASTFIRST,
     C.COURSE_NAME,
     CC.COURSE_NUMBER,
     CC.SECTION_NUMBER,
     CC.EXPRESSION,
     ST.GENDER,
     COUNT(CC.STUDENTID) STUDENTCOUNT,
     COUNT(CASE ST.GENDER WHEN 'F' THEN 1 END) Femalecount,
     COUNT(CASE ST.GENDER WHEN 'M' THEN 1 END) Malecount
FROM
     TEACHERS T JOIN CC ON CC.TEACHERID = T.DCID
     JOIN COURSES C ON C.COURSE_NUMBER=CC.COURSE_NUMBER
     JOIN STUDENTS ST ON CC.STUDENTID=ST.ID
WHERE
     CC.SCHOOLID = 100 AND TERMID>=1900
GROUP BY T.LASTFIRST, C.COURSE_NAME, CC.COURSE_NUMBER, CC.SECTION_NUMBER, CC.EXPRESSION, ST.GENDER
ORDER BY T.LASTFIRST, CC.COURSE_NUMBER, CC.SECTION_NUMBER, CC.EXPRESSION, ST.GENDER
Won't work for the OP's requirement. As i specified in my post, if you keep the GENDER column you'll continue to get 2 rows.

Similar Messages

  • Cross-Tab - conditional suppression and formatting

    I have a cross-tab report of "widgets" sold. There are $10 widgets, $20 widgets, and $50 widgets. All widgets will always cost some round dollar amount, never anything like $10.99. My report shows weekly, month-to-date, and year-to-date sales for each widget denomination by both count and total amount by state. For example:
                                   $10                          $20                              $50
         count     amount       count       amount         count       amount
    New York
           Week        5             $50           4                 $80             1               $50
            MTD
            YTD
    Delaware         1              $10
    etc.
    I also have totals by row, along with mark-up amounts (11% of gross sales amounts) and net sales (gross - mark-up). These last 3 columns are included in my dataset, not calculated in the cross-tab.
    My problem is that I can't figure out how to do 2 things:
    1.  the count columns for mark-up and net will always be blank, so I want to suppress the "count" column header
    2. I want to display the dollar amounts for those 2 columns with 2 decimal places, but the other amounts with no decimals.
    Can this be done, and if so, how?
    Thanks!
    Phil

    Hi Phil,
    I also found the following information under Cross-tab Help for suppressing rows or columns.  I hope this is what you're looking for.  Sylvie
    To suppress empty rows and columns
    Right-click the blank top-left area of the Cross-Tab and select Cross-Tab Expert from the shortcut menu.
    The Cross-Tab Expert appears.
    Click the Customize Style tab.
    Select either the Suppress Empty Rows or Suppress Empty Columns check box.
    Click OK.
    Now, when you print the report, empty rows and/or columns will not appear.
    To suppress row and column grand totals
    Right-click the blank top-left area of the Cross-Tab and select Cross-Tab Expert from the shortcut menu.
    The Cross-Tab Expert appears.
    Click the Customize Style tab.
    Select either the Suppress Row Grand Totals or Suppress Column Grand Totals check boxes.
    Click OK.
    To suppress subtotals and their labels
    If you have more than two groups in your Cross-Tab you can suppress the subtotal and label for one of them.
    Right-click the blank top-left area of the Cross-Tab and select Cross-Tab Expert from the shortcut menu.
    The Cross-Tab Expert appears.
    Click the Customize Style tab.
    Click the field whose subtotal you want to suppress.
    The Suppress Subtotal and the Suppress Label check boxes become active.
    In the Group Options area, select the Suppress Subtotal check box.
    Click the Suppress Label check box to suppress the label associated with subtotal.
    Click OK.

  • Hide top axis in cross tab report

    Hi
      How to hide top axis in webi report xir4.
    Only objects names on top axis to be hide.
    I see only option for data fold and unfold
    Please suggest.
    Thanks.

    hi,
    Please right click the top axis of Cross tab.
    Select  Hide > Hide dimension
    Now you will see only Left axis Headers and data only. Top row which has column names will be hidden.
    Regards
    Vamsee

  • How to perform sorting in a Cross-Tab report?

    Hi,
    Please advice, how sorting can be done in Crosstab report.

    Hi Sriram
    Using the group option you can get the ascending or descending sort order however if your requirement is other than that, you can use a specified order as below:
    To create a specified order for the column or row headings in the crosstab complete the following steps:
    1. Right click the cross tab to select it and from the fly out menu select 'Format Cross-tab'.
    2. In the 'Format Cross-tab' dialog box, select the field that you would like to change the order of. In this example, it is{Customer.Country}.
    3. Click the 'Group Options' button from the right side of the 'Format Cross-tab' dialog box.
    4. Select 'Specified Order' from the drop down list box on the 'Common' tab. This takes you to the 'Specified Order' tab.
    5. From the drop down list box on this tab, select the field values in the order you would like them to appear on the cross-tab.
    You can specify the conditions as per your requirement there.
    Hope this helps!!
    Regards
    Sourashree

  • Cross tab column headers display on all pages

    I made a Crystal Report with a cross tab which displays all details. In the cross tab I have 6 columns with details about the data displayed in the last 2 columns. The headers of these columns are only displayed on the first page but not on other pages. How can I add column headers to all pages? Thank you.

    Column headings should be repeated automatically, row headings can be repeated by
    1.right-clicking on the row header
    2. select Row options
    3. .select Repeat Labels on Page break
    or, when I just reviewed it in the help section it says:
    "When you create a Cross-Tab that is wider or longer than the specified page size, the program automatically spans the printing across enough pages to accommodate the size of the Cross-Tab. In the Preview tab, a line will appear at each page break. For ease in reading, column headings will be repeated on subsequent pages. Row headings can also be repeated using the Keep Columns Together option.
    To repeat row labels
    Right-click the blank top-left area of the Cross-Tab and select Cross-Tab Expert from the shortcut menu.
    The Cross-Tab Expert appears.
    Click the Customize Style tab.
    Select the Repeat Row Labels check box, if desired.
    Crystal Reports lets you designate report objects that don't expand horizontally, such as text objects, field objects, OLE objects, charts, maps, lines, boxes, and so on, to be repeated on each additional horizontal page that a Cross-Tab creates. For more information, see Repeating report objects on horizontal pages."

  • Crystal Reports Cross-Tab Report not showing all available fields

    I am running CR2008 against MS SQL Express.  I have several tables with fields and data in them and can create standard reports to show all the data in all the fields.  However, when I try to create a cross-tab report, only some of the fields appear for me to choose from.
    I created a standard report with all the fields I needed in my cross-tab report and ran a preview.  Everything was there.  I then added a cross-tab object, selected the tables only to find that fields that are in the main report are not showing up for selection in the cross-tab.

    UPDATE:  I exported the entire database from MS SQLExpress to MS Access and I am having the same issues, so it does not appear to be a problem with the database engine and, since the standard tabular reports show the fields, I am at a loss as to why they don't show up in the cross-tab or the Parameter fields.
    I am creating the cross-tab through the Cross-Tab wizard.  Is there maybe a bug in that?  Is there a way to create it otherwise?

  • "Check Statistics" in the Performance tab. How to see SELECT statement?

    Hi,
    In a previous mail on SDN, it was explained (see below) that the "Check Statistics" in the Performance tab, under Manage in the context of a cube, executes the SELECT stament below.
    Would you happen to know how to see the SELECT statements that the "Check Statistics" command executes as mentioned in the posting below?
    Thanks
    ====================================
    When you hit the Check Statistics tab, it isn't just the fact tables that are checked, but also all master data tables for all the InfoObjects (characteristics) that are in the cubes dimensions.
    Checking nbr of rows inserted, last analyzed dates, etc.
    SELECT
    T.TABLE_NAME, M.PARTITION_NAME, TO_CHAR (T.LAST_ANALYZED, 'YYYYMMDDHH24MISS'), T.NUM_ROWS,
    M.INSERTS, M.UPDATES, M.DELETES, M.TRUNCATED
    FROM
    USER_TABLES T LEFT OUTER JOIN USER_TAB_MODIFICATIONS M ON T.TABLE_NAME = M.TABLE_NAME
    WHERE
    T.TABLE_NAME = '/BI0/PWBS_ELEMT' AND M.PARTITION_NAME IS NULL
    When you Refresh the stats, all the tables that need stats refreshed, are refreshed again. SInce InfoCube queries access the various master data tables in quereis, it makes sense that SAP would check their status.
    In looking at some of the results in 7.0, I'm not sure that the 30 day check is being doen as it was in 3.5. This is one area SAP retooled quite a bit.
    Yellow only indicates that there could be a problem. You could have stale DB stats on a table, but if they don't cause the DB optimizer to choose a poor execution plan, then it has no impact.
    Good DB stats are vital to query performance and old stats could be responsible for poor performance. I'm just syaing that the Statistics check yellow light status is not a definitive indicator.
    If your DBA has BRCONNECT running daily, you really should not have to worry about stats collection on the BW side except in cases immediately after large loads /deletes, and the nightly BRCONNECT hasn't run.
    BRCONNECT should produce a lof every time it runs showing you all the tables that it determeined should have stats refreshed. That might be worth a review. It should be running daily. If it is not being run, then you need to look at running stats collection from the BW side, either in Process Chains or via InfoCube automatisms.
    Best bet is to use ST04 to get Explain Plans of a poor running InfoCube query and then it can be reviewed to see where the time is being spent and whether stats ate a culprit.

    Hi,
    Thanks, this is what I came up with:
    st05,
    check SQL Trace, Activate Trace
    Now, in Rsa1
    on Cube, Cube1,
    Manage, Performance tab, Check Statistics
    Again, back to st05
    Deactivate Trace
    then click on Displace Trace
    Now, in the trace display, after scanning through  the output,
    “ … how do I see the SELECT statements that the "Check Statistics" command executes …”
    I will appreciate your help.

  • How Should select default (Apr-mar)month in cross tab?

    HI Experts,
    I have created webi report with cross tab format. In this report how should select  Current year (April Month) to Next Year (March Month).
    For Example:
    2013 apr.......2014 may

    Hi Manikandan,
    Similar to your previous post about selecting Fiscal Year, do this in BEx, Have a range on Fiscal Month which either the user can input the range or use Customer Exits to derive it.
    The in your cross tab, having Fiscal Month in your columns will show the range selected.
    with regards
    Gill

  • Record selection issue in cross tab.

    Hello!
    I have a record  filtering problem. I tried many scenarios, and searched this forum but I cannot find an solution.
    I have a sub-report that contains a cross tab that provides a few summarized fields.
    I have two tables: a table with category of events : (field catID and Catname) and an event table with a CatID, a date field and others number field I use in the cross tab.
    I want my cross tab to show one row per category. The Catname and the summaries.
    Now all is working fine but for one issue. I need to select my event according to a time bracket.
    So I created two time parameters (start and end) and select the event dates between these two dates.
    Now, when selected, any category that does not have an event between these dates are not displayed. I would like them to be shown with "o" values.
    There must be a way of doing this. I just cannot find it.
    Esther

    In order for Crystal to show data, it must be given data to show!
    You will need to base your subreport on an SQL Command.  In that command, put the current SQL used by the report.  To this, do a UNION to a select that selects the category name, and a zero for each field that you are summing in the crosstab.
    As a simplified example, suppose the original SQL was (MS SQL):
    select c.catname, sum(e.quantity) as Quantity
    from categories c, events e
    where c.catid = {?catid parm}
    and c.catid = e.catid
    and e.date between {?start} and {?end}
    This would be changed to
    select c.catname, sum(e.quantity) as Quantity
    from categories c, events e
    where c.catid = {?catid parm}
    and c.catid = e.catid
    and e.date between {?start} and {?end}
    UNION
    select c.catname, 0 as Quantity
    from categories c
    where c.catid = {?catid parm}
    This will return an additional zero record, regardless of what data the original query returns, thus giving Crystal the data needed to display.  Note that the zero value will not affect the results in any way, except causing a zero to be displayed if no data is returned from the original query.
    Just remember that the second SELECT in the UNION has to have the same number of fields, of the same data type, as the first SELECT.
    HTH,
    Carl

  • Cross Tab - select dates

    Hi I have created a cross-tab report using only two fields. enrollment_id and create_date. What is needed is a count of the # of enrollments and a summary for the week starting on Sunday. I have been able to do this however, they only want for the previous 4 weeks - I am returning all weeks since the start.  I am not sure where to filter.  Here is what I used in a regular report which worked but is not working in the cross-tab.  The formula below gives me up to the last 4 weeks sunday to today.
    not ({ISSUANCE.CREATE_DATE}in Last4WeeksToSun) and
    not ({ISSUANCE.CREATE_DATE} in WeekToDateFromSun)
    Here's what it should look like and includes a count up to today for week of 11/30.
    Site_name    11/02/08              11/09/08     11/16/08    11/23/08    11/30/08
    #issued            25                     29               16            18             15
    Any help would be appreciated.

    I figured this one out....needed to take out "not"

  • Advanced Cross tab table and selecting values

    Hi,
    Have a question on advanced tables again.
    I have a cross tab table...
    ACCT Val1 Val2 Val3
    1234 1 2 3
    Query fires ok and returns values, however, I am trying to retrieve the value in the CO (or AM - it doesn't seem to work eitherplace) to do some manipulation. So far there is only one row in the table, but I cannot seem to retrieve the value. For example I update val1 to 210 and save. I want to get the value and round it down to 200. I am using the following code
    OAAdvancedTableBean tableBean = (OAAdvancedTableBean)webBean.findIndexedChildRecursive("BudgetRN");
    OAMessageStyledTextBean augBean = (OAMessageStyledTextBean)tableBean.getgetAttributeValue("Val1");
    String strl = (String)augBean.VALUE;
    but i end up with a cast exception on the second line where i grab val1.
    Any ideas once again appreciated!

    u will get the class cast exception... because... u said that u r updating the Val1 field... if it is updatable...... then it should be messageTextInputBean... not messageStyleTextBean....
    Secondly dont use augBean.VALUE.... use augBean.getvalue().... something should be there....

  • Cross Tab Report

    Hi,
    I'm creating a report in Crystal 2008 for SAP Business One. This report is a sales report and i want to pull the data  from three tables namely Invoice Rows, Credit Memo Rows and Down Payment Invoice Rows. Other selections in the report include the Posting Periods table, Items Group Table and the Items Master Data table.
    I'm considering a cross tab report displaying the Items Group on the Row level and the Posting Periods on the columns with Sum of row totals for item groups per items on invoice,credit memo or down payment.
    I need help regarding the kind of joining is needed to ensure that i get a correct and good report showing total sales made for item groups per period. Thus, total sales = Invoice+Downpayment - Credit Memo
    Thanks.
    John

    You can also user OJDT as the base and left join it to the Invoice, Credit note and Downpayment tables by linking DocEntry and the object tpye.
    With this option, you need to use IF statement to read information based on object type.
    Using command as suggested is simpler.  However, my preference is to use command alone. 
    Linking Command with Tables seems to have slower performance.

  • Filtering Key Figures/Columns in a Cross Tab Component

    Hello All,
    I am creating a P&L Statement for Better/Worse comparisions, and I have to show only three columns at a time based on User Drop Down Selections.
    Here is my Bex Query Out put looks like.  The Rows part is "Structure" and Columns are "Cell Restrictions".
    Now, the CrossTab component has to show only Two columns at a time, based on user selections. Here is my desired output. User will Select either ' Avg' Rate or 'End Rate'. Based on that selction, Actual's will show in the First Column and based on Selection from Second DROPDOWN for Budget/Forecast/PY, the Second Column has to be displayed.
    Is there any DropDown Component or CrossTab Component Options are there to Filter the Columns based on User Selections ?
    Appreciate your help. This is the Crucial Chart for the whole Dashboard.
    Thanks,
    Sonti.

    Hi Tammy,
    Thanks for your quick reply.
    Can it be done dynamically without the User touching it ? My Cross Tab has to show values based on the Selctions. But, I couldn't find the code to restrict Cross Tab data.
    I have to code for Dropdown box --> On Select, to check / Un check the KF's in the Filter Panel Component. And in Filter Panel Comoponent, --> On Select, DATASOURCE.setFilterExt("KF1", "KF2").
    And assign the data source to Cross Tab.
    Please correct me if I am wrong.
    Regards,
    Sonti.

  • Cross tab using sql

    Hi,
    I am using oracle 11g version.
    I have a reporting requirement where in I have to produce a cross tab as explained below. Please help me in writing an SQL query to achieve this. Many thanks.
    Sample data:
    Customer             product type
    AAA                           DVD
    AAA                           Tape
    BBB                           Tape
    CCC                                 DVD
    CCC                           Bluray
    DDD                           DVD
    DDD                           Tape
    DDD                           Bluray
    Logic
         AAA                                              BBB                         CCC                         DDD          
         DVD     Tape     Bluray                    DVD     Tape     Bluray               DVD     Tape     Bluray               DVD     Tape     Bluray
    DVD          1                    DVD                         DVD               1          DVD          1     
    Tape     1                         Tape          1               Tape                         Tape     1          1
    Bluray                              Bluray                         Bluray     1                    Bluray          1                                                                                                    Final Output
    No. of customers who purchased the products across all product types
         DVD     Tape     Bluray               
    DVD          2     1               
    Tape     2     1     1               
    Bluray     1     1                    
    Scripts:
    create table cust_product(customer varchar2(5), product_type varchar2(10));
    insert into cust_product values('AAA','DVD');
    insert into cust_product values('AAA','Tape');
    insert into cust_product values('BBB','Tape');
    insert into cust_product values('CCC','DVD');
    insert into cust_product values('CCC','Bluray');
    insert into cust_product values('DDD','DVD');
    insert into cust_product values('DDD','Tape');
    insert into cust_product values('DDD','Bluray');

    Hi,
    858747 wrote:
    ... Sample data:
    Customer             product type
    AAA                           DVD
    AAA                           Tape
    BBB                           Tape
    CCC                                 DVD
    CCC                           Bluray
    DDD                           DVD
    DDD                           Tape
    DDD                           Bluray...
    Final Output
    No. of customers who purchased the products across all product types
    `     DVD     Tape     Bluray               
    DVD          2     1               
    Tape     2     1     1               
    Bluray     1     1                    
    Sorry, I don't follow the logic. Try explaining in words, with specific examples.
    Didn't 2 distinct customers, 'CCC' and 'DDD', buy both 'DVD' and 'Bluray'? Why aren't the results
    `     DVD     Tape     Bluray               
    DVD          2     2
    Tape     2     1     1               
    Bluray     2     1                    with 2's in the corners?
    Assuming that was just mistake, you can do something like this:
    WITH     got_pairs     AS
         SELECT       a.product_type               AS product_a
         ,       NVL (b.product_type, a.product_type)     AS product_b
         FROM            cust_product     a
         LEFT OUTER JOIN     cust_product     b  ON  a.customer     = b.customer
                                AND     a.product_type != b.product_type
    SELECT       *
    FROM       got_pairs
    PIVOT       (     COUNT (*)
           FOR     product_b
           IN     ( 'DVD'
              , 'Tape'
              , 'Blueray'
    ORDER BY  CASE  product_a
              WHEN  'DVD'     THEN  1
              WHEN  'Tape'     THEN  2
              WHEN  'Bluray'     THEN  3
           END
    ;This assumes that the combination (customer, product_type) is unique, as it is in your sample data., and that the only product_types are the 3 in your sample data.
    if those assumptions are not true, then post some revised sample data and results, that show what you want in those cases.
    Sorry, I don't have an Oracle 11 database right now to test this.
    I can (and did) test the outer join, that combines a product_type with itself only if it can't be combined with anything else.
    Using a pre-Oracle 11 pivot technique, the following works:
    WITH     got_pairs     AS
         SELECT       a.product_type               AS product_a
         ,       NVL (b.product_type, a.product_type)     AS product_b
         FROM            cust_product     a
         LEFT OUTER JOIN     cust_product     b  ON  a.customer     = b.customer
                                AND     a.product_type != b.product_type
    SELECT       product_a     AS product_type
    ,       COUNT (CASE WHEN product_b = 'DVD'    THEN 1 END)     AS dvd
    ,       COUNT (CASE WHEN product_b = 'Tape'   THEN 1 END)     AS dvd
    ,       COUNT (CASE WHEN product_b = 'Bluray' THEN 1 END)     AS dvd
    FROM       got_pairs
    GROUP BY  product_a
    ORDER BY  CASE  product_a
              WHEN  'DVD'     THEN  1
              WHEN  'Tape'     THEN  2
              WHEN  'Bluray'     THEN  3
           END
    ;Notice that the sub-query got_pairs is the same in both queries.
    Thanks for posting the CREATE TABLE and INSERT statements; that's very helpful!
    Edited by: Frank Kulash on May 29, 2011 4:05 PM
    Added Oracle 9 query.

  • Cross Tab Sorting

    Hi,
    I have a cross tab report, i am summarizing total amount field i.e. is JSCOUNT, i want to sort the records with this field(JSCOUNT).
    how to do the sorting in cross tab with summarizing field[JSCOUNT], please help me
    Thanks
    Venkat

    Venkat,
         I have CRXI, click on the crosstab, (Top left corner) go to Report
    about half way down, you shold see group sort expert.  If you do not,
    you may need to click somewhere else on the crosstab.
    Note:    A report must contain a summary value in order to be able to perform a Group Sort. See Summarizing grouped data.
    Use the Group Sort Expert to identify top or bottom groups (the states with the most/fewest customers in a customer report, for example). You can also identify top or bottom percentages.
    Click Group Sort Expert
    in the " For this Group Sort"  section, select all
    you should see your summary value listed, and be able to select it now.
    Hope this helps.

Maybe you are looking for