Conditional ordering in SQL Server - sql-server

Table T:
ID | Name | Days
ID is the PK.
I do want to select * from T, order by ID descending, but on TOP to be those entries which have the ID between 1000 and 1004
select *
from T
order by Id descending // something like a *case*?
1004 - 1st / 1003 2nd / 1002 3rd / 1001 4th / 1000 5nd ... and then 6th should be the max Id, and after this all descending excepting the Ids between 1000 - 1004 which we already displayed on the TOP.
I would like to know also the linq statement.

SELECT *
FROM (VALUES (998), (999), (1000), (1001), (1002), (1003), (1004)) AS T(ID)
ORDER BY CASE
WHEN T.ID BETWEEN 1000 AND 1004 THEN 1
ELSE 2
END
, ID DESC;
Took sample data from Sick's answer.
How does this work?
It checks whether your ID is matching your criteria and assign it a
value of 1
Everything else will have 2
It will sort by this value first
Your IDs with matching criteria will ALWAYS come first
Then we sort leftovers by ID in DESC order
Output:
╔══════╗
║ ID ║
╠══════╣
║ 1004 ║
║ 1003 ║
║ 1002 ║
║ 1001 ║
║ 1000 ║
║ 999 ║
║ 998 ║
╚══════╝
I've made additional example, which generates 676.800 rows in our DB:
;WITH TestTable (ID)
AS (
SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL))
FROM sys.objects
CROSS JOIN sys.schemas
)
SELECT *
FROM TestTable AS T
ORDER BY CASE
WHEN T.ID BETWEEN 1000 AND 1004 THEN 1
ELSE 2
END
, ID DESC;
That's its result:
╔════════╗
║ ID ║
╠════════╣
║ 1004 ║
║ 1003 ║
║ 1002 ║
║ 1001 ║
║ 1000 ║
║ 676800 ║
║ 676799 ║
║ 676798 ║
║ 676797 ║
║ 676796 ║
║ 676795 ║
║ ... ║
║ 1006 ║
║ 1005 ║
║ 999 ║
║ 998 ║
║ ... ║
║ 1 ║
╚════════╝

Try this
select *
from T
order by case when ID between 1000 and 1004 then -100000 else id end,ID desc
Here -100000 is s random low value
Example :
SELECT *
FROM (VALUES (998),
(999),
(1000),
(1001),
(1002),
(1003),
(1004) ) tc (id)
ORDER BY CASE
WHEN ID BETWEEN 1000 AND 1004 THEN -100000
ELSE id
END,
ID DESC

SELECT column_name, column_name
FROM table_name
ORDER BY column_name ASC|DESC, column_name ASC|DESC;

Related

T-SQL: I'm trying to get the latest row of a column but also the sum of another column

