Sum up articles per orderID via SQL Server - sql-server

I have the following:
OrderID | Articlenumber|
--------+--------------+
1 | 123 |
2 | 222 |
1 | 799 |
1 | 987 |
2 | 444 |
3 | 212 |
2 | 222 |
1 | 898 |
and I want the following (sum up all article numbers per orderID):
orderID|articelnumber |
-------+--------------------+
1 |123, 799, 987, 898 |
2 |222, 444, 222 |
3 |212 |
or:
orderID|articelnumber|articelnumber|articelnumber |articelnumber |
-------+-------------+-------------+--------------+--------------+
1 |123 |799 |987 | 898 |
2 |222 |444 |222 | |
3 |212 | | | |
How can I do it with SQL-Server? The number of articles per orderID is variable.
Thanks a lot!

Is something like this what you want?
;WITH cte AS
(
SELECT OrderID, Articlenumber
FROM [YOUR_TABLE]
)
SELECT
OrderID,
STUFF((SELECT ',' + Articlenumber FROM [YOUR_TABLE] WHERE [YOUR_TABLE].OrderID = cte.OrderID FOR XML PATH('')), 1, 1, '') articelnumber
FROM cte
GROUP BY OrderID
ORDER BY 1

Related

Select row satisfying certain condition and rows next to it

Let's say I have a historical table keeping who has modified data
-------------------------------------------------------------
| ID | Last_Modif | User_Modif | Col3, Col4...
-------------------------------------------------------------
| 1 | 2018-04-09 12:12:00 | John
| 2 | 2018-04-09 11:10:00 | Jim
| 3 | 2018-04-09 11:05:00 | Mary
| 4 | 2018-04-09 11:00:00 | John
| 5 | 2018-04-09 10:56:00 | David
| 6 | 2018-04-09 10:53:00 | John
| 7 | 2018-04-08 19:50:00 | Eric
| 8 | 2018-04-08 18:50:00 | Chris
| 9 | 2018-04-08 15:50:00 | John
| 10 | 2018-04-08 12:50:00 | Chris
----------------------------------------------------------
I would like to find the modifs done by John and previous version before he did that, to check what he had modified. For example in this scenario I would like to return row 1,2,4,5,6,7,9,10
I am thinking of ranking first based on Last_modif then do a join to pick up the next row, but somehow the result is not correct. This seems not a LAG/LEAD case since I am not picking a single value from the next row, but instead the whole next row. Any idea ?
-- sample 1000 rows with RowNumber
with TopRows as
(select top 1000 *, ROW_NUMBER() OVER(ORDER BY Last_modif desc) RowNum from [Table])
--Reference rows : Rows modif by John
, ModifByJohn as
(Select * from TopRows where USER_MODIF = 'John')
select * from ModifByJohn
UNION
select ModifByNext.* from ModifByJohn join TopRows ModifbyNext on ModifByJohn.RowNum + 1 = ModifByNext.RowNum
order by RowNum
How will the code look like if we would like to return last 2 modifs before John did instead of 1 ?
Maybe you can take advantage of your current ID:
with x as
(
select t1.*,
(select top 1 id from tbl where id > t1.id) prev_id
from tbl t1
where t1.User_Modif = 'John'
)
select * from x;
GO
ID | Last_Modif | User_Modif | prev_id
-: | :------------------ | :--------- | ------:
1 | 09/04/2018 12:12:00 | John | 2
4 | 09/04/2018 11:00:00 | John | 5
6 | 09/04/2018 10:53:00 | John | 7
9 | 08/04/2018 15:50:00 | John | 10
with x as
(
select t1.*,
(select top 1 id from tbl where id > t1.id) prev_id
from tbl t1
where t1.User_Modif = 'John'
)
select ID, Last_Modif, User_Modif from x
union all
select ID, Last_Modif, User_Modif
from tbl
where ID in (select prev_id from x)
order by ID
GO
ID | Last_Modif | User_Modif
-: | :------------------ | :---------
1 | 09/04/2018 12:12:00 | John
2 | 09/04/2018 11:10:00 | Jim
4 | 09/04/2018 11:00:00 | John
5 | 09/04/2018 10:56:00 | David
6 | 09/04/2018 10:53:00 | John
7 | 08/04/2018 19:50:00 | Eric
9 | 08/04/2018 15:50:00 | John
10 | 08/04/2018 12:50:00 | Chris
dbfiddle here

