AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / dba / 问题 / 291190
Accepted
alexistkd
alexistkd
Asked: 2021-04-25 06:00:43 +0800 CST2021-04-25 06:00:43 +0800 CST 2021-04-25 06:00:43 +0800 CST

子查询sql的问题

  • 772

您好我收到此错误:

当不使用 EXISTS 引入子查询时,选择列表中只能指定一个表达式。

这是我当前的 SQL 查询:

DECLARE @UgpEntry VARCHAR(50)

SELECT @UgpEntry = UgpEntry FROM OITM t1 WHERE t1.ItemCode='CID-11418272385'

IF (@UgpEntry = -1)

SELECT
t1.ItemCode as sapitemcode
     , t1.CodeBars as Barcode
     , t1.ItemName as description
     ,LEFT(t1.ItemName,20) as short_description
     , 
           (select max(p.Price)
        from ITM1 p 
        where p.ItemCode = t1.ItemCode 
          and p.PriceList = 1) as [price_1]
     ,
         (SELECT T0.[ItemCode], T2.[U_NAME] as 'User that created Item', t0.updatedate

FROM AITM T0 inner join OUSR T2 on t0.usersign = t2.internal_K

WHERE T0.[ItemCode] = t1.ItemCode

group by T0.[ItemCode], T2.[U_NAME], t0.loginstanc, t0.updatedate

having t0.loginstanc = (select min(loginstanc) from AITM where itemcode = t0.loginstanc)),
     CASE t1.VatGourpSa when 'V0' THEN 4 when 'V1' THEN 1 WHEN 'V2' THEN 2 WHEN 'V3' THEN 3 END as TaxCode,t1.U_GRUPOA, t1.U_GRUPOB, t1.U_GRUPOC, t1.UgpEntry

FROM OITM t1
WHERE t1.ItemCode='CID-11418272385'
 
ELSE

SELECT t1.ItemCode as sapitemcode
     , t1.CodeBars as Barcode
     , t1.ItemName as description
     ,LEFT(t1.ItemName,20) as short_description
     , (select max(p.Price)
        from ITM9 p 
        where p.ItemCode = t1.ItemCode 
          and p.UomEntry = 1 and p.PriceList = 1) as [price_1mananitas],

           (select max(p.Price)
        from ITM1 p 
        where p.ItemCode = t1.ItemCode 
          and p.PriceList = 1) as [preciocajamananitas],
(select max(p.Price)
        from ITM9 p 
        where p.ItemCode = t1.ItemCode 
          and p.UomEntry = 1 and p.PriceList = 2) as [price_2cabra],
               (select max(p.Price)
        from ITM1 p 
        where p.ItemCode = t1.ItemCode 
          and p.PriceList = 2) as [preciocajacabra]

     , CASE t1.VatGourpSa when 'V0' THEN 4 when 'V1' THEN 1 WHEN 'V2' THEN 2 WHEN 'V3' THEN 3 END as TaxCode,t1.U_GRUPOA, t1.U_GRUPOB, t1.U_GRUPOC, t1.UgpEntry

FROM OITM t1
WHERE t1.ItemCode='CID-11418272385'

我刚刚添加了这个子查询:

(SELECT T0.[ItemCode], T2.[U_NAME] as 'User that created Item', t0.updatedate
    
    FROM AITM T0 inner join OUSR T2 on t0.usersign = t2.internal_K
    
    WHERE T0.[ItemCode] = t1.ItemCode
    
    group by T0.[ItemCode], T2.[U_NAME], t0.loginstanc, t0.updatedate
    
    having t0.loginstanc = (select min(loginstanc) from AITM where itemcode = t1.ItemCode))

当我单独运行它时,我会得到这样的结果:

在此处输入图像描述

我想要做的是在我的主查询中添加该选择,以便在原始查询中显示为新列。所以它应该像这样在末尾出现新列(T2.[U_NAME] 和 t0.updatedate):

在此处输入图像描述

我错过了什么?

谢谢你

sql-server-2008 query
  • 1 1 个回答
  • 43 Views

