Vertica: ORDER BY ASC and DESC for date and time is giving same value - database

I'm using Vertica db and trying to sort the rows by DESC and ASC in which both gives the same output. The rows value are in date and time like shown below.
testdb=> SELECT queue_entry_timestamp, acquisition_timestamp, (acquisition_timestamp-queue_entry_timestamp) AS 'queue wait' FROM V_MONITOR.RESOURCE_ACQUISITIONS WHERE node_name ILIKE '%node0001' AND pool_name = 'test01' ORDER BY 'queue_entry_timestamp' desc limit 5;
queue_entry_timestamp | acquisition_timestamp | queue wait
-------------------------------+-------------------------------+-----------------
2023-01-27 03:40:02.259231+00 | 2023-01-27 03:40:02.259239+00 | 00:00:00.000008
2023-01-27 03:40:02.258851+00 | 2023-01-27 03:40:02.25888+00 | 00:00:00.000029
2023-01-27 03:40:45.958846+00 | 2023-01-27 03:40:45.958872+00 | 00:00:00.000026
2023-01-27 03:41:41.958178+00 | 2023-01-27 03:41:41.958185+00 | 00:00:00.000007
2023-01-27 03:42:42.966845+00 | 2023-01-27 03:42:42.966851+00 | 00:00:00.000006
(5 rows)
testdb=> SELECT queue_entry_timestamp, acquisition_timestamp, (acquisition_timestamp-queue_entry_timestamp) AS 'queue wait' FROM V_MONITOR.RESOURCE_ACQUISITIONS WHERE node_name ILIKE '%node0001' AND pool_name = 'test01' ORDER BY 'queue_entry_timestamp' asc limit 5;
queue_entry_timestamp | acquisition_timestamp | queue wait
-------------------------------+-------------------------------+-----------------
2023-01-27 03:40:02.259231+00 | 2023-01-27 03:40:02.259239+00 | 00:00:00.000008
2023-01-27 03:40:02.258851+00 | 2023-01-27 03:40:02.25888+00 | 00:00:00.000029
2023-01-27 03:40:45.958846+00 | 2023-01-27 03:40:45.958872+00 | 00:00:00.000026
2023-01-27 03:41:41.958178+00 | 2023-01-27 03:41:41.958185+00 | 00:00:00.000007
2023-01-27 03:42:42.966845+00 | 2023-01-27 03:42:42.966851+00 | 00:00:00.000006
(5 rows)
Could someone let me know how to sort this ?

The problem with your query is the order by syntax:
ORDER BY 'queue_entry_timestamp'
This actually says to order by a constant string literal 'queue_entry_timestamp'. This value will always be the same, for every record in your table. So your current logic really has no ordering at all. Use this version:
ORDER BY queue_entry_timestamp

Related

Conditional Order By In SQL Server

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

SQL Server Aggregation Using Partitioning

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

Null values when using pivot function

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.

T-SQL Merging data

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...

how to check date records with in fdate and tdate range in sql server

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

Resources