简体   繁体   English

使用2个表报告状态SQL查询

[英]Using 2 tables to report on a status SQL Query

I'm writing a report that brings back Landstatus's for our new clients. 我正在写一份报告,为我们的新客户带回Landstatus的报告。 We have 3 options (Gold, Silver and Bronze and null if the user doesn't add there status). 我们有3个选项(金,银和青铜,如果用户未在其中添加状态,则为null)。 The data gets stored in 2 different tables dbo.tbl_Profile_ProjectLand and dbo.Tasks 数据存储在2个不同的表dbo.tbl_Profile_ProjectLanddbo.Tasks

At times i find that dbo.tbl_Profile_ProjectLand has a diffrent landstatus than dbo.Tasks 有时我发现dbo.tbl_Profile_ProjectLand具有不同的土地dbo.Tasks

For my report i want to use the dbo.tbl_Profile_ProjectLand as my main table and if that doesn't have a result for landstatus then use the landstatus in the dbo.Tasks 对于我的报告,我想使用dbo.tbl_Profile_ProjectLand作为我的主表,如果该表没有Landstatus的结果,则在dbo中使用dbo.Tasks

The problem is that i cant seem to get the query right. 问题是我似乎无法正确查询。 here is my code. 这是我的代码。

SELECT 
Count
            ((CASE WHEN pp.landstatus > 0 THEN pp.landstatus
            ELSE t.landstatus END)) As Taken,
                ls.Description
FROM         dbo.Tasks AS t 
    LEFT JOIN
             tbl_User AS u 
    ON t.TakenBy = u.UserId 
    LEFT JOIN dbo.tbl_Profile_ProjectLand AS pp
ON t.Fk_ProfileID = pp.Fk_ProfileID
    LEFT JOIN
             tbl_DDLandStatus AS ls 
    ON t.LandStatus = ls.Id
    LEFT JOIN 
    dbo.tbl_EnquiryType AS e
    ON t.EnquiryType = e.EnquirytypeId
where
(Month(t.DueDate) = Month(getdate()) 
AND YEAR(t.DueDate) = YEAR(getdate ()))and t.EnquiryType = 1  
GROUP BY 
ls.Description 
ORDER BY ( CASE ls.Description
              WHEN 'Gold - owned' THEN 1
              WHEN 'Silver - identified / offered' THEN 2
              WHEN 'Bronze - no land' THEN 3
              ELSE 0
            END)

This brings back the following results 这带来了以下结果

Taken   Description
40      NULL
34      Gold - owned
28      Silver - identified / offered
8       Bronze - no land

Now i have checked the 40 null result and this is not correct as there is Null value in the dbo.tbl_Profile_ProjectLand table and a 6 in the dbo.Tasks that represents a Bronze status. 现在,我已经检查了40空结果和存在空值,这是不正确dbo.tbl_Profile_ProjectLand表,并在6 dbo.Tasks表示铜奖状态。

I hope i have explained this OK as iv almost confused myself here. 我希望我已经解释了这个问题,因为我在这里几乎把自己弄糊涂了。 Any help would be greatly appreciated. 任何帮助将不胜感激。 Thanks 谢谢

I have found a way to do this however i cant seem to group them. 我已经找到了一种方法,但是我似乎无法将它们分组。 here is my new code 这是我的新代码

SELECT 
COUNT ((CASE WHEN pp.landstatus > 0 THEN pp.landstatus
            ELSE t.landstatus END)) As Taken,
((CASE WHEN ls.Description > '' THEN ls.Description
            ELSE lst.description END)) As 'Description'
FROM dbo.Tasks AS t 
LEFT JOIN tbl_DDLandStatus AS lst
    ON t.landstatus = lst.Id 
LEFT JOIN dbo.tbl_Profile_ProjectLand AS pp
    ON t.Fk_ProfileID = pp.Fk_ProfileID
LEFT JOIN tbl_DDLandStatus AS ls 
    ON pp.LandStatus = ls.Id
WHERE (Month(t.DueDate) = Month(getdate()) 
AND YEAR(t.DueDate) = YEAR(getdate ()))and t.EnquiryType = 1  
GROUP BY 
ls.Description,lst.description
ORDER BY ( CASE ls.Description
              WHEN 'Gold - owned' THEN 1
              WHEN 'Silver - identified / offered' THEN 2
              WHEN 'Bronze - no land' THEN 3
              ELSE 0
            END)

This brings back this result which is spot on but doesn't group. 这将带回该结果,该结果在现场但未分组。

Taken   Description
22      NULL
7       Bronze - no land
22      Gold - owned
14      Silver - identified / offered
5       Gold - owned
13      Gold - owned
7       Gold - owned
16      Silver - identified / offered
1       Silver - identified / offered
1       Silver - identified / offered
11      Silver - identified / offered

Any idea how i can get them grouped by description? 任何想法,我如何可以将它们按描述分组?

Can u modify the case like this and check 您可以修改这种情况并检查吗

Select count(case when pp.landstatus is not null then pp.landstatus 
              ELSE t.landstatus END) as taken , your query

Changing the nature of the joins to accommodate the difference in status values is correct. 更改联接的性质以适应状态值的差异是正确的。

I think you will also find that you don't need the case expression in the COUNT() 我想您还会发现您不需要在COUNT()中使用case表达式

All you need to do is tweak you most recent query in the group by to use the same case expression you use for the description column. 您需要做的就是调整组中的最新查询,以使用与描述列相同的大小写表达式。

SELECT
      COUNT(*) as Taken
    , ((CASE
            WHEN ls.Description > ''
                  THEN ls.Description
            ELSE lst.description
      END)) AS 'Description'
FROM dbo.Tasks AS t
      LEFT JOIN tbl_DDLandStatus AS lst             ON t.landstatus = lst.Id
      LEFT JOIN dbo.tbl_Profile_ProjectLand AS pp   ON t.Fk_ProfileID = pp.Fk_ProfileID
      LEFT JOIN tbl_DDLandStatus AS ls              ON pp.LandStatus = ls.Id
WHERE (MONTH(t.DueDate) = MONTH(GETDATE())
      AND YEAR(t.DueDate) = YEAR(GETDATE()))
      AND t.EnquiryType = 1
GROUP BY
      (CASE
            WHEN ls.Description > ''
                  THEN ls.Description
            ELSE lst.description
      END)
ORDER BY
      (CASE ls.Description
            WHEN 'Gold - owned'
                  THEN 1
            WHEN 'Silver - identified / offered'
                  THEN 2
            WHEN 'Bronze - no land'
                  THEN 3
            ELSE 0
      END)

--- ---


Below: previous attempt (fail): Not quite sure of these: 下图:先前尝试(失败):不太确定以下这些:

  • there are 2 tables not referenced (required for the count?) 有2个表未引用(需要计数吗?)
  • the case expression is the equivalent of count(*), I think you need to NOT include the else, OR do "else NULL". case表达式等于count(*),我认为您不需要包含else或执行“ else NULL”。

Try this: 尝试这个:

SELECT
      COUNT
      (CASE
            WHEN pp.landstatus > 0
                  THEN pp.landstatus
      END)                             AS Taken
    , COALESCE(ls.Description, 'NULL') AS Description
FROM dbo.Tasks AS t
  LEFT JOIN tbl_User AS u                      ON t.TakenBy = u.UserId
  LEFT JOIN dbo.tbl_Profile_ProjectLand AS pp  ON t.Fk_ProfileID = pp.Fk_ProfileID
  LEFT JOIN tbl_DDLandStatus AS ls             ON t.LandStatus = ls.Id
  LEFT JOIN dbo.tbl_EnquiryType AS e           ON t.EnquiryType = e.EnquirytypeId
WHERE (MONTH(t.DueDate) = MONTH(GETDATE())
      AND YEAR(t.DueDate) = YEAR(GETDATE()))
      AND t.EnquiryType = 1
GROUP BY
      COALESCE(ls.Description, 'NULL')
ORDER BY
      (CASE ls.Description
            WHEN 'Gold - owned'
                  THEN 1
            WHEN 'Silver - identified / offered'
                  THEN 2
            WHEN 'Bronze - no land'
                  THEN 3
            ELSE 0
      END)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM