如何根据价值在不同的列中显示一件事情的结果

时间:2019-07-19 12:23:00

标签: sql sql-server tsql

我需要显示查询结果,以便不同的ans_text出现在不同的ques_id的不同列中。例如,对于此查询,

<band height="11" splitType="Prevent">
            <printWhenExpression><![CDATA[!$F{icon:}]]></printWhenExpression>
            <image scaleImage="RealHeight">
                <reportElement x="0" y="0" width="16" height="10" />
                <imageExpression><![CDATA[$F{icon:}]]></imageExpression>
            </image>
    </band>

当前结果如下:

const clipObjToMask = (ctx, obj, mask) => {
            obj.setCoords();
            const scaleXTo1 = (1 / obj.scaleX);
            const scaleYTo1 = (1 / obj.scaleY);
            ctx.save();

            const ctxLeft = -(obj.width / 2 ) + mask.strokeWidth;
            const ctxTop = -(obj.height / 2 ) + mask.strokeWidth;
            // const ctxWidth = mask.width - mask.strokeWidth;
            // const ctxHeight = mask.height - mask.strokeWidth;

            ctx.translate(ctxLeft, ctxTop);
            ctx.scale(scaleXTo1, scaleYTo1);
            ctx.beginPath();
            ctx.rect(
              mask.left - obj.oCoords.tl.x,
              mask.top - obj.oCoords.tl.y,
              mask.width,
              mask.height
            );
            ctx.closePath();
            ctx.restore();
          };

          const clippingMask = new fabric.Rect({
            originX: 'left',
            originY: 'top',
            left: layer.objectReference.left,
            top: layer.objectReference.top,
            width: layer.objectReference.width,
            height: layer.objectReference.height,
            fill: 'transparent',
            strokeWidth: 0,
            selectable: false
          });
          this.global.canvas.add(clippingMask);
          layer.objectReference.set({
            clipTo: ctx => {
              return clipObjToMask(ctx, layer.objectReference, clippingMask);
            }
          });

问题是,如果我将where子句修改为以下内容,则不会找到非MFG和是的情况,这是不正确的(目的:找到它后,我们必须对其进行修复)

SELECT
DISTINCT
p.ID
,isnull(dbo.fnRemovePatternFromString(p.Last_Name, '%[,-.'']%'), '') as [Last]
,isnull(dbo.fnRemovePatternFromString(p.First_Name, '%[,-.'']%'), '') as [First]
,isnull(dbo.fnRemovePatternFromString(p.Middle_Initial, '%[,-.'']%'), '') as [Middle]
,fs.ANS_TEXT
,fs.ques_id

FROM person p

inner JOIN person_facilities pf ON p.ID = pf.ID
LEFT JOIN dbo.ADD_ANSW fs ON p.id = fs.id and (fs.QUES_ID = 17 or fs.QUES_ID=3)

WHERE
pf.Current_status = 'Employed'
and ( fs.QUES_ID=17 or fs.QUES_ID=3)

如何修改此查询,以便为特定ID查找Non-MFG和Ques_ID = 3的情况?对于上述结果,它将找到Sue Grafton作为结果。我以为也许可以以某种方式更改查询,以便初始查询的结果显示为:

ID    Last    First    Middle    ANS_TEXT   QUES_ID   
1     Veder   Darth    Boo       MFG        3
2     Mouse   Micky              MFG        3
2     Mouse   Micky              Yes        17
3     Cussler Clive              No         17
3     Cussler Clive              Non-MFG    3
4     Grafton Sue      M         Yes        17
4     Grafton Sue      M         Non-MFG    3

然后,如果我将where子句更改为上面显示的内容,则可能会在结果中找到Sue Grafton(非MFG和“是”)的情况。我知道在查看前200个左右的数据后,我的数据中至少有2个这样的情况。

有什么想法可以得到所需的结果吗?我对SQL比较陌生。

4 个答案:

答案 0 :(得分:1)

如何使用UNION

