how to get recent 6 dates (Max dates) [closed] - sql

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;

Related

Count distinct monthly running total [closed]

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 12 months ago.
Improve this question
I am trying to find a SQL query to the question of finding the running unique count of distinct customers each month.
This is not the number of distinct customers each month, but rather what is the new incremental total number of unique customers as each month rolls by.
For example, in January I had 10 unique customers and in February I had another 10 customers, but 5 of these who transacted in February were repeat customers and had also transacted in January; so between January and February I really only have a running total of 15 unique customers (10 in January and 5 in February).
What would be the simplest solution to achieve the running unique customer count for each month of the year?
Example Output where (compared to January) in February there were an additional 5 unique customers and in March there were an additional 10 unique customers
This might do:
Select
month,
count(*) as custs,
(select
count(distinct cust_id)
from mytable b
where b.month<=a.month) as RunningUniqueCusts
From mytable a
group by month
Or for month & region
Select
month,
region,
count(*) as custs,
(select
count(distinct cust_id)
from mytable b
where b.month<=a.month
and b.region=a.region) as RunningUniqueCustsForRegion
From mytable a
group by month, region
Update 3-Mar-2022
The following would return the unique customer ids for each month where they didn't appear previously:
SELECT TM.MONTH_ID, TM.CUST_ID
FROM MYTABLE as TM
WHERE NOT EXISTS
(SELECT 1
FROM MYTABLE as PM
WHERE PM.CUST_ID = TM.CUST_ID
and PM.MONTH < TM.MONTH)
GROUP BY TM.MONTH_ID, TM.CUST_ID
ORDER BY TM.MONTH_ID, TM.CUST_ID

Find customers who ordered in the last week every day? [closed]

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 1 year ago.
Improve this question
Recently i was asked a question in the interview. Which is there is a customer table and an order table. How to get the customers who ordered in the last week everyday and how to find the customers who order in last three consecutive days?
Appreciate your help.
Thanks in advance.
To get customers who ordered every day for the past N days I would try something like:
select customer_id
from orders
where trunc(order_date) > trunc(sysdate) - :N
group by customer_id
having count(distinct trunc(order_date)) = :N
The idea is that the WHERE clause limits your data to orders made within the last N days, then we GROUP BY to form a group for each customer, and then in the HAVING clause we check each customer to see if COUNT(DISTINCT TRUNC(ORDER_DATE)) equals N.
One way is to convert the order table to a wide table with columns corresponding to each day of interest. Not sure if there are better ways.
A brute force method uses exists. For instance, for the last three days:
select c.*
from customers c
where exists (select 1
from orders o
where o.customer_id = c.customer_id and
trunc(o.order_date) = trunc(sysdate)
) and
exists (select 1
from orders o
where o.customer_id = c.customer_id and
trunc(o.order_date) = trunc(sysdate) - interval '1' day
) and
exists (select 1
from orders o
where o.customer_id = c.customer_id and
trunc(o.order_date) = trunc(sysdate) - interval '2' day
) ;
This is one method. There are others, that I'll let you think about.

How to Improve this query for PostgreSQL [closed]

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 ?

SQL: how to group by dates in a row? [closed]

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);

Calculating balance Oracle [closed]

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;