How can we use LAG() in SQL server 2008 R2? [duplicate] - sql-server

This question already has answers here:
Alternate of lead lag function in SQL Server 2008
(3 answers)
Closed 3 years ago.
SELECT
CASE
WHEN ser.[Department Name] = LAG(SER.[Department Name]) OVER (ORDER BY SER.[Department Name]) THEN ''
ELSE SER.[Department Name]
END [Department Name] from department

You can use APPLY :
SELECT (CASE WHEN d.[Department Name] = Prev_Dept
THEN '' ELSE d.[Department Name]
END) AS DepartmentName
FROM department d OUTER APPLY
( SELECT TOP (1) d1.[Department Name] AS Prev_Dept
FROM department d1
WHERE d1.? < d.?
ORDER BY ? DESC
) AS Pd;
? indicates identify column that identifies unique sequence of data.

Related

Concat in Firebird - If one field is NULL all Content is NULL how to avoid? [duplicate]

This question already has answers here:
Concatenate with NULL values in SQL
(8 answers)
SQL using If Not Null on a Concatenation
(9 answers)
Firebird date concatention change date format
(1 answer)
Closed 1 year ago.
How does one avoid creating a null field when doing a concat with a field that is null in Firebird.
with data as ( select article_name || ' STK:' || cast(quantity as integer) || NOTES as quantity ,PREORDER_ID as PID from preorder_item )
SELECT PREORDER.NAME ,PREORDER.DELIVERY_DATE ,LIST(DATA.QUANTITY, ' | ')
FROM PREORDER
INNER JOIN DATA ON PREORDER.ID =
DATA.PID GROUP by PREORDER.NAME ,PREORDER.DELIVERY_DATE
In all the fields were my NOTES are NULL the whole content is null how can I work around this and only add the notes information when it is available.
I already tried something like
with comments as (select NOTES as NOTES,PREORDER_ID as PID FROM PREORDER_ITEM where NOTES is not NULL)
with data as ( select article_name || ' STK:' || cast(quantity as integer) as quantity ,PREORDER_ID as PID from preorder_item )
SELECT PREORDER.NAME ,PREORDER.DELIVERY_DATE ,LIST(DATA.QUANTITY, ' | ')
FROM PREORDER
INNER JOIN DATA ON PREORDER.ID = DATA.PID
INNER JOIN COMMENTS ON PREORDER.ID = COMMENTS.PID
GROUP by PREORDER.NAME ,PREORDER.DELIVERY_DATE
You can take a look to SQL function COALESCE or NULLIF which allow you to transform null value
COALESCE( Expr1, Expr2, Expr3, ... )
NULLIF (<exp1>, <exp2>)

Filtering down SQL To one value? [duplicate]

