Solved

Without Group By Clause

Posted on 2011-02-28
13
270 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
Comment Utility
Are you getting any error
0
 
LVL 23

Accepted Solution

by:
Rajkumar Gs earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
I suggest GROUP BY

@pigmentarts, Please explain so that we can get you
Raj
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 12

Author Comment

by:pigmentarts
Comment Utility
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]
Comment Utility
this article should be read: http://www.experts-exchange.com/A_3203.html
0
 
LVL 12

Author Comment

by:pigmentarts
Comment Utility
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
Comment Utility
>> ..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
Comment Utility
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
Comment Utility
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
Comment Utility
The query you provided 'RajkumarGS' seems to do the trick
0

Featured Post

Zoho SalesIQ

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

Join & Write a Comment

If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

744 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

18 Experts available now in Live!

Get 1:1 Help Now