How can I get SQL results that resemble a pivot table

I am trying to get back some results from a database and it needs to be returned in a very specific way, similar to a pivot table but from what I've been reading about them I don't know if it applies since I do not need an aggregate. This is to display RFQ information in a report. The database structure is complicated but I will use a simplified example.
RFQ table
----------------
RFQID | Requestor
----------------
555 | 789
777 | 789
Item table
--------------------------
RFQID | Line | ItemID
--------------------------
555 | 1 | P07
777 | 1 | P07
ItemQuantity table
------------------------------------
RFQID | Line | Quantity | Unit
------------------------------------
555 | 1 | 5 | In
555 | 1 | 10 | In
555 | 1 | 15 | In
777 | 1 | 30 | In
777 | 1 | 50 | In
Vendor table
-----------------------------
RFQID | Line | VendorID
-----------------------------
555 | 1 | Speedy
777 | 1 | SlowPoke
So if I enter a query like this:
SELECT M.RFQID, Requester, I.Line, ItemID, Quantity, Unit, VendorID
FROM RFQ M
JOIN Item I ON M.RFQID = I.RFQID
JOIN Vendor V ON I.Line = V.Line AND I.RFQID = V.RFQID
JOIN ItemQuantity IQ ON IQ.Line = V.Line AND IQ.RFQID = V.RFQID
WHERE ItemID IN ('P07', 'P08')
ORDER BY M.RFQID DESC
Which will produce:
555 | 789 | 1 | P07 | 5 | In | Speedy
555 | 789 | 1 | P07 | 10 | In | Speedy
555 | 789 | 1 | P07 | 15 | In | Speedy
777 | 789 | 1 | P07 | 30 | In | SlowPoke
777 | 789 | 1 | P07 | 50 | In | SlowPoke
I need to get:
RFQID| Requestor | Line | ItemID | Quantity row1 | Unit row1 | Quantity row2 | Unit row2 | Quantity row3 | Unit row3 | VendorID
555 | 789 | 1 | P07 | 5 | In | 10 | In | 15 | In | Speedy
777 | 789 | 1 | P07 | 30 | In | 50 | In | NULL | NULL | SlowPoke
Is this possible? I know I can write a SP using a cursor but I wish to avoid that route if possible.
EDIT: This is part of a report that:
1) The whole company uses
2) Consists of 14 datasets
3) Is so customized that I doubt there is a report writer that can produce it.
4) It exists in Access 2003 which we wish to get rid of
5) I have almost the whole thing done with the PrintDocument with all the attributes table based so it can be modified without compiling anything.
Using Jason's technique I was able to produce the correct output.
SELECT M.RFQID, Requester, I.Line, ItemID, Qty1, Qty2, Qty3, VendorID
FROM RFQ M
JOIN Item I ON M.RFQID = I.RFQID
JOIN Vendor V ON I.Line = V.Line AND I.RFQID = V.RFQID
JOIN (SELECT RFQID, LineItem, MAX(CASE WHEN QtyID = 1 THEN QUANTITY END) AS Qty1, MAX(CASE WHEN QtyID = 2 THEN QUANTITY END) AS Qty2, MAX(CASE WHEN QtyID = 3 THEN QUANTITY END) AS Qty3
FROM ItemQuantity
GROUP BY RFQID, Line) IQ ON IQ.Line = I.Line AND IQ.RFQID = M.RFQID
WHERE ItemID IN ('P07', 'P08')
ORDER BY M.RFQID DESC
Which results in
RFQID| Requestor | Line | ItemID | Quantity row1 | Quantity row2 | Quantity row3 | VendorID
555 | 789 | 1 | P07 | 5 | 10 | 15 | Speedy
777 | 789 | 1 | P07 | 30 | 50 | NULL | SlowPoke
Although my demo data did not include a QtyID the table can be rewritten as:
ItemQuantity table
----------------------------------------------
RFQID | Line | QtyID | Quantity | Unit
----------------------------------------------
555 | 1 | 1 | 5 | In
555 | 1 | 2 | 10 | In
555 | 1 | 3 | 15 | In
777 | 1 | 1 | 30 | In
777 | 1 | 2 | 50 | In
The Unit can be added the same way.
Thanks Jason!

