473,387 Members | 1,495 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,387 software developers and data experts.

Mysql Query grouping and having clause

57
Dear Sir

Please help me
i want a query which out put is:

Expand|Select|Wrap|Line Numbers
  1. Vendor                                Amount
  2. Computer
  3. AA                                       52000
  4. BB                                       20000
  5. Total:                                   72000
  6. Printer
  7. XX                                       12000
  8. YY                                      18000
  9. Total:                                  30000
Thanks
Dec 15 '09 #1
3 2531
mwasif
802 Expert 512MB
How your database look like?
Dec 15 '09 #2
Atli
5,058 Expert 4TB
Hey.

MySQL returns a two-dimensional list of data. What you posted is three-dimensional. You should generally avoid making MySQL jump through hoops to format the data in such a way, but rather leave that to your front-end code.

Having said that, check out the WITH ROLLUP modifier for the GROUP BY clause. It can give you a 2D representation of your 3D results. Not exactly what you posted, but close enough.
Dec 15 '09 #3
Noorain
57
I used 3 tables for this query. Please help me


Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE `item` (
  2.   `id` int(11) NOT NULL auto_increment,
  3.   `item_name` varchar(3) collate latin1_general_ci NOT NULL,
  4. PRIMARY KEY  (`id`)
  5.  
  6. INSERT INTO `item` VALUES (1, 'Computer');
  7. INSERT INTO `item` VALUES (2, 'Printer');
Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE `vndr` (
  2.   `id` int(11) NOT NULL auto_increment,
  3.   `vndr_name` varchar(255) collate latin1_general_ci NOT NULL,
  4.   PRIMARY KEY  (`id`)
  5.  
  6. INSERT INTO `vndr` VALUES (1, 'AA');
  7. INSERT INTO `vndr` VALUES (2, 'BB');
  8. INSERT INTO `vndr` VALUES (3, 'EE');
  9. INSERT INTO `vndr` VALUES (4, RR');
  10. INSERT INTO `vndr` VALUES (5, 'WW');
Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE `vt` (
  2.   `id` int(11) NOT NULL auto_increment,
  3.   `item_id` int(11) NOT NULL,
  4.   `vndr_id` int(11) NOT NULL,
  5.   `amount` double(10,2) NOT NULL,
  6.   `remarks` varchar(255) collate latin1_general_ci NOT NULL,
  7. PRIMARY KEY  (`id`)
  8. )
  9.  
  10. INSERT INTO `vt` VALUES (1, 1, 1,  25000.00, '');
  11. INSERT INTO `vt` VALUES (2, 1,1,25000.00, '');
  12. INSERT INTO `vt` VALUES ( 3, 1, 2, 25000.00, '');
  13. INSERT INTO `vt` VALUES (4, 2,4,  12000.00, '');
  14. INSERT INTO `vt` VALUES (5, 2, 3,  18000.00, '');
  15. INSERT INTO `vt` VALUES (6, 2, 3,  19000.00, '');
Thanks
Dec 15 '09 #4

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

Similar topics

5
by: Jerome | last post by:
Hi, I've got the following problem: I want my ASP page to display a certain number (based on a COUNT query), it works fine if the result is at least 1! If there are no records to be counted...
0
by: Philip Stoev | last post by:
Hi all, Please tell me if any of this makes sense. Any pointers to relevant projects/articles will be much appreciated. Philip Stoev http://www.stoev.org/pivot/manifest.htm ...
2
by: aj70000 | last post by:
This is my query select ano,max(date),a_subject from MY_TAB where table_name='xyz' and ano=877 group by a_subject,ano order by a_subject ANO max(Date) A_Subject 877 2005-01-20...
14
by: signaturefactory | last post by:
I am trying the following query in and oleDbCommand: SELECT PartLocations.LocationName, Sum(PartsJournal.Quantity) AS SumOfQuantity, PartsJournal.PartsLotNumber FROM PartLocations INNER JOIN...
2
by: m.k.ball | last post by:
Thanks Rich - that's great. Before I found this group, I thought I had a reasonable understanding of SQL (well, MySQL's implementation of it, at least) but the truth is there are great chunks that...
1
by: Regnab | last post by:
I often want to query the database with certain parameters in my query which I do not want included in the query result. The problem is when I want to group on 1, sum the 2nd (and group by) and...
24
by: clare at snyder.on.ca | last post by:
I have a SQL query I need to design to select name and email addresses for policies that are due and not renewed in a given time period. The problem is, the database keeps the information for every...
1
by: AJ | last post by:
Folllowing on from a previous post, i have created a stored query as follows. SELECT c.ID, c.Company_Name, p., 1 As QueryNbr FROM (Company AS c LEFT JOIN Sale AS s ON c.ID = s.Company_ID) LEFT...
6
Atli
by: Atli | last post by:
This is an easy to digest 12 step guide on basics of using MySQL. It's a great refresher for those who need it and it work's great for first time MySQL users. Anyone should be able to get...
4
by: dsdevonsomer | last post by:
Hello, I have one simple query joining two tables with left outer join on 3 fields and using MIN on two fields. These two tables have lot of data about 3 mil in total. I am trying to migrate db...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.