How can I get the result for the following SQL statement using JavaScript

Hi,
How can I get the result for the following SQL statement using JavaScript?

SELECT branch, region, referral, COUNT(DISTINCT LPS) FROM table GROUP BY branch, region, referral

Data:
Branch Client Referral Type LPS #
402036 402430 Psychological File Review 30
402036 402430 Psychological File Review 50
402049 402805 Chiropractic File Review 10
402049 402805 In-Home Assessment 20
402049 402806 Chiropractic File Review 30
402049 402806 Chiropractic File Review 20
402049 402806 Chiropractic File Review 10
402049 402806 Chiropractic File Review 10
402049 402807 Physiotherapy Assessment 10
402049 402807 Physiotherapy Assessment 10
402049 402807 Physiotherapy Assessment 20
402049 402808 In-Home Assessment 10
402049 402816 Chiropractic Assessment 50
402049 402827 Neurological File Review 40

Please note I have the data in ascending order.

The result should look like the following table.
Branch Client Referral Type LPS #
402036 402430 Psychological File Review 30
402036 402430 Psychological File Review 50
Psychological File Review Total 80
402430 Total 80
402036 Total 80
402049 402805 Chiropractic File Review 10
Chiropractic File Review Total 10
402049 402805 In-Home Assessment 20
In-Home Assessment Total 20
402805 Total 30
402049 402806 Chiropractic File Review 30
402049 402806 Chiropractic File Review 20
402049 402806 Chiropractic File Review 10
402049 402806 Chiropractic File Review 10
Chiropractic File Review Total 70
402806 Total 70
402049 402807 Physiotherapy Assessment 10
402049 402807 Physiotherapy Assessment 10
402049 402807 Physiotherapy Assessment 20
Physiotherapy Assessment Total 40
402807 Total 40
402049 402808 In-Home Assessment 10
In-Home Assessment Total 10
402808 Total 10
402049 402816 Chiropractic Assessment 50
Chiropractic Assessment Total 50
402816 Total 50
402049 402827 Neurological File Review 40
Neurological File Review Total 40
402827 Total 40
402049 Total 240
Grand Total 320

Thanks

Show us the javascript that you’ve come up with so far and we’ll try to help you out.

If you’re after someone to write it for you, that’s where the Marketplace comes in handy.

Most commonly you would use a server-side script such as PHP to perform the database query, and then use Ajax techniques to request the result from that php script.

Hi, Thank u for the reply. I used server-side script to pull the data. Now how can I groupby using JavaScript.

Thank you for the email. Please see below.
var ReferalsSortedArray = gMergeLevelsArray.sort(ReferalsCompareInfo);
ReferalsCountLevelsArray(ReferalsSortedArray);

function ReferalsCompareInfo(First, Second)
{
if (First.branchid < Second.branchid) return -1;
if (First.branchid > Second.branchid) return 1;

		if (First.companyid &lt; Second.companyid) return -1;
		if (First.companyid &gt; Second.companyid) return 1;
						
		if (First.Referral_Type &lt; Second.Referral_Type) return -1;
		if (First.Referral_Type &gt; Second.Referral_Type) return 1;
						
		if (First.LPS_File_Number &lt; Second.LPS_File_Number) return -1;
		if (First.LPS_File_Number &gt; Second.LPS_File_Number) return 1;
					
		return 0;		
	}

function ReferalsCountLevelsArray(SortedMergeArray)
{
var UniqueArray = new Array();
var UniqueArrayCounter = 0;
var CheckExits;

	for (var i = 0; i &lt; SortedMergeArray.length; i++ ) 
	{
		CheckExits = 'F'; 
		for (var j = 0; j &lt; UniqueArray.length; j++ ) 
		{
			if(UniqueArray[j].branchid ==  SortedMergeArray[i].branchid && UniqueArray[j].companyid  ==  SortedMergeArray[i].companyid && UniqueArray[j].Referral_Type  ==  SortedMergeArray[i].Referral_Type && UniqueArray[j].LPS_File_Number  ==  SortedMergeArray[i].LPS_File_Number) 
			{	
				CheckExits = 'T';							
			}
		}
		if(CheckExits == 'F')
		{
			UniqueArray[UniqueArrayCounter] = new Object();  
			UniqueArray[UniqueArrayCounter].branchid = SortedMergeArray[i].branchid;
			UniqueArray[UniqueArrayCounter].companyid = SortedMergeArray[i].companyid ;
			UniqueArray[UniqueArrayCounter].Referral_Type = SortedMergeArray[i].Referral_Type;	
			UniqueArray[UniqueArrayCounter].LPS_File_Number = SortedMergeArray[i].LPS_File_Number;					
			if(UniqueArray[UniqueArrayCounter].LPS_File_Number != "")
			{
				if(UniqueArray[UniqueArrayCounter].branchid == '402049')
					CurrentValues[0]= CurrentValues[0]+ 1;							
				else if(UniqueArray[UniqueArrayCounter].branchid == '402036')
					CurrentValues[1]= CurrentValues[1]+ 1;
				else if(UniqueArray[UniqueArrayCounter].branchid == '402050')
					CurrentValues[2]= CurrentValues[2]+ 1;
				CurrentValues[3] = CurrentValues[0] + CurrentValues[1] + CurrentValues[2];  
			}				
			UniqueArrayCounter = UniqueArrayCounter + 1; 	
		} 
	}		
}

The way I am doing it, I am only getting branch total instead of GROUP BY branch, region, referral and then count.
Thanks