I have the following table, it displays the SalesQty and the StockQty grouped by Article, Supplier, Branch and Month.
╔════════╦════════╦══════════╦═════════╦══════════╦══════════╗
║ Month ║ Branch ║ Supplier ║ Article ║ SalesQty ║ StockQty ║
╠════════╬════════╬══════════╬═════════╬══════════╬══════════╣
║ 201811 ║ 333 ║ 2 ║ 3122 ║ 4 ║ 11 ║
║ 201811 ║ 345 ║ 1 ║ 1234 ║ 2 ║ 10 ║
║ 201811 ║ 345 ║ 1 ║ 4321 ║ 3 ║ 11 ║
║ 201812 ║ 333 ║ 2 ║ 3122 ║ 2 ║ 4 ║
║ 201812 ║ 345 ║ 1 ║ 1234 ║ 3 ║ 12 ║
║ 201812 ║ 345 ║ 1 ║ 4321 ║ 4 ║ 5 ║
║ 201901 ║ 333 ║ 2 ║ 3122 ║ 1 ║ 8 ║
║ 201901 ║ 345 ║ 1 ║ 1234 ║ 6 ║ 9 ║
║ 201901 ║ 345 ║ 1 ║ 4321 ║ 2 ║ 8 ║
║ 201902 ║ 333 ║ 2 ║ 3122 ║ 7 ║ NULL ║
║ 201902 ║ 345 ║ 1 ║ 1234 ║ 4 ║ 13 ║
║ 201902 ║ 345 ║ 1 ║ 4321 ║ 1 ║ 10 ║
╚════════╩════════╩══════════╩═════════╩══════════╩══════════╝
Now I want to sum the SalesQty and get the latest StockQty and group them by Article, Supplier, Branch.
The final result should look like this:
╔════════╦══════════╦═════════╦═════════════╦════════════════╗
║ Branch ║ Supplier ║ Article ║ SumSalesQty ║ LatestStockQty ║
╠════════╬══════════╬═════════╬═════════════╬════════════════╣
║ 333 ║ 2 ║ 3122 ║ 14 ║ NULL ║
║ 345 ║ 1 ║ 1234 ║ 15 ║ 13 ║
║ 345 ║ 1 ║ 4321 ║ 10 ║ 10 ║
╚════════╩══════════╩═════════╩═════════════╩════════════════╝
I already tried this but it gives me an error, and i have no idea what i have to do in this case.
I've made this example so you can try it by yourself. db<>fiddle
SELECT
Branch,
Supplier,
Article,
SumSalesQty = SUM(SalesQty),
-- my attempt
LatestStockQty = (SELECT StockQty FROM TestTable i
WHERE MAX(Month) = Month
AND TT.Branch = i. Branch
AND TT.Supplier = i.Branch
AND TT.Article = i.Branch)
FROM
TestTable TT
GROUP BY
Branch, Supplier, Article
Thank you for your help!
We can try using ROW_NUMBER here, to isolate the latest record for each group:
WITH cte AS (
SELECT t.*, ROW_NUMBER() OVER (PARTITION BY Branch, Supplier, Article
ORDER BY Month DESC) rn,
SUM(SalesQty) OVER (PARTITION BY Branch, Supplier, Article) SumSalesQty
FROM TestTable t
)
SELECT
Month,
Branch,
Supplier,
Article,
SumSalesQty,
StockQty
FROM cte
WHERE rn = 1;
Inside the CTE we compute, for each Branch/Supplier/Article group a row number value, starting with 1 for the most recent month. We also compute the sum of the sales quantity over the same partition. Then, we only need to select all rows from that CTE where the row number is equal to 1.
Demo
A similar approach but without the CTE
SELECT top 1 with ties
Branch
, Supplier
, Article
, SUM(SalesQty) OVER (PARTITION BY Branch, Supplier, Article) SumSalesQty
, tt.StockQty as LatestStockQty
FROM TestTable TT
order by ROW_NUMBER() OVER (PARTITION BY Branch, Supplier, Article ORDER BY Month DESC)

ORDER BY child-parent hierarchy using MS SQL Server stored procedure

