map dates in ship week sql - sql

I'm trying to map ship_date to the correct ship_wk (our ship weeks go from Sunday to Saturday) and I'm not sure how to do it.
The table the ship week field pulls from is currently mapping them incorrectly. For example, ship_wk = 42 should show dates 10/14 - 10/20, but is showing 10/21 - 10/27.
I need to create a different ship week field within my query that uses the correct mapping. I've never worked with dates in this capacity. Any suggestions?

Related

Qlikview Replace WeekSeq number with week number

I have a master calendar that includes week and weekseq, weekseq is just an autonumber of week and year. I am having an issue with the column labels on a 52 wk report that works of current week back 52 weeks. When i use weekseq as the column header it works out correctly but this header is of no use, Is there a way for me to replace weekseq with week in my column header? I have currently been trying =week(max(WeekSEQ)) for current week but not working out as it should.
Thanks
for sure :
=week(max(WeekSEQ))
is incorrect as WeekSeq is autonumber and week process date or timestamp field only.
So you need to have Date field in your calendar and use it:
=week(max(*date_field*))
(if you don't have date field you can create it but for that I need at least snapshot of your datamodel - you can make screenshot of table viewer which you can access using Ctrl+T)

how to automate the date change in a query using transact sql

I work for a company where everyday I modify a query by changing the date of the day before, because the report is always from the previous day.
I want to automate the date change. I have made a table with two columns, one with all dates from this year and another with bits where if 0 is a working day and 1 if is a holiday.
I have successfully automated a little bit by telling if the day before is a working day then subtract 1 from the date (This is what happens everyday). But the problem is, that if is Monday appears as Friday, because Saturday and Sunday are not billable. And let's also say, that if today is Thursday and Wednesday and Tuesday we're holidays, then the report will run on Monday. I will leave you a picture, that shows how the table is made with dates.
Remembering, that if there is no holidays in the middle of the week, always will be subtract one.
The way to do this sort of thing is close to what you have done, but just extend it further. Create a BusinessDate table that has every date, and then every rule you have implemented inside it. You can go so far as to include a column such as ReportDate which will return,for every date, what date the report should be run for.
Do this once, and it will work forever more. You may have to update for future holidays once a year, but better than once a day!
It will also allow you to update things specific for your business, like quarter dates, company holidays, etc.
If you want to know more on the subject, look up topics around creating a date dimension in a data warehouse. Its the same general issue you are facing.
Too complicated for a comment and it involves a lot of guessing.
So everyday, your process starts by first determining if "today" is a work day. So you would do something like:
if exists (select * from <calendar> where date = cast (getdate() as date) and IsWorkday = 1")
begin
<do stuff>
end;
The "do stuff" section would then run a report or your query (or something that isn't very clear) using the most recent work day prior to the current date. You find that date using something like:
declare #targetdate date;
set #targetdate = (select max(date) from <calendar>
where date < cast (getdate() as date)
and IsWorkday = 1);
if #targetdate is not null
<run your query using #targetdate>
That can be condensed into less code but it is easier to understand when the logic is written step-by-step.

how to summarize sql result, week and month wise in single query

I want to summarize sql query result week wise and month wise at the same time in a grid view. Is this even possible or I am just dreaming?
Requirement:
Show last one month data and next two month's data week wise in the grid.
Example-
If the current month is September then I want to show data from 1st August to 31st October categorized in weeks.
Show the data after the next month of current month in month wise view in the same grid.
Example-
data for month November and December will be shown categorized in month not in weeks.
grid or result should look something like below -
Please suggest something to achieve this
If you want a maintainable solution, use two independent queries, one for weekly aggregation other for monthly. Depending on the input run corresponding query.
I think you must work with group by or group by ... cube
SELECT x , y from <tabel> GROUP BY date_feld( to_char( 'MM' ))
but i don't now your DBMS so i can't give you a exact example for the date handling.

SSRS Calendar Report

I am using SSRS in Visual Studio 2008 and need to create a report that displays names on a calendar based on the dates pulled from the database. I would like the calendar to display a month at a time My query pulls data that looks like the following:
Name StartDate EndDate NumDays (Shows difference between Start and EndDate)
Tim 1/1/2000 1/1/2000 1
Bill 1/4/2000 1/6/2000 3
Tim 1/10/2000 1/11/2000 2
For my report, I would need a Calendar that displays "Tim" on 1/1, 1/10, and 1/11 and display "Bill" on 1/4, 1/5, 1/6.
Any advice would be greatly appreciated. Thanks in advanced!
To do this effectively you will need a table of dates (either in your database or generate one in your SQL query). You can then left join from that table in your query which will ensure you have a row for each day. The table of dates should also identify which dates belong to the same week. (One way would be to use the DATEPART(WK,Datecolumn) function.)
Next create a tablix in your report which groups by day for the columns and by the week identifier for the rows. You'll probably need to pull in the last few days of the previous month in order to get it to align to a Sunday start date (or whatever the first day of your calendar row is).
Each cell in the tablix will now be a box on your calendar.

Access query (SQL) to return records sorted (grouped by) WEEKS

Greetings SQL gurus,
I don't know if you can help me, but I will try. I have several large databases grouped by year (each year in a different database). I want to be able to compare values from a particular week from one year to the next. For example, "show me week 17 of 2008 vs. week 17 of 2002."
I have the following definition of weeks that ideally I would use:
Only 52 weeks each year and 7 days a week (that only takes 364 days),
The first day of the first week starts from January 2nd - which means we do not use January 1st data, and
In leap year, the first day of the first week ALSO starts from the January 2nd plus we skip Feb. 29.
Any ideas?
Thanks in advance.
Best to avoid creating a table because then you have to update and maintain it to get your queries to work.
DatePart('ww',[myDate]) will give you the week number. You may run into some issues though deciding which week belongs to which year - for example if Jan 1 2003 is on Wednesday does the week belong as week 52 in 2002 or week 1 in 2003? Your accounting department will have a day of the week that is your end of week (usually Sat). I usually just pick the year that has the most days in it. DatePart will always count the first week as 1 and in the case of the example above the last week as 53. You may not care that much either way. You can create queries for each year
SELECT DatePart('ww',[myDate]) as WeekNumber,myYearTable.* as WeekNumber
FROM myYearTable
and then join the queries to get your data. You'll loose a couple days at the end of the year if one table has 52 weeks and one has 53 (most will show as 53). Or you can do it by your weekending day - this always gives you Saturday which would push a late week into the following year.
(7-Weekday([myDate]))+[myDate]
then
DatePart('ww',(7-Weekday([myDate]))+[myDate])
Hope that helps
To get the week number
'to get the week number in the year
select datepart( week, datefield)
'to get the week number in the month
select (datepart(dd,datefield) -1 ) / 7 + 1
You don't need to complicate things thinking about leap years, etc. Just compare weeks mon to sun
SInce you havea a specifc defintion of when the week starts that is differnt that the standard used by the db, I think a weeks table is the solution to your problem. For each year create a table that defines the dates contained in each week and the week number. Then by joining to that table as well as the relevant other tables, you can ask for just the data for week 17.
Table structure
Date Week
20090102 1
20090103 1
etc.
I needed to create a query that shows BOTH year AND week numbers, like 2014-52. The year shows correct when you use the Datepart() formula to convert week 53 to week 52 in the previous year, but shows the wrong year for the week that was week 1 previously that should be week 52 now. It show that week as 2015-52 instead of 2014-52.
Furthermore, it sorts the data wrong if you only use only the week number, eg:
2014-1,2014-11,2014-2
To overcome this I created the following query to insert a 0 and also to check for days in week 1 that should still fall under week 52.
ActualWeek: IIf(DatePart("ww",[SomeDate],1,3)=52 And DatePart("ww",[SomeDate])=1, DatePart("yyyy",[SomeDate],1,3)-1,DatePart("yyyy",[SomeDate],1,3)) & "-" & IIf(DatePart("ww",[SomeDate],1,3)<10,"0" & DatePart("ww",[SomeDate],1,3),DatePart("ww",[SomeDate],1,3))