I would like to know if there is a way to do the following within a SQL Server script.
Let's say I have the following table
** To make things simple, IDs in example are INT. In my real scenario, these are UNIQUEIDENTIFIER
ParentId ChildId
-----------------------
101 201
101 202
101 203
102 204
102 205
103 206
103 207
103 208
103 209
I would like to query the table to get the following result.
So far I was able to get the ChildIndex column using the ROW_NUMBER() function. I am now struggling with the ParentIndex column...
ParentId ChildId ChildIndex ParentIndex
---------------------------------------------------
101 201 1 1
101 202 2 1
101 203 3 1
102 204 1 2
102 205 2 2
103 206 1 3
103 207 2 3
103 208 3 3
103 209 4 3
Here is my query so far
SELECT ParentId,
ChildId,
ROW_NUMBER() OVER ( PARTITION BY ParentId ORDER BY ParentId DESC ) AS ChildIndex
FROM MyTable
DENSE_RANK() is all you need.
DENSE_RANK() OVER (ORDER BY ParentId DESC) AS ParentIndex
Related
I have two tables Master table and the Matched Master Table based on the same saldate , saltime , lctid ,masid the counts has to be pulled up
For count(masid,saldate,saltime,lctid)>1 , for masid 121 in master table there are 5 records in Master Matched 3 records, I need to get the output which are not in Mactched and how much in Master and the total amount with the masid
Master
ID Saldate SalTime lctid masid Sal_amt
101 1/1/2000 100 120 121 15
102 1/1/2000 100 120 121 25
103 1/1/2000 100 120 121 100
118 1/1/2001 120 118 201 25
119 1/1/2009 302 222 187 60
104 1/1/2000 100 120 121 125
108 1/1/2000 100 120 121 22
Master matched
ID Saldate SalTime lctid masid Sal_Amt
101 1/1/2000 100 120 121 15
102 1/1/2000 100 120 121 25
118 1/1/2001 120 118 201 25
119 1/1/2009 302 222 187 60
OP1-Master OP2-Master matched
Masid Count(iD) SalAMt Masid Count(iD) Sal_Amt
121 3 247 121 2 40
I have a list of customers that can have a single, or multiple, service listed. In the table that houses the changes over time there is an indicator of 'Added' or 'Removed'.
What I need: determine those service(s) that are currently active, if at all.
Here is a sample set of data:
CUST_ID SRV_ID STATUS ACTION_DATE
12345 102 Added 1/31/17 10:15
12345 189 Added 4/18/17 15:37
12345 189 Removed 4/21/17 14:08
12345 194 Added 5/2/17 14:43
12345 194 Removed 5/5/17 10:02
12345 194 Added 5/5/17 13:06
12345 69 Added 4/19/17 9:36
12345 69 Removed 5/2/17 14:43
12345 73 Added 4/20/17 10:21
12345 73 Removed 4/25/17 11:20
12345 95 Added 5/4/17 9:48
12345 95 Removed 5/4/17 10:05
Records to be returned: 102 on 1/31/17 10:15 and 194 on 5/5/17 13:06
You can find the latest row for each cust_id and serv_id using top 1 with ties and window function row_number and then filter those with status "Added":
select *
from (
select top 1
with ties *
from your_table
order by row_number() over (
partition by cust_id, srv_id
order by action_date desc
)
) t
where status = 'Added'
Produces:
CUST_ID SRV_ID STATUS ACTION_DATE
12345 102 Added 2017/01/31 10:15
12345 194 Added 2017/05/05 13:06
Demo
Like this:
SELECT SRV_ID
FROM YourTable
GROUP BY SRV_ID
HAVING MAX(CASE WHEN STATUS='Added' THEN ACTION_DATE END)
> MAX(CASE WHEN STATUS='Removed' THEN ACTION_DATE END)
SELECT RIGHT(timestamp,LEN(timestamp) -12) as DailyTime, left(roundtrip, LEN(roundtrip) -2) as HalfHourDuration, site_code
FROM tblServer_Status
WHERE timestamp >= dateadd(day, datediff(day,'19000101',CURRENT_TIMESTAMP),'19000101') AND timestamp < dateadd(day, datediff(day,'19000101',CURRENT_TIMESTAMP)+1,'19000101') AND server = 'ServerName' AND site_code = 'A'
GROUP BY timestamp, roundtrip, site_code HAVING(((COUNT(site_code))>0))
ORDER BY timestamp
I have this code that gives me this kind of output
| DailyTime | HalfHourDuration | Site_Code|
12:00AM 122 A
12:00AM 143 A
12:00AM 242 A
12:30AM 112 A
12:30AM 222 A
12:30AM 462 A
01:00AM 322 A
01:00AM 642 A
01:00AM 322 A
01:30AM 146 A
01:30AM 167 A
01:30AM 116 A
02:00AM 163 A
02:00AM 145 A
02:00AM 121 A
02:30AM 149 A
02:30AM 135 A
02:30AM 111 A
...................................
But I need to get the Latest duration per time.
Like this one
| DailyTime | HalfHourDuration | Site_Code|
12:00AM 242 A
12:30AM 462 A
01:00AM 322 A
01:30AM 116 A
02:00AM 121 A
02:30AM 111 A
Something like that.
can anyone help me configure my codes.
Thanks.
You can do this using row_number():
with t as (
<your query here without order by>
)
select t.*
from (select t.*,
row_number() over (partition by DailyTime
order by HalfHourDuration desc
) as seqnum
from t
) t
where seqnum = 1;
Add Max function to your column HalfHourDuration.
This will list out the maximum value of the roundtrip alone grouping by timestamp
I have a table as shown below :
ID OrderId IssueId EmployeeId
1 123 123 12098
2 124 123 12098
3 125 123 12098
4 123 124 12098
5 124 124 12098
6 125 124 12098
7 123 126 12098
8 124 126 12098
9 125 126 12098
where against each OrderId there is record inserted for IssueId. Now i want to filter that which IssueId is not matching the OrderId.
i.e Above sample data orderId 125, but issueId 126. I need to filter only this data from the table.
I'm using following query
Select OrderId,IssueId,EmployeeId from IssuesTable where OrderId != IssueId
i'm getting the following result.
OrderId IssueId EmployeeId
123 126 12098
124 126 12098
125 126 12098
Since each order id has got an issueId i'm getting 3rows for missing issueId.
can anyone help me on this issue. ??
Try this query:
SELECT *
FROM <table>
WHERE OrderId = IssueId
Here is a sample SQLFiddle showing how only the rows with the same OrderId and IssueId are being returned.
If you want to find the orderid which doesnot match with Issueid in any row then try this
select orderid
from yourtable
group by orderid
having count(case when orderid = IssueId then 1 end) = 0
If you want to select the IssueId along with OrderId then use this
SELECT OrderId,IssueId
FROM Yourtable A
WHERE EXISTS (SELECT 1
FROM Yourtable B
WHERE a.orderid = b.orderid
GROUP BY orderid
HAVING Count(CASE WHEN orderid = issueid THEN 1 END) = 0)
I've an xml which has multiple blocks in it as per below.
declare #Query as xml,
#poxmldoc INT
set #Query='<ProductionOrder><Header><MessageID>00000005</MessageID></Header><Body>ProductionOrderDetails>Workorder_ID>100</Workorder_ID<Item_ID>4010124</Item_ID<Publisheddata></Publisheddata><BOM><Position>1</Position><Item_ID>111</Item_ID><BOM_Qty>100</BOM_Qty><UoM>Liters</UoM>
</BOM><BOM><Position>2</Position><Item_ID>222</Item_ID><BOM_Qty>101</BOM_Qty><UoM>Kilograms</UoM></BOM><BOM><Position>3</Position><Item_ID>333</Item_ID><BOM_Qty>102</BOM_Qty><UoM>Kilograms</UoM></BOM></ProductionOrderDetails><ProductionOrderDetails><Workorder_ID>101</Workorder_ID><Item_ID>4010124</Item_ID><Publisheddata></Publisheddata><BOM><Position>1</Position><Item_ID>111</Item_ID><BOM_Qty>103</BOM_Qty><UoM>Liters</UoM></BOM><BOM><Position>2</Position><Item_ID>222</Item_ID><BOM_Qty>104</BOM_Qty><UoM>Kilograms</UoM></BOM><BOM><Position>3</Position><Item_ID>333</Item_ID><BOM_Qty>105</BOM_Qty><UoM>Kilograms</UoM></BOM></ProductionOrderDetails></Body></ProductionOrder>'
When I execute this query
EXEC SP_XML_PREPAREDOCUMENT #poxmldoc OUTPUT, #Query
SELECT *
FROM OPENXML(#poxmldoc, '/ProductionOrder/Body/ProductionOrderDetails/BOM',2)
WITH (Position INT 'Position',
Item_ID NVARCHAR(40) 'Item_ID',
BOM_Qty FLOAT 'BOM_Qty',
UoM NVARCHAR(40) 'UoM')
I get this output:
Position Item_ID BOM_Qty UoM
------------------------------------
1 111 100 Liters
2 222 101 Kilograms
3 333 102 Kilograms
1 111 103 Liters
2 222 104 Kilograms
3 333 105 Kilograms
How to get the <Workorder_ID> tag value along with this result.
Like this:
Position Item_ID BOM_Qty UoM Wo_Id
---------------------------------------------
1 111 100 Liters 100
2 222 101 Kilograms 100
3 333 102 Kilograms 100
1 111 103 Liters 101
2 222 104 Kilograms 101
3 333 105 Kilograms 101
Given your #Query XML variable, you could this to use this native XQuery to get what you're looking for:
SELECT
POSITION = XC.value('(Position)[1]', 'int'),
ItemID = XC.value('(Item_ID)[1]', 'int'),
BomQty = XC.value('(BOM_Qty)[1]', 'int'),
UnitOfMeasure = XC.value('(UoM)[1]', 'varchar(50)'),
Workorder_ID = XC.value('(../Workorder_ID)[1]', 'int')
FROM
#Query.nodes('/ProductionOrder/Body/ProductionOrderDetails/BOM') AS XT(XC)
By specifying the (../Workorder_ID) XPath, you're basically accessing the parent element of your <BOM> element in question.
Use XPath/XQuery. ie:
select
p.value('Position[1]','int'),
p.value('Item_ID[1]','int'),
p.value('BOM_Qty[1]','int'),
p.value('UoM[1]','varchar(20)'),
p.value('../Workorder_ID[1]','int')
from
#query.nodes('/ProductionOrder/Body/ProductionOrderDetails/BOM') x(p)