SQL turn row to column - sql

How can you turn row to column in SQL
FIRMA PSNR FELDNR PFLFDNR INHALT PFGLTAB PFGLTBIS
1 1 013 1 1211111 201111 20111231
1 1 014 2 0,75 2011121 20111111
1 2 013 1 1312312 2011101 20991010
1 2 014 2 1 2011101 20991010
1 3 013 1 666666 2011111 20190101
1 3 014 2 0,925 2011010 20990101
should look so: The psnr is the key and over feldnr 013 or 014 you have different value
FIRMA PSNR FELDNR PFLFDNR INHALT (Inhalt as FTE) PFGLTAB PFGLTBIS
1 1 013 1 12111111 0,75 201111 20111231
1 2 013 1 1312312 1 2011101 20991010
1 3 013 1 666666 0,925 2011111 20190101

Not 100% sure what you are asking, but here goes:
select a.FIRMA,a.PSNG,a.FELDNR,a.PFLFDNR,a.INHALT,b.INHALT as '(Inhalt as FTE)',
a.PFGLTAB,a.PFGLTBIS
FROM table a
JOIN table b ON a.FIRMA=b.FIRMA and a.PSNR=b.PSNR AND b.FELDNR='014'
WHERE a.FELDNR='013'

Related

Using Pivot or UnPivot to get Output Dynamically

