TSQL Sum with type [duplicate] - sql

This question already has answers here:
SQL Server dynamic PIVOT query?
(9 answers)
Closed 5 years ago.
I'm writing using translate
Table1
id number type
1 6 111
1 5 111
1 6 113
2 3 112
2 6 111
i need to sum group by value of "type" and "id"
the result I want to see
RESULT Table
id type111 type112 typ113
1 11 0 6
2 6 3 0

Another way to use pivoting:
SELECT id,
ISNULL([111],0) as Type111,
ISNULL([112],0) as Type112,
ISNULL([113],0) as Type113
FROM (
SELECT id,
number,
[type]
FROM Table1
) AS SourceTable
PIVOT (SUM(number) FOR [type] IN ([111], [112], [113])
) AS PivotTable;
Output:
id Type111 Type112 Type113
----------- ----------- ----------- -----------
1 11 0 6
2 6 3 0
(2 rows affected)
In case when the variety of types is high - use dynamic SQL to build and execute query.

Try using SUM with CASE:
SELECT ID,
SUM(CASE WHEN type = 111 THEN number END) AS type111,
SUM(CASE WHEN type = 112 THEN number END) AS type112,
SUM(CASE WHEN type = 113 THEN number END) AS typE113,
FROM TABLE1
GROUP BY ID

Related

Add flag on SQL Server table, based on pair of features [duplicate]

This question already has answers here:
Get top 1 row of each group
(19 answers)
Closed 8 months ago.
I have a table in SQL Server with Id, Code and Prod. I want to add a flag and end up like:
Id Code Prod Flag
0 101 A 0
1 101 B 0
2 101 A 1
3 101 A 1
4 101 B 1
5 160 B 0
6 160 A 0
7 160 B 1
8 190 A 0
9 190 B 0
For each pair Code-Prod (eg. 101-A, which has 3 cases), the flag should assume 0 for the first Id, 1 otherwise.
How do I do that?
This is ideally solved with row_number() window function, combined with an updatable CTE:
with upd as (
select *, Row_Number() over(partition by Concat(code,prod) order by id) rn
from t
)
update upd
set flag = case when rn = 1 then 0 else 1 end;

How to sum values for second id basing on first id [duplicate]

This question already has answers here:
MySQL Group By and Sum total value of other column
(5 answers)
Closed 6 years ago.
Let's assume I have table1:
id id_2 value1
1 999 1
2 999 0
3 998 0
4 998 0
5 997 1
6 997 1
And I want to produce output like this - sum values for id_2 basing on count for id
id_2 number_of
999 1
998 0
997 2
What approach would be suggested?
(DB: Oracle 12c)
You can use the group by clause in order to collate the values for a particular Id as shown below
select id_2,
sum(value1) number_of
from table1
group by id_2
order by id_2 desc;

SQL table data horizontally using PIVOT

My SQL table is
GUID Step_ID Value
----------------------------------------------
ADFE12-ASDER-... 1 10
ADFE12-ASDER-... 2 20
ADFE12-ASDER-... 3 30
ADFE12-ASDER-... 4 160
CD4563-FG567-... 1 20
CD4563-FG567-... 2 80
Q23RT5-GH678... 1 30
Q23RT5-GH678-... 2 80
Q23RT5-GH678-... 3 20
And Expected result should be
GUID 1 2 3 4
---------------------------------------------------
ADFE12-ASDER-... 10 20 30 160
CD4563-FG567-... 20 80 NULL NULL
Q23RT5-GH678-... 30 80 20 NULL
Here I need to get the details on the basis of column whose data type is GUID. I tried using PIVOT table but getting an exception because I cannot use an aggregate function on GUID column. Is there any other alternative or approach I can use to get the above desired result.
Try this:
select [GUID],[1],[2],[3],[4]
from
(
select [GUID], Step_ID, Value
from test
) d
pivot
(
max(Value)
for Step_ID in ([1],[2],[3],[4])
) piv;
SQL FIDDLE DEMO
You could try manually pivotting, I don't have a SQL Server handy to check the behavior on a GUID but this has worked well for me in the past.
select guid,
max(case when step_ID = 1 then value else null end) step_1,
max(case when step_ID = 2 then value else null end) step_2,
max(case when step_ID = 3 then value else null end) step_3,
max(case when step_ID = 4 then value else null end) step_4
from your_table
group by guid;
HTH

sql query required grouping column in single row [duplicate]

This question already has answers here:
need to return two sets of data with two different where clauses
(2 answers)
Closed 8 years ago.
I have following table structure
TicketID Status Duration
-----------------------------
1234 8 2
1233 8 10
1232 4 5
1231 8 12
1230 4 50
status 8 means Closed
status 4 means Open
It is required to have output in following way. Please do the need ful. If possible I wanted it in a single sql query.
Please help me to produce output in following way.
Row Closed (sum) Open(Sum)
---------------------------------
1 24 55
select 1 as row,
sum(case when status = 8 then 1 else 0 end) as closed,
sum(case when status = 4 then 1 else 0 end) as open
from your_table
select
1 as Row,
sum(case when Status=8 then 1 else 0 end ) as Closed_Sum,
sum(case when Status=4 then 1 else 0 end ) as Open_Sum
from
Mytable

SQL Converting Column into Rows in Single Select Statement

I need solution for converting SQL output
I am writing
SELECT Merchant_Master.Merchant_ID,
COUNT(Coupon_Type_ID) AS "Total Coupons",
Coupon_Type_ID,
CASE WHEN Coupon_Type_ID=1
THEN COUNT(Coupon_Type_ID)
END AS "Secret",
CASE WHEN Coupon_Type_ID=2
THEN count(Coupon_Type_ID)
END AS "Hot"
FROM Coupon_Master
INNER JOIN Merchant_Master
ON Coupon_Master.Merchant_ID=Merchant_Master.Merchant_ID
GROUP BY
Coupon_Master.Coupon_Type_ID,
Merchant_Master.Merchant_ID
and getting output as
Merchant_ID Total Coupons Coupon_Type_ID Secret Hot
----------- ------------- -------------- ----------- -----------
20 6 1 6 NULL
22 4 1 4 NULL
22 2 2 NULL 2
23 1 2 NULL 1
24 2 1 2 NULL
25 3 1 3 NULL
25 2 2 NULL 2
But I want output as
Merchant_ID Secret Hot_Coupons
----------- ------ -------------
20 6 0
22 4 2
23 0 1
24 2 0
25 3 2
Please, help me to solve the issue.
Move the CASE expressions inside the aggregates. I've also switched to using SUM rather than COUNT - there is a COUNT variant but it may display a warning about eliminating NULL values that I'd rather avoid.
SELECT Merchant_Master.Merchant_ID,
SUM(CASE WHEN Coupon_Type_ID=1
THEN 1 ELSE 0 END) AS "Secret",
SUM(CASE WHEN Coupon_Type_ID=2
THEN 1 ELSE 0 END) AS "Hot"
FROM Coupon_Master
INNER JOIN Merchant_Master
ON Coupon_Master.Merchant_ID=Merchant_Master.Merchant_ID
GROUP BY
Merchant_Master.Merchant_ID
Place it in a subquery and add group by Merchant_ID, Total, Coupons, Coupon_Type_ID
Aggregate the Secret and hot as SUM
select
...
SUM(secret) as secret,
SUM(Hot_Coupons) as Hot_Coupons
FROM (your original query) raw
group by Merchant_ID, Total, Coupons, Coupon_Type_ID