Show missing Type and count as 0 using t-sql

Hi All,
I have a table with 3 columns, let say  EventSchedule,ScheduleType,Count
EventSchedule      ScheduleType     Count
Conference            Type1         25
Meeting               Type1         20
Meeting               Type2         22
Appointment           Type1         40
Appointment           Type2         35
Appointment           Type3         55
I want t-sql to show output like this..
EventSchedule      ScheduleType     Count
Conference            Type1         25
Conference            Type2         0
Conference            Type3         0
Meeting               Type1         20
Meeting               Type2         22
Meeting               Type3         0
Appointment           Type1         40
Appointment           Type2         35
Appointment           Type3         55
Presentation          Type1         0
Presentation          Type2         0
Presentation          Type3         0
Note:
1)The EventSchedule column will always have values - Conference,Meeting,Appointment,Presentation         
2)The ScheduleType  column will always have values- Type1,Type2,Type3
In the above sample source data ,there was no Type2, Type3 for Conference and there was no Type3 for Meeting and there was no Type1 , Type2 and Type3 fpr Presentation. So whenever if any Type is missing it should populate that missing Type  with count
as 0.
Create statement;
Create Table Sample (EventSchedule varchar(20) null,ScheduleType varchar(10) null,Count int null)
Insert statement:
Insert into sample (EventSchedule,ScheduleType,Count) Values ('Conference','Type1',25)
Insert into sample (EventSchedule,ScheduleType,Count) Values ('Meeting' ,'Type1',20)
Insert into sample (EventSchedule,ScheduleType,Count) Values ('Meeting','Type2',22)
Insert into sample (EventSchedule,ScheduleType,Count) Values ('Appointment','Type1',40)
Insert into sample (EventSchedule,ScheduleType,Count) Values ('Appointment' ,'Type2',35)
Insert into sample (EventSchedule,ScheduleType,Count) Values ('Appointment','Type3',55)
Thanks!
sql

