Posted by Irfan Munir on July 7, 2008
Posted in Uncategorized | No Comments »
Posted by Irfan Munir on July 5, 2008
<br /> Shahed Khan (MVP)<br />
<!–
//
<!–
//
posts - 213, comments - 140, trackbacks - 68
|
||
ASP.NET tips: Golden rules for Dynamic Controls.1. Make sure your dynamic controls are Loaded on every postback. Lets play with a very simple example, ASPX <%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”Default.aspx.cs” Inherits=”_Default” %> <body> C# Code Behind public partial class _Default : System.Web.UI.Page } The above code works fine, but a common mistake is to try to conditionally load dynamic controls, if we tweak the code a little bit you will notice we loose our TextBox after any postback. The following code will not load the TextBox after our first postback. public partial class _Default : System.Web.UI.Page } Its recommended to load the dynamic controls during the Page_Init instead, because we may want to hook up our events with proper handler at an early stage. public partial class _Default : System.Web.UI.Page } 2. Do not assigning properties of a dynamic control (viewstate enabled), during Page_Init, it will not be reflected. Here is scenario of another common mistake, “123″ assigned to the Text property during Page_Init, public partial class _Default : System.Web.UI.Page } the above code will not work because, Initialization happens before LoadViewState during the control lifecycle. The value assigned to the properties during Initialization will simply get overwritten by the ViewState values. 3. If you are expecting your ViewState to retain after the postback, always assign same ID to the dynamic control The following piece of code will not work, as I am assigning a new ID to the dynamic control after each postback. The LoadViewState retrieves previously saved viewstate data using the control ID, as the control ID has changed, it doesn’t know anymore what to load, as a result it cannot load previously saved viewstate data any more. public partial class _Default : System.Web.UI.Page Thank you for being with me so far. posted @ Thursday, June 26, 2008 2:36 AM | Feedback (0) | ASP.NET tips, Making Custom Validators work in Partial Rendering mode.Introduction The ASP.NET Page class exposes the Validators property, which is a list of all the IValidator types on the page. A page keeps track of its validators, and registers a javascript array of validators automatically to the page. Example, When we add 3 RequiredFieldValidator in a page the following javascript Array will be automatically generated and added in our page automatically during the page load. Page_Validators = new Array(document.getElementById(”RequiredFieldValidator1″), The ASP.NET Page also registers couple of other script which eventually hooks up different events ( onclick, onkeypress, onchange, onblur ) to the the target control (ControlToValidate), to some predefined javascript functions that resides in WebUIValidation.js file. So when we add a validator in our Page we also notice the following script is automatically added. [WebUIValidation.js ships with ASP.NET and resides in the following folder "/aspnet_client/system_web/<version>/WebUIValidation.js".] <script type=”text/javascript”> function ValidatorOnSubmit() { ValidatorOnLoad plays the big role of hooking up the the events mentioned above, and here is a code snippet from this function, for (i = 0; i < Page_Validators.length; i++) { if (typeof(val.controltovalidate) == “string”) { keen eyes may have already noticed the val.evaluationfunction property, yes every validators needs to have this property for it to work properly under the ASP.NET validation framework. Custom validators takes advantage of this property to point to custom js functions. Custom validator developers normally use RegisterExpandoAttribute method to register this attribute. protected override void AddAttributesToRender(System.Web.UI.HtmlTextWriter writer) Problem Solution protected override void AddAttributesToRender(HtmlTextWriter writer) and code snippet from BaseValidator, the internal method AddExpandoAttribute. internal void AddExpandoAttribute(HtmlTextWriter writer, string controlId, string attributeName, string attributeValue, bool encode) After digging further I realized, AddExpandoAttribute checks the ASP.Page whether partial rendering is supported, then it registers the attribute using ScriptManager instead. I did the same with my validation control and it works for me. Here is the piece of code that solved my problem. protected override void AddAttributesToRender(System.Web.UI.HtmlTextWriter writer) Note, the I am first checking whether Partial Rendering is Supported and using the ScriptManager Type to register the property instead. The following piece of code uses Reflection to figure out whether partial rendering is supported. internal bool IsPartialRenderingSupported } private bool PartialRenderingChecked private bool IsPartialRenderingEnabled Conclusion The Page.ClientScript.RegisterExpandoAttribute may not work in Partial Rendiring mode, when a postback is performed via triggering, Hope this helps, and saves some of your time, Thank you for being with me so far. posted @ Tuesday, June 17, 2008 5:43 AM | Feedback (0) | C# 3.0 tips, Automatic PropertyDeclaring a property in C# 3.0 is super easy and super short. public class Student yes that’s it, the framework will take care of the rest, the private variables will be automatically created and the getter and setter will be automatically implemented. Here is how we can assign value to an automatic property via the constructor public class Student this.Name = name; } And finally, here is how we can declare a Readonly property public class Student public string Name { get; private set; } public Student (string name) this.Name = name; } Hope this helps, Enjoy coding. posted @ Wednesday, June 11, 2008 12:21 AM | Feedback (0) | System.Net.WebClient().DownloadString(url) for Web ScrapeingWebRequest is the abstract base class for the .NET Framework’s request/response model for accessing data from the Internet. To get content of a website, in .NET 1.0. we used to use WebRequest, which is good and also works asynchronously. public static string GetContent(string url) But in .NET 2.0, we can also use the WebClient class. It can also work asynchronous and works the same as the other one. public static string GetContent(string url) We can use any of the above method for web scrapeing in .NET. But the second approach is probably more cleaner. posted @ Monday, June 09, 2008 3:28 PM | Feedback (0) | Project SOAK winner of 2008 Imagine Cup Australia.The theme of this year’s Imagine Cup is “Imagine a world where technology enables a sustainable environment.” It had been a great honorary for me to take part as one the Judges of the 2008 Imagine Cup Australia. All the teams worked real hard and came up with breathtaking solutions. The interesting part was solutions were built on top of cutting edge technologies technologies ie. Silverlight, Virtual Earth, LINQ, ASP.NET 3.5, WCF, .NET 3.5 and SQL2005. The Project SOAK is announced the winner of Australian Imagine Cup 08. SOAK which stands for “Smart Operational Agriculture toolKit” is an integrated hardware and softwares platform that aims to help farmers make the most of the water (and other) resources on their land. It does this through an integration of a wide range of sensors which gathers data about the environment in real time, provide rich visual information to end-user about the status of the farm, and electronically controls various systems such as sprinklers. The team members of SOAK blogged more about the project, can be found here http://davidburela.wordpress.com/2008/05/22/2008-imagine-cup-australian-winner-project-soak/ http://www.istartedsomething.com/20080523/imagine-cup-australian-winner-project-soak/ Australian PC Magazine has just published published a very insightful story on this project and the competition
I found SOAK to be a brilliant piece of work, where the team combined latest technologies together and came up with a environment friendly cost effective solution. The winning team will now represent Australia at the World Imagine Cup finals in Paris in July.
The Imagine Cup’s judging panel consisted of Roger Lawrence, Microsoft Australia’s Manager Developer Evangelism; Nigel Watson, Microsoft Australia Architect Evangelist; Shekhar Kalra, computer science lecturer at RMIT University; Shahed Khan, Senior Software Engineer at Ocean Informatics MVP C#.NET, and APC, represented by its editor, Tony Sarno. posted @ Friday, May 23, 2008 11:41 PM | Feedback (0) | ASP.NET Bug, Multi View control do not save ViewState, of dynamically added controlsCouple of days back me and my colleague, we discovered an issue with the ASP.NET Multi View Control. ASP.NET 2.0 <asp:MultiView ID=”MultiView1″ runat=”server” EnableViewState=”true”> C# Code Behind if (!IsPostBack) Surprisingly, you will notice only the Textbox.Text of the Active Tab will have value, After investigating further we realized that the Viewstates of the dynamically added controls are not saved (for any of the inactive tabs). Not sure whether its a bug, the ASP.NET team may have wanted this behavior to enhance performance of the Multi View control, but if that is the case, why does it populate the TextBox.Text and also saves into Viewstate, when we try to debug !!! Do not believe me? Try it by yourself !! posted @ Saturday, May 03, 2008 3:31 AM | Feedback (1) | ASP.NET in VISTA ( IIS7 ) with VS2005 or VS2008The following 2 links may help. VS 2005: http://learn.iis.net/page.aspx/431/using-visual-studio-2005-with-iis-70/ posted @ Monday, April 28, 2008 4:13 PM | Feedback (0) | ASP.NET Tips: Using Image as Embedded Resource for ASP.NET CustomControlProblemI started writing an ASP.NET Custom Server Control, where I wanted an Image to be Embedded Resource of the Assembly itself, The following line was not working for me: Ok, lets elaborate what I did and what I missed, Step 1, I created my ClassLibrary project, added an Image, added a Custom Control class. Step 2, Made the image an embedded resource of the Assembly. Step3, Written my very simple Custom Control, where I assigned the image “src” to the WebResource URL Step 4, Then I wanted to tryout this CustomControl in my Test Website Step 5, But I got the following result. SolutionAfter investigating a bit, I realized I missed some critical bits. 1. I did not put the correct Resource URL. I discovered this by opening up the assembly via Reflector, I found that the resource URL is different than what I have put in my code. I corrected the resource URL in my code, (but still it did not work). writer.AddAttribute(HtmlTextWriterAttribute.Src, 2. I investigated further and found that I did not explicitly declare the image as WebResource in my assembly info . To get the embedded resource bit working, the following line is very important, and this solved my problem. [assembly: System.Web.UI.WebResource("MyControls.images.ferrari.jpg", "image/jpg")] Note: We can also put this directly in the class file itself. After the fix I got the following result as I have desired. SummaryI have discussed here, how to embed image in an Assembly and how to use it as WebResource. Two points to note here, which are I hope this discussion will save you some time. Thank you for being with me so far. posted @ Saturday, April 12, 2008 8:45 PM | Feedback (0) | DataTable to JSON and ToJSON() ExtensionVery recently I wrote an application where I had to deal with DataSet from a Web Service. Please note, I have no control on the Web Service and I ended up writing a small function which converts DataTable to JSON. I understand I haven’t gain anything on the web traffic, but it surely simplified my JavaScript programming. Let me go through what I did Step 1. Extract the XML Schema. string path = “Your File Path”; Step 2. Generate C# Class using Xsd.exe that ships with the .NET Framework. C:\temp>xsd mydatatable.xsd /l:cs /c Step 3. DataTable to Object conversion The Web Service returns DataSet/ DataTable, and I want to transform all data that I I receive in the DataTable, to an instance of the class that I just generated in the above step. Something like this: private T DataTableToT<T>(DataTable dataTable, T obj) The above method uses the WriteXml() to write the data of DataTable in to a MemoryStream, then using the XmlSerializer I deserialize the xml to a .NET object. Here is how we may use the this method: DataSet ds = WebService.GetDataSet(); Step 4. Serialize .NET object to JSON JavaScriptSerializer ships with System.Web.Extensions.dll and you can locate it under Namespace: System.Web.Script.Serialization. The following method returns JSON from a .NET object using JavaScriptSerializer. private string GetJSONUsingJavaScriptSerializer<T>(T obj) DataContractJsonSerializer also does pretty much the same as above, it ships with .NET Framework 3.5 : System.ServiceModel.Web.dll, and you can locate this under Namespace: System.Runtime.Serialization.Json, But we need to decorate the class with DataContract and DataMember attributes. Example [DataContract] and the following method can return a JSON string. private string GetJSONUsingDataContractJsonSerializer<T>(T obj) Conclusion Example: public static string ToJSON<T>(this T obj) and then we will be able to use it like this on a order collection, string json = orders.ToJSON(); Hope this helps. posted @ Saturday, March 22, 2008 5:31 PM | Feedback (1) | Visual Studio 2005 Debugging in VistaProblem: I was trying to debug in Visual Studio 2005 in my new machine which came with Vista Home Premium. Surprisingly I found that none of the break points are touched and I cannot debug. I tried giving all types of permission to the folder but no luck. Solution: After googling a bit, I found that I needed to run VisualStudio2005 as an Administrator, and everything started working as I expected. So all I had to do is :
Hope this saves some of your time. posted @ Tuesday, March 18, 2008 2:39 PM | Feedback (1) | Javascript Tips: Carefully use “this” when writing classes, else you may cause memory leak.Lets say we want to declare a class in Javascript, which is equivalent to the following C# class. public class Student { public string FirstName = ""; public string LastName = ""; public Student( string firstname, string lastname) { this.FirstName = firstname; this.LastName = lastname; } public string GetFullName() { return FirstName + LastName; } } <!– To write a similar class in JavaScript we can do something like the following [ but this will create memory leak, I am explaining that in a moment ] function Student ( firstname, lastname) { this.FirstName = firstname; this.LastName = lastname; this.GetFullName = function() { return this.FirstName + this.LastName; } } now in C# if we want to instantiate an object of Student and want to call the GetFullName() method, we do the following. Student student = new Student("Shahed", "Khan"); string fullname = student.GetFullName(); <!– and we can create as many objects as we want and call its methods, each of the object will maintain its own state, and all objects will use the same copy of the GetFullName() method. But Javascript has different behaviour when we do the following on the above Javascript class. var student = new Student("Shahed","Khan"); car fullname = student.GetFullName(); <!– Do not worry too much, there is a workaround for this, lets redefine the class in a different way. function Student ( firstname, lastname) { this.FirstName = firstname; this.LastName = lastname; this.GetFullName = GetFullName; } function GetFullName() { return this.FirstName + this.LastName; } <!– Notice I have moved the GetFullName function out of the class, and for this tweaking all new objects of the Student class will share the same instance of of GetFullName method and avoid memory leak. Thank you for being with me so far. Updated 24th Feb =============== Laurent from Galasoft gave some good feedback, JavaScript object oriented should be done by modifying the prototype property of the object, and never by storing methods using the “this” keyword. The workaround provided above is not good practice, as it forces the use of a global function. We should always declare methods in JavaScript object like this: function Student(firstName, lastName) { this.firstName = firstName; this.lastName = lastName; } Student.prototype = { getFullName : function() { return this.firstName + ” ” + this.lastName; } } also note correct naming convension, ( Javascript follows Java notation not C#). For JavaScript best practices please refer to the work of Microsoft Silverlight team. posted @ Thursday, February 21, 2008 5:38 PM | Feedback (1) |
Solving DNN deployment issues, Redirecting to localhost and Running DNN in a different portI was trying to host a small DNN application in one of our Server and I was facing couple of issues. Problem 1: Solution This was easy to solve. This solved my problem when I hosted the site in port 80. Problem 2: Now I tried to host the application in a different port 8080. I.e. http://domain.com:8080/dnn. and somehow when I clicking to redirect to any other page the port started to disappear. The http://domain.com:8080/ automatically turned to http://domain.com/ . Solution After googling and looking at the web.config carefully I found, its clearly documented in web.config that <!– set UsePortNumber to true to preserve the port number if you’re using a port number other than 80 (the standard) I tweaked my appsettings section and added the magic key <add key=”UsePortNumber” value=”true” /> Also I had to add a new Http Alias “domain.com:8080″ This solved my problem and started retaining the port for my http://localhost:8080 but not http://domain.com:8080. The http://domain.com:8080 was still turning to http://domain.com Note: I later discovered this was not a problem of DNN and the issue happened because of the setup of our router settings and port forwarding, which I’ll discuss next. Problem 3: Solution Our Router was Port Forwarding all traffic of 8080 to the port 80 of the machine where DNN app is hosted. I.e. 8080 –> 80. As a result even from a browser I as typing http://domain.com:8080 , the DNN Request object was getting http://domain.com and when DNN handlers and url rewriters spitting the reformatted url it was spitting http://domain.com. This was a big problem for me, initially I thought I would write a HttpHandler for 404 page not found, but soon realized it will never hit the server with the spitted Url so that didn’t work. Then I thought I would tweak the DNN handlers to handle this scenario, but later tweaked the IIS and Router to handle this. 1. In IIS I added, support for 8080 to my Default Website. 2. In Router instead of forwarding to port 80 I started forwarding 8080 to 8080. Waaa la, This solved my issue. Hope this helps and Thank you for being with me so far. posted @ Tuesday, February 19, 2008 5:14 PM | Feedback (2) | Dotnet Nuke Tips: Two common error while writing the SqlDataProviderTwo common errors done while writing the SqlDataProvider SQL for Dotnet Nuke Modules are 1. Not saving the file that contains SqlDataProvider SQL codes in the correct format. A quick trick is to open the files in NotePad and save them as “Unicode”. 2. Not putting atleast 2 line breaks after each GO statement in the SQLDataProvider SQL code. Hope this helps. posted @ Thursday, February 14, 2008 1:00 PM | Feedback (0) | LINQ Tips: Implementing IQueryable ProviderCheck out the following from Matt Warrens blog posts, if you are interested on how to implement IQueryable Provider. source: http://blogs.msdn.com/mattwar/archive/2007/07/30/linq-building-an-iqueryable-provider-part-i.aspx Part I - Reusable IQueryable base classes posted @ Wednesday, February 06, 2008 5:50 PM | Feedback (0) | LINQ Tips: Querying ArrayList via LINQProblem ArrayList students = GetStudents(); Cause Solution Here is the signature of the Cast operator: Cast can take a nongeneric IEnumerable and returns a generic IEnumerable<T>. ArrayList students = GetStudents(); posted @ Tuesday, February 05, 2008 4:30 PM | Feedback (1) | |
||
|
Powered by: Copyright © Shahed Khan |
||
<!–
//
Posted in General Concepts | No Comments »
Posted by Irfan Munir on June 12, 2008
borrowed from : http://www.samoratech.com/topicofinterest/swAnalyticalFuntions.htm#Ranked_func_bk
xmlns:o=”urn:schemas-microsoft-com:office:office”
xmlns:w=”urn:schemas-microsoft-com:office:word”
xmlns=”http://www.w3.org/TR/REC-html40″>
id=”_x0000_t136″ coordsize=”21600,21600″ o:spt=”136″ adj=”10800″ path=”m@7,0l@8,0m@5,21600l@6,21600e”>
o:connectangles=”270,180,90,0″/>
strokeweight=”1.5pt”>
fitpath=”t” string=”Analytical functions”/>
style=’mso-tab-count:13′>
style=’color:maroon’>SAMEER WADHWA
style=”mso-spacerun: yes”> Wadhwa_S@Hotmail.com
In this article I have
tried to aware you about some of the analytic functions provided by oracle
8i.These funtions are very powerful and ease to use.
name=”Rollup_and_cube_bk”>ROLLUP AND CUBE
AGGREGATE FUNCTIONS
To understand the power of ROLLUP and CUBE functions ,consider the
following SQL statement :-
ora816 SamSQL :> compute sum of totsal on deptno
ora816 SamSQL :> break on deptno
ora816 SamSQL :> select deptno,job,sum(sal) totsal from emp group
by deptno,job;
style=”mso-spacerun: yes”> DEPTNO JOB TOTSAL
———-
——— ———-
style=”mso-spacerun: yes”> 10 CLERK 1300
style=”mso-spacerun: yes”> MANAGER 2450
style=”mso-spacerun: yes”> PRESIDENT 5000
********** style=”mso-spacerun: yes”> ———-
sum style=”mso-spacerun: yes”> 8750
style=”mso-spacerun: yes”> 20 ANALYST 6000
style=”mso-spacerun: yes”> CLERK 1900
style=”mso-spacerun: yes”> MANAGER 2975
********** style=”mso-spacerun: yes”> ———-
sum style=”mso-spacerun: yes”> 10875
style=”mso-spacerun: yes”> 30 CLERK 950
style=”mso-spacerun: yes”> MANAGER 2850
style=”mso-spacerun: yes”> SALESMAN 5600
********** style=”mso-spacerun: yes”> ———-
sum style=”mso-spacerun: yes”> 9400
ora816 SamSQL :> select deptno,job,sum(sal) totsal from emp
group by ROLLUP(deptno,job);
style=”mso-spacerun: yes”> DEPTNO JOB TOTSAL
———-
——— ———-
style=”mso-spacerun: yes”> 10 CLERK 1300
style=”mso-spacerun: yes”> 10 MANAGER 2450
id=”_x0000_t88″ coordsize=”21600,21600″ o:spt=”88″ adj=”1800,10800″ path=”m0,0qx10800@0l10800@2qy21600@11,10800@3l10800@1qy0,21600e”
filled=”f”>
textboxrect=”0,@4,7637,@5″/>
style=’font-size:10.0pt;mso-bidi-font-size:12.0pt;font-family:”Courier New”;
color:#3366FF’> 10 PRESIDENT style=”mso-spacerun: yes”> 5000
style=”mso-spacerun: yes”> 10 8750 Total of Deptno 10
style=”mso-spacerun: yes”> 20 ANALYST 6000
style=”mso-spacerun: yes”> 20 CLERK 1900
style=”mso-spacerun: yes”> 20 MANAGER 2975
style=”mso-spacerun: yes”> 20 style=’font-size:10.0pt;mso-bidi-font-size:12.0pt;font-family:”Courier New”;
color:red’>10875
style=”mso-spacerun: yes”> 30 CLERK 950
style=”mso-spacerun: yes”> 30 MANAGER 2850
style=”mso-spacerun: yes”> 30 SALESMAN 5600
type=”#_x0000_t88″ style=’position:absolute;left:0;text-align:left;
margin-left:297pt;margin-top:5.55pt;width:9pt;height:18pt;z-index:6′>
style=’font-size:10.0pt;mso-bidi-font-size:12.0pt;font-family:”Courier New”;
color:#3366FF’> 30 style=”mso-spacerun: yes”> 9400
style=”mso-spacerun: yes”> style=’font-size:10.0pt;mso-bidi-font-size:12.0pt;font-family:”Courier New”;
color:red’>29025 Grand Total
So if you compare the two output you will notice that you are getting
the same output. By using rollup
you can avoid compute and break clausesfrom SQL. style=”mso-spacerun: yes”> This will mostly helpful in style=”mso-spacerun: yes”> PL/SQL
. We do not have to put logic
for computing values on break of groups.
ora816
SamSQL :> select deptno,job,sum(sal) totsal from emp group by
CUBE(deptno,job);
Fri Mar 23
style=”mso-spacerun:
yes”> NuGenesis
Report
style=”mso-spacerun: yes”> DEPTNO JOB TOTSAL
———-
——— ———-
style=”mso-spacerun: yes”> 10 CLERK 1300
style=”mso-spacerun: yes”> 10 MANAGER
2450
type=”#_x0000_t88″ style=’position:absolute;left:0;text-align:left;
margin-left:4in;margin-top:8.25pt;width:9pt;height:18pt;z-index:4′>
style=’font-size:10.0pt;mso-bidi-font-size:12.0pt;font-family:”Courier New”;
color:#3366FF’> 10 PRESIDENT style=”mso-spacerun: yes”> 5000
style=”mso-spacerun: yes”> 10 8750 Total of Deptno 10
style=”mso-spacerun: yes”> 20 ANALYST 6000
style=”mso-spacerun: yes”> 20 CLERK 1900
style=”mso-spacerun: yes”> 20 MANAGER 2975
style=”mso-spacerun: yes”> 20 10875
style=”mso-spacerun: yes”> 30 CLERK 950
style=”mso-spacerun: yes”> 30 MANAGER 2850
style=”mso-spacerun: yes”> 30 SALESMAN 5600
style=”mso-spacerun: yes”> 30 9400
type=”#_x0000_t88″ style=’position:absolute;left:0;text-align:left;
margin-left:4in;margin-top:2.95pt;width:18pt;height:54pt;z-index:2′>
style=’font-size:10.0pt;mso-bidi-font-size:12.0pt;font-family:”Courier New”;
color:#3366FF’> ANALYST style=”mso-spacerun: yes”> 6000 style=’font-size:10.0pt;mso-bidi-font-size:12.0pt;font-family:”Courier New”;
color:#3366FF’>
style=”mso-spacerun: yes”> CLERK 4150 style=’font-size:10.0pt;mso-bidi-font-size:12.0pt;font-family:”Courier New”;
color:#3366FF’>
style=”mso-spacerun: yes”> MANAGER 8275 Total w.r.t JOB
style=”mso-spacerun: yes”> PRESIDENT style=’font-size:10.0pt;mso-bidi-font-size:12.0pt;font-family:”Courier New”;
color:maroon’>5000
style=”mso-spacerun: yes”> SALESMAN 5600 style=’font-size:10.0pt;mso-bidi-font-size:12.0pt;font-family:”Courier New”;
color:#3366FF’>
id=”_x0000_t87″ coordsize=”21600,21600″ o:spt=”87″ adj=”1800,10800″ path=”m21600,0qx10800@0l10800@2qy0@11,10800@3l10800@1qy21600,21600e”
filled=”f”>
textboxrect=”13963,@4,21600,@5″/>
style=’font-size:10.0pt;mso-bidi-font-size:12.0pt;font-family:”Courier New”;
color:#3366FF’> style=’font-size:10.0pt;mso-bidi-font-size:12.0pt;font-family:”Courier New”;
color:red’>Grand Total 29025
Cube also do a total with respect to second group
for example JOB in our case . Also at end you will see the grand total
Conclusion : Rollup and
Cube are the aggregate function which allows developers and dbas to avoid
compute and break clauses and simplify logic of programming
name=”Ranked_func_bk”>Ranked Function in 8i (816) style=’font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:”Times New Roman”;
mso-fareast-font-family:”MS Mincho”;color:red’>
Suppose you have a data in table which you want to rank in a specified
order for example you have a table test and you want to rank a value of repcol.
style=’font-size:12.0pt;mso-bidi-font-size:10.0pt;mso-fareast-font-family:”MS Mincho”;
color:blue’>Ora816 SamSQL> select * from test;
REPCOL
VALUE
———-
———-
A
100
A style=”mso-spacerun: yes”> 200
A
300
B
1000
B
900
B
800
A
500
B
400
B
500
Ora816
SamSQL> select repcol,value, style=’font-size:11.0pt;mso-bidi-font-size:10.0pt;mso-fareast-font-family:”MS Mincho”;
color:red’>rank() over ( partition by repcol
style=”mso-spacerun: yes”> 2 style=”mso-spacerun: yes”> order
by value desc ) ranked_value
style=”mso-spacerun: yes”> style=”mso-spacerun: yes”> 3
from test;
REPCOL
VALUE RANKED_VALUE
———- ———- ————
A
500 1
A
300 2
A
200 3
A
100 4
B style=”mso-spacerun: yes”> 1000 1
B
900 2
B
800 3
B
500 4
B
400 5 style=’font-size:11.0pt;mso-bidi-font-size:10.0pt;mso-fareast-font-family:”MS Mincho”;
color:blue’>
The
above value is ranked by the rank function provided by 8.1.6 style=’font-size:12.0pt;mso-bidi-font-size:10.0pt;mso-fareast-font-family:”MS Mincho”;
color:blue’>
style=’font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:”Times New Roman”;
mso-fareast-font-family:”MS Mincho”;color:red’>Use of Case in SELECT style=’font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:”Times New Roman”;
mso-fareast-font-family:”MS Mincho”;color:red’>
Case Statement are similar to decode , it is more
flexible and gives better performace
Ora816
SamSQL> ed
Wrote
file afiedt.buf
style=”mso-spacerun: yes”> 1
select sum(case style=’font-size:11.0pt;mso-bidi-font-size:10.0pt;mso-fareast-font-family:”MS Mincho”;
color:blue’> when style=’font-size:11.0pt;mso-bidi-font-size:10.0pt;mso-fareast-font-family:”MS Mincho”;
color:blue’> repcol=’A’ then style=’font-size:11.0pt;mso-bidi-font-size:10.0pt;mso-fareast-font-family:”MS Mincho”;
color:blue’> value else style=’font-size:11.0pt;mso-bidi-font-size:10.0pt;mso-fareast-font-family:”MS Mincho”;
color:blue’> 0 end style=’font-size:11.0pt;mso-bidi-font-size:10.0pt;mso-fareast-font-family:”MS Mincho”;
color:blue’>
sum_of_A,
style=”mso-spacerun: yes”> style=”mso-spacerun: yes”> 2
sum(Case style=’font-size:11.0pt;mso-bidi-font-size:10.0pt;mso-fareast-font-family:”MS Mincho”;
color:blue’> when style=’font-size:11.0pt;mso-bidi-font-size:10.0pt;mso-fareast-font-family:”MS Mincho”;
color:blue’> repcol=’B’ then style=’font-size:11.0pt;mso-bidi-font-size:10.0pt;mso-fareast-font-family:”MS Mincho”;
color:blue’> value else style=’font-size:11.0pt;mso-bidi-font-size:10.0pt;mso-fareast-font-family:”MS Mincho”;
color:blue’> 0 end style=’font-size:11.0pt;mso-bidi-font-size:10.0pt;mso-fareast-font-family:”MS Mincho”;
color:blue’>
Sum_of_B,
style=”mso-spacerun: yes”> 3
sum(case style=’font-size:11.0pt;mso-bidi-font-size:10.0pt;mso-fareast-font-family:”MS Mincho”;
color:blue’> when style=’font-size:11.0pt;mso-bidi-font-size:10.0pt;mso-fareast-font-family:”MS Mincho”;
color:blue’>value = 500 then style=’font-size:11.0pt;mso-bidi-font-size:10.0pt;mso-fareast-font-family:”MS Mincho”;
color:blue’> 1 else style=’font-size:11.0pt;mso-bidi-font-size:10.0pt;mso-fareast-font-family:”MS Mincho”;
color:blue’> 0 end style=’font-size:11.0pt;mso-bidi-font-size:10.0pt;mso-fareast-font-family:”MS Mincho”;
color:blue’>
Value_Eq_500,
style=”mso-spacerun: yes”> 4
sum(case style=’font-size:11.0pt;mso-bidi-font-size:10.0pt;mso-fareast-font-family:”MS Mincho”;
color:blue’> when style=’font-size:11.0pt;mso-bidi-font-size:10.0pt;mso-fareast-font-family:”MS Mincho”;
color:blue’> value > 100 then style=’font-size:11.0pt;mso-bidi-font-size:10.0pt;mso-fareast-font-family:”MS Mincho”;
color:blue’>1 else style=’font-size:11.0pt;mso-bidi-font-size:10.0pt;mso-fareast-font-family:”MS Mincho”;
color:blue’> 0 end style=’font-size:11.0pt;mso-bidi-font-size:10.0pt;mso-fareast-font-family:”MS Mincho”;
color:blue’>
Value_Gre_100
style=”mso-spacerun: yes”> 5*
from test
Ora816
SamSQL> /
SUM_OF_A style=”mso-spacerun: yes”> SUM_OF_B VALUE_EQ_500 VALUE_GRE_100
———- ———- ———— ————-
1100 style=”mso-spacerun: yes”> 3600 2 8
Use of Group by in CASE
Ora816
SamSQL> ed
Wrote
file afiedt.buf
style=”mso-spacerun: yes”> 1
select repcol,sum(case when style=’font-size:11.0pt;mso-bidi-font-size:10.0pt;mso-fareast-font-family:”MS Mincho”;
color:blue’> repcol=’A’ then style=’font-size:11.0pt;mso-bidi-font-size:10.0pt;mso-fareast-font-family:”MS Mincho”;
color:blue’> value else style=’font-size:11.0pt;mso-bidi-font-size:10.0pt;mso-fareast-font-family:”MS Mincho”;
color:blue’> 0 end style=’font-size:11.0pt;mso-bidi-font-size:10.0pt;mso-fareast-font-family:”MS Mincho”;
color:blue’>
sum_of_A,
style=”mso-spacerun: yes”> 2
sum(Case when style=’font-size:11.0pt;mso-bidi-font-size:10.0pt;mso-fareast-font-family:”MS Mincho”;
color:blue’> repcol=’B’ then style=’font-size:11.0pt;mso-bidi-font-size:10.0pt;mso-fareast-font-family:”MS Mincho”;
color:blue’> value else style=’font-size:11.0pt;mso-bidi-font-size:10.0pt;mso-fareast-font-family:”MS Mincho”;
color:blue’> 0 end style=’font-size:11.0pt;mso-bidi-font-size:10.0pt;mso-fareast-font-family:”MS Mincho”;
color:blue’>
Sum_of_B,
style=”mso-spacerun: yes”> 3
sum(case when style=’font-size:11.0pt;mso-bidi-font-size:10.0pt;mso-fareast-font-family:”MS Mincho”;
color:blue’> value = 500 then style=’font-size:11.0pt;mso-bidi-font-size:10.0pt;mso-fareast-font-family:”MS Mincho”;
color:blue’> 1 else style=’font-size:11.0pt;mso-bidi-font-size:10.0pt;mso-fareast-font-family:”MS Mincho”;
color:blue’>0 end style=’font-size:11.0pt;mso-bidi-font-size:10.0pt;mso-fareast-font-family:”MS Mincho”;
color:blue’>
Value_Eq_500,
style=”mso-spacerun: yes”> 4
sum(case when style=’font-size:11.0pt;mso-bidi-font-size:10.0pt;mso-fareast-font-family:”MS Mincho”;
color:blue’> value > 100 then style=’font-size:11.0pt;mso-bidi-font-size:10.0pt;mso-fareast-font-family:”MS Mincho”;
color:blue’>1 else style=’font-size:11.0pt;mso-bidi-font-size:10.0pt;mso-fareast-font-family:”MS Mincho”;
color:blue’> 0 end style=’font-size:11.0pt;mso-bidi-font-size:10.0pt;mso-fareast-font-family:”MS Mincho”;
color:blue’>
Value_Gre_100
style=”mso-spacerun: yes”> 5
from test
style=”mso-spacerun: yes”> 6* group
by repcol
Ora816 SamSQL> /
REPCOL
SUM_OF_A SUM_OF_B VALUE_EQ_500
VALUE_GRE_100
———- ———- ———- ————
————-
A
1100 0 style=”mso-spacerun: yes”> 1 3
B
0 3600 style=”mso-spacerun: yes”> 1 5
Ora816
SamSQL> ed
Wrote
file afiedt.buf
style=”mso-spacerun: yes”> 1
select (case style=’font-size:11.0pt;mso-bidi-font-size:10.0pt;mso-fareast-font-family:”MS Mincho”;
color:blue’> when style=’font-size:11.0pt;mso-bidi-font-size:10.0pt;mso-fareast-font-family:”MS Mincho”;
color:blue’> value between 100 and 300 then style=’font-size:11.0pt;mso-bidi-font-size:10.0pt;mso-fareast-font-family:”MS Mincho”;
color:blue’> ‘100-300′
style=”mso-spacerun: yes”> 2
when style=’font-size:11.0pt;mso-bidi-font-size:10.0pt;mso-fareast-font-family:”MS Mincho”;
color:blue’> value between 400 and 700 then style=’font-size:11.0pt;mso-bidi-font-size:10.0pt;mso-fareast-font-family:”MS Mincho”;
color:blue’> ‘400-700′
style=”mso-spacerun: yes”> 3
when style=’font-size:11.0pt;mso-bidi-font-size:10.0pt;mso-fareast-font-family:”MS Mincho”;
color:blue’> value between 800 and 900 then style=’font-size:11.0pt;mso-bidi-font-size:10.0pt;mso-fareast-font-family:”MS Mincho”;
color:blue’> ‘800-900′
style=”mso-spacerun: yes”> 4
when style=’font-size:11.0pt;mso-bidi-font-size:10.0pt;mso-fareast-font-family:”MS Mincho”;
color:blue’> value > 900 then style=’font-size:11.0pt;mso-bidi-font-size:10.0pt;mso-fareast-font-family:”MS Mincho”;
color:blue’> ‘>900′ end style=’font-size:11.0pt;mso-bidi-font-size:10.0pt;mso-fareast-font-family:”MS Mincho”;
color:blue’>
VALUE_RANGE,
style=”mso-spacerun: yes”> 5
count(*) as VALUE_COUNT
style=”mso-spacerun: yes”> 6
from test
style=”mso-spacerun: yes”> 7
group by
style=”mso-spacerun: yes”> 8
(case when value between 100 and 300 then ‘100-300′
style=”mso-spacerun: yes”> 9
when value between 400 and 700 then ‘400-700′
style=”mso-spacerun: yes”> 10
when value between 800 and 900 then ‘800-900′
style=”mso-spacerun: yes”> 11*
when value > 900 then ‘>900′ end )
Ora816
SamSQL> /
VALUE_R VALUE_COUNT
——- ———–
100-300
3
400-700
3
800-900
2
>900
1
Ora816
SamSQL> ed
Wrote
file afiedt.buf
style=”mso-spacerun: yes”> 1 select
(case when value between 100 and 300 then ‘100-300′
style=”mso-spacerun: yes”> 2
when value between 400 and 700 then ‘400-700′
style=”mso-spacerun: yes”> 3
when value between 800 and 900 then