[Solved] SQL Multiple count on same row with dynamic column

Since you are using SQL Server then you can implement the PIVOT function and if you have an unknown number of period values, then you will need to use dynamic SQL: DECLARE @cols AS NVARCHAR(MAX), @query AS NVARCHAR(MAX) select @cols = STUFF((SELECT distinct ‘,’ + QUOTENAME(‘PeriodId’+cast(periodid as varchar(10))) from Periods FOR XML PATH(”), TYPE ).value(‘.’, … Read more

[Solved] How to group rows with same value in sql? [closed]

Try this DECLARE @temp TABLE(col1 varchar(20),col2 int, col3 varchar(20)) insert into @temp values (‘data1′, 123 , ’12/03/2009’),(‘data1′, 124 , ’15/09/2009’), (‘data2 ‘,333 ,’02/09/2010’),(‘data2 ‘,323 , ’02/11/2010’), (‘data2 ‘,673 , ’02/09/2014’),(‘data2′,444 , ’05/01/2010’) SELECT (CASE rno WHEN 1 THEN col1 ELSE ” END )AS col1, col2, col3 FROM ( SELECT ROW_NUMBER() OVER(PARTITION BY Col1 ORDER BY … Read more

[Solved] updating values in one table from another table using DYNAMIC SQL in MSSQL

Try following query: UPDATE TableB SET [1] = ISNULL(z.[1],TableB.[1]), [2] = ISNULL(z.[2],TableB.[2]), [3] = ISNULL(z.[3],TableB.[3]), [4] = ISNULL(z.[4],TableB.[4]), [5] = ISNULL(z.[5],TableB.[5]), [6] = ISNULL(z.[6],TableB.[6]), [7] = ISNULL(z.[7],TableB.[7]) FROM ( SELECT [1],[2],[3],[4],[5],[6],[7] FROM (SELECT Id, Value FROM TableA)AS p PIVOT (MAX(Value) FOR Id IN([1],[2],[3],[4],[5],[6],[7]))AS pvt )z EDIT In order to have dynamic pivot use following query: … Read more

[Solved] get quarter of current date in sql server 2008 [closed]

You can use datepart if you have quarters specified as 1,2,3 and 4 as: declare @date date = getdate() select case when datepart(MM, @date) IN (4,5,6) then ‘Q1’ when datepart(MM, @date) IN (7,8,9) then ‘Q2’ when datepart(MM, @date) IN (10,11,12) then ‘Q3’ when datepart(MM, @date) IN (1,2,3) then ‘Q4’ end as Quater solved get quarter … Read more

[Solved] Find string according to words count

A) Use String_Split or something similar to create a table for each word. LG 55 Vacuum theater home #table_of_words (text varchar(32)) text LG 55 Vacuum Theater Home B) join the created table with the main table using an expression like SELECT DISTINCT Description FROM main_table Main JOIN #table_of_words WD ON Main.Description Like ‘%’+WD.text+’%’ If you … Read more

[Solved] What’s wrong with this SQL?

SELECT COALESCE(s.state, c.state) AS state , COALESCE(s.city, c.city) AS city , COALESCE(s.Suppliers, 0) AS Suppliers , COALESCE(c.Consumers, 0) AS Consumers FROM ( SELECT Tb_Supplier.State , Tb_Supplier.City , COUNT(Tb_Supplier.Name) AS Suppliers FROM Tb_Supplier GROUP BY Tb_Supplier.City , Tb_Supplier.State ) AS s FULL OUTER JOIN ( SELECT Tb_Consumer.State , Tb_Consumer.City , COUNT(Tb_Consumer.Name) AS Consumers FROM Tb_Consumer GROUP … Read more

[Solved] sql query – Get the difference time between swipe in – Swipe out for employee

select empid, sum ( datediff ( MINUTE, case when timesheet.timein < @timeframe_start then @timeframe_start else timesheet.timein end, case when timesheet.timeout > @timeframe_end then @timeframe_end else timesheet.timeout end ) ) as total_duration from ( select timein.empid, timein.swipe_time as timein, timeout.swipe_time as timeout from tbltest timein left join tblTest timeout on timein.empid = timeout.empid and timeout.eventtype=”ex” and … Read more

[Solved] Return row in SQL that returning null value? to show them not as blank value

You can left join to a derived table containing the “numbers” (well, strings with a possible numeric representation actually). SELECT d.clientnumber FROM (VALUES (‘00602’), (‘00897’), (‘00940′)) AS n (n) LEFT JOIN dataentry AS de ON de.clientnumber = n.n; That’ll give you as many NULLs as “numbers” in the list that aren’t present in the table … Read more

[Solved] How to add the total row after the blank line?

Assume that you table contains information about Company, Charge, Commission and you want to get total per each company and also per each of its column. As you didn’t provide any table schemas or your data sample, I created a table, populated with data from your question. I’d suggest the next query: create table test … Read more

[Solved] why the stored procedure not work and showbox “incorrect syntax near ‘)’. ” [closed]

You have a surplus comma here: @answer nvarchar (100)=null, ) also checkisn’t a good column name as it is a reserved keyword, and the datetype doesn’t take a size parameter. This should work: CREATE PROCEDURE dbo.storMember ( @Check nchar (1), @UserName nvarchar (15), @Passowerd nvarchar (15)=null, @Name nvarchar (15)=null, @Phone nvarchar (15)=null, @email nvarchar (30)=null, … Read more