I have some difficulties working on a query with conditional order by.
Here is a sample data :
|Id | Date-In | Date-Out |
|1 | 01/01/21 | NULL |
|2 | 03/01/21 | NULL |
|3 | 05/01/20 | 11/01/21 |
|3 | 12/01/21 | NULL |
|4 | 12/12/21 | 15/01/21 |
|5 | 17/01/21 | 21/01/21 |
I want to sort the data like this :
|Id | Date-In | Date-Out |
|5 | 17/01/21 | 21/01/21 |
|4 | 12/12/20 | 15/01/21 |
|3 | 05/01/21 | 11/01/21 |
|3 | 12/01/21 | NULL |
|2 | 03/01/21 | NULL |
|1 | 01/01/21 | NULL |
When there is a move-out sort DESC
When there is a move-out and a move-in sort move-out DESC, move-in DESC, Id DESC (Bold) Else sort move-in DESC
I tried this solution :
SELECT Id
,[move_in_date]
,[move_out_date]
,LAG(move_out_date) OVER(Partition By Id ORDER BY move_in_date) as Lag_out
FROM MyTable
ORDER BY isnull(move_out_date,LAG(move_out_date) OVER(Partition By Id ORDER BY move_in_date)) DESC
I don't know if this will work with every scenario i mentioned in my first post
I have been trouble writing a SQL query to roll up multiple balances based on a similar ID group and display the balance against products with a flag of N. I imagine I need to use a partition function or a max funcction to do this.
The desired results are in the table below underneath the sample dataset. Would anyone have a fix for this available?
Would anyone know logic to help for this? Basically I need to group everything by the ID and where there is a flag of N roll the balances up to that record, if there is no record with a flag of N we just aggregate by pdct_Type_C.
SELECT
Client,
SUM(Limit) Limit,
SUM(Balance) Balance,
SUM(Exposure) Exposure,
MAX(CASE WHEN Flag = 'N' THEN Pdct_type_c ELSE NULL END) Pdct_type_c,
ID
FROM Table
GROUP BY Client, ID
SAMPLE DATASET
Client | Limit | Balance | Exposure | Pdct_type_c | Flag | ID
--------------------------------------------------------------------------------
John | 60,000,000.00| - | 5,000,000| DERIV | N | 2
John | - | 1,000,000.00 | - | FX | y | 2
John | - | 2,000,000.00 | - | IC | y | 2
John | 1,000,000.00 | 3,000,000.00 | - | DCO | y | 3
John | 1,000,000.00 | 3,000,000.00 | - | DCO | y | 3
CURRENT RESULTS
Client | Limit | Balance | Exposure | Pdct_type_c | Flag | ID
--------------------------------------------------------------------------------
John | 60,000,000.00| 3,000,000.00 | 5,000,000| DERIV | N | 2
John | 2,000,000.00 | 6,000,000.00 | - | NULL | Y | 3
DESIRED RESULTS
Client | Limit | Balance | Exposure | Pdct_type_c | Flag | ID
--------------------------------------------------------------------------------
John | 60,000,000.00| 3,000,000.00 | 5,000,000| DERIV | N | 2
John | 2,000,000.00 | 6,000,000.00 | - | DCO | Y | 3
It is entirely possible that this is doable with a windowing function. However here is the old fashioned way of doing it
This shows us just the records that have an N entry
SELECT
ID,
MIN(Pdct_type_c) Pdct_type_c
FROM Table
WHERE Flag = 'N'
GROUP BY ID
This outer joins to it to decide what to group on
SELECT
T.Client,
SUM(T.Limit) Limit,
SUM(T.Balance) Balance,
SUM(T.Exposure) Exposure,
ISNULL(N.Pdct_type_c, T.Pdct_type_c) Pdct_type_c
CASE WHEN N.Pdct_type_c IS NULL THEN T.Flag ELSE 'N' END Flag,
T.ID
FROM Table T
LEFT OUTER JOIN
(
SELECT
ID,
MIN(Pdct_type_c) Pdct_type_c
FROM Table
WHERE Flag = 'N'
GROUP BY ID
) N
ON T.ID = N.ID
GROUP BY T.Client, T.ID,
ISNULL(N.Pdct_type_c, T.Pdct_type_c),
CASE WHEN N.Pdct_type_c IS NULL THEN T.Flag ELSE 'N' END
Using tsql I have a number of summed columns I need to pivot out across various groupings and payment statuses.
Before:
+---------------+----------+----------------------------+
| policy number | Grouping | £ Amount |
+---------------+----------+----------------------------+
| #123 | a | £1 (for issued payment) |
| #123 | b | £11(for cancelled payment) |
| #123 | a | £21(for cancelled payment) |
| #123 | b | £31(for issued payment) |
+---------------+----------+----------------------------+
Desired after:
+--------+----------------+----------------+-------------------+-------------------+
| policy | Group a Issued | Group b Issued | Group a cancelled | Group b cancelled |
+--------+----------------+----------------+-------------------+-------------------+
| #123 | £1 | £31 | £21 | £11 |
+--------+----------------+----------------+-------------------+-------------------+
Despite using isnull function when producing the summed columns in the before table, all my results are null. I have checked my pre-pivot table exhaustively and there are no null values.
Note because I have to pivot out the groupings across multiple payments types, I have had to use duplicated group columns.
I've imported data from an XML file by using SSIS to SQL Server.
The result what I got in the database is similar to this:
+-------+---------+---------+-------+
| ID | Name | Brand | Price |
+-------+---------+---------+-------+
| 2 | NULL | NULL | 100 |
| NULL | SLX | NULL | NULL |
| NULL | NULL | Blah | NULL |
| NULL | NULL | NULL | 100 |
+-------+---------+---------+-------+
My desired result would be:
+-------+---------+---------+-------+
| ID | Name | Brand | Price |
+-------+---------+---------+-------+
| 2 | SLX | Blah | 100 |
+-------+---------+---------+-------+
Is there a pretty solution to solve this in T-SQL?
I've already tried it with a SELECT MAX(ID) and then a GROUP BY ID, but I'm still stuck with the NULL values. Also I've tried it with MERGE, but also a failure.
Could someone give me a direction where to search further?
You can select MAX on all columns....
SELECT MAX(ID), MAX(NAME), MAX(BRAND), MAX(PRICE)
FROM [TABLE]
Click here for a fiddley fidd fiddle...
Hi friends i have small doubt in sql server
here i want data based on condition
same id and status is equal to s then that date value be
how to write query in sql server
Table :emp
id |status |date(mm-dd-yy) |fdate(mm-dd-yy) |tdate(mm-dd-yy)
1 | S |03-16-11 | |
1 | b | | 03-15-11 |03-18-11
1 | s |03-17-11 | |
1 | b | | 04-20-12 |04-30-12
1 | S |04-20-12 | |
1 | s |04-10-12 | |
1 | s |10-01-14 | |
1 | b | |10-02-14 |10-25-14
2 | s |01-18-12 | |
2 | b | |01-18-12 |01-28-12
2 | b | |03-10-13 |03-24-13
2 | s |03-16-13 | |
2 | s |03-10-13 | |
2 | s |03-23-13 | |
2 | b | |04-20-13 |04-27-13
2 | s |07-01-14 | |
the table (status = s, id, date) compare it with status = b, same id number and date ( Date value from status s) with the date range of fdate and tdate .
if that data with in range then Billing yes other wise billing no
output like
id |status |date(mm-dd-yy) |fdate(mm-dd-yy) |tdate(mm-dd-yy) |Billing
1 | S |03-16-11 | | |yes
1 | s |03-17-11 | | |yes
1 | S |04-20-12 | | |yes
1 | s |04-10-12 | | |no
1 | s |10-01-14 | | |no
2 | s |01-18-12 | | |yes
2 | s |03-16-13 | | |yes
2 | s |03-10-13 | | |yes
2 | s |03-23-13 | | |yes
2 | s |07-01-14 | | |no
i tried query like below
select *
from ( select * from emp a where status ='s') a
inner join (select * from emp b where status='b') b
on a.pn=b.pn
where a.date<=b.date1 and a.date>=b.date2
its not give exactely result.
please tell me how to write query in sql server .
Try
select a.Id,
a.status,
a.date,
a.fdate,
a.tdate,
max(IsNull(case when a.date between b.fDate and b.tDate
then 'yes'
else 'no'
end, 'no')) Billing
from emp a
left join emp b
on a.Id=b.Id
where a.status ='s'
and b.status = 'b'
group by a.Id,
a.status,
a.date,
a.fdate,
a.tdate
Some questions/comments:
What are the fields: pn, date1 and date2?
date1 in your query is, I guess, bigger than date2