SELECT
DISTINCT
p.ID
,isnull(dbo.fnRemovePatternFromString(p.Last_Name, '%[,-.'']%'), '') as [Last]
,isnull(dbo.fnRemovePatternFromString(p.First_Name, '%[,-.'']%'), '') as [First]
,isnull(dbo.fnRemovePatternFromString(p.Middle_Initial, '%[,-.'']%'), '') as [Middle]
,fs.ANS_TEXT
,fs.ques_id

FROM person p

inner JOIN person_facilities pf ON p.ID = pf.ID
LEFT JOIN dbo.ADD_ANSW fs ON p.id = fs.id and (fs.QUES_ID = 17 or fs.QUES_ID=3)

WHERE
pf.Current_status = 'Employed'
and (fs.ans_text='yes' and fs.QUES_ID=17)

UNION

SELECT
DISTINCT
p.ID
,isnull(dbo.fnRemovePatternFromString(p.Last_Name, '%[,-.'']%'), '') as [Last]
,isnull(dbo.fnRemovePatternFromString(p.First_Name, '%[,-.'']%'), '') as [First]
,isnull(dbo.fnRemovePatternFromString(p.Middle_Initial, '%[,-.'']%'), '') as [Middle]
,fs.ANS_TEXT
,fs.ques_id

FROM person p

inner JOIN person_facilities pf ON p.ID = pf.ID
LEFT JOIN dbo.ADD_ANSW fs ON p.id = fs.id and (fs.QUES_ID = 17 or fs.QUES_ID=3)

WHERE
pf.Current_status = 'Employed'
and (fs.ans_text like ('Non-MFG') and fs.QUES_ID=3)

UNION

SELECT
DISTINCT
p.ID
,isnull(dbo.fnRemovePatternFromString(p.Last_Name, '%[,-.'']%'), '') as [Last]
,isnull(dbo.fnRemovePatternFromString(p.First_Name, '%[,-.'']%'), '') as [First]
,isnull(dbo.fnRemovePatternFromString(p.Middle_Initial, '%[,-.'']%'), '') as [Middle]
,fs.ANS_TEXT
,fs.ques_id

FROM person p

inner JOIN person_facilities pf ON p.ID = pf.ID
LEFT JOIN dbo.ADD_ANSW fs ON p.id = fs.id and (fs.QUES_ID = 17 or fs.QUES_ID=3)

WHERE
pf.Current_status = 'Employed'
and (fs.ans_text=null and fs.QUES_ID=3)

答案 1 :(得分:1)

我认为您正在寻找这样的WHERE条款

where pf.Current_status = 'Employed'
  and fs.ques_id in (3,17)
  and pf.id = fs.id 
  and fs.ans_text =
     ( case when fs.anx_text in ('Non-MFG','MFG','Yes') then
                  'Yes'
            when fs.anx_text in ('Non-MFG','No') then      
                  'Non-MFG'
        end ) 

为查询删除重复的ID值。

答案 2 :(得分:1)

如果我理解您的要求,则您正在尝试查找所有被指定为“非MFG”的雇员,并且对问题3的回答为“是”。

我会使用您的初始查询的修改版本来获取被指定为“非MFG”的员工。由于我们在表ADD_ANSW上有一个过滤器,因此无论如何有效地使其成为内部联接,因此我将其指定为此类。另外,由于我们在WHERE子句中进行过滤,因此我将过滤器从联接中移除了。

从那里,我添加了一个WHERE...EXISTS子句以将结果限制为问题3答案为“是”的“非MFG”员工。 (我总是只在SELECT 1查询中使用EXISTS,以明确我们没有返回任何结果,只是确认是否存在某些东西。)

然后,为了获得所需的输出,我在SELECT子句中包含了一个内联子查询,以返回编号为3的问题的答案(我们已经知道,因此并不十分令人兴奋)。 / p>

