Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Without Group By Clause

Posted on 2011-02-28
13
Medium Priority
?
308 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 2000 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 free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

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…
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 …
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

916 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