Find increasing values by year [closed] - sql-server

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
I have a table that contains a list of debts owed by customers. I need to write a SQL query to find which customers have an increasing amount of debt each year. Basically, I am trying to find the "bad apples", the people who continually get debts, in an increasing amount, every year. I suppose I am looking for a trend?
I have created a SQL Fiddle with some sample data. In the example, I don't care about customerId 2174 as they have only had two debts, many years ago, however, customerId 5218 has had an increasing amount of debts over the last 3-4 years.
Ideally, I'd like a list of customerIds that show a "trend" of increasing quantity of debts. i.e. they have 1 in 2015, 5 in 2016, 30 in 2017 etc.

You may try this.
; with cte as
(SELECT customerId
,YEAR(debtDate) AS debtYear
,COUNT(*) AS debtCount
FROM dbo.Debts
----- apply year filter over there if want to select record for particular year range
GROUP BY YEAR(debtDate)
,customerId
)
, CT AS
(
SELECT C.* FROM CTE AS C
CROSS APPLY (
SELECT * FROM CTE AS CC
WHERE C.customerId=CC.customerId
AND C.debtYear>CC.debtYear
AND C.debtCount>=CC.debtCount
) AS D
)
SELECT DISTINCT customerId FROM CT --- IF NEED JUST CUSTOMER NAME
------- OR IF NEED ALL DETAILS ADD debtYear, debtCount COLUMN

Related

Sql query for counting players [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.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
Count of players who have scored within 500 runs of the leader from particular country. For example if from India A scores 1500 runs then how many players have scored more than 1000 runs.
I have run this code but this is not giving me my desired result.
select COUNT( [Player ] )as no_of_players,country from [dbo].[Sheet1$]
where runs >=
All (select max(runs)-500 from [dbo].[Sheet1$] group by country )
group by country
You can use a window function for this
SELECT
COUNT(*) AS no_of_players,
country
FROM (
SELECT *,
MAX(runs) OVER (PARTITION BY country) AS MaxRuns
FROM [dbo].[Sheet1$] s1
) s1
WHERE runs >= MaxRuns - 500;
Note that COUNT(nonNullValue) is the same as COUNT(*) or COUNT(1)

How to delete top (1) row from SQL if row count reached to 10000 [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
I have made fault history log into HMI through SQL database,
I am deleting top row by PLC TAG by using following code
DELETE TOP (10) PERCENT FROM [dbo].[ABC]
I want to delete first row while my row-count reached to 10000
in other way to say that ....
a new fault record add in log and first old record should delete and so on...
If I understansd you correctly, you need to number the rows using ROW_NUMBER() and then execute appropriate DELETE and INSERT statements:
DELETE t
FROM (
SELECT *, ROW_NUMBER() OVER (ORDER BY PLC DESC) RN
FROM [dbo].[ABC]
) t
WHERE RN >= 10000
INSERT [dbo].[ABC] (PLC) VALUES (...)

find orders for three consecutive weeks [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 4 years ago.
Improve this question
I have table like the below which has overlapping weeks from two years 2018 and 2019. I want to identify the first set of 3 consecutive weeks for each customer using TSQL.
The resultset should have the customer_id and the max(order_date) from the first set of 3 consecutive weeks like below.
I created my own data. Try running this (this uses windowing functions which is a 2012 and on feature):
create table i1(i1 int identity (1,1) primary key, idate date, name varchar(25));
insert into i1 values ('1-1-2019','b'), ('1-8-2019','b'), ('1-15-2019','b'),
('1-1-2019','c'), ('1-8-2019','c'), ('1-24-2019','c'),('1-31-2019','c')
;
with ct1 as (select name,idate, datepart(ww,idate) -lag(datepart(ww,idate),1,0) over (partition by name order by idate) diff
,row_number() over (partition by name order by idate) id from i1),
ct2 as (
select name , idate,sum(diff) over ( partition by name order by idate rows between 3 preceding and current row) diff2
from ct1)
select name, idate
from ct2 where diff2=3

SQL Server query for finding some latest records from each ID [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 7 years ago.
Improve this question
I have a table like this..
ID City
-----------------
1 Jakarta
1 Bogor
2 Bandung
2 Surabaya
3 Solo
3 Null
I want the query would returns:
ID City
---------------
1 Bogor
2 Surabaya
3 Null
Any ideas? I need your help.. I used some methods such as rank , distinct, max.. but the result is not what I expected..
You can use a CTE with the ROW_NUMBER-function:
WITH CTE AS
(
SELECT Id, City, Rn = ROW_NUMBER() OVER (Partition By ID
Order by Id DESC)
FROM dbo.Cities
)
SELECT Id, City
FROM CTE
WHERE RN = 1
Altough i don't know which column you want to use to determine the "latest" record. Use that in Order by Id DESC.

how should i write this query? if there are tables customer, order, and product then (online shopping) [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
if there are tables customer, order, and product then (online shopping)
1) all customers who have purchased more than one products in same order
2) top 10 customers who have spent most money
if there are doctors, patients and appointments then (hospital management)
1) all patients who have been diagnosed by more than one doctors
2) top 10 patients who have most appointments
if there are students, teachers and courses (school management system)
1) all students who have taken more than one course from same teacher
2) top 10 students with respect to scores
i'll do first one :
schema should be something like this customer--> orders-->products
1. select *,count(*) from customers c join order o on c.order_id=o.orders_id join products p on
p.product_id=c.product_id where count(*)>1 group by c.order_id,p.product_id
2.select top 10 from customers order by spent_money desc
SELECT patients, doctors FROM hospital_management GROUP BY patients,doctors HAVING COUNT(doctors) > 1
SELECT TOP 10 FROM hospital_management ORDER BY appointments DESC

Resources