How to rename column before unpivot? SQL Server

Help I want to rename a column before using the unpivot method
My table is like this:
List item
id| value| ENE| FEB| MAR| ABR| MAY
1 | dsads|2000|2334|2344|2344|2344
after unpivot I get something like this
id| value| month| amount
1 | dads| ENE | 2000
2 | sadf| FEB | 2334
but I want something like this
id| value| month| amount
1 | dads| 01 | 2000
2 | sadf| 02 | 2334
This is my query
select
[año], [Empresa], [Region], [Suc], [CC], [Cuenta],
[Subcuenta], [Descripcion], Periodo, Importe
from
(select * from [dbo].['P Cargado$']) S
unpivot
(Importe for Periodo in
([ENE], [FEB], [MAR], [ABR], [MAY], [JUN], [JUL], [AGO], [SEP],[OCT], [NOV], [DIC])
) AS unpvt;
If you use cross apply(values ..) to unpivot, you could do so like this:
select t.id, t.value, x.*
from t
cross apply (values (1,ene),(2,feb),(3,mar),(4,abr),(5,may)) x (Mnth,Amount)
rextester demo: http://rextester.com/SZDP50356
returns:
+----+-------+------+--------+
| id | value | Mnth | Amount |
+----+-------+------+--------+
| 1 | dsads | 1 | 2000 |
| 1 | dsads | 2 | 2334 |
| 1 | dsads | 3 | 2344 |
| 1 | dsads | 4 | 2344 |
| 1 | dsads | 5 | 2344 |
+----+-------+------+--------+

SQL Server query for next row value where previous row value

This query gives me Event values from 1 to 20 within an hour, how to add to that if a consecutive Event value is >=200 as well?
SELECT ID, count(Event) as numberoftimes
FROM table_name
WHERE Event >=1 and Event <=20
GROUP BY ID, DATEPART(HH, AtHour)
HAVING DATEPART(HH, AtHour) <= 1
ORDER BY ID desc
In this dummy 24h table:
+----+-------+--------+
| ID | Event | AtHour |
+----+-------+--------+
| 1 | 1 | 11:00 |
| 1 | 4 | 11:01 |
| 1 | 1 | 11:02 |
| 1 | 20 | 11:03 |
| 1 | 200 | 11:04 |
| 1 | 1 | 13:00 |
| 1 | 1 | 13:05 |
| 1 | 2 | 13:06 |
| 1 | 500 | 13:07 |
| 1 | 39 | 13:10 |
| 1 | 50 | 13:11 |
| 1 | 2 | 13:12 |
+----+-------+--------+
I would like to select IDs with Event with values with range between 1 and 20 followed immediately by value greater than or equal to 200 within an hour.
Expected result should be something like that:
+----+--------+
| ID | AtHour |
+----+--------+
| 1 | 11 |
| 1 | 13 |
| 2 | 11 |
| 2 | 14 |
| 3 | 09 |
| 3 | 12 |
+----+--------+
or just how many times it has happened for unique ID instead of which hour.
Please excuse me I am still rusty with post formatting!
CREATE TABLE data (Id INT, Event INT, AtHour SMALLDATETIME);
INSERT data (Id, Event, AtHour) VALUES
(1,1,'2017-03-16 11:00:00'),
(1,4,'2017-03-16 11:01:00'),
(1,1,'2017-03-16 11:02:00'),
(1,20,'2017-03-16 11:03:00'),
(1,200,'2017-03-16 11:04:00'),
(1,1,'2017-03-16 13:00:00'),
(1,1,'2017-03-16 13:05:00'),
(1,2,'2017-03-16 13:06:00'),
(1,500,'2017-03-16 13:07:00'),
(1,39,'2017-03-16 13:10:00')
;
; WITH temp as (
SELECT rownum = ROW_NUMBER() OVER (PARTITION BY id ORDER BY AtHour)
, *
FROM data
)
SELECT a.id, DATEPART(HOUR, a.AtHour) as AtHour, COUNT(*) AS NumOfPairs
FROM temp a JOIN temp b ON a.rownum = b.rownum-1
WHERE a.Event BETWEEN 1 and 20 AND b.Event >= 200
AND DATEDIFF(MINUTE, a.AtHour, b.AtHour) <= 60
GROUP BY a.id, DATEPART(HOUR, a.AtHour)
;