Hi HoroChan-
This is the solution I found. I created 2 cte's. In first CTE I created the list with 1 to 12 numbering and did left join with the second cte(source data).
WITH myListCTE AS (
SELECT 'Conference' AS EventSchedule ,'Type1' AS ScheduleType, 1 AS Rn
UNION ALL    
SELECT 'Conference' AS EventSchedule, 'Type2' AS ScheduleType, 2 AS Rn 
UNION ALL       
SELECT 'Conference' AS EventSchedule, 'Type3' AS ScheduleType, 3 AS Rn         
UNION ALL
SELECT 'Meeting' AS EventSchedule,'Type1' AS ScheduleType, 4 AS Rn   
UNION ALL     
SELECT 'Meeting' AS EventSchedule,'Type2' AS ScheduleType, 5 AS Rn 
UNION ALL       
SELECT 'Meeting' AS EventSchedule,'Type3' AS ScheduleType, 6 AS Rn        
UNION ALL
SELECT 'Appointment' AS EventSchedule,'Type1' AS ScheduleType, 7 AS Rn
UNION ALL         
SELECT 'Appointment' AS EventSchedule ,'Type2' AS ScheduleType, 8 AS Rn      
UNION ALL  
SELECT 'Appointment' AS EventSchedule,'Type3' AS ScheduleType, 9 AS Rn    
UNION ALL
SELECT 'Presentation' AS EventSchedule,'Type1' AS ScheduleType, 10 AS Rn
UNION ALL         
SELECT 'Presentation' AS EventSchedule,'Type2' AS ScheduleType, 11 AS Rn
UNION ALL        
SELECT 'Presentation' AS EventSchedule,'Type3' AS ScheduleType, 12 AS Rn         
, myCTE AS (
SELECT EventSchedule,ScheduleType, Count
, Case WHEN EventSchedule='Conference' AND ScheduleType='Type1' THEN 1
       WHEN EventSchedule='Conference' AND ScheduleType='Type2' THEN 2
       WHEN EventSchedule='Conference' AND ScheduleType='Type3' THEN 3
       WHEN EventSchedule='Meeting' AND ScheduleType='Type1' THEN 4
       WHEN EventSchedule='Meeting' AND ScheduleType='Type2' THEN 5
WHEN EventSchedule='Meeting' AND ScheduleType='Type3' THEN 6
WHEN EventSchedule='Appointment' AND ScheduleType='Type1' THEN 7
WHEN EventSchedule='Appointment' AND ScheduleType='Type2' THEN 8
WHEN EventSchedule='Appointment' AND ScheduleType='Type3' THEN 9
WHEN EventSchedule='Presentation' AND ScheduleType='Type1' THEN 10
WHEN EventSchedule='Presentation' AND ScheduleType='Type2' THEN 11
WHEN EventSchedule='Presentation' AND ScheduleType='Type3' THEN 12
END AS NewRn
FROM Sample
SELECT mlc.EventSchedule,mlc.ScheduleType,ISNULL(mc.Count,0) AS Count,mlc.Rn
FROM myListCTE mlc
LEFT JOIN myCTE mc on mc.NewRn=mlc.Rn
ORDER BY mlc.Rn
Thanks!
sql

Similar Messages

  • What Message type and Process Code to use for Purchase Order Change in 4.6C

    I like to implement in SAP R/3 4.6C a scenario for Purchase Order Change, via inbound IDoc.
    I have been searching, but could not find what Message Type, Idoc Basic Type and Process Code to use.
    I can not imagine this functionality does not exist in 4.6C.
    Any suggestions for my problem are welcome.

    Hello,
    In this case you should use the message PORDCH and BAPI method BAPI_PO_CHANGE but only exist as of release 4.7.
    Function module    BAPI_IDOC_INPUT1
    Idoc Type              PORDCH01
    Message Type      PORDCH
    So check carefully the following OSS notes:
      [Notes 589066 - BAPI_PO_CHANGE: Incorrect account assignment to network |https://service.sap.com/sap/support/notes/589066] (not be fooled by the description)
    [ Notes 197958 - BAPIs for purchase orders - Missing Functions|https://service.sap.com/sap/support/notes/197958]
    Regards,
    Andrea
    Edited by: Andrea Olivieri on Sep 3, 2009 10:17 AM

  • How to obtain system form's type and count?

    Hi all
    i wish to open a system form by calling the GetForm method of the Forms collection.
    My question is how do i know the form's type and count in order to get it?
    appreciate the help
    Yoav

    To open Activity form, you need to have the menuUID.
    in order to get it , Click on View of the toolbar, and then System Information
    (or CtrlShiftD)
    Now I guess you know you have more information about items in forms and menu on the bottom left corner of Business One.
    Then in the toolbar, click on Modules, Business Partnet, and put the mouse on the menu Activty.
    You will see in the bottom left corner the value:Menu Uid = 2563
    HTH
    Sebastien

  • Write / store xml data in Xe and retrieve stored data using pl/sql

    Hi to all,
    i'm searching a tutorial on:
    A - how to write / store xml data in Xe and retrieve stored data using pl/sql
    I don't want to use other technologies, because i use htmldb and my best practice is with pl/sql.
    I was reading an ebook (quite old maybe) however it's about oracle 9 and it's talking about xmltype:
    1 - I don't understand if this is a user type (clob/varchar) or it's integrated in Oracle 9 however i will read it (it's chapter 3 titled Using Oracle xmldb).
    Please dont'reply here: i would be glad if someone can suggest me a good tutorial / pdf to achieve task A in Oracle XE.
    Thanx

    Thank you very much Carl,
    However my fault is that i've not tried to create the table via sql plus.
    Infact i was wrong thinking that oracle sql developer allows me to create an xmltype column via the create table tool.
    however with a ddl script like the following the table was created successfully.
    create table example1
    keyvalue varchar2(10) primary key,
    xmlcolumn xmltype
    Thank you very much for your link.
    Message was edited by:
    Marcello Nocito

  • How can one  read a Excel File and Upload into Table using Pl/SQL Code.

    How can one read a Excel File and Upload into Table using Pl/SQL Code.
    1. Excel File is on My PC.
    2. And I want to write a Stored Procedure or Package to do that.
    3. DataBase is on Other Server. Client-Server Environment.
    4. I am Using Toad or PlSql developer tool.

    If you would like to create a package/procedure in order to solve this problem consider using the UTL_FILE in built package, here are a few steps to get you going:
    1. Get your DBA to create directory object in oracle using the following command:
    create directory TEST_DIR as ‘directory_path’;
    Note: This directory is on the server.
    2. Grant read,write on directory directory_object_name to username;
    You can find out the directory_object_name value from dba_directories view if you are using the system user account.
    3. Logon as the user as mentioned above.
    Sample code read plain text file code, you can modify this code to suit your need (i.e. read a csv file)
    function getData(p_filename in varchar2,
    p_filepath in varchar2
    ) RETURN VARCHAR2 is
    input_file utl_file.file_type;
    --declare a buffer to read text data
    input_buffer varchar2(4000);
    begin
    --using the UTL_FILE in built package
    input_file := utl_file.fopen(p_filepath, p_filename, 'R');
    utl_file.get_line(input_file, input_buffer);
    --debug
    --dbms_output.put_line(input_buffer);
    utl_file.fclose(input_file);
    --return data
    return input_buffer;
    end;
    Hope this helps.

  • Missing images and not able to use copy and paste anymore - just blank rect

    Hello!
    I've a problem that really scares me.
    I use severall png and pdf images. But at one point keynote looses the media and just displays these grey rectangles instead of some images. I tried to figure out if it just happens with pdf images and replaced the missing pdf images through png24 images with transparency. It first worked fine (they showed up) and then it tells images can not be saved. If I open the presentation again, the images are missing. And it seems that the more images and slides i put in the presentation, the more images are missing…
    But there is more: I even can't copy and paste these images anymore. If I try to copy one of the images I just have replaced (because they have been lost by saving), it just pastes the blank grey rectangle instead of the image.
    This really is a problem because I now have to repetitive tasks (like same animation for the same image in the same size) over and over again. And when I save the doc – they are al gone again…
    It really scares me and I don't have an idea how to solve this.
    It also happens, if I repleace an pdf image with an png image and than try to copy this new png image. However I am able to drag the image from the Finder again.
    I am working on a PB G4, 1.5 GHz, 1 GB RAM.
    My Keynote Version is 3.0.2.
    The Keynote Dokument is saved and opened from a G4 X-Serve wich is only used by Macs (so no Windows Filesystem anywhere). I still have anough space on both, my local and the server volume. I don't transfer the Keynote document. So it should not be a problem with the names on the files… And the files are just exported from Adobe Illustrator (png 24 files with "save for web" and the pdf files are saved as Illustrator pdf) – so they should really be fine and not corrupted…
    At this time the Keynote document has a size of 179 MB (because of a video) and only 16 slides. And the theme was built from scratch – so no import from Powerpoint or so…
    I looked through the Keynote preferences and found a thing like (sorry self-translated from german version to english) "scale placed images to slide-size". I wondered if keynote saves a copy of the sacaled image after that and corrupts the image itself? But I don't think so.
    I've had these problems with earlier versions of Keynote, but it occured just sporadically and I could solve it through pasting it again. But this time I can not solfe it anymore.
    Does anybody encounter same problem? Or has a hint?
    I really would appreciate, because I have to finish the presentation very soon and are not able to work properly with that. Furthermore, I am scared of saving and opinging the presentation again, because the lost files can not be replaced…

    I now have a hint:
    As a colleague said, he encountered some problems with usagerights on our fileserver (XServe). He said he wasn't able to read files he put on the server himself again…
    If this is true, the same could have happened to Keynote for placed images, as Keynote writes these images as separate files in the doc package…
    I will try to work on my local volume and see if it solfes the problem.
    But sooner or later I will have to place the whole thing on the server again…
    Do you think this is the reason?

  • Not showing missed calls and dropping calls

    I have had a Thunderbolt for several months now and I have had a problem with missed calls not showing up and voice mail showing up with missed calls, I have talked to tech and reported  this and I received a refurbished phone and still have the same problem any know issues with this?

    Hi bbarront,
    I apologize for the delayed response and its unfortunate you are still missing calls with the replacement device.  Can you please advise if you have taken advantage of the newest Software Update? Additionally, are you advising that you don't receive a notification of a missed call and the only way you realize it is when you check your voicemail?  I look forward to hearing from you soon.
    Thank you for your involvement in our community forums,

  • Elitepad 900 tablet shows missing serial and product info when booting

    Hi,
    i recently updated my tablet from windows 8 to windows 8.1 and then updated bios. after this upgrade on boot screen it shows missing aerial number and product info. please note that i have not changed the board or any other hardware. please help me to resolve this.
    regards
    Raees

    Hello Malik-Raees,
    I am sorry, but to get your issue more exposure I would suggest posting it in the commercial forums since this is a commercial product. You can do this at http://h30499.www3.hp.com/hpeb/ .
    I hope this helps!
    Best regards,
    I worked on behalf of HP.

  • Set default font type and size in formcreated using Acrobat X Pro

    I have a word document that I used Acrobat X Pro to create a PDF and then a Form.  The Form has over 200 text fields.  Is there a way to globally set the font type and size to one type, for instance Arial 10 so that when someone opens the Form and fills it in, the global font type and size is preset         

    Are you talking about fields that you create manually or field created with the Form Wizard? Exactly what version of Acrobat are you using? What OS?
    In any case, using a script is a quick and easy approach:
    for (var i = 0; i < numFields; i++) {
        var fName = getNthFieldName(i);
        var f = getField(fName);
        if (f.type === "text") {
            f.textFont = font.Times;
            f.textSize = 9;
            // Other properties go here
    I use this type of thing all the time to quickly alter a form. Such a script isn't added anywhere, just executed (e.g., JavaScript console). Over time a developer can build up a collection of utility scripts that can do all sorts of things to dramatically speed up development. It would be bad if this option were not available.
    If there is a bug, it should certainly be addressed, so don't think I'm arguing against that, but I'm not seeing it on any of my systems. I can successfully set the default field properties by selecting "Use Current Properties as New Defaults". The Form Wizard does not use these defaults, but that's a different matter.

  • ISU Change Register Group and Rate type and Rate fact Group using BAPIs

    Hi,
    Can anyone please help me on how to use ISU_O_DEVICEMOD_OPEN and ISU_S_DEVICEMOD_CHANGE for changing the Register Group and Rate type and Rate fact Group.
    Thanks,
    John

    Hello,
    Can an expert please reply to this thread? I have the same issue.
    Thanks,
    Andre

  • My elitepad 900 tablet shows missing serial and modle # after bios uptae

    My tablet started to show the missing serial and product number after i have upgraded the bios to the latest version.  now my windows 8.1 is asking to reactivate windows.
    i have not changed the board or any other hardware update. what i did is only drivers and bios updates after i upgraded dro windows 8 pro to to windows  windows 8.1 pro.
    please help to get it resolved.
    thanks
    Raees

    Hello Malik-Raees,
    I am sorry, but to get your issue more exposure I would suggest posting it in the commercial forums since this is a commercial product. You can do this at http://h30499.www3.hp.com/hpeb/ .
    I hope this helps!
    Best regards,
    I worked on behalf of HP.

  • Set Aggregation type of Count Distinct to use correct table aggregation in

    Hi there,
    Currently I use OBIEE 10.1.3.4.1 , and there is a case where a fact table consist of 2 logical table source: detail and aggregate table, which has some measure using count distinct as aggregation type. The problem is everytime I browse the measure with no dimension at all , it always use detail table not aggegation one..
    Really appreciate for any suggestion ..
    thanks a lot

    Hi,
    I don't think it's the same case as mine. Let say I have 2 table : detail and aggegate
    Detail Table consists 4 fields:
    *) Period
    *) Market
    *) Region
    *) Measure : Customer ID, Sales
    Aggregate Table consists 3 fields :
    *) Period
    *) Region
    *) Measure : Customer ID, Sales
    in the measure I set aggregation type for each field:
    *) Sales >> set as Sum
    *) Customer ID >> copy as "Number of Customer" and set as Count Distinct
    In each LTS' contents I set the level of aggregation using "Get Levels" feature..
    Then I try to browse via Presentation and do some querys belows:
    a) only choose single field of measure : Sales, the session shows that the value is taken from aggregation table and just as I expected.
    b) choose period and sales, the session shows that the values are taken from aggregation table, and still just as I expected.
    c) choose period, sales , and market, the session shows that the values are taken from detail table, just as I expected.
    d) only choose single field of measure : "Number of Customer", the session shows that the value is taken from detail table , this is NOT as I expected. It suppose to take the value from aggregation table..
    e) choose period and "Number of Customer", the session shows that the value is taken from detail table , this is also NOT as I expected. It suppose to take the value from aggregation table..
    I've tried to override the aggregation , but still confuse how to apply in measure "Number of Customer" and did not work at all..
    any idea ?
    thanks a lot

  • Not able to open a PDF file in Creative Cloud Illustrator. It shows missing links and so forth

    I'm working on CS5 and saving my AI files as PDF. This is all for print, I have created the outlines, and flattened transparency between an image I had placed in on the PDF. My CreativeCLoud AI is not able to open the PDF. It shows that it is missing links. How do I fix this?

    Provide the files linked to.

  • What file type and codec should I use for creating a file to send to have an HDCAM copy made

    I'm prepping to take a video file of my film into a tape dubbing house to have an HDCAM copy of my project made. I'm using a PC, so Apple ProRes is not an option. What file type, codec, and settings do you recommend?
    I was considering MXF, but the highest quality codec is XDCAM 50, which only does up to 50Mbps (I think). I believe HDCAM works at 140Mbps, so I would rather not give them a file that's compressed to almost 1/3rd that, especially since the source is a 5K 5120x2560 project. DPX maybe? I've never exported DPX before because it's not an option on my copy of CS6 - maybe that has to be downloaded separately? I was considering a Quicktim in H.264 at 140Mbps, but I think that would mean some serious rendering for the tape house. I would like to be able to offer them a few different options and see what they prefer, but this is an area where my knowledge runs thin. Any suggestions would be much appreciated. I'm hoping there's an industry standard that I can stick to.

    DPX maybe? I've never exported DPX before because it's not an option on my copy of CS6 - maybe that has to be downloaded separately?
    Your CS6 'Format' dropdown list should include DPX:
    Among the options you listed, DPX would be my choice.
    Be certain the dupe house can accept these files before proceeding.

  • Missing Camera and Exposure information by using Aperture 2

    I am using Aperture 2 and .mac web gallery. However, I note that the Camera and exposure information are missing for photos if I use the Aperture 2 for uploading my photos. However, the Image and File information are okay.
    By using the iPhoto, the Image, File, Camera and Exposure information can be shown in my .mac web gallery. Which setting should I use in Aperture 2 to allow me to upload the photo information in my .mac Web gallery? Thanks.

    In the Web Album settings you will need to change the settings to upload "Optimized Image" in order to get metadata included in the images you upload to .mac.
    You current settings only upload small JPEG versions of your images.
    Please, note that the upload time will increase significantly when you change to "Optimized Images" since they are much larger.
    Karsten