I have a table with the following structure that I want to receive output from this table dynamically. But I do not know how to use Pivot and UnPivot.
The list of fields I use is as follows.
SELECT [RoomID]
,[RoomNumber]
,[RoomType]
,[RoomTypeDescription]
,[RoomBed]
,[PriceOfPerNight]
,[RoomStatuse]
,[RoomStatuseDesc]
,[RoomFloorID]
,[RoomFloorTitle]
FROM [HotelOnline].[dbo].[XtblRooms]
RoomID RoomNumber RType RDesc Beds Price RoomStatuse RDesc FloorID RoomFloorTitle
1 RM100001 2 Degree 2 6 9000000.00 1 Free 1 Floor 001
2 RM100002 1 Degree 1 4 6000000.00 1 Free 1 Floor 001
3 RM100003 2 Degree 2 3 4500000.00 1 Free 1 Floor 001
4 RM100004 3 Degree 3 5 4800000.00 1 Free 1 Floor 001
5 RM100005 1 Degree 1 3 4700000.00 1 Free 1 Floor 001
6 RM100006 1 Degree 1 6 7500000.00 1 Free 1 Floor 001
7 RM100007 1 Degree 1 5 7000000.00 1 Free 1 Floor 001
8 RM100008 1 Degree 1 2 2500000.00 1 Free 1 Floor 001
9 RM100009 3 Degree 3 3 3500000.00 1 Free 1 Floor 001
10 RM100010 3 Degree 3 8 8000000.00 1 Free 1 Floor 001
11 RM100011 2 Degree 2 5 6500000.00 1 Free 2 Floor 002
12 RM100012 3 Degree 3 2 3800000.00 1 Free 2 Floor 002
13 RM100013 2 Degree 2 5 9650000.00 1 Free 2 Floor 002
14 RM100014 3 Degree 3 2 2500000.00 1 Free 2 Floor 002
15 RM100015 2 Degree 2 2 4500000.00 1 Free 2 Floor 002
16 RM100016 3 Degree 3 4 4000000.00 1 Free 2 Floor 002
17 RM100017 1 Degree 1 2 2500000.00 1 Free 2 Floor 002
18 RM100018 3 Degree 3 3 4500000.00 1 Free 2 Floor 002
19 RM100019 2 Degree 2 5 5000000.00 1 Free 2 Floor 002
20 RM100020 2 Degree 2 4 4500000.00 1 Free 2 Floor 002
21 RM100021 1 Degree 1 6 7500000.00 1 Free 3 Floor 003
22 RM100022 2 Degree 2 3 3000000.00 1 Free 3 Floor 003
23 RM100023 3 Degree 3 3 2500000.00 1 Free 3 Floor 003
24 RM100024 1 Degree 1 3 2500000.00 1 Free 3 Floor 003
25 RM100025 2 Degree 2 5 4800000.00 1 Free 3 Floor 003
26 RM100026 3 Degree 3 4 4000000.00 1 Free 3 Floor 003
27 RM100027 2 Degree 2 2 1800000.00 1 Free 3 Floor 003
28 RM100028 3 Degree 3 5 4700000.00 1 Free 3 Floor 003
29 RM100029 1 Degree 1 3 3500000.00 1 Free 3 Floor 003
30 RM100030 2 Degree 2 6 4600000.00 1 Free 3 Floor 003
31 RM100031 2 Degree 2 5 4500000.00 1 Free 4 Floor 004
32 RM100032 1 Degree 1 2 3500000.00 1 Free 4 Floor 004
33 RM100033 3 Degree 3 4 3700000.00 1 Free 4 Floor 004
34 RM100034 2 Degree 2 3 2800000.00 1 Free 4 Floor 004
35 RM100035 3 Degree 3 6 5500000.00 1 Free 4 Floor 004
36 RM100036 2 Degree 2 4 3700000.00 1 Free 4 Floor 004
37 RM100037 3 Degree 3 6 5800000.00 1 Free 4 Floor 004
38 RM100038 1 Degree 1 3 4000000.00 1 Free 4 Floor 004
39 RM100039 1 Degree 1 5 5500000.00 1 Free 4 Floor 004
40 RM100040 1 Degree 1 6 6500000.00 1 Free 4 Floor 004
41 RM100041 1 Degree 1 4 4500000.00 1 Free 5 Floor 005
42 RM100042 2 Degree 2 6 5500000.00 1 Free 5 Floor 005
43 RM100043 2 Degree 2 4 4000000.00 1 Free 5 Floor 005
44 RM100044 2 Degree 2 3 3500000.00 1 Free 5 Floor 005
45 RM100045 3 Degree 3 3 3000000.00 1 Free 5 Floor 005
46 RM100046 3 Degree 3 5 4000000.00 1 Free 5 Floor 005
47 RM100047 3 Degree 3 4 3900000.00 1 Free 5 Floor 005
48 RM100048 2 Degree 2 5 4700000.00 1 Free 5 Floor 005
49 RM100049 2 Degree 2 3 3800000.00 1 Free 5 Floor 005
50 RM100050 3 Degree 3 5 4700000.00 1 Free 5 Floor 005
This is the output I need.
Floor 001 Floor 002 Floor 003 Floor 004 Floor 005
==========================================================
RM100001 RM100012 RM100028 RM100033 RM100049
RM100002 RM100013 RM100029 RM100033 RM100050
.. . . . . .
I tried several ways but did not get the answer. Help if possible. I tried several ways but did not get the answer. Help if possible. If possible, use the dynamic method to get the answer. It does not matter if it is not for you. My problem will be solved in the same way as usual.
Because I am "illiterate" I was unable to answer this question:
--If you want a full working example for your data, provide full DDL and DML statements.
WITH YourTable AS(
SELECT *
FROM (VALUES
('1','RM100001','2','Degree 2','6','9000000.00','1','Free','1','Floor 001'),
('2','RM100002','1','Degree 1','4','6000000.00','1','Free','1','Floor 001'),
('3','RM100003','2','Degree 2','3','4500000.00','1','Free','1','Floor 001'),
('4','RM100004','3','Degree 3','5','4800000.00','1','Free','1','Floor 001'),
('5','RM100005','1','Degree 1','3','4700000.00','1','Free','1','Floor 001'),
('6','RM100006','1','Degree 1','6','7500000.00','1','Free','1','Floor 001'),
('7','RM100007','1','Degree 1','5','7000000.00','1','Free','1','Floor 001'),
('8','RM100008','1','Degree 1','2','2500000.00','1','Free','1','Floor 001'),
('9','RM100009','3','Degree 3','3','3500000.00','1','Free','1','Floor 001'),
('10','RM100010','3','Degree 3','8','8000000.00','1','Free','1','Floor 001'),
('11','RM100011','2','Degree 2','5','6500000.00','1','Free','2','Floor 002'))V(RoomID,RoomNumber,RType,RDesc,Beds,Price,RoomStatuse,RDesc2,FloorID,RoomFloorTitle)),
/*
Having multiple columns with the same name is a design flaw (not allowed).
I have named the second Rdesc as RDesc2, but this is also flawed (it breaks normalisation rules).
Fix your design, normalise your data.
*/
RNs AS(
SELECT RoomNumber,
RoomFloorTitle,
ROW_NUMBER() OVER (PARTITION BY RoomFloorTitle ORDER BY RoomNumber) AS RN --ORDER BY should be your ID/always ascending column
FROM YourTable)
SELECT MAX(CASE RoomFloorTitle WHEN 'Floor 001' THEN RoomNumber END) AS Floor001,
MAX(CASE RoomFloorTitle WHEN 'Floor 002' THEN RoomNumber END) AS Floor002
FROM RNs
GROUP BY RN;
db<>fiddle
SELECT *
FROM (
select RoomStatuseDesc,
RoomNumber,
count(*) over (partition by RoomStatuseDesc order by
RoomNumber) rm2 from XtblRooms)as XtblRooms
PIVOT (max(RoomNumber) --as R_count, min(RoomNumber) as r_start
for RoomStatuseDesc in('Floor1','Floor2', 'Floor3', 'Floor4', 'Floor5'))pvt

