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 ...
Related
I tried adding two numbers that are present in two different columns but it's not adding up when there are no numbers present in the second column(B). Please find the screenshot of the table and the query I was using to achieve this.
Not getting the value present in COLUMN A in total sales.
The query which I ran but wasn't successful.
SELECT Date,
SUM(sales a) as "total_a",
SUM(sales b) as "total_b",
("total_a"+"total_b") as "total_sales"
FROM data_table
GROUP BY Date;
I would suggest:
SELECT Date,
SUM(sales_a) as "total_a",
SUM(sales_b) as "total_b",
COALESCE(SUM(sales_a, 0) + COALESCE(SUM(sales_b, 0)) as "total_sales"
FROM data_table
GROUP BY Date;
I do know that Amazon Redshift allows the re-use of table aliases -- contravening the SQL standard. However, I find it awkward to depend on that functionality; and it can lead to hard-to-find-errors if your column aliases match existing column names.
You can't reuse column aliases in the same scope, so your query should error. You need to repeat the SUM() expressions.
Then: if one of the sums returns NULL, it propagates the the results of the addition. You can use coalesce() to avoid that:
SELECT Date,
SUM(sales_a) as total_a,
SUM(sales_b) as total_b,
COALESCE(SUM(sales_a), 0) + COALESCE(SUM(sales_b), 0) as total_sales
FROM data_table
GROUP BY Date;
How to create a table report with WTD integrated inside the report. e.g.
some option i could think of is creating an sp that returns a temp table, inside the sp is a loop that every week it will insert a wtd totals for that week. another one is if it can be achieved in the reporting service. so far no luck with those.
You can use grouping sets and order by. You don't show what your data looks like, but the idea is:
select date, sum(sales), sum(orders)
from t
group by grouping sets ( (date), (year(date), datepart(week, date)) )
order by max(date), grouping(date);
Here is a db<>fiddle.
Note: This leaves the "WTD" out, because that is a string and you seem to want to put it in a date column.
You can convert the date to a string and use coalece() (or case logic using grouping()):
select coalesce(convert(varchar(255), date), 'WTD'),
Here is a db<>fiddle.
I have a table as follows:
ParentActivityID | ActivityID | Timestamp
1 A1 T1
2 A2 T2
1 A1 T1
1 A1 T5
I want to select unique ParentActivityID's along with Timestamp. The time stamp can be the most recent one or the first one as is occurring in the table.
I tried to use DISTINCT but i came to realise that it dosen't work on individual columns. I am new to SQL. Any help in this regard will be highly appreciated.
DISTINCT is a shorthand that works for a single column. When you have multiple columns, use GROUP BY:
SELECT ParentActivityID, Timestamp
FROM MyTable
GROUP BY ParentActivityID, Timestamp
Actually i want only one one ParentActivityID. Your solution will give each pair of ParentActivityID and Timestamp. For e.g , if i have [1, T1], [2,T2], [1,T3], then i wanted the value as [1,T3] and [2,T2].
You need to decide what of the many timestamps to pick. If you want the earliest one, use MIN:
SELECT ParentActivityID, MIN(Timestamp)
FROM MyTable
GROUP BY ParentActivityID
Try this:
SELECT [ParentActivityId],
MIN([Timestamp]) AS [FirstTimestamp],
MAX([Timestamp]) AS [RecentTimestamp]
FROM [Table]
GROUP BY [ParentActivityId]
This will provide you the first timestamp and the most recent timestamp for each ParentActivityId that is present in your table. You can choose the ones you need as per your need.
"Group by" is what you need here. Just do "group by ParentActivityID" and tell that most recent timestamp along all rows with same ParentActivityID is needed for you:
SELECT ParentActivityID, MAX(Timestamp) FROM Table GROUP BY ParentActivityID
"Group by" operator is like taking rows from a table and putting them in a map with a key defined in group by clause (ParentActivityID in this example). You have to define how grouping by will handle rows with duplicate keys. For this you have various aggregate functions which you specify on columns you want to select but which are not part of the key (not listed in group by clause, think of them as a values in a map).
Some databases (like mysql) also allow you to select columns which are not part of the group by clause (not in a key) without applying aggregate function on them. In such case you will get some random value for this column (this is like blindly overwriting value in a map with new value every time). Still, SQL standard together with most databases out there will not allow you to do it. In such case you can use min(), max(), first() or last() aggregate function to work around it.
Use CTE for getting the latest row from your table based on parent id and you can choose the columns from the entire row of the output .
;With cte_parent
As
(SELECT ParentActivityId,ActivityId,TimeStamp
, ROW_NUMBER() OVER(PARTITION BY ParentActivityId ORDER BY TimeStamp desc) RNO
FROM YourTable )
SELECT *
FROM cte_parent
WHERE RNO =1
I have a table that is described with two columns: an index, and a date.
How would I run a query so that: for each date, it tallies how many entries are for that date, and does it for every date that appears?
I know I can COUNT for a specific date, but I'm lost as to how to do this for each date.
(I'm using SQLite, but a description for any SQL language would be very helpful). Thanks!
select `date`, count(*)
from your_table
group by `date`
select index, date, COUNT(*) from tbl
group by index, date
check this.... let me know if it works.....
If a field is not in the "group by" list then you cannot include it unless you are performing some aggregate function on it (count, sum, etc.).
select "date_field", count(*) from "table" group by "date_field";
Also, SQLite does not have to use backticks (`) like MySQL, you can use double quotes.
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)