subquery

MS SQL Server pivot table with subquery in column clause

本小妞迷上赌 提交于 2020-01-27 07:46:25
问题 Im sure this is a simple technique although I can't find an answer so far! I have TIMESTAMP | POINTNAME | VALUE 2012-10-10 16:00:00 AHU01 20 2012-10-10 16:00:00 AHU02 25 2012-10-10 16:00:15 AHU01 26 2012-10-10 16:00:15 AHU02 35 etc... ( for approx 800 POINTNAMES) with many pointnames I dont want to list each one in the 'IN' clause of the pivot 'FOR' (as syntax given below) definition but would like to use perhaps a subquery. So what I would like is all the POINTNAME values as columns with A

MS SQL Server pivot table with subquery in column clause

本秂侑毒 提交于 2020-01-27 07:45:31
问题 Im sure this is a simple technique although I can't find an answer so far! I have TIMESTAMP | POINTNAME | VALUE 2012-10-10 16:00:00 AHU01 20 2012-10-10 16:00:00 AHU02 25 2012-10-10 16:00:15 AHU01 26 2012-10-10 16:00:15 AHU02 35 etc... ( for approx 800 POINTNAMES) with many pointnames I dont want to list each one in the 'IN' clause of the pivot 'FOR' (as syntax given below) definition but would like to use perhaps a subquery. So what I would like is all the POINTNAME values as columns with A

How to improve SQL Server query containing nested sub query

大城市里の小女人 提交于 2020-01-26 04:35:16
问题 I was given this SQL query by my boss and told to improve/optimize it DECLARE @pol_0 int, @pol_1 int, @pol_2 int, @pol_3 int, @pol_4 int, @pol_5plus int, @peril_0 int, @peril_1 int, @peril_2 int, @peril_3 int, @peril_4 int, @peril_5plus int, @loc_1 int, @loc_2_10 int, @loc_11_100 int, @loc_101_1000 int, @loc_1001_5000 int, @loc_5001plus int, @locfass int, @polfass int, @pollim int, @polattpt int, @polded int, @maxded int, @polres int, @sublimit int, @sitelim int, @siteded int, @SS int, @WX

Using sub query to group by specific column is outputting the same erroneous result

[亡魂溺海] 提交于 2020-01-26 03:55:07
问题 I asked this question few days ago on SO, but with no valid answer for it. The problem was that the following query: SELECT t1.patient_id, CONVERT(aes_decrypt(t4.patient_name_en, :encKey) USING utf8mb4) as patient_name_en, min(t3.date_of_visit) as date_of_visit, t2.diagnosis_name, max(ifnull(t5.date_of_assessment, 'N/A')) as date_of_assessment, ifnull(t5.assessment_result, 0) as assessment_result FROM consultation t1 LEFT JOIN diagnosis t2 ON t1.diagnosis_id = t2.diagnosis_id LEFT JOIN visit

Which is better CTE or Inner-Selects in PostgreSQL

久未见 提交于 2020-01-26 02:18:09
问题 Given below is a sample of my two different query execution with same result The select query using inner-select . select p.product from ( select * from tbl_a where productid not in (select productid from tbl_b) ) p order by p.product And select query using CTE . with cte as ( select * from tbl_a where productid not in (select productid from tbl_b) ) select product from cte order by product So my question is which among the above is good to use? And At what condition can use each of them? 回答1

How can I use sub query to count the result before limiting?

馋奶兔 提交于 2020-01-26 01:28:05
问题 I have two queries on the same table which have identical where clause. This: SELECT qa.id, qa.subject, qa.category cat, qa.keywords tags, qa.body_html, qa.amount, qa.visibility, qa.date_time, COALESCE(u.reputation, 'N') reputation, COALESCE(Concat(u.user_fname, ' ', u.user_lname), 'unknown') NAME, COALESCE(u.avatar, 'anonymous.png') avatar, ( SELECT COALESCE(Sum(vv.value),0) FROM votes vv WHERE qa.id = vv.post_id AND 15 = vv.table_code) AS total_votes, ( SELECT COALESCE(Sum(vt.total_viewed)

How to limit an SQL result set to not too common items

橙三吉。 提交于 2020-01-24 11:58:07
问题 Problem: I have a list of names and addresses. Some names (persons) have the same address (street, zip code, town) like others. I want to select all those names with addresses with no more than three occurrences and from the rest the first three names each of a bunch pointing to the same address. Example: Albert | Adr1 Berta | Adr1 Cesar | Adr1 Donald | Adr1 Eric | Adr2 Fritz | Adr2 Gerd | Adr2 Henry | Adr3 The result set should be Albert | Adr1 Berta | Adr1 Cesar | Adr1 Eric | Adr2 Fritz |

How to limit an SQL result set to not too common items

 ̄綄美尐妖づ 提交于 2020-01-24 11:58:05
问题 Problem: I have a list of names and addresses. Some names (persons) have the same address (street, zip code, town) like others. I want to select all those names with addresses with no more than three occurrences and from the rest the first three names each of a bunch pointing to the same address. Example: Albert | Adr1 Berta | Adr1 Cesar | Adr1 Donald | Adr1 Eric | Adr2 Fritz | Adr2 Gerd | Adr2 Henry | Adr3 The result set should be Albert | Adr1 Berta | Adr1 Cesar | Adr1 Eric | Adr2 Fritz |

Passing an expression to a method in NHibernate results in Object of type 'ConstantExpression' cannot be converted to type 'LambdaExpression'

Deadly 提交于 2020-01-23 16:16:53
问题 This problem occurs in both NHibernate 2 and 3. I have a Class A that has a member set of class B. Querying the classes directly executes nicely. But when I pass one of the expressions involving class B into a method I get the following error: System.ArgumentException: Object of type 'System.Linq.Expressions.ConstantExpression' cannot be converted to type 'System.Linq.Expressions.LambdaExpression'. As far as I can see I am passing the exact same expression into the Any() method. But for some

Subquery is running faster than join?

风格不统一 提交于 2020-01-17 05:12:08
问题 I am trying to identify value from one column which does exist in another column in same table Sub query SELECT DISTINCT `Wear it With - Outfits 1` FROM `product list` WHERE `Wear it With - Outfits 1` NOT IN (SELECT `sku` FROM `product list`) ...returns result in 2.7287sec I tried to replace sub query by left join SELECT DISTINCT table1.`Wear it With - Outfits 1` FROM `product list` as table1 LEFT JOIN `product list` as table2 ON table1.`Wear it With - Outfits 1`=table2.sku WHERE table2.sku