I have a table named Department with 3 column: DepartmentID, DepartmentName, ParentID.
See SQL Fiddle for details
I want to order the results by ParentID and Generation. Like this:
╔═════════╦════════════════╦════════════╦══════════╗
║ ChildID ║ DepartmentName ║ Generation ║ ParentID ║
╠═════════╬════════════════╬════════════╬══════════╣
║ 1 ║ Manager ║ 0 ║ NULL ║
║ 6 ║ Sales ║ 1 ║ 1 ║
║ 7 ║ Fleet ║ 1 ║ 1 ║
║ 4 ║ Cargo ║ 2 ║ 7 ║
║ 5 ║ Transport ║ 2 ║ 7 ║
║ 2 ║ IT ║ 2 ║ 6 ║
║ 3 ║ Food ║ 2 ║ 6 ║
╚═════════╩════════════════╩════════════╩══════════╝
I tried different ORDER BYs but none worked.
My stored procedure:
WITH Hierarchy(ChildId, DeparmentName, Generation, ParentID)
AS
(
SELECT DepartmentID, DeparmentName, 0, ParentID
FROM Departments AS FirtGeneration
WHERE ParentID IS NULL
UNION ALL
SELECT NextGeneration.DepartmentID, NextGeneration.DeparmentName, Parent.Generation + 1, Parent.ChildId
FROM Departments AS NextGeneration
INNER JOIN Hierarchy AS Parent ON NextGeneration.ParentID = Parent.ChildId
)
SELECT * FROM Hierarchy
OPTION(MAXRECURSION 32767)
I use MS SQL Server 2005
Try storing the path to the top in the hierarchical query:
WITH Hierarchy(ChildId, DeparmentName, Generation, ParentID, Path) AS (
SELECT DepartmentID, DepartmentName, 0, ParentID,
RIGHT('000' + CAST(DepartmentID as VARCHAR(MAX)), 3) as Path
FROM Departments FirstGeneration
WHERE ParentID IS NULL
UNION ALL
SELECT NextGeneration.DepartmentID, NextGeneration.DeparmentName, Parent.Generation + 1, Parent.ChildId,
Path + '-->' + CAST(RIGHT('000' + CAST(NextGeneration.DepartmentID as VARCHAR(MAX)), 3)
FROM Departments NextGeneration INNER JOIN
Hierarchy Parent
ON NextGeneration.ParentID = Parent.ChildId
)
SELECT h.*
FROM Hierarchy h
ORDER BY path
OPTION(MAXRECURSION 32767);

SQL Query Group by issue

I have a table with records like history table.
I have created following Query to get the desired result. However, it is not giving me final result that I want.
DECLARE #Customer_Id BIGINT=4
DECLARE #Month INT=2
SELECT
FH.UniqueFileId,FH.DocumentNo,MIN(FH.DateStamp) AS ReceivedTime,MAX(FH.DateStamp) SentTime
FROM
[dbo].[FileMovement_History] FH
INNER JOIN [dbo].[User_Master] UM ON UM.User_Id=FH.User_Id
WHERE
FH.UniqueFileId Is Not NULL AND FH.UniqueFileId!=''
AND (#Customer_Id IS NULL OR UM.Customer_id = #Customer_Id)
AND MONTH(FH.DateStamp)=#Month
GROUP BY
FH.UniqueFileId,FH.DocumentNo
Order BY
FH.UniqueFileId
╔════════════════╦══════════════════════╦═════════════════════════════╦═════════════════════════════╗
║ UniqueFileId ║ DocumentNo ║ ReceivedTime ║ SentTime ║
╠════════════════╬══════════════════════╬═════════════════════════════╬═════════════════════════════╣
║ HS5536387_10HE ║ NULL ║ 2017-02-02 13:13:37.3451906 ║ 2017-02-02 13:19:19.0114047 ║
║ HS5536387_10HE ║ 120097583 / MC / CL7 ║ 2017-02-02 13:19:18.9801503 ║ 2017-02-02 13:19:19.1051605 ║
║ HS5536387_1HE ║ NULL ║ 2017-02-02 13:13:34.6887572 ║ 2017-02-02 13:21:37.7405652 ║
║ HS5536387_1HE ║ 3701341 ║ 2017-02-02 13:21:37.6936641 ║ 2017-02-02 13:21:37.8499434 ║
║ HS5536387_3HE ║ NULL ║ 2017-02-02 13:13:35.5950873 ║ 2017-02-02 13:19:19.9958519 ║
╚════════════════╩══════════════════════╩═════════════════════════════╩═════════════════════════════╝
I want the following result where I need one entry for one UniqueFileId with minimum of datestamp and maximum of datestamp. I want extra column where is shows total timeduration between minimum and maximum date.
╔════════════════╦══════════════════════╦═════════════════════════════╦═════════════════════════════╗
║ UniqueFileId ║ DocumentNo ║ ReceivedTime ║ SentTime ║
╠════════════════╬══════════════════════╬═════════════════════════════╬═════════════════════════════╣
║ HS5536387_10HE ║ 120097583 / MC / CL7 ║ 2017-02-02 13:13:37.3451906 ║ 2017-02-02 13:19:19.0114047 ║
║ HS5536387_1HE ║ 3701341 ║ 2017-02-02 13:13:34.6887572 ║ 2017-02-02 13:21:37.7405652 ║
║ HS5536387_3HE ║ NULL ║ 2017-02-02 13:13:35.5950873 ║ 2017-02-02 13:19:19.9958519 ║
╚════════════════╩══════════════════════╩═════════════════════════════╩═════════════════════════════╝
Do a grouping only by the UniqueFileId field, and instead take the MIN(DocumentNo) value of this column:
SELECT FH.UniqueFileId,
MIN(FH.DocumentNo) AS DocumentNo,
MIN(FH.DateStamp) AS ReceivedTime,
MAX(FH.DateStamp) AS SentTime
FROM [dbo].[FileMovement_History] FH
INNER JOIN [dbo].[User_Master] UM
ON UM.User_Id = FH.User_Id
WHERE COALESCE(FH.UniqueFileId, '') <> '' AND
(#Customer_Id IS NULL OR UM.Customer_id = #Customer_Id) AND
MONTH(FH.DateStamp) = #Month
GROUP BY FH.UniqueFileId
ORDER BY FH.UniqueFileId
If you want to show the difference in time between the sent and received time in the format hh:mm:ss, then you can use the following expression:
CAST(DATEDIFF(HOUR, MIN(FH.DocumentNo), MAX(FH.DocumentNo)) AS NVARCHAR(200)) + ":" +
CAST(DATEDIFF(MINUTE, MIN(FH.DocumentNo), MAX(FH.DocumentNo)) % 60 AS NVARCHAR(200)) +":" +
CAST(DATEDIFF(SECOND, MIN(FH.DocumentNo), MAX(FH.DocumentNo)) % 60 AS NVARCHAR(200))

Microsoft sql select distinct certain columns

I am changing mysql queries to microsoft sql server 2008 queries, and in order to apply limit I need to select a ROW_NUMBER() that accumlates over each row and then apply a where condition WHERE RowNum >= 0 AND RowNum < 0 + 20 to select a certain limit with offset.
My issue however is that I can no longer select distinct because I have a column RowNum that is different with each row, here's my query:
USE mydatabase
GO
WITH Results_CTE
AS (
SELECT DISTINCT C.firstname
,C.lastname
,C.id AS clientid
,QC.category_name
,QR.id
,QR.cid
,QR.catid
,QR.rhid
,ROW_NUMBER() OVER (
ORDER BY QR.id DESC
) AS RowNum
FROM cms_question_report QR
,cms_clients C
,cms_questioncategory QC
WHERE C.id = QR.cid
AND QR.catid = '3'
AND QR.catid = QC.id
)
SELECT *
FROM Results_CTE
WHERE RowNum >= 0
AND RowNum < 0 + 20
And the results are:
╔═══════════╦══════════╦══════════╦═══════════════╦═══════╦═════╦═══════╦══════╦════════╗
║ firstname ║ lastname ║ clientid ║ category_name ║ id ║ cid ║ catid ║ rhid ║ RowNum ║
╠═══════════╬══════════╬══════════╬═══════════════╬═══════╬═════╬═══════╬══════╬════════╣
║ test ║ testing ║ 121 ║ Activity Log ║ 81499 ║ 121 ║ 3 ║ 0 ║ 19 ║
║ test ║ testing ║ 121 ║ Activity Log ║ 81498 ║ 121 ║ 3 ║ 0 ║ 20 ║
║ test ║ testing ║ 121 ║ Activity Log ║ 81497 ║ 121 ║ 3 ║ 0 ║ 21 ║
║ test ║ test ║ 120 ║ Activity Log ║ 81496 ║ 120 ║ 3 ║ 0 ║ 22 ║
║ test ║ test ║ 120 ║ Activity Log ║ 81495 ║ 120 ║ 3 ║ 0 ║ 23 ║
║ test ║ test ║ 120 ║ Activity Log ║ 81494 ║ 120 ║ 3 ║ 0 ║ 24 ║
║ test ║ test ║ 120 ║ Activity Log ║ 81493 ║ 120 ║ 3 ║ 0 ║ 25 ║
╚═══════════╩══════════╩══════════╩═══════════════╩═══════╩═════╩═══════╩══════╩════════╝
I took an example from the middle of the result. What I'm after is for example:
╔═══════════╦══════════╦══════════╦═══════════════╦═══════╦═════╦═══════╦══════╦════════╗
║ firstname ║ lastname ║ clientid ║ category_name ║ id ║ cid ║ catid ║ rhid ║ RowNum ║
╠═══════════╬══════════╬══════════╬═══════════════╬═══════╬═════╬═══════╬══════╬════════╣
║ test ║ testing ║ 121 ║ Activity Log ║ 81497 ║ 121 ║ 3 ║ 0 ║ 21 ║
║ test ║ test ║ 120 ║ Activity Log ║ 81496 ║ 120 ║ 3 ║ 0 ║ 22 ║
╚═══════════╩══════════╩══════════╩═══════════════╩═══════╩═════╩═══════╩══════╩════════╝
Where I am trying to group the results by clientid. How do I achieve that?
USE ctarspla_mercycs
GO
WITH Results_CTE AS
(SELECT *, ROW_NUMBER() OVER (ORDER BY CLIENTID DESC) AS RowNum
FROM (SELECT DISTINCT
C.firstname,
C.lastname,
C.id as clientid,
QC.category_name,
QR.catid,
QR.rhid
FROM
cms_question_report QR,
cms_clients C,
cms_questioncategory QC
WHERE C.id=QR.cid
AND QR.catid=3
AND QR.catid=QC.id) t1)
SELECT * FROM Results_CTE
WHERE RowNum >= 0
AND RowNum < 0 + 20

table values as table header

The query:
select Escuser,Eslevel from WF_UserConfiguration
is returning me the table bellow:
╔═════════════════════╗
║ Escuser Eslevel ║
╠═════════════════════╣
║ A000 1 ║
║ A010 4 ║
║ A021 3 ║
║ ABCD 1 ║
║ C067 3 ║
║ C099 1 ║
║ C252 2 ║
╚═════════════════════╝
My problem is I want to get the following output
╔═════════════════════════════╗
║ 1 2 3 4 ║
╠═════════════════════════════╣
║ A000 C252 A021 A010 ║
║ ABCD C067 ║
║ C099 ║
╚═════════════════════════════╝
The table headers 1, 2, 3 and 4 are EsLevel values of first query result.
How should I get the following result (I mean what query)?
The answer using pivot:
See live demo
select
[1],
[2],
[3],
[4]
from
(
select
Escuser,
Eslevel,
Row_number() over(partition by Eslevel order by escuser asc) as r
from WF_UserConfiguration
)src
pivot
(
max(escuser)
for Eslevel in
(
[1],[2],[3],[4]
)
)p

Resources