SQL Select statement and show only changed

I have the simple select script and it generates following audit table.
SELECT *
FROM Mytable
WHERE File = '123456A'
Output:
ID
File
StatusA
StatusB
User
UpdateDate
1
123456A
A
0
Tom
2021-01-01
12
123456A
B
0
Jack
2021-01-05
19
123456A
A
1
Alicia
2021-02-09
56
123456A
B
1
Jason
2021-03-09
87
123456A
A
1
Jason
2021-03-10
107
123456A
B
0
Ellie
2021-03-26
203
123456A
A
0
lucy
2021-04-08
239
123456A
B
1
Ellie
2021-04-16
I am trying to retrieve the rows when only column StatusB is changed. So it will generates the table like this.
SELECT *
FROM Mytable
WHERE File = '123456A'
-AND StatusB is changed
ID
File
StatusA
StatusB
User
UpdateDate
1
123456A
A
0
Tom
2021-01-01
19
123456A
A
1
Alicia
2021-02-09
107
123456A
B
0
Ellie
2021-03-26
239
123456A
B
1
Ellie
2021-04-16
In this case, I can see Alicia and Ellie changed the column StatusB. I am still thinking how to accomplish this goal.
Thanks,
-Ming
You can use lag():
select t.*
from (select t.*,
lag(statusB) over (order by updatedate) as prev_statusB
from Mytable t
where File = '123456A'
) t
where prev_statusB is null or prev_statusB <> statusB;

Exclude rows where keys match, but are on different rows

I'm looking for the best way to produce the result set in the scenario provided. My cust3 column isn't identifying the repeated values in the indvid2 column. The end result I'm looking for is to exclude the rows where key1 and key2 match (ids:1,2,6 and 7), then sum accounts where the acctids match.If there's a better way to code this, I welcome all suggestions. Thanks!
WITH T10 as (
SELECT acctid,invid,(
case
when invid like '%-R' then left (InvID,LEN(invid) -2) else InvID
END) as InvID2
FROM table x
GROUP BY acctID,invID
),
T11 as (
SELECT acctid, Invid2, COUNT(InvID2) as cust3
FROM T10
GROUP BY InvID2,acctid
HAVING
COUNT (InvID2) > 1
)
select DISTINCT
a.acctid,
a.name,
b.invid,
C.invid2,
D.cust3,
b.amt,
b.key1,
b.key2
from table a
inner join table b (nolock) on a.acctid = b.acctid
inner join T10 C (nolock) on b.invid = c.invid
inner join T11 D (nolock) on C.invid2 = D.invid2
Resultset
id acctID name invid invid2 Cust3 amt key1 key2
1 123 James 101 101 2 $500 NULL 6789
2 123 james 101-R 101 2 ($500) 6789 NULL
3 123 James 102 102 2 $350 NULL NULL
4 123 James 103 103 2 $200 NULL NULL
5 246 Tony 98-R 98 2 ($750) 7423 NULL
6 432 David 45 45 2 $100 NULL 9634
7 432 David 45-R 45 2 ($100) 9634 NULL
8 359 Stan 39-R 39 2 ($50) 6157 NULL
9 753 George 95 95 2 $365 NULL NULL
10 753 George 108 108 2 $100 NULL NULL
Desired Resultset
id acctID name invid invid2 Cust3 amt key1 key2
1 123 James 101 101 2 $500 NULL 6789
2 123 james 101-R 101 2 ($500) 6789 NULL
3 123 James 102 102 1 $350 NULL NULL
4 123 James 103 103 1 $200 NULL NULL
5 246 Tony 98-R 98 1 ($750) 7423 NULL
6 432 David 45 45 2 $100 NULL 9634
7 432 David 45-R 45 2 ($100) 9634 NULL
8 359 Stan 39-R 39 1 ($50) 6157 NULL
9 753 George 95 95 1 $365 NULL NULL
10 753 George 108 108 1 $100 NULL NULL
Then to sum amt by acctid
id acctid name amt
1 123 James $550
2 246 Tony ($750)
3 359 Stan ($50)
4 753 George $465
Something like:
;WITH Keys as (
SELECT Key1.acctID, [Key] = Key1.Key1
FROM YourTable as Key1
INNER JOIN YourTable as Key2
ON Key1.Key1 = Key2.Key2 and Key1.acctID = Key2.acctID
)
SELECT t.acctID, t.name, amt = SUM(t.amt)
FROM YourTable as t
LEFT JOIN Keys as k
ON t.acctID = k.acctID and (t.Key1 = [Key] or t.Key2 = [Key])
WHERE k.acctID is Null
GROUP BY t.acctID, t.name

