在WHERE IN子句中使用SELECT語句,即WHERE blah in(SELECT b ...)

[英]Using a SELECT Statement in the WHERE IN Clause i.e. WHERE blah in (SELECT b…)


I am trying to do some complex pivot query... and I need to do a select in the IN Clause...

我正在嘗試做一些復雜的數據透視查詢......我需要在IN子句中做一個選擇...

Example of the Pivot

樞軸的示例

Looking at the Example in the above link, you can see they use the employee ids explicitly in their pivot. I would like to be able to do select instead of specifying the employee ids.

查看上面鏈接中的示例,您可以看到他們在其數據透視中明確使用了員工ID。我希望能夠選擇而不是指定員工ID。

The only way I can think of doing it now, is by creating my pivot query in a string and using manipulation in a Stored Prodcedure to build the employee ids into a string, insert that into my PIVOT clause, and execute the resulting SQL....

我現在想到的唯一方法是在字符串中創建我的數據透視查詢,並在Stored Prodcedure中使用操作將員工ID構建到字符串中,將其插入到我的PIVOT子句中,然后執行生成的SQL。 ..

Anyone think of a better way to do this?

有人想過更好的方法嗎?

1 个解决方案

#1


You pretty much have to use dynamic SQL when construction pivot queries when the pivot column contents cannot be hard coded.

當樞軸列內容無法進行硬編碼時,您在構造數據透視查詢時幾乎必須使用動態SQL。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2009/02/14/7255db400b975ec9f15ad237e8735000.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com