首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用“AS”语法?

如何使用“AS”语法?
EN

Stack Overflow用户
提问于 2020-01-17 06:24:09
回答 1查看 51关注 0票数 0

我需要在不同的表中使用不同的数据,因为我尝试了所有的代码,它们都是一个接一个地工作的,ı不是一起工作的,我尝试了两种方法,但是ıts和工作的ı都认为roung语法ıresarch,但是ı在sql中是新的。

我的第一条路:

代码语言:javascript
复制
            string cmd = @" SELECT COUNT(Ref) AS data,"; 
        cmd += @" (select count(CustomerID) as data2 from Contract where DATEDIFF(DAY,StartDate,GETDATE()) between 0 and 30),";
        cmd += @" (select count(distinct(CustomerID)) as data3 from Contract where FinishDate > GETDATE()),";
        cmd += @" (select count(Ref) as data4 from Support where DATEDIFF(DAY,StartDate,GETDATE()) between 0 and 30)";
        cmd += @" FROM Customer WHERE (Deleted = 0 or Deleted is null) ";

我的第二条路:

代码语言:javascript
复制
    string cmd = @" SELECT COUNT(Ref) ,"; 
        cmd += @" (select count(CustomerID) from Contract where DATEDIFF(DAY,StartDate,GETDATE()) between 0 and 30) as data2 ,";
        cmd += @" (select count(distinct(CustomerID))  from Contract where FinishDate > GETDATE()) as data3,";
        cmd += @" (select count(Ref)  from Support where DATEDIFF(DAY,StartDate,GETDATE()) between 0 and 30) as data4";
        cmd += @" FROM Customer WHERE (Deleted = 0 or Deleted is null) AS data ";
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-17 06:35:34

别名必须在查询级别分配,而不是在子查询(第一个变体)中分配。

其中条件不能有别名(第二个变体)。

代码语言:javascript
复制
SELECT COUNT(Ref) AS data, 
       ( select count(CustomerID)
         from Contract 
         where DATEDIFF(DAY,StartDate,GETDATE()) between 0 and 30) as data2 , 
       ( select count(distinct(CustomerID))
         from Contract 
         where FinishDate > GETDATE()) as data3, 
       ( select count(Ref) 
         from Support 
         where DATEDIFF(DAY,StartDate,GETDATE()) between 0 and 30)  as data4
FROM Customer 
WHERE (Deleted = 0 or Deleted is null) 
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59782183

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档