473,466 Members | 1,381 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

count & multiple group by

2 New Member
I'm trying to consolidate inventory records in db. (I'm displaying the result in a table form but for this post I'm putting it simple)

table_name= PC:
department - msoffice
IT - 2000
IT - 2002
IT - 2000
SNM - 2000
SNM - 2003
Finance - 2003
-----------------------------------

expected output:
msoffice - dept - qty
2000 - IT - 2
2000 - SNM - 1
2002 - IT - 1
2003 - Finance - 1
2003 - SNM - 1

Group total by msoffice:
2000 = 3 (2+1)
2002 = 1
2003 = 3 (2+2)

Grand Total : 7
----------------------------------

i have tried this method in 2 different queries that give me 2 outputs. 1 displays the Group Total and the other one displays the grouped result.

Query#1:
Expand|Select|Wrap|Line Numbers
  1. $qry=mysql_query("SELECT `msoffice`,`department`, COUNT(`msoffice`) as 'qty', COUNT(`department`) as 'dept' FROM pc GROUP BY `department`");
  2. while ($result=mysql_fetch_array($qry)){
  3. echo $result['msoffice']." - " .$result['qty'] ."<BR>";
  4. }
  5.  
output for query #1:
2000 - 3
2002 - 1
2003 - 3
--------------------------------------…
Query #2:
Expand|Select|Wrap|Line Numbers
  1. $qry2=mysql_query("SELECT `msoffice`,`department`, COUNT( `department`) as 'qty' FROM pc GROUP BY `msoffice`,`department`");
  2. while ($result2=mysql_fetch_array($qry2)){
  3. echo $result2['department']." - ' . $result2['msoffice'] . " - " .$result2['qty']. "<BR>";
  4. }
  5.  
output for query #2:
IT - 2000 - 2
SNM - 2000 - 1
IT - 2002 - 1
Finance - 2003 - 1
SNM - 2003 - 1
--------------------------------------…

problem:
1) ***MAIN Issue*** I don't know how to get the group total by msoffice. And the overall total i think i can use the row count (is there any better way?)
2) Can the 2 query be combined as 1? If can what is the best method (guide me with example)?

You help is greatly appreciated.
Mar 6 '11 #1
0 1424

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Josh Lessard | last post by:
Hi all. I'm maintaining a C++ program and I've come across a nasty piece of code that works, but I just don't understand why. I'm not actually this part of the program, but I really want to know...
0
by: mts | last post by:
First, let me say that I hope I have selected the correct discussion group for these questions. I would like to display the version (major.minor.build) of my project in the final build. I can...
2
by: Alex | last post by:
Hi all, I'm running into a road block, and I know I've done this before. I'm getting fields from two tables, and I need to do a count of similar items with it showing some extra info. Here's...
0
by: midnight_use_only | last post by:
although i don't think this is do-able using SQL, i would ask and maybe an expert can help me out. assum i have the following table: Date Code Count 2006-06-06 abc 1234...
2
by: mhrbacek | last post by:
I would like to count the number of occurrances of any rows with a particular column value, and show a separate count for each day. We have a table that uses a timestamp to differentiate the date...
1
by: ntocher | last post by:
Is it possible in a single query to search up to 12 tables for a similar text string and count the number each time a duplicate is found? I have a table for each month with a string field that...
7
sharijl
by: sharijl | last post by:
I have a SQL query which returns the total amount of issues: SELECT issue,Count(issue) as total FROM mytable Group by issue This works but I need the total of the same catagory in three tables...
0
by: mike0870 | last post by:
Hi, I've been at this one for hours and cannot not find any posts of anyone having the same problem. Ther scenario is, I need to fill a drop down box with a value in the grid row to pass to the...
3
by: weirdguy | last post by:
Hello all, Before you read my question, I would advise you to visit the Image-Link. The image-link shows 4Tables - its field, Primary, Foreign Keys, Relationships. The bottom of the Image, it...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.