GROUP BY

In this blog we would like to perform the group by operation of sql in cpp.The group by operation helps in grouping the rows of a particular attribute having same values.This is one of the most important feature of sql.Let's code it down.

Initially we will copy the data from the secondary memory to the primary memory using the copying function as discussed above.
We are basically going to perform grouping by single attribute and the user would be given a choice to select the attribute by which he wants to perform the grouping operation.So here we provide the user with a small menu for selecting the attributes.
The user enters his choice and it is stored in the choice variable.
         
           int choice;
           cout<<endl<<"\nEnter the attribute by which you want to group the books ";
           cout<<endl<<"\n1.Name\n2.Author\n3.Publication\n4.Number of pages\n5.Count available ";
           cout<<endl<<"\nEnter proper choice ";
           cin>>choice;

Now since we have to find the duplicates in the column of the specific attribute we use the hashing technique where we store the frequency of the duplicate in a different array.So for this we declare a structure array and then store the frequencies of the duplicates in the respective array.
  
   struct store
             {
               int vart;//to count the number of duplicates
               books *p[10];//array pointer to store the same attribute object address
             };

           store manas[50];
           int up=-1;//flag for manas array top position 

Now we perform hashing operation on the yash array and then store the address of the duplicates in the respective array of pointers(p[]) for the specific member of the structure array(manas[]).
We have now got the data stored in the respective structure array manas[].so we shall now print only those elements whose duplicate frequency is more than one for that specific attribute.

 for(int j=0;j<=up;j++)
                   {
                      // cout<<endl;
                       if(manas[j].vart>0)
                         {
                           for(i=0;i<=manas[j].vart;i++)
                             {

                                cout<</*endl<<"\nBOOK "<<l++*/endl<<endl;
                                cout<<setw(25)<<(manas[j].p[i])->name;
                                cout<<setw(20)<<(manas[j].p[i])->author;
                                cout<<setw(15)<<(manas[j].p[i])->publication;
                                cout<<setw(7)<<(manas[j].p[i])->pages;
                                cout<<setw(8)<<(manas[j].p[i])->counti;

                            }
                        }

                   }
 Now let us see the snaps of this grouping operation where in we will try to group the data by specific attribute of the books.

 1.Group by publication
      


Here we can see that the books with the publication name bharti and dharma are grouped together.

2.Group by author



Similarly here we can see that books with author name anant mhatre and ramesh gaikwad are grouped together.

So we have successfully implemented the grouping by function of sql in cpp using basic hashing technique.Let us now move onto the last function that is order by. 

WRITTEN BY-
PUSHKAR SADAPHAL(L 59)
Email id-pushkar.sadaphal17@vit.edu
  
   

Comments

Post a Comment

Popular posts from this blog

SEARCHING

ORDER BY PART-2