pivot multi column and mutil row - sql

this is my origin data:
ID New LikeNew Old Warehouse
1 10 100 20 LA
1 12 130 40 CA
2 90 200 10 LA
2 103 230 30 CA
i want to get the following format:
ID LA_new LA_likeNew LA_Old CA_New CA_LikeNew CA_Old
1 10 100 20 12 130 40
2 90 200 10 103 230 30
I can only pivot on each column but can not do on all 3 columns(New, LikeNew, Old) , so how can I do that?

You can accomplish this by using conditional logic to create your fields and grouping:
select id,
max(case when warehouse = 'LA' then new else null end) LA_New,
max(case when warehouse = 'LA' then likenew else null end) LA_likeNew,
max(case when warehouse = 'LA' then old else null end) LA_Old,
max(case when warehouse = 'CA' then new else null end) CA_New,
max(case when warehouse = 'CA' then likenew else null end) CA_likeNew,
max(case when warehouse = 'CA' then old else null end) CA_Old
from yourtable
group by id

Alternatively, you can use WITH Common Table Expression
DECLARE #T AS TABLE
(
id INT ,
New INT ,
likeNew INT ,
old INT ,
Warehouse VARCHAR(50)
)
INSERT INTO #T
( id, New, likeNew, old, Warehouse )
VALUES ( 1, 10, 100, 20, 'LA' ),
( 1, 12, 130, 40, 'CA' ),
( 2, 90, 200, 10, 'LA' ),
( 2, 103, 230, 30, 'CA' );
WITH cte
AS ( SELECT *
FROM #T
WHERE Warehouse = 'LA'
),
cte1
AS ( SELECT *
FROM #T
WHERE Warehouse = 'CA'
)
SELECT a.id ,
a.new [LA_New] ,
a.likeNew [LA_LikeNew] ,
a.Old [LA_Old] ,
b.new [CA_New] ,
b.likeNew [CA_LikeNew] ,
b.Old [CA_Old]
FROM cte A
JOIN cte1 B ON a.id = b.id
Result:
id LA_New LA_LikeNew LA_Old CA_New CA_LikeNew CA_Old
----------- ----------- ----------- ----------- ----------- ----------- -----------
1 10 100 20 12 130 40
2 90 200 10 103 230 30

Related

How can I retrieve this schema by SQL query?

This is my table and its data :
-------------------------------------
rid mid qty price tname
-------------------------------------
10 A 1000 400 Buy
11 A 2000 420 Buy
12 B 1700 600 Buy
13 A 600 450 Sell
And I want to have such output :
----------------------------------------------------------------
mid SUM_Buy tname SUM_Sell tname SUM_Buy_minus_SUM_Sell
----------------------------------------------------------------
A 3000 Buy 600 Sell 2400
B 1700 Buy NULL NULL NULL
Thank you for updating your question with consumable data. An even better approach is posting ddl and sample data so people can just grab it and roll. I did that for you.
declare #something table
(
rid int
, mid char(1)
, qty int
, price int
, tname varchar(10)
)
insert #something values
(10, 'A', 1000, 400, 'Buy')
, (11, 'A', 2000, 420, 'Buy')
, (12, 'B', 1700, 600, 'Buy')
, (13, 'A', 600 , 450, 'Sell')
In that type of format it is super easy for others to help.
You can solve this using conditional aggregation. I used tname1 and tname2 because outside of SSMS you don't want multiple columns to have the same name. But those are probably just noise and not really needed as they provide no benefit to the results.
select s.mid
, Sum_Buy = sum(case when tname = 'Buy' then qty end)
, tname1 = 'Buy'
, Sum_Sell = sum(case when tname = 'Sell' then qty end)
, tname2 = 'Sell'
, SUM_Buy_minus_SUM_Sell = sum(case when tname = 'Buy' then qty end) - sum(case when tname = 'Sell' then qty end)
from #something s
group by s.mid
order by s.mid
You might try this ( use grouping by mid column with aggregations by contribution of case..when statements ) :
with t(rid,mid,qty,price,tname) as
(
select 10,'A',1000,400,'Buy' union all
select 11,'A',2000,420,'Buy' union all
select 12,'B',1700,600,'Buy' union all
select 13,'A',600,450,'Sell'
)
select t.mid, sum(case when tname='Buy' then qty else 0 end) as SUM_Buy,
min(case when tname='Buy' then tname else null end) as tname,
sum(case when tname='Sell' then qty else null end) as SUM_Sell,
max(case when tname='Sell' then tname else null end) as tname,
(sum(case when tname='Buy' then qty else 0 end) -
sum(case when tname='Sell' then qty else null end)) as
SUM_Buy_minus_SUM_Sell
from t
group by t.mid

how to separate and sum 2 columns based on condition