SQL Server : pivot a single column with fixed number or columns

I have this simple query that brings the siblings of a given item.
select
PC.SKU
from
ProdC PC
where
Parent_ID in (select Parent_ID
from ProdC
where SKU = 4536)
and ParentFlag <> 'P'
and SKU <> 4536
I'd like to display up to 6 siblings horizontally. So it would look something like this:
Sib1 Sib2 Sib3 Sib4 Sib5 Sib6
=============================================
4532 4539 4548 4552 4561 4562
3512 3536
5632 5636 5640
Now each of these row are for a different item. some parent have 2 child, some have up to 8 but I only want to show 6 max. There's a priority column for the children. I can sort it by that column desc to get the 6 newest child.
Any help appreciated.
I have a table in my test database with records with multiple child records for each parent record, created the following query to only get the top 6 child records, but it only picks the immediate child records, if you have another level of children records then you probably need to look into recursive CTE etc.
;WITH X AS
(
Select *
,ROW_NUMBER() OVER (PARTITION BY Parent_ID ORDER BY ID) rn
from TableName
),
Y AS (
Select * , 'Sib' + Cast(rn AS Varchar(10)) Sibs
FROM X
Where rn < 7
)
Select *
from
(
Select Parent_ID , Sibs , ID
FROM Y
) a
PIVOT (MAX(ID)
FOR Sibs
IN (Sib1,Sib2,Sib3,Sib4,Sib5,Sib6))p
Result Set
+----------+------+------+------+------+-------+------+
| ParentID | Sib1 | Sib2 | Sib3 | Sib4 | Sib5 | Sib6 |
+----------+------+------+------+------+-------+------+
| 0 | 0 | 139 | 258 | 266 | 285 | 500 |
| 139 | 140 | 141 | 142 | 143 | 144 | 162 |
| 142 | 5062 | 5063 | NULL | NULL | NULL | NULL |
| 143 | 5041 | 5042 | 5043 | 5044 | 5045 | 5046 |
| 144 | 5050 | 5051 | 5052 | 5053 | 5054 | 5055 |
| 258 | 5823 | 5824 | 5825 | 5826 | 11269 | NULL |
| 266 | 5822 | 5912 | 5913 | 5914 | 5915 | 5916 |
| 285 | 2139 | 3855 | 4172 | 4173 | NULL | NULL |
+----------+------+------+------+------+-------+------+
EDIT
After you have provided some sample data your query should look something like..
;WITH X AS
(
Select *
,ROW_NUMBER() OVER (PARTITION BY Parent_Sku ORDER BY sku) rn
from #ProdC
),
Y AS (
Select * , 'Sib' + ISNULL(NULLIF(Cast(rn -1 AS Varchar(10)), '0'),'') Sibs
FROM X
Where rn < 8
)
Select Sib1,Sib2,Sib3,Sib4,Sib5,Sib6
from
(
Select Parent_Sku , Sibs , sku
FROM Y
) a
PIVOT (MAX(sku)
FOR Sibs
IN (Sib,Sib1,Sib2,Sib3,Sib4,Sib5,Sib6,Sib7))p
Result set
| Sib1 | Sib2 | Sib3 | Sib4 | Sib5 | Sib6 |
|------|------|------|--------|--------|--------|
| 4532 | 4536 | 4539 | 4548 | 4552 | (null) |
| 3512 | 3536 | 4561 | 4562 | (null) | (null) |
| 5632 | 5636 | 5640 | (null) | (null) | (null) |

Resources