久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

T-sql case 返回錯誤值

T-sql case returns wrong value(T-sql case 返回錯誤值)
本文介紹了T-sql case 返回錯誤值的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在努力讓我的 SQL 查詢工作.當我的 IN 找不到一行時,我想像圖片中的那樣返回一行,但我希望將 ifuserholdscertificate 設置為NO".

I'm struggling to get my SQL query to work. When my IN FAILS to find a row I want to return a row just like the ones in the picture but I want the ifuserholdscertificate to be set to 'NO'.

SELECT tuc.id,
       tu.firstName,
       tuc.uid,
       tuc.value,
       tc.name,
       count(tuc.value) over (PARTITION BY tuc.uid) AS 'amount',
       'certificate DESIRABLE' AS 'typeofthing' ,
       'YES' AS 'HasorNot',
       ifuserholdscertificate = CASE
                                    WHEN count(tuc.value) = 0 THEN 'NO'
                                    ELSE 'YES'
                                END
FROM t_user_certificates tuc,
     t_certificates tc,
     t_users tu
WHERE tuc.value IN (4,
                    12,
                    31)
  AND tuc.value = tc.id
  AND tu.id = tuc.uid
GROUP BY tuc.id,
         tu.firstName,
         tuc.uid,
         tuc.value,
         tc.name

這是查詢生成的數據!

正如您所看到的,即使有些人在數量行中只得到 2,它仍然不會獲取一行并將 ifuserholdscertificate 設置為NO".

As you can see even if some people only get 2 in the amount row it will still not fetch a row and set ifuserholdscertificate to 'NO'.

按請求更新!

select  tuc.id,
count(tuc.value) as 'counten',
tu.firstName,
tuc.uid,
tuc.value, 
tc.name, 
count(tuc.value) over (PARTITION BY tuc.uid) as 'amount',
'certificateDESIRABLE'  as 'typeofthing' ,
'YES' as 'HasorNot',
HasOrders = CASE
                WHEN count(tuc.value) = 0 THEN 'NO'
                ELSE 'YES'
            END
from t_user_certificates tuc                   
left outer join t_certificates tc
on tuc.value = tc.id
left outer join t_users tu
on tu.id = tuc.uid
GROUP BY tuc.id, tu.firstName, tuc.uid, tuc.value, tc.name

在計數中總是 1 并且總是是"

Alwyas one 1 in the count and always 'YES'

推薦答案

這可能更符合您的要求.你總是返回 1,沒有用戶沒有證書,因為你在 where 子句中有 IN 位.因此,即使使用外連接,您也只能從 t_user_certificates 返回具有這些值的行.它實際上變成了一個內部連接.ANSI 連接語法是您的朋友.它將 JOIN 邏輯與過濾器分開.

This might be more like what you're looking for. You're always returning 1, and no users w/o certificates because you have the IN bit in the where clause. So even with an outer join, you only return rows from t_user_certificates with those values. It effectively becomes an inner join. ANSI join syntax is your friend. It separates JOIN logic from filters.

SELECT tuc.id,
       tu.firstName,
       tuc.uid,
       tuc.value,
       tc.name,
       count(tuc.value) AS 'amount',
       'certificate DESIRABLE' AS 'typeofthing' ,
       'YES' AS 'HasorNot',
       ifuserholdscertificate = CASE
                                    WHEN count(tuc.value) > 0 THEN 'YES'
                                    ELSE 'NO'
                                END
FROM
    t_users tu
LEFT JOIN t_user_certificates tuc
    ON
    tu.id = tuc.uid
    AND 
    tuc.value IN
    (
        4
        , 12
        , 31
    )
LEFT JOIN
     t_certificates tc
    ON
    tuc.value = tc.id

GROUP BY tuc.id,
         tu.firstName,
         tuc.uid,
         tuc.value,
         tc.name;

這篇關于T-sql case 返回錯誤值的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Modify Existing decimal places info(修改現有小數位信息)
The correlation name #39;CONVERT#39; is specified multiple times(多次指定相關名稱“CONVERT)
T-SQL left join not returning null columns(T-SQL 左連接不返回空列)
remove duplicates from comma or pipeline operator string(從逗號或管道運算符字符串中刪除重復項)
Change an iterative query to a relational set-based query(將迭代查詢更改為基于關系集的查詢)
concatenate a zero onto sql server select value shows 4 digits still and not 5(將零連接到 sql server 選擇值仍然顯示 4 位而不是 5)
主站蜘蛛池模板: 999久久久 | 97色在线观看免费视频 | 精品日韩电影 | 精品美女久久久久久免费 | 免费看国产一级特黄aaaa大片 | 伊人久操 | 黄色在线免费网站 | 特黄一级 | 欧美精品一区二区三区一线天视频 | 欧美精品一区在线发布 | 欧美黑人狂野猛交老妇 | 精品一级 | 国产精品久久久乱弄 | 本道综合精品 | 中文字幕在线一区二区三区 | 成人国产精品久久久 | 99精品99久久久久久宅男 | 久草视频在线播放 | 91久久久久久 | 成人在线视频一区 | 精品欧美一区二区三区久久久 | 亚洲最大av网站 | 欧美一级α片 | 日本福利视频免费观看 | 欧美在线一区二区视频 | 欧美一区二区三区 | 超碰人人人 | 国产精品一区二区久久精品爱微奶 | 欧美精品一区免费 | 人人干天天干 | 久久久久久综合 | 日韩精品一区二区三区 | av一区在线观看 | 日韩和的一区二在线 | 在线精品一区 | 日韩一区二区三区视频在线观看 | 欧美成人hd| 亚洲精品久久久久久宅男 | 国产精品久久久久久久久婷婷 | 久久久成人免费一区二区 | 国产性色视频 |