Solved

query and sub query

Posted on 2014-07-22
4
192 Views
Last Modified: 2014-07-23
hello
i have a view sale_tran , it contain data for sales transactions
i want to retrieve a report to give me the following reslut :
date , day_name , total_sales , specific_period_sales (launch or dinner)
i wrote this sql

select a.dob,a.dname,
(select sum(b.gamt) 
from sale_item b where b.dob = a.dob and b.unit = a.unit and b.dname = a.dname) all_sales,
(select sum(c.gamt) 
from sale_item c where c.dob = a.dob
and c.unit = a.unit
and c.period = 2
 and c.dname = a.dname
) noon_sales
from sale_item a
where a.dob > = '07-aug-13' and a.dob <='28-jun-14' 
and a.unit = '2001'
 and a.dname IN ('Fri','Sat')
GROUP BY a.DOB,a.dname
order by a.dob;

Open in new window


but it return not group expression at line 2
0
Comment
Question by:NiceMan331
  • 2
4 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 40211298
and b.unit = a.unit
in the subquery raises this issue...

so, you need to add the unit column into the group by, or remove it from the conditions in the subqueries
0
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 200 total points
ID: 40211300
and as unit seems to be a fixed value, adding it to the group by seems the simplest here:
select a.dob,a.dname,
(select sum(b.gamt) 
from sale_item b where b.dob = a.dob and b.unit = a.unit and b.dname = a.dname) all_sales,
(select sum(c.gamt) 
from sale_item c where c.dob = a.dob
and c.unit = a.unit
and c.period = 2
 and c.dname = a.dname
) noon_sales
from sale_item a
where a.dob > = '07-aug-13' and a.dob <='28-jun-14' 
and a.unit = '2001'
 and a.dname IN ('Fri','Sat')
GROUP BY a.DOB,a.dname, a.unit
order by a.dob;
                                  

Open in new window

0
 
LVL 48

Accepted Solution

by:
PortletPaul earned 300 total points
ID: 40214169
everything comes from the one table (or view) so there is no need for the correlated subqueries that I can see
SELECT
      a.dob
    , a.dname
    , SUM(a.gamt)                              AS all_sales
    , SUM(CASE
            WHEN a.period = 2 THEN a.gamt END) AS noon_sales
FROM sale_item a
WHERE a.dob >= '07-aug-13'
      AND a.dob <= '28-jun-14'
      AND a.unit = '2001'
      AND a.dname IN ('Fri', 'Sat')
GROUP BY
      a.DOB
    , a.dname
ORDER BY
      a.dob;

Open in new window

0
 

Author Comment

by:NiceMan331
ID: 40214884
both solution are correct , but the code of portlpal is easier for me
thanx
0

Featured Post

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.

Join & Write a Comment

Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: http://www.e-e.com/A_8429.html Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many…
Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…

759 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

23 Experts available now in Live!

Get 1:1 Help Now