Solved

Without Group By Clause

Posted on 2011-02-28
13
296 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

828 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