我的数据:
$(document).ready(function(){
$(".comments_link").click(function(e){
e.preventDefault();
$(".section").toggle();
});
})
我需要选择DISCINCT并返回如下内容:
USERNAME | DATE
---------------------
USER1 | 7-1-2016
USER1 | 7-5-2016
USER1 | 7-8-2016
USER2 | 7-2-2016
USER2 | 7-5-2016
USER2 | 7-6-2016
所以,要帮助理解像这样的查询的需要。 我想以更“类似日历”的格式输出任何给定周的列表,其中数据以更线性(每行一行)格式存储。
谢谢。
答案 0 :(得分:1)
这称为$unsorted = json_decode(stripslashes($_POST['unsorted']));
。以下是使用table pivoting
的一个选项:
conditional aggregation
答案 1 :(得分:0)
在转动之前,如果你不打算用一堆条件case语句,你将需要一个日期表,因为你想要包含NULL。 I.E.如果你有一个日期的BUNCH。我建议首先填充CTE
并加入PIVOT
。然后,您将使用动态SQL
--Staging table
IF OBJECT_ID ('tempdb..#HoldingTable') IS NOT NULL DROP TABLE #HoldingTable
--This is the date range you care about. You can set it to the MAX / MIN of your table if you want
DECLARE @DateFrom DATE
SET @DateFrom = '2016-07-01'
DECLARE @DateTo DATE
SET @DateTo = '2016-07-10'
;WITH DateRanges AS
(
SELECT @DateFrom AS 'DateValue'
UNION ALL
SELECT DATEADD(DAY, 1, DateValue)
FROM DateRanges
WHERE DateValue < @DateTo
),
--This is just me populating your test data
Data as(
SELECT 'USER1' AS USERNAME, '7/1/2016' AS DATE
UNION ALL
SELECT 'USER1' AS USERNAME, '7/5/2016' AS DATE
UNION ALL
SELECT 'USER1' AS USERNAME, '7/8/2016' AS DATE
UNION ALL
SELECT 'USER2' AS USERNAME, '7/2/2016' AS DATE
UNION ALL
SELECT 'USER2' AS USERNAME, '7/5/2016' AS DATE
UNION ALL
SELECT 'USER2' AS USERNAME, '7/6/2016' AS DATE
)
--Bring everything together for the dynamic SQL
SELECT *
INTO #HoldingTable
FROM DateRanges dt
LEFT JOIN Data d on d.DATE = dt.DateValue
OPTION (MAXRECURSION 1000)
--You'll see your data in vertical form here... we are about to pivot this
SELECT * FROM #HoldingTable
DECLARE @DynamicPivotQuery AS NVARCHAR(MAX)
DECLARE @ColumnName AS NVARCHAR(MAX)
--Here is your dynamic pivot based off your date range
--Get distinct values of the PIVOT Column
SELECT @ColumnName= ISNULL(@ColumnName + ',','')
+ QUOTENAME(DateValue)
FROM (SELECT DISTINCT DateValue FROM #HoldingTable) AS DateValue
--Prepare the PIVOT query using the dynamic
SET @DynamicPivotQuery =
N'SELECT USERNAME, ' + @ColumnName + '
FROM #HoldingTable
PIVOT(MAX(Date)
FOR DateValue IN (' + @ColumnName + ')) AS PVTTable WHERE USERNAME IS NOT NULL'
--Execute the Dynamic Pivot Query
EXEC sp_executesql @DynamicPivotQuery
答案 2 :(得分:0)
您可以像这样转动表格(SELECT [USERNAME],
[7/1/2016], [7/2/2016], [7/3/2016], [7/4/2016], [7/5/2016],[7/6/2016],[7/7/2016],[7/8/2016]
FROM [yourTable] AS SourceTable
PIVOT
(
MAX([DATE])
FOR [DATE] IN ([7/1/2016], [7/2/2016], [7/3/2016], [7/4/2016], [7/5/2016],[7/6/2016],[7/7/2016],[7/8/2016])
) AS PivotTable;
是原始数据表):
$('#in').change(function() {
$(this).val(parseFloat($(this).val()).toFixed(2));
})
答案 3 :(得分:0)
您可以将STUFF与XML一起使用,如下所示:
declare @calendar table(dates datetime)
declare @i tinyint = 1
while (@i<=8)
begin
insert into @calendar
select '7-'+cast(@i as char(1))+'-2016'
set @i=@i+1
end
declare @test table (userid varchar(10), userDate datetime)
insert into @test
select 'user1','7-1-2016'
union
select 'user1','7-5-2016'
union
select 'user1','7-8-2016'
union
select 'user2','7-2-2016'
union
select 'user2','7-5-2016'
union
select 'user2','7-6-2016'
; with CTE as (
select d.userid,dates,userDate
from (select * from @calendar c cross apply (select distinct userid from @test) u) d
left join @test t
on d.dates=t.userDate
and d.userid=t.userid
--order by d.userid,t.userdate,d.dates
)
SELECT userId,
STUFF((
SELECT ','+
case when userDate is null then 'null' else convert(varchar(20),userDate,101) end
AS [text()]
FROM CTE CC
WHERE CC.userid=C.userid
FOR XML PATH('')),1,1,'') AS Dates
FROM (
select distinct userId
FROM CTE
) C