具有子表结果为NULL的SQL子查询

时间:2018-08-06 10:47:09

标签: sql

我尝试仅在单个表中选择子查询。但由于某种原因,我得到了NULL结果。

我的桌子看起来像这样。

| id | ItemCode | ItemAmount | Counter  |
-----------------------------------------
| 1  | 001      | 1          | Counter-1 |
| 2  | 001      | 1          | Counter-2 |
| 3  | 002      | 2          | Counter-1 |
| 4  | 002      | 2          | Counter-2 |
| 5  | 002      | 1          | Counter-2 |

我已经尝试过此SQL:

select 
    id,
    itemCode, 
    (select ItemAmount where Counter = 'Counter-1') as 'Count 1 Result',
    (select Counter where Counter = 'COUNTER-1') as 'Count Is 1',
    (select ItemAmount where Counter = 'Counter-2') as 'Count 2 Result',
    (select Counter where Counter = 'COUNTER-2') as 'Count Is 2',
from 
    My_Table

我得到的结果是:

| id | ItemCode | Count 1 Result | Count Is 1 | Count 2 Result | Count Is 2 |
----------------------------------------------------------------------
| 1  | 001      | 1               | Counter-1 | NULL           | NULL |
| 2  | 001      | NULL            | NULL      | 1              | Counter-2    |
| 3  | 002      | 2               | Counter-1 | NULL           | NULL |
| 4  | 002      | NULL            | NULL      | 2              | Counter-2    |
| 5  | 002      | NULL            | NULL      | 1              | Counter-2    |

如您所见,我得到了带有NULL值的NULL结果。我该如何处理这样的结果:

| id | ItemCode | Count 1 Result | Count Is 1 | Count 2 Result | Count Is 2 |
-------------------------------------------------------------------------
| 1  | 001      | 1               | Counter-1 | 2              | Counter-2 |
--------------------------------------------------------------------------
| 2  | 002      | 2               | Counter-1 | 3              | Counter-2 |
--------------------------------------------------------------------------

我要使其不再为NULL值,并且如果计数器和物料代码具有相同的值,则SUM物料数量中不存在双物料代码。

有可能用一张桌子做吗?如果是我该怎么做。预先感谢

4 个答案:

答案 0 :(得分:1)

尝试条件聚合,例如:

SELECT min(id) id,
       itemcode,
       sum(CASE
             WHEN counter = 'Counter-1' THEN
               itemamount
             ELSE
               0
           END) count1result,
       'Counter-1' countis1,
       sum(CASE
             WHEN counter = 'Counter-2' THEN
               itemamount
             ELSE
               0
           END) count2result,
       'Counter-2' countis2
       FROM my_table
       GROUP BY itemcode;

答案 1 :(得分:0)

您可以尝试使用集合函数条件来实现。

这是SQL服务器示例:

#------------------
markdownToHTML("MyReport.Rmd", output="MyReport.html", options=c("toc", "use_xhtml", "smartypants",  "mathjax", "highlight_code"))

send.mail(from = "myemail@example.com",
            to = c("myemail@example.com", 
                   "myotheremail@example.com"),
            subject = "Email with a Markdown document in HTML at the message body",
            body = "MyReport.html",
            html = TRUE,
            inline = TRUE,
            smtp = list(host.name = "localhost"),
            send = TRUE)
#------------------

查询1

CREATE TABLE My_Table(
  id INT,
  ItemCode VARCHAR(50),
  ItemAmount INT,
  Counter VARCHAR(50)
);


 INSERT INTO My_Table VALUES (1, '001', 1 ,'Counter-1');
 INSERT INTO My_Table VALUES (2, '001', 1 ,'Counter-2');
 INSERT INTO My_Table VALUES (3, '002', 2 ,'Counter-1');
 INSERT INTO My_Table VALUES (4, '002', 2 ,'Counter-2');
 INSERT INTO My_Table VALUES (5, '002', 1 ,'Counter-2');

Results

select 
    ROW_NUMBER() OVER(ORDER BY itemCode) id,
    itemCode, 
    SUM(CASE WHEN Counter = 'Counter-1' THEN ItemAmount ELSE 0 END) as 'Count 1 Result',
    MAX(CASE WHEN Counter = 'COUNTER-1' THEN Counter END) as 'Count Is 1',
    SUM(CASE WHEN Counter = 'Counter-2' THEN ItemAmount ELSE 0 END) as 'Count 2 Result',
    MAX(CASE WHEN Counter = 'COUNTER-2' THEN Counter  END) as 'Count Is 2'
from 
    My_Table
GROUP BY 
    itemCode

答案 2 :(得分:0)

尽管我不确定在不打扰您的代码的情况下期望什么,但我已经给出了查询。根据您的要求实施

\u1f310 => "\uD83C\uDF10"

答案 3 :(得分:0)

对于数据检索部分,以下内容将为您提供数据, 通过对ItemCode和Counter分组:

select
    ItemCode,
    Counter,
    min(Id) as Id,
    sum(ItemAmount) as ItemAmount,
from 
    My_Table
group by ItemCode, Counter

ItemCode  Counter    Id   ItemAmount
========  =========  ===  ==========
001       Counter-1    1           1
001       Counter-2    1           2
002       Counter-1    3           1
002       Counter-2    3           3

要显示称为“ 数据透视表”表的行到列,有几种解决方案,一种是通用SQL解决方案:

在现代SQL中:

select ItemCode,
    ItemAmount filter (where Counter = 'Counter-1') as A1,
    ItemAmount filter (where Counter = 'Counter-2') as A2
from (select
        ItemCode,
        Counter,
        min(Id) as Id,
        sum(ItemAmount) as ItemAmount,
    from 
        My_Table
    group by ItemCode, Counter)
group by ItemCode

如果使用的SQL仍不支持filter

select ItemCode,
    sum(case when Counter = 'Counter-1' then ItemAmount end) as A1,
    sum(case when Counter = 'Counter-2' then ItemAmount end) as A2,