Don't want null when used case in SQL Server [closed] - sql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have table data like that you show in this screenshot:
and I have query like this so I don't want null values in output, means I don't want field like this

You can get the data next to each other using a trick with row_number(), so that you'll assign numbers to both + and - groups, and then group by it:
select
max(case when Parameter = '+' then Description end),
max(case when Parameter = '-' then Description end)
from (
select
*,
row_number() over (partition by Parameter order by Id) as RN
from
#tmp
) X
group by RN
Example

Related

Tracking Employee Changes - SQL query [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 11 hours ago.
Improve this question
I have this table of employees that gets updated at the end of each month. I know how to detect changes (2 rows; one of old and one of new) but I need to summarize the changes as showing in the second table using sql query.
With JobHistory AS(
Select
*,
LEAD([DepartmentName]) over (Partition by [EmployeeNumber] order by [ASofDate]) as NewDepartmentName,
LAG([DepartmentName]) over (Partition by [EmployeeNumber] order by [ASofDate]) as OldDepartmentName
FROM [EmployeeDirectory]
)
Select *
FROM JobHistory
WHERE (
(NewDepartmentName <> [DepartmentName])
OR
( OldDepartmentName <> [DepartmentName])
)
ORDER by AsOfDate, EmployeeNumber
[Table]
Required Results

Select the most recent date from the table [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have a purchase order table that has a list of order numbers and associated order lines with different dates. I want to a query to fetch the orders with orderlines that has the most recent date.
I want the fetch to result in something like below
Here is a way to do this using row_number
select * from (
select t.*
,row_number() over(partition by ordernumber,orderlinenumber order by date desc) as rnk
from <your_table> t
)x
where x.rnk=1

To select max date and time [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have data with column createdate like '24/04/2019 14:52:38',24/04/2019 14:52:37,24/04/2019 14:52:35,24/03/2019 14:52:38 etc.
how to get data based on max date and time in SQL query.
select
top(1) *
from
xx
order by
createdate desc
something like this? (works if createdate column is of date/datetime/timestamp type)
select
*
from
<table_name>
where
createdate = (select
max(createdate)
from
<table_name>)

sql in ms-access [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have a table name customers with two columns, case id and owner. I need to write a query to select random 5 caseids for every name in owner column.please help
For a start, you need something like:
SELECT TOP 5
ID,
[Case ID],
[Owner],
Rnd(-Timer()*[ID]) AS RandomRecord
FROM
[Cases]
ORDER BY
Rnd(-Timer()*[ID]);
to be used as a subquery filtered on OwnerID of your Owners' table.
I once posted an article on this with a lot more details:
Random Rows in Microsoft Access
You can use in:
select t.*
from t
where t.id in (select top 5 id
from t as t2
where t2.name = t.name
order by Rnd(-Timer()*[ID])
);

SQL query on table [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a table like the following
sl.no Machine Id date status
I need to get the last status for each machine ID for each day.(Everyday each machine ID will have many entries .I need to get the last entry for that day for each machine ID)
Please help
Hope this will solve your issue
Select MachineID, Date,
(Select Top 1 t1.Status
FROM table As t1
WHERE t1.MachineID = table.MachineID
AND t1.Date = table.Date
order by slno desc)
FROM table
Group BY MachineID, Date