SQL Server:交叉应用-为空结果记录0

时间:2019-03-21 12:25:22

标签: sql-server cross-apply

T1是公司及其(多个用户)的表,T2是注册用户的表。我计算出,对于T1中的每个公司,他们在T2中有多少用户,但是需要c3才能显示在#regUser == 0的结果表中:

T1:

company         user
c1               u1
c1               u2
c2               u2
c2               u3
c3               u4
c3               u1

T2:

user
u2
u3

因此,结果表应如下所示:

company         #regUser
c1                 1
c2                 2
c3                 0

使用以下代码,我只会得到非空公司的结果:

select t1s.company, count(1)
from (select * from t1) t1s
  cross apply (select *
               from t2 t2s
               where t2s.reguser = t1s.[user]) t12s
group by t1s.company

谢谢

4 个答案:

答案 0 :(得分:3)

只使用左联接

   select t1.company,count(t2.user)
     from t1 left join t2 on t1.user=t2.user
group by t1.company

根据您的要求,子查询不是必需的

但是,如果您想使用apply,则需要如下查询

select t1s.company, count(t12s.Users)
from (select * from t1) t1s
  outer apply (select Users
               from t2 t2s
               where t2s.Users = t1s.[Users]) t12s
group by t1s.company

输出

company     #regUser
c1          1
c2          2
c3          0

demolink

答案 1 :(得分:2)

您可以使用GROUP BY来获取左表的所有信息以及来自右表的匹配信息。通过使用COUNT,您可以按公司对行进行分组,并获得每个公司的SELECT t1.company, COUNT(t2.[user]) AS regUser FROM t1 LEFT JOIN t2 ON t1.[user] = t2.[user] GROUP BY t1.company ORDER BY t1.company ASC 注册用户:

CROSS APPLY

您也可以使用SELECT t1.company, SUM(CASE WHEN t1.[user] = t2.[user] THEN 1 ELSE 0 END) AS regUser FROM t1 CROSS APPLY t2 GROUP BY t1.company ORDER BY t1.company ASC 解决此问题:

if

demo on dbfiddle.uk

答案 2 :(得分:1)

您需要的只是左联接:

select company,count(t2.[user])
from t1 left outer join t2 on t1.[user]=t2.[user]
group by company

问题的查询过于复杂。例如,from (select * from t1) t1s等效于from t1 as t1s

答案 3 :(得分:0)

只是LEFT JOINSUM()

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.managem.model.Region.pays, could not initialize proxy - no Session
    org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:582)
    org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:201)
    org.hibernate.collection.internal.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:145)
    org.hibernate.collection.internal.PersistentBag.size(PersistentBag.java:261)
    com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:97)
    com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:25)
    com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:727)
    com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:719)
    com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155)
    com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:145)
    com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:107)
    com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:25)
    com.fasterxml.jackson.databind.ser.DefaultSerializerProvider._serialize(DefaultSerializerProvider.java:480)
    com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:400)
    com.fasterxml.jackson.databind.ObjectWriter$Prefetch.serialize(ObjectWriter.java:1392)
    com.fasterxml.jackson.databind.ObjectWriter.writeValue(ObjectWriter.java:913)
    org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.writeInternal(AbstractJackson2HttpMessageConverter.java:287)
    org.springframework.http.converter.AbstractGenericHttpMessageConverter.write(AbstractGenericHttpMessageConverter.java:103)
    org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:289)
    org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor.handleReturnValue(HttpEntityMethodProcessor.java:223)
    org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:82)
    org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:119)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:800)
    org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1038)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:897)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

返回:

CREATE TABLE T1(
  Company VARCHAR(20),
  Users VARCHAR(20)
);

CREATE TABLE T2(
  Users VARCHAR(20)
);

INSERT INTO T1 VALUES
('c1', 'u1'),
('c1', 'u2'),
('c2', 'u2'),
('c2', 'u3'),
('c3', 'u4'),
('c3', 'u1');

INSERT INTO T2 VALUES
('u2'),
('u3');

SELECT T1.Company,
       SUM(CASE WHEN T2.Users IS NULL THEN 0 ELSE 1 END) Cnt
FROM T1 LEFT JOIN T2
ON T1.Users = T2.Users
GROUP BY T1.Company;

Live Demo