I'm doing a select statement and I have a column I would like to separate into 2 columns based on their type, and then get the sum of the amounts grouped by an ID
I want all the gold and platinum types in one column, and all the silver and bronze in a 2nd column, then summed and grouped by the ID so it looks like this :
I tried doing a union like this:
SELECT
ID,
SUM(Amount) AS "Gold/Platinum",
0 AS "Bronze/Silver"
FROM
table
WHERE
Type IN ('gold', 'platinum')
GROUP BY
ID
UNION ALL
SELECT
ID,
SUM(Amount) AS "Bronze/Silver",
0 AS "Gold/Platinum"
FROM
table
WHERE
Type IN ('bronze', 'silver')
GROUP BY
ID
The gold/platinum column will be correct, but I get nothing in the bronze/silver column
Use conditional aggregation:
select id,
sum(case when Type in ('gold', 'platinum') then amount else 0 end) as gold_platinum,
sum(case when Type in ('bronze', 'silver') then amount else 0 end) as bronze_silver
from t
group by id
order by id;
You can run this in SSMS:
DECLARE #data TABLE( [ID] INT, [Type] VARCHAR(10), [Amount] INT );
INSERT INTO #data ( [ID], [Type], [Amount] ) VALUES
( 1, 'gold', 100 )
, ( 1, 'gold', 50 )
, ( 1, 'bronze', 75 )
, ( 2, 'silver', 10 )
, ( 2, 'bronze', 20 )
, ( 3, 'gold', 35 )
, ( 4, 'silver', 20 )
, ( 4, 'platinum', 30 );
SELECT
[ID]
, SUM( CASE WHEN [Type] IN ( 'gold', 'platinum' ) THEN Amount ELSE 0 END ) AS [Gold/Platinum]
, SUM( CASE WHEN [Type] IN ( 'bronze', 'silver' ) THEN Amount ELSE 0 END ) AS [Bronze/Silver]
FROM #data
GROUP BY [ID]
ORDER BY [ID];
Returns
+----+---------------+---------------+
| ID | Gold/Platinum | Bronze/Silver |
+----+---------------+---------------+
| 1 | 150 | 75 |
| 2 | 0 | 30 |
| 3 | 35 | 0 |
| 4 | 30 | 20 |
+----+---------------+---------------+

SQL How do I transpose and group data into static columns? [duplicate]

This question already has answers here:
TSQL Pivot without aggregate function
(9 answers)
Closed 4 years ago.
I have a table with the following data:
UID LAST FIRST FUND AMOUNT STATUS
1 Smith John C 100 1
1 Smith John B 250 1
1 Smith John E 150 1
2 Jones Meg B 275 1
2 Jones Meg F 150 1
3 Carter Bill A 100 1
I would like to transpose the FUND, AMOUNT and STATUS values for each UID into a single row for each UID. The resulting table would have columns added for FUND_1, AMT_1, STATUS_1, FUND_2, AMT_2, STATUS_2, FUND_3, AMT_3, STATUS_3. Each UID may or may not have a total of 3 funds. If they do not, the remaining fund, amt, and status columns are left blank. The resulting table would appear as:
UID LAST FIRST FUND_1 AMT_1 STATUS_1 FUND_2 AMT_2 STATUS_2 FUND_3 AMT_3 STATUS_3
1 Smith John C 100 1 B 250 1 E 150 1
2 Jones Meg B 275 1 F 150 1
3 Carter Bill A 100 1
For clarification, this is how the data would move from the existing table to the resulting table for UID 1:
It seems I am unable to use PIVOT because FUND_1, FUND_2, FUND_3 will be different fund categories for each person. The question, TSQL Pivot without aggregate function helps but doesn't answer my question since I have multiple rows in what would be the the DBColumnName in that question.
This is a pretty common conditional aggregation. Notice how I posted consumable data as a table and insert statements. To be honest it took longer to do that portion than the actual code to select the data. You should do this in the future. Also you should avoid using keywords as column names.
declare #Something table
(
UID int
, LAST varchar(10)
, FIRST varchar(10)
, FUND char(1)
, AMOUNT int
, STATUS int
)
insert #Something values
(1, 'Smith', 'John', 'C', 100, 1)
, (1, 'Smith', 'John', 'B', 250, 1)
, (1, 'Smith', 'John', 'E', 150, 1)
, (2, 'Jones', 'Meg', 'B', 275, 1)
, (2, 'Jones', 'Meg', 'F', 150, 1)
, (3, 'Carter', 'Bill', 'A', 100, 1)
;
with SortedValues as
(
select *
, RowNum = ROW_NUMBER() over(partition by UID order by (select null))
from #Something
)
select UID
, Last
, First
, Fund_1 = max(case when RowNum = 1 then Fund end)
, Amt_1 = max(case when RowNum = 1 then Amount end)
, Status_1 = max(case when RowNum = 1 then Status end)
, Fund_2 = max(case when RowNum = 2 then Fund end)
, Amt_2 = max(case when RowNum = 2 then Amount end)
, Status_2 = max(case when RowNum = 2 then Status end)
, Fund_3 = max(case when RowNum = 3 then Fund end)
, Amt_3 = max(case when RowNum = 3 then Amount end)
, Status_3 = max(case when RowNum = 3 then Status end)
from SortedValues
group by UID
, Last
, First
order by UID
, Last
, First