SELECT
DISTINCT
  p.ID
  ,isnull(dbo.fnRemovePatternFromString(p.Last_Name, '%[,-.'']%'), '') as [Last]
  ,isnull(dbo.fnRemovePatternFromString(p.First_Name, '%[,-.'']%'), '') as [First]
  ,isnull(dbo.fnRemovePatternFromString(p.Middle_Initial, '%[,-.'']%'), '') as [Middle]
  ,fs.ANS_TEXT AS Designation
  ,(SELECT fs3.ANS_TEXT FROM dbo.ADD_ANSW AS fs3 WHERE fs3.QUES_ID=17 AND fs3
  .id = p.id) AS Answer
FROM 
  person p
INNER JOIN 
  person_facilities pf 
    ON p.ID = pf.ID
INNER JOIN 
  dbo.ADD_ANSW fs 
    ON p.id = fs.id 
WHERE
  pf.Current_status = 'Employed'
  AND 
  fs.QUES_ID=3
  AND 
  fs.ANS_TEXT 'Non-MFG'
  AND
  EXISTS (SELECT 1 
          FROM dbo.ADD_ANSW AS fs2 
          WHERE fs2.QUES_ID=17 AND fs2.ANS_TEXT = 'Yes' AND fs2.id = p.id);

答案 3 :(得分:0)

我相信您的情况需要导出多个临时表,然后将它们加入最终输出中。查看此解决方案是否适合您:

SELECT DISTINCT
    P.ID
    ,ISNULL(DBO.FNREMOVEPATTERNFROMSTRING(P.LAST_NAME, '%[,-.'']%'), '') AS [LAST]
    ,ISNULL(DBO.FNREMOVEPATTERNFROMSTRING(P.FIRST_NAME, '%[,-.'']%'), '') AS [FIRST]
    ,ISNULL(DBO.FNREMOVEPATTERNFROMSTRING(P.MIDDLE_INITIAL, '%[,-.'']%'), '') AS [MIDDLE]
    ,FS.ANS_TEXT AS Designation
    ,FS.QUES_ID

INTO
    #TEMP1

FROM
    PERSON P

JOIN
    PERSON_FACILITIES PF
    ON P.ID = PF.ID

LEFT JOIN
    DBO.ADD_ANSW FS
    ON P.ID = FS.ID
    AND (FS.QUES_ID = 17 OR FS.QUES_ID=3)

WHERE
    PF.CURRENT_STATUS = 'EMPLOYED'
    AND (FS.QUES_ID=17 OR FS.QUES_ID=3)
    AND FS.ANS_TEXT = 'Non-MFG'
;

SELECT DISTINCT
    P.ID
    ,ISNULL(DBO.FNREMOVEPATTERNFROMSTRING(P.LAST_NAME, '%[,-.'']%'), '') AS [LAST]
    ,ISNULL(DBO.FNREMOVEPATTERNFROMSTRING(P.FIRST_NAME, '%[,-.'']%'), '') AS [FIRST]
    ,ISNULL(DBO.FNREMOVEPATTERNFROMSTRING(P.MIDDLE_INITIAL, '%[,-.'']%'), '') AS [MIDDLE]
    ,FS.ANS_TEXT AS Answer
    ,FS.QUES_ID

INTO
    #TEMP2

FROM
    PERSON P

JOIN
    PERSON_FACILITIES PF
    ON P.ID = PF.ID

LEFT JOIN
    DBO.ADD_ANSW FS
    ON P.ID = FS.ID
    AND (FS.QUES_ID = 17 OR FS.QUES_ID=3)

WHERE
    PF.CURRENT_STATUS = 'EMPLOYED'
    AND (FS.QUES_ID=17 OR FS.QUES_ID=3)
    AND FS.ANS_TEXT = 'Yes'
;

SELECT DISTINCT
    A.ID
    ,A.LAST
    ,A.FIRST
    ,A.MIDDLE
    ,A.Designation
    ,B.Answer

FROM
    #TEMP1 A

JOIN
    #TEMP2 B
    ON A.ID = B.ID
;