Tips on formatting SQLite query results

Below is the bill table that I am working with

building      flatname   flatdescription   amount      pastpayments     receiptno
1234          name a     a                 123         0                0
1234          name a     a                 12          10               39
1234          name a     a                 125         125              40
1235          name a     a                 133         10               41
1235          name b     b                 125         125              50
1234          name c     c                 100         90               0

I need to retrieve rows where the difference between amount and payments is greater than zero for building code 1234, and display them with style B if there is a receipt number > 0 within the same name, otherwise no style.

The expected result from my code should be as follows:

         name a     39    with style B
         name c     0

Any suggestions on how to achieve this?

I have tried the following code but it returns all selections without any style differentiation

            t.executeSql('SELECT receiptno AS mr, flatdescription, flatname, buildingcode FROM bill WHERE amount - pastpayments> 0 AND buildingcode = ? GROUP BY buildingcode, flatname ORDER BY flatdescription DESC',[buildingcode], function(t, resultflat) {


            var i,
                len = resultflat.rows.length,
                row;

            if (len > 0 ) {
                items.push('<br>');
                for (i = 0; i < len; i += 1) {
                    row = resultflat.rows.item(i);

                    if (row.mr > 0) {

                        items.push('<li data-theme="b" data-icon="false" style="height:40px; padding: 7px 0 0 0; font-size: 1.2em"><a href="#displayflat" data-flat="' + row.flatname + '" data-description="' + row.flatdescription + '">' + row.flatdescription + '...' + row.flatname + '</a></li>');

                        } else {
                        items.push('<li data-icon="false" style="height:40px; padding: 7px 0 0 0; font-size: 1.2em"><a href="#displayflat" data-flat="' + row.flatname + '" data-description="' + row.flatdescription + '">' + row.flatdescription + '...' + row.flatname + '</a></li>');

                        }
                }
            }

Answer №1

When looking at the data, it's important to note that there are several entries within the category marked with building=1234 and flatname='name a'. Simply referencing receiptno will retrieve a value from one of these entries at random.

To ensure accuracy and obtain the highest value in the group, it is recommended to utilize MAX(receiptno). This method guarantees retrieval of the largest non-zero receiptno within the specified group.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Retrieve information from jsonObject

Can anyone help me with counting the number of passes and fails for each subject in a JSON object array? Here is an example: [{"Subject":"Maths","status:"Pass"},{"Subject":"Maths","status:"Pass"}, {"Subject":"Maths","status:"Fail"},{"Subject":"Maths ...

The click event is malfunctioning. Could someone demonstrate the correct way to troubleshoot this issue?

I encountered an error message that says: Uncaught ReferenceError: toFahrenheit is not defined at HTMLInputElement.onclick I am currently investigating the root cause of this issue, but more importantly, I am seeking guidance on the steps to follow in or ...

Guide on restricting the character count and displaying the leftover characters using PHP with Ajax

I've been working on implementing a feature to display the remaining characters in a PHP AJAX call. I was successful using JavaScript, but I'm having trouble doing it with AJAX in PHP. Can someone provide assistance? <script type="text/javasc ...

Is there any difference between keeping JavaScript at the bottom of the page versus including it in the <head> tag inside document.ready?

Would it make a difference if I place my JavaScript code at the bottom of the page versus inside <head> within document.ready? I'm torn between these two approaches, referenced here: http://api.jquery.com/ready/ and http://developer.yahoo.com/p ...

Transmit information from JavaScript to HTML and retrieve it using PHP

Hey there, I'm not entirely sure if I'm doing this correctly. Can someone please provide me with an example of how to fetch data in JavaScript and send it to HTML? JS $(document).ready(function() { var chosenYear = $('#choose_year'); ...

Switching between different sections of a webpage

Seeking assistance with implementing a transition effect between sections on a single-page application. All sections are located on the same page, but only one section is displayed at a time. When an event occurs, the display property of the requested sect ...

What steps can be taken to ensure Vue application admin page contents are not displayed without proper authentication?

By implementing role-based authentication in Vue app, we can efficiently manage routes and components visibility between regular users and administrators. As the number of roles increases, the application size grows, making it challenging to control CRUD ...

The CSS code I wrote was only targeting one specific page in CodeIgniter, rather than being applied to all

As I work on constructing a basic website utilizing the Codeigniter Framework, I've opted to utilize a responsive HTML5 template. However, I've encountered an issue where the CSS is only being applied to the main URL of the site and not to other ...

HighCharts.js - Customizing Label Colors Dynamically

Can the label color change along with gauge color changes? You can view my js fiddle here to see the current setup and the desired requirement: http://jsfiddle.net/e76o9otk/735/ dataLabels: { format: '<div style="margin-top: -15.5px; ...

AdonisJs experiencing issues with web socket.io functionality

Having trouble with Web socket.io in AdonisJs. I have been using a library called adonis5-scheduler to run tasks within Adonis. Shown below is one of my tasks: import { BaseTask } from 'adonis5-scheduler/build' export default class GetRoulette ...

Steps to assign the user id where the isDisliked field is not true

I'm struggling to populate the user from the likedBy field where isDisliked is false. Can you please refer to the document schema https://i.sstatic.net/jb8x8.png for more information? Although I've tried using the query below, I can't seem ...

Delineating the Boundaries of an stl Object in three.js

Greetings to all, I have been encountering numerous discussions on this subject, yet none of the proposed solutions seem to work for me. I am currently working with an object that is loaded using the STLLoader in three.js, and I need to obtain its boundin ...

Generate an iterative loop inside an SQL SELECT query until the chain is disrupted

This is a concept for a database that contains information about events happening globally. EVENT SELECT place_nm FROM eventplace INNER JOIN relatedplaces ON placeid = rel_placeid1 INNER JOIN place ON rel_placeid2 = place_id [where the loop should begin: ...

The point in the vector data is incorrectly positioned on the map in OpenLayers

I am looking to display a world map using the default OpenLayers WMS, along with a single point on it that will have interactive events like onhover. Below is my code snippet: var options = { projection: ...

When the browser is first opened, a cookie is read only once

I am currently working on an application that customizes results based on the user's location, specifically the city they are browsing from. The location information will only be used for displaying relevant content and nothing else. I came across a h ...

Error with preventing text selection in Internet Explorer

To prevent selection on a specific HTML element, I attempted to use the "user-select" CSS property in order to achieve this functionality across all browsers. Here is an example of how it was implemented: .section1{ -webkit-user-select: none; /* ...

How come this constant can be accessed before it has even been declared?

I find it fascinating that I can use this constant even before declaring it. The code below is functioning perfectly: import { relations } from 'drizzle-orm' import { index, integer, pgTable, serial, uniqueIndex, varchar } from 'drizzle-orm ...

Leverage the Frontend (Headless Commerce) to utilize the Admin API and retrieve products from Shopify

Attempting to retrieve products from Shopify using the Admin API (GraphQL) through my frontend. I utilized the following code: *I implemented "axios" on Quasar Framework, utilizing Headless Commerce const response = await this.$axios({ url: "https: ...

What is the best way to calculate the total duration (hh:mm) of all TR elements using jQuery?

I currently have 3 input fields. The first input field contains the start time, the second input field contains the end time, and the third input field contains the duration between the start and end times in HH:mm format. My goal is to sum up all the dur ...

How can jQuery data() be used with multiple arguments?

Is it possible to add multiple data variables to an element before triggering a specific action? If so, how can I achieve this? $("#dlg_box").data("r_redirect","index.php").dialog("open"); ...