Solved

Without Group By Clause

Posted on 2011-02-28
13
280 Views
Last Modified: 2012-05-11
Hi All,

I'm trying to write this query to group results by ev_id but can't seem to  once I start selecting from the order table (TAmount). The query is as follows:

SELECT E.ev_id,E.ev_name, (SELECT count(*) as evOrdTot from tbl_book_orders where or_ev_id = E.ev_id) AS totOrd
,( SELECT SUM(oi_qty * oi_price) AS TotAmount from tbl_book_orderItems WHERE oi_or_id = BO.or_id) AS TAmount
FROM tbl_booking_events E
LEFT JOIN  tbl_book_orders BO ON E.ev_id = BO.or_ev_id
LEFT JOIN tbl_book_orderItems OI ON BO.or_id = OI.oi_or_id

Thanks
0
Comment
Question by:pigmentarts
  • 6
  • 4
  • 2
  • +1
13 Comments
 
LVL 11

Expert Comment

by:rajvja
ID: 34996675
Are you getting any error
0
 
LVL 23

Accepted Solution

by:
Rajkumar Gs earned 500 total points
ID: 34996695
I think your query could be rewritten like
SELECT  E.ev_id ,
        E.ev_name ,
        COUNT(BO.order_id) AS evOrdTot ,
        SUM(OI.oi_qty * OI.oi_price) AS TotAmount
FROM    tbl_booking_events E
        LEFT JOIN tbl_book_orders BO ON E.ev_id = BO.or_ev_id
        LEFT JOIN tbl_book_orderItems OI ON BO.or_id = OI.oi_or_id
GROUP BY E.ev_id ,
        E.ev_name

Open in new window

0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 34996698
In my query,
  COUNT(BO.order_id) AS evOrdTot ,
replace by
  COUNT(*) AS evOrdTot ,
SELECT  E.ev_id ,
        E.ev_name ,
        COUNT(BO.*) AS evOrdTot ,
        SUM(OI.oi_qty * OI.oi_price) AS TotAmount
FROM    tbl_booking_events E
        LEFT JOIN tbl_book_orders BO ON E.ev_id = BO.or_ev_id
        LEFT JOIN tbl_book_orderItems OI ON BO.or_id = OI.oi_or_id
GROUP BY E.ev_id ,
        E.ev_name

Open in new window


What is your issue ?

Raj
0
 
LVL 11

Expert Comment

by:rajvja
ID: 34996707
Hi,

  I think the author asked to execute the query without GROUP BY

  If not getting an error, are the results unexpected?

This might be due to LEFT JOIN. If there is no match, it returns NULL. NULL multiplies with any other value is also NULL.
0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 34996734
Sorry. I forgot about question title.

Without GROUP BY  same ev_id may shown repeatly in the result with the same count and sum

Try INNER JOIN
SELECT  E.ev_id ,
        E.ev_name ,
        ( SELECT    COUNT(*) AS evOrdTot
          FROM      tbl_book_orders
          WHERE     or_ev_id = E.ev_id
        ) AS totOrd ,
        ( SELECT    SUM(oi_qty * oi_price) AS TotAmount
          FROM      tbl_book_orderItems
          WHERE     oi_or_id = BO.or_id
        ) AS TAmount
FROM    tbl_booking_events E
        INNER JOIN tbl_book_orders BO ON E.ev_id = BO.or_ev_id
        INNER JOIN tbl_book_orderItems OI ON BO.or_id = OI.oi_or_id

Open in new window

0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 34996738
I suggest GROUP BY

@pigmentarts, Please explain so that we can get you
Raj
0
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 
LVL 12

Author Comment

by:pigmentarts
ID: 34996765
Well I am not getting any errors the query runs fine just that I am getting duplicate results which I don't want. I am trying to give users a summary of items ordered for an event but only want to show the event and a total of all orders placed for that event. I'm sure there may be a better way of writing it.
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 34996783
this article should be read: http://www.experts-exchange.com/A_3203.html
0
 
LVL 12

Author Comment

by:pigmentarts
ID: 34996806
Currently the results look as such with the event 'Test2' being repeated when it should be grouped with the one below. I need the Query to be fast as there will be alot of data to get through.


Event      ev_id
Test2        1            1250
Test2        1            NULL
Test           2            NULL
Dinner        3            NULL
0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 34996833
>> ..I am getting duplicate results ..
That I mentioned already-
Without GROUP BY  same ev_id may shown repeatly in the result with the same count and sum

Try this query - http:#34996698
Why you are trying to avoid GROUP BY ?

Raj
0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 34996852
Also try your query itself by replacing LEFT JOIN with INNER JOIN - http:#34996734

NULL is because of LEFT JOIN.
Better option is  using GROUP BY
0
 
LVL 12

Author Comment

by:pigmentarts
ID: 34996903
The only reason I was trying to avoid the Group By Clause is because they can be in-efficient when running against a database that has thousands of results which this will have. If this is the best way then I'll go ahead and use the Group By clause, I just thought there may have been a better way for writing this script.
0
 
LVL 12

Author Comment

by:pigmentarts
ID: 34997314
The query you provided 'RajkumarGS' seems to do the trick
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
When writing XML code a very difficult part is when we like to remove all the elements or attributes from the XML that have no data. I would like to share a set of recursive MSSQL stored procedures that I have made to remove those elements from …
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now