This question already has answers here:
Get top 1 row of each group
(19 answers)
Closed 1 year ago.
I'm writing a query for SAP B1,using Microsoft SQL Server Management Studio 17.
I am wanting to return the "MIN" DocDate, but show the field "ShipDate" alongside it, with only one result...
SELECT T0.ItemCode,
T3.ShipDate AS 'Next Date',
MIN(T3.DocDate) AS 'OrderDate'
FROM OITM T0
LEFT OUTER JOIN POR1 T3 ON T0.ItemCode = T3.ItemCode
WHERE T3.LineStatus = 'O' AND T3.ShipDate > GETDATE()
Group By T0.Itemcode, T3.ShipDate
This returns the following results;
ItemCode
ShipDate
OrderDate
VT3021026
2021-05-14
2021-05-04
VT3021026
2021-06-01
2021-05-10
This is an example of what one single item is returning, however I want to return only one value for each item. Almost like a "TOP 1" for each individual item. (obviously top 1 won't work in the overall select clause because it will be required to produce a list of multiple items)
If I understand you correctly, then this code will solve the problem.
DECLARE #date datetime2 = GETDATE();
WITH T(ItemCode, OrderDate) AS
(
SELECT
T0.ItemCode,
MIN(T3.DocDate) AS 'OrderDate'
FROM OITM T0
LEFT JOIN POR1 T3
ON T0.ItemCode = T3.ItemCode
WHERE T3.LineStatus = 'O'
AND T3.ShipDate > #date
GROUP BY T0.Itemcode, T3.ShipDate
)
SELECT T.ItemCode,
T4.ShipDate AS 'Next Date',
T.OrderDate
FROM T
LEFT JOIN POR1 T4
ON T.[OrderDate] = T4.DocDate
AND T.ItemCode = t4.ItemCode;

Left join not giving all record Whats wrong with my query [duplicate]

This question already has answers here:
SQL Server left joining
(2 answers)
Closed 7 years ago.
When I do a left outer join, I expect to get all the records that the query would return all record form the left table which not matched with the right table.
I want to select the record even the month data is not available in table. Suppose I have one record for five month. So when I will execute the my query i want total 12 record [left outer join] with 0 values form right table[client_service] table.
my Query is
SELECT YEAR(CLIENT_SERVICE.cliserv_effective_date) As Year
, MonthsTable.MonthName As Month
, COUNT(CLIENT_SERVICE.cliserv_id) As ClientsInService
FROM MonthsTable
left outer join CLIENT_SERVICE
ON MonthsTable.MonthName = DATENAME(month,CLIENT_SERVICE.cliserv_effective_date)
WHERE CLIENT_SERVICE.serv_id = 24
AND CLIENT_SERVICE.cliserv_status = 'Active'
AND YEAR(CLIENT_SERVICE.cliserv_effective_date) BETWEEN 2014 AND 2015
GROUP BY YEAR(CLIENT_SERVICE.cliserv_effective_date)
, DATENAME(month,CLIENT_SERVICE.cliserv_effective_date)
, [MonthsTable].MonthName
, MONTH(CLIENT_SERVICE.cliserv_effective_date)
ORDER BY YEAR(CLIENT_SERVICE.cliserv_effective_date)
, MONTH(CLIENT_SERVICE.cliserv_effective_date);
Thanks in advance!!!
You have column of the right table in the where clause. put it in the on clause:
SELECT YEAR(CLIENT_SERVICE.cliserv_effective_date) As Year,
MonthsTable.MonthName As Month, COUNT(CLIENT_SERVICE.cliserv_id) As ClientsInService
FROM MonthsTable left outer join CLIENT_SERVICE ON MonthsTable.MonthName = DATENAME(month,CLIENT_SERVICE.cliserv_effective_date) and CLIENT_SERVICE.serv_id = 24
AND CLIENT_SERVICE.cliserv_status = 'Active'
AND YEAR(CLIENT_SERVICE.cliserv_effective_date) BETWEEN 2014 AND 2015
GROUP BY YEAR(CLIENT_SERVICE.cliserv_effective_date), DATENAME(month,CLIENT_SERVICE.cliserv_effective_date),
[MonthsTable].MonthName,MONTH(CLIENT_SERVICE.cliserv_effective_date)
ORDER BY YEAR(CLIENT_SERVICE.cliserv_effective_date),
MONTH(CLIENT_SERVICE.cliserv_effective_date)

How to modify this query to use between startindex and endindex rownum [duplicate]

This question already has answers here:
SQL ROWNUM how to return rows between a specific range
(7 answers)
ORA-00936: missing expression oracle
(2 answers)
Closed 8 years ago.
How to modify this query so that I get the results between rownum 10 and 21 for example:
SELECT DAL_ROWNOTABLE.DAL_ID
FROM (SELECT ticket.id AS "DAL_ID"
FROM ticket_table ticket
WHERE (ticket.type = N'I' ) AND
(ticket.tenant IS NULL OR
ticket.tenant IN (SELECT tgm.tenant_id
FROM tenant_group_member tgm
WHERE tgm.tenant_group = HEXTORAW('30B0716FEB5F4E4BB82A7B7AA3A1A42C')
)
)
ORDER BY ticket.id
) DAL_ROWNOTABLE
WHERE rownum <= 21;
Any help will be appreciated.
Change rownum to row_number():
SELECT DAL_ROWNOTABLE.DAL_ID
FROM (SELECT ticket.id AS "DAL_ID",row_number() over (order by ticket_id) as seqnum
FROM ticket_table ticket
WHERE (ticket.type = N'I' )AND
(ticket.tenant IS NULL OR
ticket.tenant IN(SELECT tgm.tenant_id
FROM tenant_group_member tgm
WHERE tgm.tenant_group = HEXTORAW('30B0716FEB5F4E4BB82A7B7AA3A1A42C')
)
)
) DAL_ROWNOTABLE
WHERE seqnum between 10 and 21;

How to merge a select statement with a calculated value as new columns? [duplicate]

This question already has answers here:
How to merge a select statement with a new dynamic values as columns?
(4 answers)
Closed 9 years ago.
In my sql server code, I have this select statement
select distinct
a.HireLastName,
a.HireFirstName,
a.HireID,
a.Position_ID,
a.BarNumber,
a.Archived,
a.DateArchived,
b.Position_Name
from NewHire a
join Position b on a.Position_ID = b.Position_ID
join WorkPeriod c on a.hireID = c.HireID
where a.Archived = 0 and c.InquiryID is not null
order by a.HireID DESC, a.HireLastName, a.HireFirstName
And I want to add a new column to it. However this column is not a column from a table, its just used to store a float from a calculation I make from existing columns.
The number I get is calculated like this:
#acc is the a.HireID from the above select statement.
CAST((select COUNT(*) from Hire_Response WHERE HireID = #acc AND (HireResponse = 0 OR HireResponse = 1)) as FLOAT) /
CAST((select COUNT(*) from Hire_Response WHERE HireID = #acc) as FLOAT)
How can I do this?
Thanks.
You can use the code that GBN did for you on your other question as a sub-query
select distinct
a.HireLastName,
a.HireFirstName,
a.HireID,
a.Position_ID,
a.BarNumber,
a.Archived,
a.DateArchived,
b.Position_Name,
d.percentage
from NewHire a
inner join Position b on a.Position_ID = b.Position_ID
inner join WorkPeriod c on a.hireID = c.HireID
left join (select NH.HireID, ISNULL(1E0 * COUNT(CASE WHEN HireResponse IN (0,1) THEN HR.HireID END) / NULLIF(COUNT(HR.HireID), 0) , 0) AS percentage
from NewHire NH LEFT JOIN Hire_Response HR ON NH.HireID = HR.HireID
group by NH.HireID) d
ON a.HireID = d.HireID
where a.Archived = 0 and c.InquiryID is not null
order by a.HireID DESC, a.HireLastName, a.HireFirstName
... this will add the column percentage to your current query. This could probably be improved, but it should give you a god idea of how you can bring the results of your questions together.

Resources