假设我们有一个带有用户ID、日历日期和任务的表格Name作为列。
请让我从 sql Query 中知道, 对于用户 id, 是否可以找到它是否是当天在表格中输入的第一张记录 。
假设我们有一个带有用户ID、日历日期和任务的表格Name作为列。
请让我从 sql Query 中知道, 对于用户 id, 是否可以找到它是否是当天在表格中输入的第一张记录 。
select case when ((select count(*) from table where convert(Varchar,calendardate,111)=Convert(Varchar,GetDate(),111) and userid=(your userid))>0) then 1 else 0 end
如果表格在同一天含有一个用户的多个记录,您必须忽略时间部分。
Select Count(*) From <TableName> Whre convert(Varchar,CalendarDate,111)=Convert(Varcar,GetDate(),111)
表中记录数的计数
declare @count int
select @count=count(*) from table where
convert(Varchar,CalendarDate,111)=Convert(Varcar,GetDate(),111)
and UserID=@userid
if @count = 1
begin
print first record
end
如果计数为 & gt; 1, 则计数为 & gt; 1 而非第一个记录, 如果计数为第一个记录
您可以使用窗口函数 < a href=" http://msdn.microsoft.com/ en- us/library/ms186734.aspx" rel = "nofollow" >ROW_NUMBER () 在这里 。
SELECT UserID,
CalendarDate,
TaskName,
ROW_NUMBER() OVER(PARTITION BY UserID, DATEADD(DAY, 0, DATEDIFF(DAY, 0, CalendarDate)) ORDER BY CalendarDate, TaskName) AS RowNumber
FROM T
或者如果您在2008年或以后有SQL-Server:
ROW_NUMBER() OVER(PARTITION BY UserID, CAST(CalendarDate AS DATE) ORDER BY CalendarDate, TaskName) AS RowNumber
这样做是指定用户编号和日历日期(随着时间段被删除)的每一种组合,它按日期顺序(留下时间段,然后是任务名称)自有递增序列。 partition by
告诉 >> ROW_NUMBER
函数何时重新开始 1 (即新日期或新的用户代号),然后 > ORDER by
一节告诉 ROW_NUMOER
如何排序序列。 例如 。
Select count(*) from table where userid =:userid and date =current_date
如果您想要在插入输入的记录后检查, 输入的记录是先运行还是先运行, 在插入前插入其它记录后运行 。
I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...
I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域
We have a restaurant table that has lat-long data for each row. We need to write a query that performs a search to find all restaurants within the provided radius e.g. 1 mile, 5 miles etc. We have ...
What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2
I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...
we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...
I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...
I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...