Solved

Without Group By Clause

Posted on 2011-02-28
13
304 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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
 
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 143

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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

635 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