Home > database >  How to write the SQL difficult!!
How to write the SQL difficult!!

Time:10-04

The first week of sales in the second week of the third week sales... N week sales
Name ten 25 45


Sales of the first week 10, 15, in the second week of sales in the third week sales of 25, accumulative total sales, the key to weeks to do statistics, such as query number 10.1 - on December 1, this period of sales data, expert advice please!!!!!!

CodePudding user response:

Make such a field work out how many weeks of sales date is the date, and then do a crosstab in this field

CodePudding user response:

SQL server
Use the
 select datepart (week, date field) weeks, the sum (val) from TB group by datepart (week, date field) 

CodePudding user response:

Master, I'm not familiar with this function well also wrote a more trouble than you that
SELECT DATEDIFF (dd, DATEADD (wk, a DATEDIFF (wk, a 0, getdate ()), 0) - 1, 'date fields')/7 + 1 week from the TAB

CodePudding user response:

I understand the meaning of handsome old tortoise, but I mean is: on March 1st - March 8; On March 9th, March 16; On March 17th, March 24; .,,,,,,,, ask everybody to give directions, thank you!!!!!!!!!!

CodePudding user response:

Proposal written in a stored procedure, have a cycle,

CodePudding user response:

Write a function to determine the starting date for the week. Then define a variable li_weekday, let it since the add ~ if more than 7, then to assign a value to 1.
If li_weekday & gt; 7 then li_weekday=1
The else
Li_weekday=li_weekday + 1
//associated SQL statement
End the if

CodePudding user response:

My that is right for you

CodePudding user response:

The SELECT DATEDIFF (dd, '2007-03-01', '2007-03-24')/7 + 1 from the TAB

After do crosstab

CodePudding user response:

Problem is not clear, you the first week of the second week, what is the third week? By the natural month or the year?

CodePudding user response:

Have your sales record in the database is, a date or something? The column of whether inside have how many weeks?

CodePudding user response:

Otherwise, according to the statistics of temporary alone generates a new field, as the weeks statistics list!
  • Related