1 个回答

  • Voted
  1. Best Answer
    alexistkd
    2021-04-26T05:49:55+08:002021-04-26T05:49:55+08:00

    用过的:

    WITH sub AS (
        SELECT 
            t1.itemcode as sapitemcode, 
            t1.CodeBars as Barcode, 
            t1.ItemName as description, 
            LEFT(t1.ItemName,20) as short_description,
            --MAX(case when T0.PriceList = 1 then t0.Price else null end) as price_1,  
            case when T0.PriceList = 1 then t0.Price else null end as price_1,  
            NULL as precio_tableta,
            CASE t0.PriceList when '1' THEN 1 END as Price_level_1, 
            --CASE t1.SalUnitMsr when 'PZA' THEN 1 WHEN 'UND' THEN 1 when 'CAJA' THEN 1 when '' THEN NULL END as uom_group_id,
            case when t1.NumInSale > 1 THEN '1' END as uom_group_id,
            (   SELECT T5.[ItemCode]
                FROM AITM T5 inner join OUSR T6 on t5.usersign = t6.internal_K
                WHERE T5.[ItemCode] = 'CID-11418272385'
                group by T5.[ItemCode], T6.[U_NAME], t5.loginstanc, t5.updatedate
                having t5.loginstanc = (select min(loginstanc) 
                            from AITM 
                            where itemcode = 'CID-11418272385')) AS col1,
            (   SELECT T6.[U_NAME] as 'User that created Item'
                FROM AITM T5 inner join OUSR T6 on t5.usersign = t6.internal_K
                WHERE T5.[ItemCode] = 'CID-11418272385'
                group by T5.[ItemCode], T6.[U_NAME], t5.loginstanc, t5.updatedate
                having t5.loginstanc = (select min(loginstanc) 
                            from AITM 
                            where itemcode = 'CID-11418272385')) aS col2,
            (   SELECT t5.updatedate
                FROM AITM T5 inner join OUSR T6 on t5.usersign = t6.internal_K
                WHERE T5.[ItemCode] = 'CID-11418272385'
                group by T5.[ItemCode], T6.[U_NAME], t5.loginstanc, t5.updatedate
                having t5.loginstanc = (select min(loginstanc) 
                            from AITM 
                            where itemcode = 'CID-11418272385')) AS col3,
            CASE t1.VatGourpSa when 'V0' THEN 4 when 'V1' THEN 1 WHEN 'V2' THEN 2 WHEN 'V3' THEN 3 END as Impuesto, 
            t1.U_GRUPOA as Grupo, 
            t1.U_GRUPOB as Departamento, 
            t1.U_GRUPOC as Categoria, 
            t1.NumInSale as ItemsPerSalesUnit, 
            t1.SellItem as SellItem 
        from ITM1 T0
        inner join oitm t1 on t0.itemcode = t1.itemcode
        inner join ouom t2 on t2.uomentry = t0.uomentry  
        where t1.ItemCode='CID-11418386891' 
        and PriceList='1'
    )
    SELECT sub.sapitemcode, 
        sub.Barcode, 
        sub.description, 
        sub.short_description,
        MAX(sub.price_1),  
        sub.precio_tableta,
        sub.Price_level_1, 
        sub.uom_group_id,
        sub.col1,
        sub.col2,
        sub.col3,
        sub.Impuesto, 
        sub.Grupo, 
        sub.Departamento, 
        sub.Categoria, 
        sub.ItemsPerSalesUnit, 
        sub.SellItem 
    FROM sub
    GROUP BY sub.Barcode, 
        sub.description, 
        sub.short_description,
        sub.precio_tableta,
        sub.Price_level_1, 
        sub.uom_group_id,
        sub.col1,
        sub.col2,
        sub.col3,
        sub.Impuesto, 
        sub.Grupo, 
        sub.Departamento, 
        sub.Categoria, 
        sub.ItemsPerSalesUnit, 
        sub.SellItem
    
    • 0

相关问题

  • 连接不同地理区域的数据库的最佳实践

  • 死锁的主要原因是什么,可以预防吗?

  • 我在索引上放了多少“填充”?

  • 是否有开发人员遵循数据库更改的“最佳实践”类型流程?

  • 从 SQL Server 2008 降级到 2005

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目

    • 12 个回答
  • Marko Smith

    如何让sqlplus的输出出现在一行中?

    • 3 个回答
  • Marko Smith

    选择具有最大日期或最晚日期的日期

    • 3 个回答
  • Marko Smith

    如何列出 PostgreSQL 中的所有模式?

    • 4 个回答
  • Marko Smith

    列出指定表的所有列

    • 5 个回答
  • Marko Smith

    如何在不修改我自己的 tnsnames.ora 的情况下使用 sqlplus 连接到位于另一台主机上的 Oracle 数据库

    • 4 个回答
  • Marko Smith

    你如何mysqldump特定的表?

    • 4 个回答
  • Marko Smith

    使用 psql 列出数据库权限

    • 10 个回答
  • Marko Smith

    如何从 PostgreSQL 中的选择查询中将值插入表中?

    • 4 个回答
  • Marko Smith

    如何使用 psql 列出所有数据库和表?

    • 7 个回答
  • Martin Hope
    Jin 连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目 2014-12-02 02:54:58 +0800 CST
  • Martin Hope
    Stéphane 如何列出 PostgreSQL 中的所有模式? 2013-04-16 11:19:16 +0800 CST
  • Martin Hope
    Mike Walsh 为什么事务日志不断增长或空间不足? 2012-12-05 18:11:22 +0800 CST
  • Martin Hope
    Stephane Rolland 列出指定表的所有列 2012-08-14 04:44:44 +0800 CST
  • Martin Hope
    haxney MySQL 能否合理地对数十亿行执行查询? 2012-07-03 11:36:13 +0800 CST
  • Martin Hope
    qazwsx 如何监控大型 .sql 文件的导入进度? 2012-05-03 08:54:41 +0800 CST
  • Martin Hope
    markdorison 你如何mysqldump特定的表? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 对 SQL 查询进行计时? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas 如何从 PostgreSQL 中的选择查询中将值插入表中? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 列出所有数据库和表? 2011-02-18 00:45:49 +0800 CST

热门标签

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve