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
Related
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 12 months ago.
Improve this question
I have a table whose Date column needs pushing ahead by 1 day.
My update query is:
UPDATE TABLENAME
SET DATECOL=DATECOL+1
Is this correct approach? Or do I need to use CTE, for example:
;WITH CTE AS (
SELECT ID, DATECOL
FROM TABLENAME)
UPDATE T
SET T.DATECOL=CTE.DATECOL+1
FROM TABLENAME T
JOIN CTE ON T.ID=CTE.ID
To add a value to any part of date you can use DATEADD function. In your case the part time is DAY.
UPDATE TABLENAME
SET DATECOL=DATEADD(DAY, 1, DATECOL)
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)
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 (...)
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
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.