I am very new to Oracle. I am writing a SQL statement against an Oracle 10g database. My table has a date field, DATA_DT, with multiple entries for each date. I want to get SUM of the number field, BQWP, for each of these dates. To get the sum of BQWP for a specific date, my select statement would be:
SELECT SUM(BQWP)
FROM tasks
WHERE TRUNC(DATA_DT) = TO_DATE('07/19/2013', 'mm/dd/yyyy');
Now, how can I loop through all dates and get the SUM for each in a single SQL Query?
You have to aggregate the entries by date:
SELECT TRUNC(DATA_DT),SUM(BQWP)
FROM tasks
GROUP BY TRUNC(DATA_DT)
Look at this document for further information:
http://docs.oracle.com/cd/E11882_01/server.112/e26088/functions003.htm
You can use GROUP BY
SELECT TRUNC(DATA_DT) as data_dt, SUM(BQWP) as sumBqwp
FROM tasks
GROUP BY TRUNC(DATA_DT)
Related
Can you filter a SQL table based on an aggregated value, but still show column values that weren't in the aggregate statement?
My table has only 3 columns: "Composer_Tune", "_Year", and "_Rank".
I want to use SQL to find which "Composer_Tune" values are repeated in each annual list, as well as which ranks the duplicated items had.
Since I am grouping by "Composer_Tune" & "Year", I can't list "_Rank" with my current code.
The image shows the results of my original "find the duplicates" query vs what I want:
Current vs Desired Results
I tried applying the concepts in this Aggregate Subquery StackOverflow post but am still getting "_Rank is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause" from this code:
WITH DUPE_DB AS (SELECT * FROM DB.dbo.[NAME] GROUP BY Composer_Tune, _Year HAVING COUNT(*)>1)
SELECT Composer_Tune, _Year, _Rank
FROM DUPE_DB
You need to explicitly declare the columns used in the Group By expression in the select columns.
You can use the following documentation if you are using transact sql for the proper use of Group By.
Simply join the aggregated resultset to original unit level table:
WITH DUPE_DB AS (
SELECT Composer_Tune, _Year
FROM DB.dbo.[NAME]
GROUP BY Composer_Tune, _Year
HAVING COUNT(*) > 1
)
SELECT n.Composer_Tune, n._Year, n._Rank
FROM DB.dbo.[NAME] n
INNER JOIN DUPE_DB
ON n.Compuser_Tune = DUPE_DB.Composer_Tune
AND n._Year = DUPE_DB._Year
ORDER n.Composer_Tune, n._Year
I'm working on multiple rows with the same Date and don't know how to combine and count for the amount customers.
The raw Data
Here is my expected result
You need to use "group by" like:
select [Date], count(customer_id)
from tablename
group by [Date]
I have a table BAR_DATA with two fields: LongDate, Time. Both are long integers. No Access Date/Time involved here.
For each distinct LongDate value there are hundreds of records, each with Time value which may be distinct or duplicate within that LongDate.
I need to create an SQL statement that will group by LongDate and give me a count of distinct Times within each LongDate.
The following SQL statement, (built by an Acess query) does NOT work (some LongDates are omitted):
Query A
SELECT DISTINCT BAR_DATA.LongDate, Count(BAR_DATA.Time) AS CountOfTime
FROM BAR_DATA
GROUP BY BAR_DATA.LongDate
HAVING (((Count(BAR_DATA.Time))<>390 And (Count(BAR_DATA.Time))<>210));
However, if I use Query B to reference Query DistinctDateTime, it does work:
Query B
SELECT DistinctDateTime.LongDate, Count(DistinctDateTime.Time) AS CountOfTime
FROM DistinctDateTime
GROUP BY DistinctDateTime.LongDate
HAVING (((Count(DistinctDateTime.Time))<>390 And (Count(DistinctDateTime.Time))<>210));
Query DistinctDateTime
SELECT DISTINCT BAR_DATA.LongDate, BAR_DATA.Time
FROM BAR_DATA;
My problem:
I need to get Query B and Query DistinctDateTime wrapped into a single SQL statement so I can paste it into a VBA function. I presume there
is some subquery techniques, but I have failed at every attempt, and find no pertinent example.
Any help will be greatly appreciated. Thanks!
Subquery your distinct table inside and perform your aggregates outside until you get the desired result:
SELECT DistinctDateTime.LongDate, Count(DistinctDateTime.Time) AS CountOfTime
FROM
(
SELECT DISTINCT BAR_DATA.LongDate, BAR_DATA.Time
FROM BAR_DATA
) AS DistinctDateTime
GROUP BY DistinctDateTime.LongDate
HAVING (((Count(DistinctDateTime.Time))<>390 And (Count(DistinctDateTime.Time))<>210));
my data format is like this
query guid result time
I want to write a sql like
select
query,
count(query),
count(distinect guid),
result
from
table
group by
query
second column means the number of same querys,third column means the number of the distinct guids,the fourth column means the newest result,while same query may have several results and we chose the newest result by the time.since its logic is a little complex,how can i write a sql to do all these things?
select a.md5,a.cnt,a.wide,b.main_level from (select md5,count(md5) cnt,count(distinct guid) wide,max(time) maxtime from hive group by md5) a join hive b on a.maxtime = b.time ;
Is it possible in PostgreSQL to select one column from a table within a particular date span (there is a date column) and - here is the catch! - add the table together. Like for making a sales report?
Based on your comment, I think you are referring to SUM(). This is an aggregate function
SELECT SUM(amount)
FROM sales_orders
WHERE date BETWEEN '2011-03-06' and '2011-04-06' -- not sure what your date is.
If I understand you correctly, you are looking for this:
SELECT sum(amount)
FROM sales_orders
WHERE date ...