I have following data:
date |weekno
1/1/2015 | 1
1/2/2015 | 1
1/3/2015 | 1
1/1/2014 | 1
1/1/2014 | 1
1/2/2014 | 1
1/3/2014 | 1
1/4/2014 | 1
1/4/2015 | 2
1/5/2015 | 2
1/6/2015 | 2
1/7/2015 | 2
1/8/2015 | 2
1/9/2015 | 2
1/10/2015 | 2
based on this I tried below query
;WITH CTE AS (
SELECt
Date,
Weekno ,
ROW_NUMBER() OVER(PARTITION BY WEEKno, YEAR(date) ORDER BY date ASC) AS RN
FROM
dateinfo
)
select
a.Date,
a.Weekno,
CAST(a.WEEKno AS VARCHAR) + ' ' +
convert(varchar(10),DATEADD(wk,DATEDIFF(wk,0,a.Date),0),110)
as Firstdayofweeknodate
FROM
dateinfo AS A
JOIN CTE AS B
ON A.WEEKno=B.Weekno AND
YEAR(A.Date)=YEAR(B.Date)
WHERE
B.RN = 1
it gives following output:
Date |Weekno |Firstdayofweeknodate
2014-01-01 |1 |1 12-30-2013
2014-01-02 |1 |1 12-30-2013
2014-01-03 |1 |1 12-30-2013
2014-01-04 |1 |1 12-30-2013
2015-01-01 |1 |1 12-29-2014
2015-01-02 |1 |1 12-29-2014
2015-01-03 |1 |1 12-29-2014
2015-01-04 |2 |2 01-05-2015
2015-01-05 |2 |2 01-05-2015
2015-01-06 |2 |2 01-05-2015
2015-01-07 |2 |2 01-05-2015
2015-01-08 |2 |2 01-05-2015
2015-01-09 |2 |2 01-05-2015
2015-01-10 |2 |2 01-05-2015
I want output like below:
Date |Weekno |Firstdayofweeknodate
2014-01-01 |1 |1 01-01-2014
2014-01-02 |1 |1 01-01-2014
2014-01-03 |1 |1 01-01-2014
2014-01-04 |1 |1 01-01-2014
2015-01-01 |1 |1 01-01-2015
2015-01-02 |1 |1 01-01-2015
2015-01-03 |1 |1 01-01-2015
2015-01-04 |2 |2 01-05-2015
2015-01-05 |2 |2 01-05-2015
2015-01-06 |2 |2 01-05-2015
2015-01-07 |2 |2 01-05-2015
2015-01-08 |2 |2 01-05-2015
2015-01-09 |2 |2 01-05-2015
2015-01-10 |2 |2 01-05-2015
Here is a syntax you could use:
SET datefirst 7
SELECT d,
cast(case when datepart(week, d) = 1 THEN dateadd(yy, datediff(yy, 0, d), 0)
ELSE dateadd(week, datediff(week, 0, d), -1) END as date) first_day_of_week,
datepart(week, d) week
FROM
(values
('2013-12-31'),('2014-12-31'),
('2014-01-01'),('2014-01-02'),
('2014-01-03'),('2014-01-04'),
('2014-01-05'),('2015-01-03'),
('2015-01-04'),('2015-01-05'),
('2015-01-06'),('2015-01-07'),
('2015-01-08'),('2015-01-09'),
('2015-01-10'),('2015-01-11')
) x(d)
Result:
d first_day_of_week week
2013-12-31 2013-12-29 53
2014-12-31 2014-12-28 53
2014-01-01 2014-01-01 1
2014-01-02 2014-01-01 1
2014-01-03 2014-01-01 1
2014-01-04 2014-01-01 1
2014-01-05 2014-01-05 2
2015-01-03 2015-01-01 1
2015-01-04 2015-01-04 2
2015-01-05 2015-01-04 2
2015-01-06 2015-01-04 2
2015-01-07 2015-01-04 2
2015-01-08 2015-01-04 2
2015-01-09 2015-01-04 2
2015-01-10 2015-01-04 2
2015-01-11 2015-01-11 3
Related
I have a data like below
Input Df
+----------+-----------------------------------+--------------|
|SALES_NO |SALE_LINE_NUM | CODE_1 | CODE_3 | CODE_2 |
+----------+----------------------------|------+---|----------|
|123 |1 | ABC | E456 | GHF989 |
|123 |2 | EDF | EFHJ | WAEWA |
|234 |1 | 2345 | 985E | AWW |
|234 |2 | WERWE | | |
|234 |3 | ERC | AERER | |
|456 |1 | WER | AWER | |
+----------+-----------------------------------+--------------|
Output will be created like : for each unique sales_no, sales_line_num create a each new row for different code column if code is not null and order for the same.
For code_1, order will be 1.
For code_2, order will be 2.
Output df
SALES_NO SALES_LINE_NUM CODE ORDER
123 1 ABC 1
123 1 E456 2
123 1 GHF989 3
123 2 EDF 1
123 2 EFHJ 2
123 2 WAEWA 3
234 1 2345 1
234 1 985E 2
234 1 AWW 3
234 2 WERWE 1
234 3 ERC 1
234 3 AERER 2
456 1 WER 1
456 1 AWER 2
Can anyone please help? Thanks in advance
For this dataset:
var ds = spark.sparkContext.parallelize(Seq(
(123, 1, "ABC", "E456", "GHF989"),
(123, 2, "EDF", "EFHJ", "WAEWA"),
(234, 1, "2345", "985E", "AWW"),
(234, 2, "WERWE", "", ""),
(234, 3, "ERC", "AERER", ""),
(456, 1, "WER", "AWER", ""),
)).toDF("SALES_NO", "SALE_LINE_NUM", "CODE_1", "CODE_3", "CODE_2")
We need to unpivot through stack as below:
ds = ds.selectExpr(
"SALES_NO",
"SALE_LINE_NUM",
"stack(3, CODE_1, '1', CODE_2, '2', CODE_3, '3') as (CODE, ORDER)"
)
Which should give what you want:
+--------+-------------+------+-----+
|SALES_NO|SALE_LINE_NUM|CODE |ORDER|
+--------+-------------+------+-----+
|123 |1 |ABC |1 |
|123 |1 |GHF989|2 |
|123 |1 |E456 |3 |
|123 |2 |EDF |1 |
|123 |2 |WAEWA |2 |
|123 |2 |EFHJ |3 |
|234 |1 |2345 |1 |
|234 |1 |AWW |2 |
|234 |1 |985E |3 |
|234 |2 |WERWE |1 |
+--------+-------------+------+-----+
More about unpivoting can be found here.
Good luck!
I have a table with StudentIds ordered by row number, I want to update NewDummyNumber column with the values that are calculated by some logic.
This is my original table:
|StudentId|RegNumber|RowNum|NewDummyNo|
+---------+---------+------+----------+
|282840 |15005 |1 |NULL |
|282841 |15006 |2 |NULL |
|282877 |15040 |3 |NULL |
|282878 |15041 |4 |NULL |
|282879 |15042 |5 |NULL |
|282880 |15043 |6 |NULL |
|282881 |15044 |7 |NULL |
|282882 |15045 |8 |NULL |
|282837 |15002 |9 |NULL |
|282838 |15003 |10 |NULL |
---------------------------------------
The logic for NewDummyColumn is:
if SkipNumber = 2 then the records will be shuffle like 1st record as it is with dummy number as 1001 then two records are skip for now and 4th record newdummy number will be 1002 and so on up to 10 then this cycle will continue. The result should be like
-- for first Cycle
|StudentId|RegNumber|RowNum|NewDummyNo|
+---------+---------+------+----------+
|282840 |15005 |1 |1001 |
|282841 |15006 |2 |NULL |
|282877 |15040 |3 |NULL |
|282878 |15041 |4 |1002 |
|282879 |15042 |5 |NULL |
|282880 |15043 |6 |NULL |
|282881 |15044 |7 |1003 |
|282882 |15045 |8 |NULL |
|282837 |15002 |9 |NULL |
|282838 |15003 |10 |1004 |
---------------------------------------
For the second cycle:
|StudentId|RegNumber|RowNum|NewDummyNo|
+---------+---------+------+----------+
|282840 |15005 |1 |1001 |
|282841 |15006 |2 |NULL |
|282877 |15040 |3 |1005 |
|282878 |15041 |4 |1002 |
|282879 |15042 |5 |NULL |
|282880 |15043 |6 |1006 |
|282881 |15044 |7 |1003 |
|282882 |15045 |8 |NULL |
|282837 |15002 |9 |1007 |
|282838 |15003 |10 |1004 |
---------------------------------------
For the third cycle
|StudentId|RegNumber|RowNum|NewDummyNo|
---------------------------------------
|282840 |15005 |1 |1001 |
|282841 |15006 |2 |1008 |
|282877 |15040 |3 |1005 |
|282878 |15041 |4 |1002 |
|282879 |15042 |5 |1009 |
|282880 |15043 |6 |1006 |
|282881 |15044 |7 |1003 |
|282882 |15045 |8 |1010 |
|282837 |15002 |9 |1007 |
|282838 |15003 |10 |1004 |
---------------------------------------
Now NewDummyNo colunm not contain any NULL Values so this is the final result.
Please suggest how to achieve this - my #table is:
create table #DUMMYNUMBER
(
StudentId int,
OrderStudentRegNumber int,
RwNumber int,
NewDummyNumber int
)
insert into #DUMMYNUMBER(StudentId, OrderStudentRegNumber, RwNumber)
values
(282840, 15005, 1),
(282841, 15006, 2),
(282877, 15040, 3),
(282878, 15041, 4),
(282879, 15042, 5),
(282880, 15043, 6),
(282881, 15044, 7),
(282882, 15045, 8),
(282837, 15002, 9),
(282838, 15003, 10)
If number of records and skipNumber+1 are coprimes, you can do following:
DECLARE #skip int = 2;
DECLARE #total int = (SELECT COUNT(*) FROM #DUMMYNUMBER);
DECLARE #skip int = #skipNumber+1;
SELECT TOP(#total) StudentId, OrderStudentRegNumber, RwNumber, 1000+ROW_NUMBER() OVER (ORDER BY N) NewDummyNo FROM (
SELECT *, R+A*#total N, CASE WHEN (R+A*#total)%#skip=1 THEN 'X' END X FROM (
SELECT *, ROW_NUMBER() OVER (ORDER BY RwNumber) R
FROM #DUMMYNUMBER
) Num
CROSS APPLY (VALUES (0),(1),(2)) T(A) --number of loops, equal to (skip+1), use tally or recursive generator if needed
) T WHERE X IS NOT NULL
If they are not, infinite loop happens.
How to move the data from different rows with same ID in same rows but different column?
For example,
I have this table
tblEx
--------------
|ID|Buy |Sell|
|--+----+----|
|1 |10 | |
|1 | |11 |
|2 |20 | |
|2 | |0 |
|3 |0 | |
|3 | |30 |
--------------
Desired Output:
--------------
|ID|Buy |Sell|
|--+----+----|
|1 |10 |11 |
|2 |20 |0 |
|3 |0 |30 |
--------------
Based from the given example and desired result, you can use MAX()
SELECT ID, MAX(Buy) AS Buy, MAX(Sell) AS Sell
FROM TableName
GROUP BY ID
Here is my query
Select distinct ASSOCIATE_ID,
Rate_Billed,
Currency,
RateMultiplier,
UOM,
MONTH,
YEAR= MAX(YEAR) over (partition by associate_id)
from asso_billinghrs
Below is the sample data.
|ASSOCIATE_ID |Rate_Billed | Currency| RateMultiplier| UOM |MONTH|YEAR|
|---------------|------------|----------|-----------------|-----|-----|----|
|1 |23.78 |USD |1 |B |11 |2013|
|1 |23.78 |USD |1 |B |2 |2014|
|1 |23.78 |USD |1 |B |3 |2014|
|2 |1 |INR |0.0146701 |C |1 |2017|
|2 |1 |INR |0.0147451 |C |1 |2017|
Below is the output
|ASSOCIATE_ID| Rate_Billed|Currency|RateMultiplier|UOM|MONTH|YEAR|
|------------|-------------|--------|--------------|---|-----|----|
|1 |23.78 |USD |1 |B |3 |2014|
|2 |1 |INR |0.0147451 |C |1 |2017|
Get latest salary of each associate in sql table having month and year column also. Each associate have minimum 4 to 5 records
Thanks
Try this :
WITH CTE AS(
Select distinct ASSOCIATE_ID,
Rate_Billed,
Currency,
RateMultiplier,
UOM,
MONTH,
YEAR,
ROW_NUMBER()over (partition by associate_id ORDER BY [YEAR],[MONTH] DESC) RN
from asso_billinghrs)
SELECT *
FROM CTE
WHERE RN=1
You may try using RANK() to get the following output.
select * from
(select *,RANK() over(partition by ASSOCIATE_ID order by tblyear desc,tblmonth desc,ratemultiplier desc)as r from asso_billinghrs)T
where r = 1
Output:
|ASSOCIATE_ID| Rate_Billed|Currency|RateMultiplier|UOM|MONTH|YEAR|
|------------|-------------|--------|--------------|---|-----|----|
|1 |23.78 |USD |1 |B |3 |2014|
|2 |1 |INR |0.0147451 |C |1 |2017|
FIDDLE
I have small question about SQL Server.
I have a table with sample data like this:
id | month | stat | count
---------------------------------
1 | 1 | admit | 7
2 | 8 | admit | 47
1 | 7 | admit | 28
2 | 9 | admit | 11
3 | 12 | dischr | 4
4 | 10 | openc | 5
1 | 11 | admit | 1
2 | 6 | admit | 5
2 | 4 | admit | 8
1 | 3 | dischr | 10
2 | 2 | admit | 30
3 | 5 | dischr | 20
1 | 8 | admit | 13
3 | 8 | dischr | 1
4 | 9 | admit | 30
2 | 10 | admit | 20
3 | 10 | deschr | 20
Based on this when month=1 then January and when month=2 then February all the way through 12, based on that condition I got output like below.
Month |id |Admit |Dischr |OpenC
August |1 |13 |NULL |NULL
January |1 |7 |NULL |NULL
July |1 |28 |NULL |NULL
March |1 |NULL |10 |NULL
November|1 |1 |NULL |NULL
April |2 |8 |NULL |NULL
August |2 |47 |NULL |NULL
February|2 |30 |NULL |NULL
June |2 |5 |NULL |NULL
October 2 |20 |NULL |NULL
September|2 |11 |NULL |NULL
August |3 |NULL |1 |NULL
December|3 |NULL |4 |NULL
May |3 |NULL |20 |NULL
October |3 |NULL |NULL |NULL
October |4 |NULL |NULL |5
September|4 |30 |NULL |NULL
but I want output like month wise proper order and output look like below
Month |id |Admit |Dischr |OpenC
January |1 |7 |NULL |NULL
February|2 |30 |NULL |NULL
March |1 |NULL |10 |NULL
April |2 |8 |NULL |NULL
May |3 |NULL |20 |NULL
June |2 |5 |NULL |NULL
July |1 |28 |NULL |NULL
August |1 |13 |NULL |NULL
August |2 |47 |NULL |NULL
August |3 |NULL |1 |NULL
September|2 |11 |NULL |NULL
September|4 |30 |NULL |NULL
October |2 |20 |NULL |NULL
October |3 |NULL |NULL |NULL
October |4 |NULL |NULL |5
November|1 |1 |NULL |NULL
December|3 |NULL |4 |NULL
Please tell me query how to solve this issue in SQL Server.
You need to convert the month column to date and use it in order by with ID
SELECT [Month],
id,
Admit,
Dischr,
OpenC
FROM yourTable
ORDER BY Cast([Month] + '01 2010' AS DATE),Id
or
order by DATEPART(MM,[Month]+ ' 01 2010'),Id
Note : '1 2010' in cast is just a static value to convert the month varchar column to date
Add order by clause to your query to ensure your data is ordered according to your requirement. Something like this
SELECT ....
FROM yourTable
ORDER BY Month, Id