我正在用C#编写一个读写SQLite的程序(System.Data.Sqlite) 我有一个看起来像这样的表
UserID ProdID Value1 Value2 TIME
------------------------------------------------
1 1 10 1 2011-06-01 10:20:30.333
1 2 20 3 2011-06-01 10:20:30.444
2 1 100 11 2011-06-01 10:20:30.333
2 2 22 32 2011-06-01 10:20:30.444
1 1 20 2 2011-06-01 10:30:30.333
1 2 30 4 2011-06-01 10:30:30.444
2 1 22 22 2011-06-01 10:30:30.333
2 2 33 44 2011-06-01 10:30:30.444
1 1 10 1 2011-06-02 10:20:30.333
1 2 20 3 2011-06-02 10:20:30.444
2 1 11 11 2011-06-02 10:20:30.333
2 2 22 32 2011-06-02 10:20:30.444
1 1 20 2 2011-06-02 10:30:30.333
1 2 30 4 2011-06-02 10:30:30.444
2 1 22 22 2011-06-02 10:30:30.333
2 2 33 44 2011-06-02 10:30:30.444
我需要为每个用户提供三件事。
1)每个产品的最后一次输入,用于Value1和2,总计为两个值,即所有产品的总和。 即对于2011-06-01的用户1,它将是Value1 = 50和Value2 = 6.
我实际上使用了:SELECT * FROM TickData AS a,;
(SELECT USERID, DATE(TIME) AS JUSTDATE, MAX(TIME) AS MTIME;
FROM TickData;
GROUP BY 1,2;
) AS b;
WHERE a.USERID = b.USERID;
AND a.TIME = b.MTIME;
AND STRFTIME('%Y-%m',a.TIME) = STRFTIME('%Y-%m','NOW');
AND a.USERID = 1;
返回:
UserID ProdID Value1 Value2 TIME
------------------------------------------------
1 1 20 2 2011-06-01 10:30:30.333
1 2 30 4 2011-06-01 10:30:30.444
2011-06-01。 然后我遍历数据并在日期相同时添加值。 给我Value1 = 50和Value2 = 6.
2)所有产品的每天Value1的最高入口总额。 即对于2011-06-01的用户2,它将是Value1 = 133。
UserID ProdID Value1 Value2 TIME
------------------------------------------------
2 1 100 11 2011-06-01 10:20:30.333
2 2 33 44 2011-06-01 10:30:30.444
3)所有产品的Value1每天的最低条目总计。 即对于2011-06-01的用户2,它将是Value1 = 44。
UserID ProdID Value1 Value2 TIME
------------------------------------------------
2 2 22 32 2011-06-01 10:20:30.444
2 1 22 22 2011-06-01 10:30:30.333
我还没有运气2或3。
有没有人有任何吸烟
答案 0 :(得分:0)
好好在周末完成自己的工作
1)
SELECT sum(value1), sum(value2) FROM TickData AS a,
(SELECT USERID, PRODID, DATE(TIME) AS JUSTDATE, MAX(TIME) AS MTIME
FROM TickData
GROUP BY 1,2,3
) AS b
WHERE a.USERID = b.USERID
AND a.TIME = b.MTIME
AND STRFTIME('%Y-%m',a.TIME) = STRFTIME('%Y-%m','NOW')
AND a.USERID = 1
GROUP BY JUSTDATE
2)
SELECT sum(Mval) FROM TickData AS a,
(SELECT USERID, PRODID, DATE(TIME) AS JUSTDATE,ID, MAX(value1) AS Mval
FROM TickData
GROUP BY 1,2,3
) AS b
WHERE a.USERID = b.USERID
AND a.ID = b.ID
AND STRFTIME('%Y-%m',a.TIME) = STRFTIME('%Y-%m','NOW')
AND a.USERID = 2
GROUP BY JUSTDATE
3)
SELECT sum(Mval) FROM TickData AS a,
(SELECT USERID, PRODID, DATE(TIME) AS JUSTDATE,ID, MIN(value1) AS Mval
FROM TickData
GROUP BY 1,2,3
) AS b
WHERE a.USERID = b.USERID
AND a.ID = b.ID
AND STRFTIME('%Y-%m',a.TIME) = STRFTIME('%Y-%m','NOW')
AND a.USERID = 2
GROUP BY JUSTDATE
编辑: 可以使用一个查询完成2和3:
SELECT sum(Mval), sum(Mval2) FROM TickData AS a,
(SELECT USERID, PRODID, DATE(TIME) AS JUSTDATE,ID, MAX(PL) AS Mval,MIN(PL) AS Mval2
FROM TickData
GROUP BY 1,2,3
) AS b
WHERE a.USERID = b.USERID
AND a.TIME = b.MTIME
AND STRFTIME('%Y-%m',a.TIME) = STRFTIME('%Y-%m','NOW')
AND a.USERID = 2
GROUP BY JUSTDATE