Recursive CTE and Count Info from recursive table

I have this table called tblLandAreas:
Level 1 = Bloq
Level 2 = Ships
Level 3 = Sides
Level 4 = Beds
Ship is a child of Bloq, Sides is a child of Ship and Bed is a child of Side.
And I need to show a report this way:
Only when _parentid is null
I have tried this but it is not working:
Try the following:
DECLARE #t TABLE
(
ID INT ,
ParentID INT ,
Level INT
)
INSERT INTO #t
VALUES ( 1, NULL, 1 ),
( 29, NULL, 1 ),
( 38, 29, 2 ),
( 32, 1, 2 ),
( 18, 1, 2 ),
( 41, 29, 2 ),
( 42, 41, 3 ),
( 43, 41, 3 ),
( 44, 41, 3 ),
( 45, 44, 4 )
;WITH cte AS(SELECT *, id AS baseid FROM #t WHERE ParentID IS NULL
UNION ALL
SELECT t.*, c.baseid FROM cte c JOIN #t t ON c.ID = t.ParentID)
SELECT baseid,
SUM(CASE WHEN Level = 2 THEN 1 ELSE 0 END) ships,
SUM(CASE WHEN Level = 3 THEN 1 ELSE 0 END) sides,
SUM(CASE WHEN Level = 4 THEN 1 ELSE 0 END) beds
FROM cte
WHERE ParentID IS NOT NULL
GROUP BY baseid
Output:
baseid ships sides beds
1 2 0 0
29 2 3 1
To project on your structure:
;WITH Bloque AS(SELECT LandAreaId, _ParentId, _Level, LandAreaId AS baseLandAreaId
FROM tblLandAreas WHERE _ParentId IS NULL
UNION ALL
SELECT a.LandAreaId, a._ParentId, a._Level, B.baseLandAreaId
FROM Bloque B
JOIN tblLandAreas a ON B.LandAreaId = a._ParentId)
SELECT baseLandAreaId AS LandAreaId,
SUM(CASE WHEN _Level = 2 THEN 1 ELSE 0 END) [#Ships],
SUM(CASE WHEN _Level = 3 THEN 1 ELSE 0 END) [#Sides],
SUM(CASE WHEN _Level = 4 THEN 1 ELSE 0 END) [#Beds]
FROM Bloque
WHERE _ParentId IS NOT NULL
GROUP BY baseLandAreaId

Filter the same payments by different persons - SQL

can you please help me with the following problem :
i have a table Payments :
Id ROLE Payment
1 A 100
1 A 100
1 R 50
1 R 50
1 R 50
2 A 100
2 A 100
2 R 50
2 R 50
2 R 100
Now the ID is one cash loan ( so we have two of them). Role A/R means there are two ppls paying that loan (A-one guy, R- second guy).
Now i need to SUM all the payments for specific ID which is still ok, BUT when the payment for role A is same as for role R i want to count for SUM just one of them..
I was trying to solve it with inner join on the same table where id and payment is the same and role is different but still have the problem that when i have this:
2 A 100
2 A 100
2 R 100
the result is not 100 but 200 ( i will count (A100,R100) = 100 , A100 = 100)
Thank you for any help
UPDATE:
#Giorgi Nakeuri - for given example, the result should looke like :
ID Payment
1 350
2 300
UPDATE : for better understanding : In the given example(for id 2)i have 100payment for R and A so we count it just once = 100, then i have one 100payment just for A = 100, and two payments 50 just for R = 100, 100+100+100 = 300 payment for id 2
Try:
DECLARE #t TABLE ( ID INT, R CHAR(1), P INT )
INSERT INTO #t
VALUES ( 1, 'A', 100 ),
( 1, 'A', 100 ),
( 1, 'R', 50 ),
( 1, 'R', 50 ),
( 1, 'R', 50 ),
( 2, 'A', 100 ),
( 2, 'A', 100 ),
( 2, 'R', 50 ),
( 2, 'R', 50 ),
( 2, 'R', 100 );
WITH cte
AS ( SELECT id ,
P ,
SUM(CASE WHEN R = 'A' THEN 1 ELSE 0 END) cd1 ,
SUM(CASE WHEN R = 'R' THEN 1 ELSE 0 END) cd2
FROM #t
GROUP BY id ,
P
)
SELECT ID ,
SUM(P * CASE WHEN cd1 > cd2 THEN cd1 ELSE cd2 END) AS Sum
FROM cte
GROUP BY ID
Output:
ID Sum
1 350
2 300
Have you try to distinct the query?
SELECT distinct yourField from yourTable