我有一张表格,内有以下几行:
""https://i.sstatic.net/FTwGa.png" alt="此处输入图像描述"/ >
我想创建一个显示以下结果的视图(不改变原来的表格) :
""https://i.sstatic.net/R7XmY.png" alt="此处输入图像描述"/ >
对于每条年、日、日、月和年都一样的行,我想留下一条与成本和计数一致的行,并在其他行中插入0。
我有一张表格,内有以下几行:
""https://i.sstatic.net/FTwGa.png" alt="此处输入图像描述"/ >
我想创建一个显示以下结果的视图(不改变原来的表格) :
""https://i.sstatic.net/R7XmY.png" alt="此处输入图像描述"/ >
对于每条年、日、日、月和年都一样的行,我想留下一条与成本和计数一致的行,并在其他行中插入0。
这里的便携式方法不需要 < code> paptition 。 我假设一组中超过一行的您不会有相同的 < code> datetimeIN 值 :
select t.id, t.day, t.month, t.year,
case when tm.id is null then 0 else t.cost end as cost,
case when tm.id is null then 0 else t.Count end as Count,
t.datetimeIN, t.datetimeOUT
from MyTable t
left outer join (
select id, day, month, year, min(datetimeIN) as minIN
from MyTable
group by id, day, month, year
) tm on t.id = tm.id
and t.day = tm.day
and t.month = tm.month
and t.year = tm.year
and t.datetimeIN = tm.minIN
你可以做这样的事情:
SELECT id, day, month, year,
CASE WHEN nNum = 1 then cost else 0 end as cost,
CASE WHEN nNum = 1 then "Count" else 0 end as "Count",
datetimeIN, datetimeOUT
FROM (
SELECT id, day, month, year,
cost, "Count", datetimeIN, datetimeOUT,
row_number() OVER (PARTITION BY id, day, month, year
ORDER BY datetimeIN) as nNum
FROM TableName
) A
它使用 < code> row_ number () 来编号行,然后使用 < code> CASE 语句来挑出第一个语句,使其行为不同。
http://sqlfiddle.com/#! 3/f6fc8/5" rel="nofollow" > 见这里的 SQL Fiddle 工作。
或,使用共同表格表达式:
with commonTableExp ([day], [month], [year], minDate) as (
select [day], [month], [year], min(datetimeIn)
from #temp
group by [day], [month], [year])
select id,
dt.[day],
dt.[month],
dt.[year],
case when datetimein = minDate then [cost] else 0 end,
case when datetimein = minDate then [count] else 0 end,
dateTimeIn
from #temp dt join commonTableExp cte on
dt.[day] = cte.[day] and
dt.[month] = cte.[month] and
dt.[year] = cte.[year]
order by dateTimeIn
Select id, [day], [month], [year], Case When K.RowID = 1 Then [cost] Else 0 End as Cost, Case When K.RowID = 1 Then [count] Else 0 End as [count], [DateTimeIN], [DateTimeOut] From
(
select ROW_NUMBER() Over(Partition by id, [day], [month], [year] Order by ID ) as RowID, * From Testing
)K
Drop table Testing
http://sqlfiddle.com/ #! 3/ 6772b/ 1, rel = “ nofollow” > 详情请见 SQL Fiddle
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: ...