Update Table A from loop data on table B

I want to update table qcard with data every record from q_id table
q_id table field and have 5 record
---------------
q_id groupcode
---- ---------
1 A01
2 A02
3 A03
4 A05
5 A06
qcard table now have data in field qcard 10001-2000
field loop q_id groupcode not data
qcard loop q_id groupcode
----- ---- ---- ---------
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
I need to update qcard table with all record from q_id table for each loop (loop I set from 01,02,03,....to 99) if finish each loop have all record from q_id table
if works qcard table this data
-------------------------------
qcard loop q_id groupcode
----- ---- ---- ---------
10001 01 1 A01
10002 01 2 A02
10003 01 3 A03
10004 01 4 A04
10005 01 5 A05
10006 02 1 A01
10007 02 2 A02
10008 02 3 A03
10009 02 4 A04
10010 02 5 A05
...... untill
1xxxx 99 1 A01
1xxxx 99 2 A02
1xxxx 99 3 A03
1xxxx 99 4 A04
1xxxx 99 5 A05
Try this Answer,
CREATE TABLE #Numbers(N INT)
INSERT INTO #NUMBERS(N)
SELECT TOP 495 ROW_NUMBER() OVER(ORDER BY T1.NUMBER) AS N
FROM MASTER..spt_values T1
CROSS JOIN MASTER..spt_values t2
SELECT N,CASE WHEN LEN(CAST((N+4)/5 as VARCHAR))<2 THEN '0'+CAST((N+4)/5 as VARCHAR) ELSE CAST((N+4)/5 as VARCHAR) END[Loop]
,CASE WHEN N%10 in(1,6) THEN 1 WHEN N%10 in(2,7) THEN 2 WHEN N%10 in(3,8) THEN 3 WHEN N%10 in(4,9) THEN 4 WHEN N%10 in(5,0) THEN 5 END q_id
from #Numbers
UPDATE A SET A.Loop=B.Loop, AND A.q_id=B.q_id
FROM qcard A,#Numbers B
WHERE (A.qcard%10000)=B.N
UPDATE B
SET B.groupcode=A.groupcode
FROM q_id A,qcard B
WHERE A.q_id=B.q_id
DROP TABLE #Numbers

3 Tables, JOIN query and alphabetic order

