Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
select * from TNAME where
to_char (tran_date,'YYYY') = 2018
Extract day? Your code suggests year, so - I'll use a year instead. Here are some options to do that:
SQL> select count(*) from dual where extract(year from sysdate) = 2018;
COUNT(*)
----------
1
SQL> select count(*) from dual where to_char(sysdate, 'yyyy') = '2018';
COUNT(*)
----------
1
SQL> select count(*) from dual where trunc(sysdate, 'yyyy') = date '2018-01-01';
COUNT(*)
----------
1
SQL>
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
select SUM(count.counts), CountMostUtilizedApps
from
( (select count(*) from performance.apps WHERE start_date >= '2021-02-06 00:00:00'
AND start_date <= '2021-02-09 00:00:00' and dh.parent_id in ('1234','8910') and group_id in ('4567','1112') group by crashes order by crashes desc limit 5) counts
(select count(*) from performance.apps WHERE start_date >= '2021-02-06 00:00:00'
AND start_date <= '2021-02-09 00:00:00' and parent_id in ('1234','8910') and group_id in ('4567','1112') order by AppTime limit 10) CountMostUtilizedApps
)
The most important thing is to properly write code, even if this is SQL script and ask question in question content, not only in title.
Like in following refcatored SQL query (from yours question):
SELECT
SUM(count.counts)
,CountMostUtilizedApps
FROM
SELECT
SUM(count.counts)
,CountMostUtilizedApps
FROM
(
(
SELECT count(*) from performance.apps
WHERE
start_date >= '2021-02-06 00:00:00'
AND start_date <= '2021-02-09 00:00:00'
AND dh.parent_id IN ('1234','8910')
AND group_id IN ('4567','1112')
GROUP BY
crashes
ORDER BY
crashes desc limit 5
) counts
(
SELECT
count(*)
FROM
performance.apps
WHERE
start_date >= '2021-02-06 00:00:00'
AND start_date <= '2021-02-09 00:00:00'
AND parent_id IN ('1234','8910')
AND group_id IN ('4567','1112')
ORDER BY
AppTime limit 10
) CountMostUtilizedApps
)
Asking: How to Improve this following query for PostgreSQL ?
Is not enough. Be more specific. What you exactly mean ?
I'm not PostgreSQL expert, and more important thing as any other here you must to give more details, because nobody here do not know what your database contains .
As I said I'm not PostgreSQL expert but for my first look your's SQL Query should'nt works at all...Isn't it true ?
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
For example DECLARE #a = 10;
Required output be like:
1) 16/5/2020
2) 15/5/2020
3) 14/5/2020
4) 13/5/2020
5) 12/5/2020
6) 11/5/2020
7) 10/5/2020
8) 9/5/2020
9) 8/5/2020
10) 7/5/2020
If Declare #b = 5 then get last five days of dates in output.
Snowflake:
SELECT DATEADD(DAY, -SEQ4(), CURRENT_DATE()) AS generated_date
FROM TABLE(GENERATOR(ROWCOUNT=>10))
ORDER BY generated_date desc;
SEQn() functions are not guaranteed to be gap free, so you need to use ROW_NUMBER()
WITH tally AS (
SELECT ROW_NUMBER() OVER(ORDER BY SEQ4()) AS rn
FROM TABLE(GENERATOR(ROWCOUNT=>10))
)
SELECT DATEADD(DAY, -rn, CURRENT_DATE()) AS generated_date
FROM tally
ORDER BY generated_date
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I need recent 6 dates count
My code is
select DUE_DATE, count(*)
from DATA
group by DUE_DATE
03/24/2018 10
03/17/2018 20
03/10/2018 15
03/03/2018 23
02/24/2018 42
02/17/2018 32
02/10/2018 15
02/03/2018 17
01/27/2018 23
select DUE_DATE, count(*) from DATA group by DUE_DATE order by DUE_DATE desc limit 6
The simplest case would be if every date is presented and/or you're only interested in 6 most recent dates regardless of whether they're presented in your table or not:
select DUE_DATE, count(*)
from DATA
where DUE_DATE >= trunc(sysdate) - 5
group by DUE_DATE
order by DUE_DATE desc
(Note: dates that are not presented in your table won't be shown).
On the other hand, if you need 6 most recent dates from the subgroup of dates you have in your table, then you'll first need a subquery to fetch you those dates and than use count on those dates only:
select DUE_DATE, count(*)
from DATA
where DUE_DATE in (select distinct DUE_DATE
from DATA
order by DUE_DATE desc limit 6)
group by DUE_DATE
I hope I helped!
Are you looking for fetch first?
select DUE_DATE, count(*)
from DATA
group by DUE_DATE
order by DUE_DATE desc
fetch first 6 rows only;
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
It might be already answered before, but apparently I am not googling it right.
Let's say I have a table:
RecordingDate
2018-07-01
2018-07-02
2018-07-06
2018-07-09
2018-07-10
2018-07-11
2018-07-12
2018-07-16
2018-07-17
2018-07-18
I want to group the data by the date of the first recording and count days in a row with recordings:
DateOfFirstRecording RecordingsInARow
2018-07-01 2
2018-07-06 1
2018-07-09 4
2018-07-16 3
How do I do that ?
You can subtract an increasing sequence, to get a constant date. This is the idea:
select min(date), max(date), count(*)
from (select t.*, row_number() over (order by date) as seqnum
from t
) t
group by date - seqnum * interval '1 day'
order by min(date);
Date operations vary significantly depending on the database, but all databases support this functionality with some syntax.
EDIT:
In SQL Server, this looks like:
select
min(RecordingDate) as DateOfFirstRecording,
count(*) as RecordingsInARow
from (select Recordings.*, row_number() over (order by RecordingDate) as seqnum
from Recordings
) t
group by dateadd(day, - seqnum, RecordingDate)
order by min(RecordingDate);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I've got a problem. I just wanna calc the balance of my table.
my select statement is:
select date, ammount, ?? as Balance
from table
where accountnr = 123
order by date
Output should look like this:
Date Ammount Balance
07/02/2016 -145.55 945.65
25/01/2016 349.45 1091.20
11/11/2015 340.25 741.75
30/09/2015 369.10 401.50
05/04/2015 32.40 32.40
I tried so long, with different ways without luck.
You can do it in a single table scan (i.e. without any joins or correlated sub-queries) with an analytical query:
SELECT "date",
amount,
SUM( amount ) OVER ( ORDER BY "date" ) AS balance
FROM your_table;
If there are multiple accounts in the table then:
SELECT account_number,
"date",
amount,
SUM( amount ) OVER ( PARTITION BY account_number ORDER BY "date" ) AS balance
FROM your_table;