Maybe you are looking for

  • Adobe Photoshop Elements 10 activation issue on MAC running Parallels Desktop for Windows 7

    I have a MacBook Pro laptop running both OSX and Windows 7 (using Parallels Desktop). I installed the MAC version of Adobe Photoshop Elements 10 when I booted in the MAC OSX mode. I also Installed Adobe Photoshop 10 in the Windows 7 mode (running as

  • Is it possible to replace my iPhone 4 to one without the newest iOs 5.01 update?

    Since going to the APPLE store and being told yesterday that the 50.1 update was the best update ever and trusting an APPLE employee. I've had nothing but problems, my iPhone's battery drains faster. Turning brightness low, turning everything off. Th

  • Daylight Savings Time for the USA

    I am trying to add a new entry for the new daylight savings time change implemented in the United States in a 4.6C system.  I am trying to make the change per SAP note 919538.  The problem is when I go into transaction STZBC, and try to add a new ent

  • Automating repetitive queries

    Hi, I'm quite new to Oracle PL/SQL and I'm using Oracle SQL Developer to write some queries. I am often asked to run a query for a list of values and output each set of results individually. For example: Select Count(*) from ORDERS where ZIPCODE='XXX

  • Appraisal Report

    How do everyone! I am trying to create an excel spreadsheet containing appraisal statistics. I have fallen over at the first hurdle. I looked at the table PA0024 (i.e.infotype 0025) and it is completely empty. Where does one find the data???? Does an