sql-server

SQL Server Query Error -ORDER BY clause is invalid in views

回眸只為那壹抹淺笑 提交于 2021-02-12 11:40:15
问题 I am running the below code. When I run this code, I get error message: The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified. I'm not sure why I am getting this error message. Help is much appreciated. SELECT a.DELINQ_BUCKET_GROUP, a.vv_count FROM ( SELECT DELINQ_BUCKET_GROUP, CASE WHEN DELINQ_BUCKET_GROUP IS NULL THEN SUM(CASE WHEN DELINQ_BUCKET_GROUP IS NULL THEN 1 ELSE 0 END) ELSE COUNT

Select the non-null value if exists else null, but always select the row

情到浓时终转凉″ 提交于 2021-02-11 18:22:58
问题 I hope I can explain myself good, so I have 2 tables with one-to-many connection (TableA and TableB). Now, TableB has a column with a foreign key form TableA, and an ID (foreign key) to a third TableC. Now usually 2 to 3 rows share the same FK from TableA and this are my possible scenarios: All rows with same FK_TableA have the same FK_TableC All rows with same FK_TableA have NULL for FK_TableC All rows with same FK_TableA have either NULL or same FK_TableC like so: TableB -------------------

SQL Server : find duplicate record by column value

依然范特西╮ 提交于 2021-02-11 18:16:37
问题 I have a value like below dataset. Now how I can find the duplicate DataSetID like: 201 & 401 is duplicate record. 回答1: Use PIVOT and ROW_Number For Non Duplicates FIDDLE DEMO SELECT * FROm Tbl WHERE DateSetID IN ( SELECT DateSetID FROM ( SELECT DateSetID,[Name], [Age], [Gender],ROW_NUMBER() OVER (PARTITION BY [Name], [Age], [Gender] ORDER BY DateSetID) RN FROM (SELECT * FROM Tbl) AS SourceTable PIVOT(MAX(ColumnB) FOR ColumnA IN ([Name], [Age], [Gender]) ) AS PivotTable)Tmp WHERE RN = 1 );

SQL Server : find duplicate record by column value

半世苍凉 提交于 2021-02-11 18:16:06
问题 I have a value like below dataset. Now how I can find the duplicate DataSetID like: 201 & 401 is duplicate record. 回答1: Use PIVOT and ROW_Number For Non Duplicates FIDDLE DEMO SELECT * FROm Tbl WHERE DateSetID IN ( SELECT DateSetID FROM ( SELECT DateSetID,[Name], [Age], [Gender],ROW_NUMBER() OVER (PARTITION BY [Name], [Age], [Gender] ORDER BY DateSetID) RN FROM (SELECT * FROM Tbl) AS SourceTable PIVOT(MAX(ColumnB) FOR ColumnA IN ([Name], [Age], [Gender]) ) AS PivotTable)Tmp WHERE RN = 1 );

c# onclick and onclientclick at the same time

て烟熏妆下的殇ゞ 提交于 2021-02-11 17:48:06
问题 I have onclientclick and onclick events written for Exit button on my webpage. When clicked, it should first execute the update statement and then close the window. <asp:Button ID="ExitButton" runat="server" OnClientClick="javaScript:self.close(); return false;" OnClick="ExitButton_Click" UseSubmitBehavior="false" Text="Exit" Width="102px" CssClass="CssStyle2" Height="29px" /> protected void ExitButton_Click(object sender, EventArgs e) { string sqlUpd = @"UPDATE top (2) [db1].[TestTable] set

How to convert varchar to decimal(38,2)?

梦想的初衷 提交于 2021-02-11 17:15:32
问题 I have a problem converting from a varchar '000000113754' to decimal 1137.54 I'm comparing two expressions: 1. select STUFF('000000113754', LEN('000000113754') - 1, 0, '.') 2. select STUFF([dbo].fn_PrepareNumeric(('000000113754' + '-'), '+','-'), LEN('000000113754') - 1, 0, '.') The function fn_PrepareNumeric just checks for the sign and appends '-' in front if the sign is - . That's all. The problem is that the first expression gives me the correct varchar - 0000001137.54 , and the second

How to convert varchar to decimal(38,2)?

妖精的绣舞 提交于 2021-02-11 17:14:50
问题 I have a problem converting from a varchar '000000113754' to decimal 1137.54 I'm comparing two expressions: 1. select STUFF('000000113754', LEN('000000113754') - 1, 0, '.') 2. select STUFF([dbo].fn_PrepareNumeric(('000000113754' + '-'), '+','-'), LEN('000000113754') - 1, 0, '.') The function fn_PrepareNumeric just checks for the sign and appends '-' in front if the sign is - . That's all. The problem is that the first expression gives me the correct varchar - 0000001137.54 , and the second

How to convert varchar to decimal(38,2)?

十年热恋 提交于 2021-02-11 17:14:29
问题 I have a problem converting from a varchar '000000113754' to decimal 1137.54 I'm comparing two expressions: 1. select STUFF('000000113754', LEN('000000113754') - 1, 0, '.') 2. select STUFF([dbo].fn_PrepareNumeric(('000000113754' + '-'), '+','-'), LEN('000000113754') - 1, 0, '.') The function fn_PrepareNumeric just checks for the sign and appends '-' in front if the sign is - . That's all. The problem is that the first expression gives me the correct varchar - 0000001137.54 , and the second

Simple SQL pivot

允我心安 提交于 2021-02-11 17:10:36
问题 Googling SQL PIVOT brings up answers to more complex situations than I need with aggregations, and although I did find this simple SQL Pivot Query , it's pivoting on a single table, whereas I have two, it's doing a rank partition which I don't know is necessary, I can't actually get it to work, plus it's 5 years old and I'm hoping there's an easier way. I am sure this is a duplicate question so if someone can find it then please do! People table: PersonID ======== 1 2 3 Device table: DeviceID

Simple SQL pivot

折月煮酒 提交于 2021-02-11 17:09:56
问题 Googling SQL PIVOT brings up answers to more complex situations than I need with aggregations, and although I did find this simple SQL Pivot Query , it's pivoting on a single table, whereas I have two, it's doing a rank partition which I don't know is necessary, I can't actually get it to work, plus it's 5 years old and I'm hoping there's an easier way. I am sure this is a duplicate question so if someone can find it then please do! People table: PersonID ======== 1 2 3 Device table: DeviceID