如何在SQL中的行中查找给定日期列中的月份的星期一

时间:2018-10-31 14:26:01

标签: sql sql-server tsql sql-server-2008 ssms

这些所有日期都是10月的星期一,我希望它动态进行。最好只对星期一,星期二,星期三做同样的事,最后一列应该是sum [如excel表中所示

从以下位置选择ClientName,JobName,JobTime,[2018-09-03],[2018-09-10],[2018-09-17],[2018-09-24]  (从StaleFileReport中选择ClientName,JobName,JobTime,convert(date,reporttime,101)作为[ReportDate],其中('2018.09.03','2018.09.10','2018.09.17'中的convert(varchar,ReportTime,102) ,'2018.09.24')  按ClientName,JobName,JobTime,convert(date,reporttime,101))x分组 枢 (        计数([ReportDate])        FOR [[2018-09-03],[2018-09-10],[2018-09-17],[2018-09-24]中的[ReportDate] )AS P

https://i.stack.imgur.com/5aECo.png https://i.stack.imgur.com/Cgu7A.png

2 个答案:

答案 0 :(得分:2)

您可以使用如下脚本:

DECLARE @PassedInDate AS DATE;

SET @PassedInDate = GETDATE();

WITH    CTE ( N )
          AS (
               SELECT   1
               FROM     ( VALUES ( 1), ( 1), ( 1), ( 1), ( 1), ( 1), ( 1), ( 1), ( 1) ) a ( N )
               ),
        CTE2 ( N )
          AS (
               SELECT   0 -- 0 based
               UNION ALL
               SELECT TOP 30 -- limit it up front, never more than 31 days in a month!
                        ROW_NUMBER() OVER ( ORDER BY (SELECT   NULL) )
               FROM     CTE a
               CROSS JOIN CTE b
             ),
        CTE3 ( N )
          AS (
               SELECT   DATEADD(DAY, N, DATEADD(MONTH, DATEDIFF(MONTH, 1, @PassedInDate), 0))
               FROM     CTE2
               WHERE    DATEADD(DAY, N, DATEADD(MONTH, DATEDIFF(MONTH, 1, @PassedInDate), 0)) < 
                        DATEADD(MONTH, DATEDIFF(MONTH, 1, @PassedInDate) + 1, 0)
             )
SELECT  N AS [DATE]
FROM    CTE3
WHERE DATENAME(WEEKDAY, N) = 'Monday';

参考:https://www.sqlservercentral.com/Forums/Topic1654075-391-1.aspx

答案 1 :(得分:1)

使用WITH命令进行设置,您可以将其添加到SQLquery中,然后在查询中使用日期表

override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        let url = "http://api.musixmatch.com/ws/1.1/track.lyrics.get?track_id=12693365&apikey=63ee7da5e2ee269067ecc42b25590922"

        let musixrequest = MusicMatchRequest()
        let endResults =  musixrequest.gettingLyrics(url: url)
        if !endResults.isEmpty{
            print("The end results are \(endResults)")
        }else{
            print("No results found")
        }


    }

您的查询将包括

public class MusicMatchRequest : NSObject{

public override init(){}

 public func gettingLyrics(url : String) -> String {

    var endResults = ""

    DefaultProvider.request(Route(path:"\(url)")).responseJSON { (response:Response<Any>) in
        switch response.result{
        case .success(let json):
         endResults = String(describing:json)
            print(endResults)
          case .failure(let error):
            print("error: \(error)")
            }
        }
        return endResults
    }


}