English 中文(简体)
问 题
原标题:SQL query Question

Im 目前学习,有一些问题,是如何做以下工作:

表:

Planned Order
    id  Item    Date
    0   1   2011-01-24
    1   1   2011-01-26
    2   1   2011-01-28
    3   2   2011-01-24
    4   3   2011-01-27

Customer Order
    id  Item    Date
    4   2   2011-01-25
    3   2   2011-01-24
    2   2   2011-01-24
    1   1   2011-01-26
    0   1   2011-01-24

我不想提出以下问题:

Item  Category          2011-01-24      2011-01-25   2011-01-26   
1     Customer_Order    1               NULL         1      
1     Planned_Order     1               NULL         1
2     Customer_Order    2               NULL         NULL
2     Planned_Order     1               NULL         NULL
3     Planned_Order     NULL            NULL         NULL

Where the count under the date is how many times the item was ordered that day.

即便有原 code法,还是有可能这样做? 我认识到,可以通过其他语文做到这一点,例如 per,操纵数据,并通过多种数据基获取,但我希望从原始数字中直接这样做。

我找不到一个指挥(raw)来从一栏中转换一个电梯,将其转换成行。 我是如何问及上述日期和每一日期的顺序。

问题回答

在Sql服务器(2005/2008年)中,经营人必须做什么(PIVOT)。

SELECT Item,Category,[2011-01-24], [2011-01-25], [2011-01-26]
FROM
(SELECT Item,  Planned_Order  Category, Date
 FROM   [Planned Order]
 union all
 SELECT Item,  Customer_Order , Date
 FROM   [Customer Order]
 ) AS SourceTable
PIVOT
(
COUNT(*)
FOR Date IN ([2011-01-24], [2011-01-25], [2011-01-26])
) AS PivotTable;

2. 可行的办法:

select item,  Customer_Order  Category,
  COUNT(case when Date= 2011-01-24  then 1 else 0 end) as `2010-01-24`,
  COUNT(case when Date= 2011-01-25  then 1 else 0 end) as `2010-01-25`,
  COUNT(case when Date= 2011-01-26  then 1 else 0 end) as `2010-01-26`
from customer_order
GROUP BY item
union all
select item,  Planned_Order  Category,
  COUNT(case when Date= 2011-01-24  then 1 else 0 end) as `2010-01-24`,
  COUNT(case when Date= 2011-01-25  then 1 else 0 end) as `2010-01-25`,
  COUNT(case when Date= 2011-01-26  then 1 else 0 end) as `2010-01-26`
from planned_order
GROUP BY item
ORDER BY item, Category

The problem is that you need to know all the dates you want to display up front. The only DBMS (I know of) to support a dynamic list of columns is Oracle. Other DBMS will require you to generate dynamic SQL to create the CASE statements, one for each distinct date encountered.

唯一不可运输的部分是,由于使用日期作为外地名称,你需要

`2010-01-24` backticks here for MySQL
"2010-01-24" or [2010-01-24] for SQL Server
"2010-01-24" for Oracle
etc




相关问题
SQL SubQuery getting particular column

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 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

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

PHP array callback functions for cleaning output

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 ...

OracleParameter and DBNull.Value

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 ...

Running numbers in SQL

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 ...

How to get SQL queries for each user where env is production

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: ...

热门标签