I am currently working with three tables where I am trying to figure out how to use a join to display once the title_id of any book with Dennis McCann as an editor. The tables have in common title_id and editor_id. Cant find a way to piece it all together. How to display once the title_id of any book with Dennis McCann as an editor?
SELECT * FROM title_editors;
EDITOR_ID TITLE_ EDITOR_ORDER
----------- ------ ------------
826-11-9034 Bu2075 2
826-11-9034 PS2091 2
826-11-9034 Ps2106 2
826-11-9034 PS3333 2
826-11-9034 PS7777 2
826-11-9034 pS1372 2
885-23-9140 MC2222 2
885-23-9140 MC3021 2
885-23-9140 Tc3281 2
885-23-9140 TC4203 2
885-23-9140 TC7777 2
321-55-8906 bU1032 2
321-55-8906 BU1111 2
321-55-8906 BU7832 2
321-55-8906 PC1035 2
321-55-8906 PC8888 2
321-55-8906 BU2075 3
777-02-9831 pc1035 3
777-02-9831 PC8888 3
943-88-7920 BU1032 1
943-88-7920 bu1111 1
943-88-7920 BU2075 1
943-88-7920 BU7832 1
943-88-7920 PC1035 1
943-88-7920 pc8888 1
993-86-0420 PS1372 1
993-86-0420 PS2091 1
993-86-0420 PS2106 1
993-86-0420 PS3333 1
993-86-0420 pS7777 1
993-86-0420 MC2222 1
993-86-0420 MC3021 1
993-86-0420 Tc3218 1
993-86-0420 TC4203 1
993-86-0420 TC7777 1
35 rows selected.
SQL> SELECT * FROM title_authors;
AUTHOR_ID TITLE_ AUTHOR_ORDER ROYALTY_SHARE
----------- ------ ------------ -------------
409-56-7008 Bu1032 1 .6
486-29-1786 PS7777 1 1
486-29-1786 pC9999 1 1
712-45-1867 MC2222 1 1
172-32-1176 Ps3333 1 1
213-46-8915 BU1032 2 .4
238-95-7766 PC1035 1 1
213-46-8915 Bu2075 1 1
998-72-3567 pS2091 1 .5
899-46-2035 PS2091 2 .5
998-72-3567 PS2106 1 1
722-51-5454 mc3021 1 .75
899-46-2035 MC3021 2 .25
807-91-6654 tC3218 1 1
274-80-9391 BU7832 1 1
427-17-2319 pC8888 1 .5
846-92-7186 PC8888 2 .5
756-30-7391 PS1372 1 .75
724-80-9391 PS1372 2 .25
724-80-9391 bu1111 1 .6
267-41-2394 bU1111 2 .4
672-71-3249 TC7777 1 .4
267-41-2394 TC7777 2 .3
472-27-2349 Tc7777 3 .3
648-92-1872 TC4203 1 1
25 rows selected.
SQL> SELECT * FROM editors;
EDITOR_ID EDITOR_LNAME EDITOR_FNAME EDITOR_POSITION PHONE ADDRESS CITY ST ZIP
----------- ----------------- ------------- --------------- ------------ -------------------- ------------ -- ------
321-55-8906 DeLongue Martinella Project 415 843-2222 3000 6th St. BERKELEY Ca 94710
723-48-9010 Sparks MANfred cOPY 303 721-3388 15 Sail DENVER Co 80237
777-02-9831 Samuelson Bernard proJect 415 843-6990 27 Yosemite OAKLAND Ca 94609
777-66-9902 Almond Alfred copy 312 699-4177 1010 E. DeVON CHICAGO Il 60018
826-11-9034 Himmel Eleanore pRoject 617 423-0552 97 Bleaker BOSTON Ma 02210
885-23-9140 Rutherford-Hayes Hannah PROJECT 301 468-3909 32 Rockbill Pike ROCKBILL MD 20852
993-86-0420 McCann Dennis acQuisition 301 468-3909 32 Rockbill Pike ROCKBill MD 20852
943-88-7920 Kaspchek Christof acquisitiOn 415 549-3909 18 Severe Rd. BERKELEY CA 94710
234-88-9720 Hunter Amanda acquisition 617 432-5586 18 Dowdy Ln. BOSTON MA 02210
You can try join on the table Editors and Ttile_Editors using the Editor_ID that will give you the matching records and you can filter out Only for the ' Dennis McCann ' using either multiple conditions in join or the where clause as,
WITHOUT WHERE
SELECT DISTINCT te.title_id,ed.EDITOR_ID,ed.EDITOR_LNAME,ed.EDITOR_FNAME
FROM
title_editors te JOIN editors ed
ON te.EDITOR_ID = ed.EDITOR_ID
AND ed.EDITOR_LNAME = 'McCann'
AND ed.EDITOR_FNAME = 'Dennis'
ORDER BY te.title_id
USing WHERE
SELECT DISTINCT te.title_id,ed.EDITOR_ID,ed.EDITOR_LNAME,ed.EDITOR_FNAME
FROM
title_editors te JOIN editors ed
ON te.EDITOR_ID = ed.EDITOR_ID
WHERE
ed.EDITOR_LNAME = 'McCann'
AND ed.EDITOR_FNAME = 'Dennis'
ORDER BY te.title_id
It would be easier with the in operator:
SELECT DISTINCT title_id
FROM title_editors
WHERE editor_id IN (SELECT editor_id
FROM editors
WHERE editor_fname = 'Dennis' AND
editor_lname = 'McCann')
ORDER BY title_id ASC