我该如何为此编写查询?我不是用 SQL 编写这么多复杂查询的专家。我忘了在这里提到出价 3、4。
问问题
138 次
4 回答
4
根据大卫的查询,消除重复出价并通过支票类型进行限制。
SELECT
a.bid, min(a.time) checkin, ISNULL(min(b.time), '') checkout
FROM
myTable a
LEFT JOIN
myTable b ON a.bid = b.bid
WHERE
a.type = "Check In"
AND
b.type = "Check Out"
GROUP BY
a.bid
ORDER BY
a.time
于 2009-12-11T10:56:50.850 回答
2
SELECT
a.bid, a.time checkin, ISNULL(b.time, '') checkout
FROM
myTable a
LEFT JOIN
myTable b ON a.bid = b.bid AND b.type = 'Check Out'
WHERE
a.type = 'Check In'
ORDER BY
a.time
编辑
为了回应您的评论,看到有多个相同的记录bid
,并且您只希望在输出中每个记录一个bid
,您需要指定输出中需要哪些记录。当有多个时,您如何决定选择哪个值?如果您总是想要最早签到和最晚结帐,您可以执行以下操作:
SELECT
a.bid,
MIN(a.time) checkin,
ISNULL((SELECT
MAX(time)
FROM
myTable
WHERE
bid = a.bid
AND type = 'Check Out'), '') checkout
FROM
myTable a
WHERE
a.type = 'Check In'
GROUP BY
a.bid
如果那不是您想要的;调整使用MIN
和MAX
适应您的需要。
于 2009-12-11T10:48:04.890 回答
1
这将找到每个投标 ID 的最短签入和签出时间。
select bid,
(select min(time) from table b where a.bid = b.bid and type = "Check In") as CheckIn,
(select min(time) from table c where a.bid = c.bid and type = "Check Out") as CheckOut
from table c
group by c.bid
于 2009-12-11T10:51:18.773 回答
0
我想你正在寻找这样的东西:
SELECT
`bid`,
MAX(IF(`type` = 'Check In', `time`, NULL)) AS `CheckIn`,
MAX(IF(`type` = 'Check Out', `time`, NULL)) AS `CheckOut`
FROM `yourtable`
GROUP BY `bid`
于 2009-12-11T10:56:38.620 回答