SQL Server order by syntax with Case When and a Constant - sql-server
I'm reading TSQL code someone else wrote and find a somewhat weird syntax. It's doing order by a string. I did some test and the following is the code. Anyone can help me to explain it? Thanks.
First Query
SELECT *
FROM dbo.Products
Result:
ProductID ProductName SupplierID CategoryID QuantityPerUnit UnitPrice UnitsInStock UnitsOnOrder ReorderLevel Discontinued
----------- ------------------------------- ----------- ----------- -------------------- --------------------- ------------ ------------ ------------ ------------
1 Chai 1 1 10 boxes x 20 bags 18.00 39 0 10 0
2 Chang 1 1 24 - 12 oz bottles 19.00 17 40 25 0
3 Aniseed Syrup 1 2 12 - 550 ml bottles 10.00 13 70 25 0
4 Chef Anton's Cajun Seasoning 2 2 48 - 6 oz jars 22.00 53 0 0 0
...
*/
Second query:
SELECT *
FROM dbo.Products
WHERE ProductID < 10
ORDER BY '3';
Result:
Msg 408, Level 16, State 1, Line 1 A constant expression was
encountered in the ORDER BY list, position 1.
Third Query
SELECT *
FROM dbo.Products
WHERE ProductID < 10
ORDER BY CASE WHEN SupplierID = 2 THEN '1'
WHEN SupplierID = 1 THEN '2'
ELSE '3'
END;
Result:
ProductID ProductName SupplierID CategoryID QuantityPerUnit UnitPrice UnitsInStock UnitsOnOrder ReorderLevel Discontinued
----------- ---------------------------------------- ----------- ----------- -------------------- --------------------- ------------ ------------ ------------ ------------
4 Chef Anton's Cajun Seasoning 2 2 48 - 6 oz jars 22.00 53 0 0 0
5 Chef Anton's Gumbo Mix 2 2 36 boxes 21.35 0 0 0 1
1 Chai 1 1 10 boxes x 20 bags 18.00 39 0 10 0
2 Chang 1 1 24 - 12 oz bottles 19.00 17 40 25 0
3 Aniseed Syrup 1 2 12 - 550 ml bottles 10.00 13 70 25 0
6 Grandma's Boysenberry Spread 3 2 12 - 8 oz jars 25.00 120 0 25 0
7 Uncle Bob's Organic Dried Pears 3 7 12 - 1 lb pkgs. 30.00 15 0 10 0
8 Northwoods Cranberry Sauce 3 2 12 - 12 oz jars 40.00 6 0 0 0
9 Mishi Kobe Niku 4 6 18 - 500 g pkgs. 97.00 29 0 0 1
(9 row(s) affected)
*/
"Order by" has to be able to translate each row into a value, then those values can be compared. "Order by '3'" doesn't make any sense as a useful query, as it's not using the row - hence the error message of ordering by a constant expression.
"Order by (some expression returning a string)" makes perfect sense. I would personally have used numbers rather than strings, but fundamentally it's still just ordering by a value.
Would you have found it odd to see "order by ProductName"? That's ordering by a string too.
Hopefully that helps - it's not really clear which bit was causing a problem though.
In your first SQL query, the original coder might have meant
ORDER BY 3
which means "order by the 3rd column" (which is SupplierId), in ascending order.
In the second query, as #Kokizzo has explained, the author has hard coded the query so that Products from supplierId 2 are at the top, followed by those from supplierId 1, and then all rows from other suppliers. The purpose isn't clear, but for example, this could be a nefarious attempt to promote a certain supplier's products above others e.g. in a web search result page.
The CASE WHEN .. ELSE ... END can be equated to a simple function applied to each row, which takes the supplierId as input and returns the precedence of that row used in the ORDER BY clause.
CASE WHEN SupplierID = 2 THEN '1'
WHEN SupplierID = 1 THEN '2'
ELSE '3'
END
is equal to (in pseudocode):
if supplierId = 2 then
order_value = 1
else if supplierId = 1 then
order_value = 2
else
order_value = 3
end
so the order now is according to the order_value
Related
SQL Server add a column and populate multiple rows based on conditions across multiple columns
I have a table that looks like this: Timestamp CPID Con Context Type Value 2018-01-01 03:11 1 2 6 8 0 2018-01-01 03:11 1 2 3 8 0 2018-01-01 03:11 1 2 3 3 100 2018-01-01 03:15 2 1 6 8 16 2018-01-01 03:15 2 1 3 8 15 2018-01-01 03:15 2 1 3 3 200 I want to add a column called new_column, and populate it with 1s when Value=0 when Context=6. I want to consider Timestamp, CPID and Con as a group, so that when for a given group has Context=6, the other rows in that group are also assigned 1 in new_column. The result would look like this: Timestamp CPID Con Context Type Value new_column 2018-01-01 03:11 1 2 6 8 0 1 2018-01-01 03:11 1 2 3 8 0 1 2018-01-01 03:11 1 2 3 3 100 1 2018-01-01 03:15 2 1 6 8 16 0 2018-01-01 03:15 2 1 3 8 15 0 2018-01-01 03:15 2 1 3 3 200 0 Notes: the row orders are not always the same, so I can't just fill down 2 rows every time; I also cannot directly ALTER Table because it is read only. I'm still new to SQL so struggling with this one.
You can create a view by using exists : select t.*, (case when exists (select 1 from table t1 where t1.Timestamp = t.Timestamp and t1.CPID = t.CPID and t1.Con = t.Con and (t1.Context = 6 or t1.Value = 0) ) then 1 else 0 end) as new_column from table t;
Get output of multiple Counts in one query
Could you help me how to write a query for the following issue: There are two tables: Table persons: P_id Name BirthDate 1 N1 2016-08-02 2 N2 2015-05-02 3 N3 2013-06-01 4 N4 2014-01-09 Table visited:(p_id is foreign key to table persons) Id. Visitor_id. P_id. Visit_date 1 10 1 2017-03-05 2 11 2 2017-01-01 3 10 2 2017-02-03 4 12 3 2016-05-07 5 11 4 2016-04-09 6 10 1 2017-04-09 We are going to get the count of visited by each Visitor and also count of visited distinct person on filter on for those person who their age are under 1, between 1 and 2, between 2 and 3 at date of visit_date by each visitor_id. The results should be like : Under_one Bet_one_two Bet_two_three Visitor_id VisitedCount/PersonCount VisitedCount/PersonCount VisitedCount/PersonCount 10 2 1 1 1 0 0 11 0 0 1 1 1 1 12 0 0 0 0 1 1 Between 1 and 2 means the result of subtracting visited_date and birthdate (for example : the result of 2013/03/05 - 2011/06/07) is between 1 and 2 years.
I don't know if I can give you the output laid out exactly as you have specified, but this SELECT visited.Visitor_id, visited.P_id, Int(([Visit_date]-[BirthDate])/365) AS Age, Count(persons.P_id) AS NumVisits FROM persons INNER JOIN visited ON persons.P_id = visited.P_id GROUP BY visited.Visitor_id, visited.P_id, Int((-[BirthDate]+[Visit_date])/365); returns Visitor_id P_id Age NumVisits 10 1 0 2 10 2 1 1 11 2 1 1 11 4 2 1 12 3 2 1
select timestamps within ranges
I have got 2 tables. Example Table 1: ID episode_id episode_start episode_end ---------------------------------------------------- 1 1 1 2 1 2 4 5 1 3 96 105 1 4 110 114 2 1 1 4 2 2 13 24 Example Table 2 ID timestamp Other_info -------------------------------- 1 1 111 1 2 142 1 3 114 1 4 112 1 5 116 1 6 123 2 1 145 2 2 156 2 3 154 I would like to merge the two tables based upon table2.timestamp = between table1.episode_start and table1.episode_end. The final table should be a subset of table 2 with only the timestamprows where there is an episode. Question: How to do this? What is the most computational efficient way? EDIT: In reallity my tables are much longer. So, for example, the episode_start of ID = 1 be the same as the episode start of ID=1200 in episode 12. So simply merging them does not work. EDIT: EXPECTED OUTPUT: ID timestamp Other_info -------------------------------- 1 1 111 1 2 142 1 4 112 1 5 116 2 1 145 2 2 156 2 3 154 and so on. The merge is basically some sort of filter of table 2 by table 1.
select table2.id, timestamp, other_info from table1 left join table2 on table2.timestamp between table1.episode_start and table1.episode_end where table2.id = table1.id Output: id timestamp Other_info --------------------------------------- 1 1 111 1 2 142 1 4 112 1 5 116 2 1 145 2 2 156 2 3 154
How to count number of months in T-SQL
I've got a problem in SQL Server. "Whate'er is well conceived is clearly said, And the words to say it flow with ease", Nicolas Boileau-Despreaux Well, I don't think I'll be able to make it clear but I'll try ! And I'd like to apologize for my bad english ! I've got this table : id ind lvl result date 1 1 a 3 2017-01-31 2 1 a 3 2017-02-28 3 1 a 1 2017-03-31 4 1 a 1 2017-04-30 5 1 a 1 2017-05-31 6 1 b 1 2017-01-31 7 1 b 3 2017-02-28 8 1 b 3 2017-03-31 9 1 b 1 2017-04-30 10 1 b 1 2017-05-31 11 2 a 3 2017-01-31 12 2 a 1 2017-02-28 13 2 a 3 2017-03-31 14 2 a 1 2017-04-30 15 2 a 3 2017-05-31 I'd like to count the number of month the combo {ind, lvl} remain in the result 1 before re-initializing the number of month to 0 if the result is not 1. Clearly, I need to get something like that : id ind lvl result date BadResultRemainsFor%Months 1 1 a 3 2017-01-31 0 2 1 a 3 2017-02-28 0 3 1 a 1 2017-03-31 1 4 1 a 1 2017-04-30 2 5 1 a 1 2017-05-31 3 6 1 b 1 2017-01-31 1 7 1 b 3 2017-02-28 0 8 1 b 3 2017-03-31 0 9 1 b 1 2017-04-30 1 10 1 b 1 2017-05-31 2 11 2 a 3 2017-01-31 0 12 2 a 1 2017-02-28 1 13 2 a 3 2017-03-31 0 14 2 a 1 2017-04-30 1 15 2 a 3 2017-05-31 0 So that if I was looking for the number of months the result was 1 for the date 2017-05-31 with the id 1 and the lvl a, I know it's been 3 months.
Assume all the date the the end day of month: ;WITH tb(id,ind,lvl,result,date) AS( select 1,1,'a',3,'2017-01-31' UNION select 2,1,'a',3,'2017-02-28' UNION select 3,1,'a',1,'2017-03-31' UNION select 4,1,'a',1,'2017-04-30' UNION select 5,1,'a',1,'2017-05-31' UNION select 6,1,'b',1,'2017-01-31' UNION select 7,1,'b',3,'2017-02-28' UNION select 8,1,'b',3,'2017-03-31' UNION select 9,1,'b',1,'2017-04-30' UNION select 10,1,'b',1,'2017-05-31' UNION select 11,2,'a',3,'2017-01-31' UNION select 12,2,'a',1,'2017-02-28' UNION select 13,2,'a',3,'2017-03-31' UNION select 14,2,'a',1,'2017-04-30' UNION select 15,2,'a',3,'2017-05-31' ) SELECT t.id,t.ind,t.lvl,t.result,t.date ,CASE WHEN t.isMatched=1 THEN ROW_NUMBER()OVER(PARTITION BY t.ind,t.lvl,t.id-t.rn ORDER BY t.id) ELSE 0 END FROM ( SELECT t1.*,c.MonthDiff,CASE WHEN c.MonthDiff=t1.result THEN 1 ELSE 0 END AS isMatched ,CASE WHEN c.MonthDiff=t1.result THEN ROW_NUMBER()OVER(PARTITION BY t1.ind,t1.lvl,CASE WHEN c.MonthDiff=t1.result THEN 1 ELSE 0 END ORDER BY t1.id) ELSE null END AS rn FROM tb AS t1 LEFT JOIN tb AS t2 ON t1.ind=t2.ind AND t1.lvl=t2.lvl AND t2.id=t1.id-1 CROSS APPLY(VALUES(ISNULL(DATEDIFF(MONTH,t2.date,t1.date),1))) c(MonthDiff) ) AS t ORDER BY t.id id ind lvl result date ----------- ----------- ---- ----------- ---------- -------------------- 1 1 a 3 2017-01-31 0 2 1 a 3 2017-02-28 0 3 1 a 1 2017-03-31 1 4 1 a 1 2017-04-30 2 5 1 a 1 2017-05-31 3 6 1 b 1 2017-01-31 1 7 1 b 3 2017-02-28 0 8 1 b 3 2017-03-31 0 9 1 b 1 2017-04-30 1 10 1 b 1 2017-05-31 2 11 2 a 3 2017-01-31 0 12 2 a 1 2017-02-28 1 13 2 a 3 2017-03-31 0 14 2 a 1 2017-04-30 1 15 2 a 3 2017-05-31 0
By slightly tweaking your input data and slightly tweaking how we define the requirement, it becomes quite simple to produce the expected results. First, we tweak your date values so that the only thing that varies is the month and year - the days are all the same. I've chosen to do that my adding 1 day to each value1. The fact that this produces results which are one month advanced doesn't matter here, since all values are similarly transformed, and so the monthly relationships stay the same. Then, we introduce a numbers table - here, I've assumed a small fixed table is adequate. If it doesn't fit your needs, you can easily locate examples online for creating a large fixed numbers table that you can use for this query. And, finally, we recast the problem statement. Instead of trying to count months, we instead ask "what's the smallest number of months, greater of equal to zero, that I need to go back from the current row, to locate a row with a non-1 result?". And so, we produce this query: declare #t table (id int not null,ind int not null,lvl varchar(13) not null, result int not null,date date not null) insert into #t(id,ind,lvl,result,date) values (1 ,1,'a',3,'20170131'), (2 ,1,'a',3,'20170228'), (3 ,1,'a',1,'20170331'), (4 ,1,'a',1,'20170430'), (5 ,1,'a',1,'20170531'), (6 ,1,'b',1,'20170131'), (7 ,1,'b',3,'20170228'), (8 ,1,'b',3,'20170331'), (9 ,1,'b',1,'20170430'), (10,1,'b',1,'20170531'), (11,2,'a',3,'20170131'), (12,2,'a',1,'20170228'), (13,2,'a',3,'20170331'), (14,2,'a',1,'20170430'), (15,2,'a',3,'20170531') ;With Tweaked as ( select *, DATEADD(day,1,date) as dp1d from #t ), Numbers(n) as ( select 0 union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9 ) select id, ind, lvl, result, date, COALESCE( (select MIN(n) from Numbers n1 inner join Tweaked t2 on t2.ind = t1.ind and t2.lvl = t1.lvl and t2.dp1d = DATEADD(month,-n,t1.dp1d) where t2.result != 1 ), 1) as [BadResultRemainsFor%Months] from Tweaked t1 The COALESCE is just there to deal with the edge case, such as for your 1,b data, where there is no previous row with a non-1 result. Results: id ind lvl result date BadResultRemainsFor%Months ----------- ----------- ------------- ----------- ---------- -------------------------- 1 1 a 3 2017-01-31 0 2 1 a 3 2017-02-28 0 3 1 a 1 2017-03-31 1 4 1 a 1 2017-04-30 2 5 1 a 1 2017-05-31 3 6 1 b 1 2017-01-31 1 7 1 b 3 2017-02-28 0 8 1 b 3 2017-03-31 0 9 1 b 1 2017-04-30 1 10 1 b 1 2017-05-31 2 11 2 a 3 2017-01-31 0 12 2 a 1 2017-02-28 1 13 2 a 3 2017-03-31 0 14 2 a 1 2017-04-30 1 15 2 a 3 2017-05-31 0 1An alternative way to perform the adjustment is to use a DATEADD/DATEDIFF pair to perform a "floor" operation against the dates: DATEADD(month,DATEDIFF(month,0,date),0) as dp1d Which resets all of the date values to be the first of their own month rather than the following month. This may fell more "natural" to you, or you may already have such values available in your original data.
Assuming the dates are continously increasing in month, you can use window function like so: select t.id, ind, lvl, result, dat, case when result = 1 then row_number() over (partition by grp order by id) else 0 end x from ( select t.*, dense_rank() over (order by e, result) grp from ( select t.*, row_number() over (order by id) - row_number() over (partition by ind, lvl, result order by id) e from your_table t order by id) t ) t;
SQL query to get Groups and sub groups hierarchy
Account table ac_id ac_name st_id ----------- ------------- ----------- 1 LIABILITES 1 2 ASSET 1 3 REVENUE 1 4 EXPENSES 1 5 EQUITY 1 Groups table grp_id grp_name ac_no grp_of st_id type_ cmp_id ----------- ------------------- ---------- -------- --------- --------- -------- 1 Capital Account 1 0 1 0 0 2 Current Liability 1 0 1 0 0 3 Loan Liability 1 0 1 0 0 4 Suspense A/C 1 0 1 0 0 5 Current Assets 2 0 1 0 0 6 Fixed Assests 2 0 1 0 0 7 Investment 2 0 1 0 0 8 Misc. Expenses 2 0 1 0 0 9 Direct Income 3 0 1 0 0 10 Indirect Income 3 0 1 0 0 11 Sale Account 3 0 1 0 0 12 Direct Expense 4 0 1 0 0 13 Indirect Expense 4 0 1 0 0 14 Purchase Account 4 0 1 0 0 15 Sundry Creditors 2 1 1 0 0 16 Sundry Debitors 5 1 1 0 0 17 Bank Account 5 1 1 0 0 18 Cash In Hand 5 1 1 0 0 19 Duties & Taxes 2 1 1 0 0 20 Salary 12 1 1 0 0 21 Personal 5 1 1 0 0 22 Loan 2 0 1 0 0 23 Customer 16 1 1 0 0 34 Vendor 15 1 1 0 0 38 Sale Softwares 11 1 1 1 1 46 Stock In Hand 5 1 1 1 1 47 test 1 1 1 1 1 48 test in 47 1 1 1 1 Query to get all groups hierarchy. declare #ac_no as int =2 ;With CTE(grp_id,grp_name,ac_no,Level) AS ( SELECT grp_id,grp_name,ac_no,CAST(1 AS int) FROM Groups WHERE grp_id in (select grp_id from Groups where (ac_no=#ac_no) and grp_of=0) UNION ALL SELECT o.grp_id,o.grp_name,o.ac_no,c.Level+1 FROM Groups o INNER JOIN CTE c ON c.grp_id=o.ac_no --where o.ac_no=2 and o.grp_of=1 ) select * from CTE Result is ok for ac_no=2/3/4 grp_id grp_name ac_no Level ----------- ------------------- ----------- ------ 5 Current Assets 2 1 6 Fixed Assests 2 1 7 Investment 2 1 8 Misc. Expenses 2 1 22 Loan 2 1 16 Sundry Debitors 5 2 17 Bank Account 5 2 18 Cash In Hand 5 2 21 Personal 5 2 46 Stock In Hand 5 2 23 Customer 16 3 But when I try to get result for ac_no=1; I get error : Msg 530, Level 16, State 1, Line 4 The statement terminated. The maximum recursion 100 has been exhausted before statement completion.
I think the issue is that you end up in an infinite recursion as you have a row that is it's own parent/child (eg. grp_id = ac_no). I think it should work if you add a limiting clause to the recursive member like this: DECLARE #ac_no AS int = 1; WITH CTE (grp_id , grp_name , ac_no , Level ) AS ( SELECT grp_id, grp_name, ac_no, CAST( 1 AS int ) FROM Groups WHERE grp_id IN (SELECT grp_id FROM Groups WHERE ac_no = #ac_no AND grp_of = 0) UNION ALL SELECT o.grp_id, o.grp_name, o.ac_no, c.Level + 1 FROM Groups o INNER JOIN CTE c ON c.grp_id = o.ac_no --where o.ac_no=2 and o.grp_of=1 WHERE c.ac_no <> c.grp_id ) SELECT * FROM CTE;