Posts

Sqlserver Max with Over  if you have alteast one entry in your table than this will be fastest Declare @tblMinMaxExample table  (   pk integer not null identity(1,1) primary key,   dataYearMonth integer,   dataYear integer,   dataMonth integer,   recordedValue float,   attributeA varchar(1),   attributeB varchar(1) ) insert into @tblMinMaxExample values (201401, 2014, 1,   200.0, 'A', 'a') insert into @tblMinMaxExample   select '201412', '2014', max(dataMonth) over(order by pk) ,  100.0, 'L', 'l' from @tblMinMaxExample  else use this Declare @tblMinMaxExample table  (   pk integer not null identity(1,1) primary key,   dataYearMonth integer,   dataYear integer,   dataMonth integer,   recordedValue float,   attributeA varchar(1),   attributeB varchar(1) ) --insert into @tblMinMaxExample values (201...
This is good example. CONVERT COLUMN VALUES TO COMMA SEPARATED ONE ROW VALUE I have a query the returns country....state...and cities  the cities are creating ore rows than required, I need to re-write my query so that the cities column instead come in single row separated by commas... here is the data for more understanding CREATE TABLE [ dbo ].[ test12 ]( [ title ] [ varchar ]( 51 ) NULL , [ subtitle ] [ varchar ]( 52 ) NULL , [ value ] [ varchar ]( 53 ) NULL ) INSERT INTO test12 ( title , subtitle , value ) VALUES ( 'USA' , 'PA' , 'PHILLY' ) INSERT INTO test12 ( title , subtitle , value ) VALUES ( 'USA' , 'PA' , 'PITTSBURG' ) INSERT INTO test12 ( title , subtitle , value ) VALUES ( 'USA' , 'PA' , 'WARREN' ) INSERT INTO test12 ( title , subtitle , value ) VALUES ( 'USA' , 'PA' , 'UNION' ) INSERT INTO test12 ( title , subtitle...

How do I compare all the column values based on the Partition by with ID

I have a case where there are many rows for the same ID, I just wanted to know if there is any way to find out whether the given ID includes some specific value or not. Table 1 ID Dept Salary Flag Date 1 IT 5000 N 2017-01-01 1 IT 5000 N 2017-01-02 1 IT 5000 N 2017-01-03 1 IT 5000 N 2017-01-04 1 IT 5000 N 2017-01-05 2 HR 4000 N 2017-01-01 2 HR 4000 N 2017-01-02 2 HR 4000 Y 2017-01-03 2 HR 4000 N 2017-01-04 2 HR 4000 N 2017-01-05 3 Fin 4500 N 2017-01-08 3 Fin 4500 N 2017-01-09 3 Fin 4500 N 2017-01-10 3 Fin 4500 N 2017-01-11 3 Fin 4500 ...

SQL Server 2008 R2: Get first not null values from column

In sqlserver, when you have multiple data with some column have null value and you need the row which not null. So this example will help you. CREATE TABLE Family ( ID int , Name varchar ( 20 ), Gender char ( 1 ) ); INSERT INTO Family VALUES ( 1 , 'Ram' , 'M' ), ( 2 , 'Suraj' , 'M' ), ( 3 , 'Sunitha' , 'F' ), ( 4 , 'Deepika' , 'F' ), ( 5 , 'Minakshi' , 'F' ), ( 6 , 'Somu' , 'M' ); CREATE TABLE Child_parent ( Child_ID int , Parent_ID int ); INSERT INTO Child_parent VALUES ( 1 , 2 ), ( 1 , 3 ), ( 4 , 5 ), ( 4 , 6 );   Expected Result : Child_ID ChildName FatherName MotherName -----------------------------------------------   1         Ram       Suraj      Sunitha  4         Deepika   Somu       Minakshi Query...

❣DIY Fairy House Lamp Using Coke Plastic Bottles❣

Image
Difference between asynchronous (AJAX) and synchronous request? Now-a-days, this is a very common question that been asked in all most every basic interview – What is the main strength of AJAX? Or why and when should we use AJAX in web application? And the most common answer is – “AJAX does not refresh or reload the whole page”. But, the more perfect answer is – “AJAX is an asynchronous technology where others request are synchronous.” So, what is the difference? In a word, a program does not wait for response after requesting an asynchronous call whereas synchronous does so. Here is a simple example – function check() { var a=0; a = getStatus(“getstatus.php?id=5”); if(a==1) { alert(“active”); } else { alert(“not active”); } } Here getStatus() function sends a AJAX request to the server with “getstatus.php?id=5” url and the php file decides (from database may be) the status and output/response as 1 or 0. But, this function wi...

Read /Write in XML column in sqlserver

This is simple example to read and write in xml column of a table in sqlserver . You can directly execute in sqlserver too. ----------------- To insert/update in xml column declare @t table (id int, xmlCol xml) insert into @t values(1,' 14747 ') select * from @t; update @t set xmlcol = ' 14747 14749 ' where id =1 select * from @t --even you cast as select cast ( xmlcol as varchar(max)) , * from @t ----------------- Select example-----1st example -- Declare @responce xml; DECLARE @douvalue varchar(100); SELECT @responce = ' 60.4655 ' select @douvalue = @responce.value('(/double)[1]', 'varchar(100)'); select @responce, @douvalue AS DownloadEnvironment --Declare @responce xml; SELECT @responce = ' 60.4655 ' ;WITH XMLNAMESPACES(DEFAULT 'http://www.webserviceX.NET/') select @douvalue = @responce.value('/double[1]', 'varchar(100)'); select @douvalue ------2nd example - To retrieve value --- declare @xml xml  set @xml...