473,396 Members | 1,916 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to sum the table records?

Hi all,

I using the "list" function to extract the sales text delimited file
and insert into the table. How to find out the best sales item and
worst sales item?

item | qty
---------------------------------------
11111 | 2
22222 | 1
33333 | 1
44444 | 1
55555 | 3
22222 | 2
11111 | 1
55555 | 4
22222 | 2
66666 | 1

Thanks

Alex Murphy

May 7 '06 #1
5 4511
Rik
Alex Murphy wrote:
Hi all,

I using the "list" function to extract the sales text delimited file
and insert into the table. How to find out the best sales item and
worst sales item?


Summing is easy:

SELECT `item`, SUM(`qty`) AS 'total_sold' FROM table_name GROUP BY `item`

Extracting best and worst in one query in one go is a little bit trickier.
I'm sure it's possible in one query, but I'm not familiar enough with JOINS
or subqueries to figure that one out.

That's more of a database question, which database are you using?

Grtz,
--
Rik Wasmus
May 7 '06 #2
The database is using text file to store the data. I using "list" and
"split" function to split the "|" delimiter.
This text file will upload to the server from the shop everydays.

May 7 '06 #3
Rik
Alex Murphy wrote:
The database is using text file to store the data. I using "list" and
"split" function to split the "|" delimiter.
This text file will upload to the server from the shop everydays.


OK, you don't use a database, only a textfile. Check:
http://nl3.php.net/manual/en/function.fgetcsv.php and the like
I'd say this should be database work, but hey, arrays work too, only with a
little bit more hassle.
It would be a lot of help if you would have mentioned HOW you create the
arrays, keys etc.

If you've made an array containing: index=>array(item, qty)

/* sum the different rows on item */
foreach($array as $row){
$totals[$row['item']] += $row['qty']
}

/* get max and min value */
$max = max($totals);
$min = min($totals);

/* We need an array_search_all, mark meves posted one on php.net */

function array_search_all($needle,$haystack){
return array_keys(array_filter($haystack, create_function('$s','return $s
== \''.addslashes($needle).'\';')));
}

/* get id's where the qty is max or min */
$max_items = array_search_all($max, $totals);
$min_items = array_search_all($in, $totals);

Is that what you want, or is my lack of detailed knowledge of the english
language causing me to do something completely different?

Grtz,
--
Rik Wasmus
May 7 '06 #4
Oh.....Thanks Rik....Thanks your help.
Alex Murphy

May 8 '06 #5
On 2006-05-07, Alex Murphy <mu***********@gmail.com> wrote:
The database is using text file to store the data. I using "list" and
"split" function to split the "|" delimiter.
This text file will upload to the server from the shop everydays.


Well, then it's about time to start looking at the manual of your SQL
DBMS. You'll find a section that allows you to use 'normalize' the data
and you'll find a section that allows you to import CSV (or other delimiter
separated value) files.

--
Met vriendelijke groeten,
Tim Van Wassenhove <http://timvw.madoka.be>
May 10 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

36
by: toedipper | last post by:
Hello, I am designing a table of vehicle types, nothing special, just a list of unique vehicle types such as truck, lorry, bike, motor bike, plane, tractor etc etc For the table design I am...
20
by: Guru | last post by:
Hi I have a table which contains number of rows. I want to fetch the last 5 records from the table. I know for the first 'n' records we can use FETCH FIRST n ROWS ONLY command. But i want to...
4
by: Scott Kinney | last post by:
I have an inventory database. I want to delete out-of-stock items from the main database, but keep them in a separate table so that I can reference data about them. I created a copy of the item...
28
by: Lee Rouse | last post by:
Hello all, This is going to be a rather lengthy "question". I have an Access 2k database, separated front end/back end. Front end copies are on about 30 workstations and used frequently during...
6
by: Paul T. Rong | last post by:
Dear all, Here is my problem: There is a table "products" in my access database, since some of the products are out of date and stopped manufacture, I would like to delete those PRODUCTS from...
6
by: Sven Pran | last post by:
Probably the answer is there just in front of me only awaiting me to discover it, but: 1: I want to build a query that returns all records in one table for which there is no successful "join"...
15
by: Hi5 | last post by:
Hi, I am designing a database for a client in which It has a client table including the followings: 1-Table Client 2-Table lookupcategory 3-Table Ctegory
9
by: VMI | last post by:
We have this huge application that's based on storing tons of data on a dataTable. The only problem we're having is that storing LOTS of data (1 million records) into a datatable will slow down the...
3
by: BrianDP | last post by:
I have a database with a split front end/back end. There is a key table in the back end called Catalog, and it is sort of a central key table for all sorts of things. It's a list of all the jobs...
7
by: john | last post by:
In my form I have a master table and a details table linked 1xM. I can search through the whole parent table but I also like to be able to search through the child table fields to find parent...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.