how to get the summation of column in the last row - sql

select csm.csmCustomerName, cur.curNameOfCurrency,
sum(sot.sotItemTotalAmount)as 'TotalItemsAmount',
SUM(sorTotalTaxAmountValue) as 'TotalTax',
SUM(sorTotalChargeDetailsAmountValue) as 'TotalCharges',
(sum(sorTotalTaxAmountValue)+sum(sorTotalChargeDetailsAmountValue)+sum(sot.sotItemTotalAmount)) as 'NetAmount'
from dbo.SalesOrder sor join dbo.Currency cur
on sor.sorCurrencyId=cur.curId
join dbo.CustomerMaster csm
on sor.sorCustomerMasterId=csm.csmId
join SalesOrderItemDetails sot
on sot.sotSalesOrderId=sor.sorId
Group by csmCustomerName, curNameOfCurrency with rollup;
I want the sum of TotalItemsAmount, TotalTax, TotalCharges and NetAmount in the last row of respective columns.
In result set, I get duplication of each row, could anyone correct the mistake in my code.
C1 C2 C3 C4 C5 C6
A USD 1 7 2 10
B USD 3 6 3 12
C USD 5 3 0 8
D USD 4 2 1 7
13 18 6 37

It is done via the grouping sets like:
DECLARE #t TABLE ( code CHAR(3), a INT, b INT )
INSERT INTO #t
VALUES ( 'USD', 1, 2 ),
( 'USD', 5, 1 ),
( 'USD', 10, 7 ),
( 'EUR', 15, 13 )
SELECT code ,
SUM(a) AS a ,
SUM(b) AS b
FROM #t
GROUP BY GROUPING SETS(( code ), ( ))
Output:
code a b
EUR 15 13
USD 16 10
NULL 31 23

Related

JOIN two tables in PostgreSQL and GROUP BY

I have the following tables in PostgreSQL :
table1:
MARKET character varying 10
COST1 Number
MARKET COST1
A 3
A 7
B 10
table2:
MARKET character varying" 10
COST2 Number
MARKET COST2
A 12
A 13
B 15
B 15
I am trying to generate a report that will display both cost1 and cost1 of each market. Something like this.
MARKET COST1 COST2
A 10 25
B 10 30
I executed the following query :
select table1."MARKET", sum(table1."COST1"), sum(table2."COST2")
from table1 inner join table2 on table1."MARKET" = table2."MARKET" GROUP
BY(table1."MARKET")
My OP is the following. The cost values are more than what it should be:
MARKET COST1 COST2
A 20 50
B 20 50
What am I doing wrong? Any help is appreciated!
Alternatively (you need code that begins at line 13; CTE is here so that I could use some data; disregard Oracle-stylish example).
SQL> with table1 (market, cost1) as
2 (select 'a', 3 from dual union all
3 select 'a', 7 from dual union all
4 select 'b', 10 from dual
5 ),
6 table2 (market, cost2) as
7 (select 'a', 12 from dual union all
8 select 'a', 13 from dual union all
9 select 'b', 15 from dual union all
10 select 'b', 15 from dual
11 )
12 -- you need what follows:
13 select x.market, sum(x.cost1) as cost1, sum(y.cost2) as cost2
14 from (select a.market, sum(a.cost1) cost1
15 from table1 a
16 group by a.market) x
17 join
18 (select b.market, sum(b.cost2) cost2
19 from table2 b
20 group by b.market) y
21 on x.market = y.market
22 group by x.market;
M COST1 COST2
- ---------- ----------
a 10 25
b 10 30
SQL>
Do the aggregation before the join. Or, use union all and group by:
select market, sum(cost1) as cost1, sum(cost2) as cost2
from ((select market, cost1, 0 as cost2
from table1
) union all
(select market, 0, cost2
from table2
)
) mc
group by market;

To create a column Summing the values from another column in the same view

View:
A | B
10 1
15 2
12 3
5 2
2 1
2 1
Output View:
A | B | C
10 1 14
15 2 20
12 3 12
5 2 20
2 1 14
2 1 14
I need to sum the values from column A based on column B. So, all the values from column B having value 1 extract values from column A and then sum it to column C.
I don't see the point, but:
SELECT t.a
,t.b
,sumtab.c
FROM [yourtable] t
INNER JOIN (
SELECT t.b
,sum(t.a) AS C
FROM [yourtable] t
GROUP BY t.b
) AS sumtab
ON t.b = sumtab.b
You could use SUM() OVER like this
DECLARE #SampleData AS TABLE
(
A int,
B int
)
INSERT INTO #SampleData
(
A,
B
)
VALUES
( 10, 1),
( 15, 2),
( 12, 3),
( 5 , 2),
( 2 , 1),
( 2 , 1)
SELECT *,
sum(sd.A) OVER(PARTITION BY sd.B) AS C
FROM #SampleData sd
Returns
A B C
-----------
10 1 14
2 1 14
2 1 14
15 2 20
5 2 20
12 3 12

SQL Matrix to array

I am working with T-SQL and I have a table that looks like Matrix (8x8).
My objective is to make that Matrix (table) into array using Pivot ... I have read forums and more stuff that i managed to find but i still can't make a code for it ...
ID Bucket B1 B2 B3 B4
5 1 20 21 45 12
6 2 12 18 19 48
7 3 19 78 40 78
8 4 72 34 12 17
So all I need to do is to make "three dimensional array" from that table, and to save row, column and value ... to be something like this
Row Column Value
1 1 20
1 2 21
1 3 45
1 2 12
etc
etc
etc
4 3 12
4 4 17
Does anyone have any idea how I could write that code in T-SQL?
ps. Reason i'm doing this, is because i want to multiply my matrix with itself. So it's easier to multiply it if i have it in pivot table.
Thank you
Try unpivoting your data :
DECLARE #table TABLE (id INT, Bucket INT, B1 INT, B2 INT, B3 INT, B4 INT)
INSERT INTO #table VALUES
(5,1,20,21,45,12),
(6,2,12,18,19,48),
(7,3,19,78,40,78),
(8,4,72,34,12,17)
SELECT rn AS [ROW],
VALUE AS [ColumnNumber],
orders AS [VALUE]
FROM
(
SELECT ROW_NUMBER () OVER (ORDER BY id) AS rn,id, Bucket, B1 [1], B2 [2], B3 [3], B4 [4]
FROM #table
) AS t
UNPIVOT
(
orders
FOR VALUE IN([1], [2],[3],[4])
) AS pvt
Check this MSDN Doc for more details of PIVOT and UNPIVOT.
Why use PIVOT? You can get the data out with a simple SELECT query, unless I am missing something here?
DECLARE #Matrix TABLE (
Id INT,
Bucket INT,
B1 INT,
B2 INT,
B3 INT,
B4 INT);
INSERT INTO #Matrix VALUES (5, 1, 20, 21, 45, 12);
INSERT INTO #Matrix VALUES (6, 2, 12, 18, 19, 48);
INSERT INTO #Matrix VALUES (7, 3, 19, 78, 40, 78);
INSERT INTO #Matrix VALUES (8, 4, 72, 34, 12, 17);
SELECT
Bucket AS [Row],
1 AS [Column],
B1 AS Value
FROM
#Matrix
UNION ALL
SELECT
Bucket AS [Row],
2 AS [Column],
B2 AS Value
FROM
#Matrix
UNION ALL
SELECT
Bucket AS [Row],
3 AS [Column],
B3 AS Value
FROM
#Matrix
UNION ALL
SELECT
Bucket AS [Row],
4 AS [Column],
B4 AS Value
FROM
#Matrix
ORDER BY
1, 2;
Results:
Row Column Value
1 1 20
1 2 21
1 3 45
1 4 12
2 1 12
2 2 18
2 3 19
2 4 48
3 1 19
3 2 78
3 3 40
3 4 78
4 1 72
4 2 34
4 3 12
4 4 17

T-SQL results in to columns

I have a table (t1) like below
Id Name RelId
1 a 2
2 b 3
3 c 4
4 d 3
5 e 6
The other table (t2)
Id data FK Order
1 aa 2 2
2 bb 2 3
3 cc 2 1
4 dd 2 4
5 ee 2 5
6 ff 3 3
7 gg 3 2
8 hh 3 1
9 ii 4 7
10 jj 4 4
11 kk 4 1
12 ll 4 3
13 mm 6 1
14 nn 6 2
15 oo 6 3
16 pp 6 4
My output result am looking for is
+----+------+-------+-------+------+----------+
| id | name | RelId | Col 1 | Col2 | Col-Oth |
+----+------+-------+-------+------+----------+
| 1 | a | 2 | cc | aa | bb,dd,ee |
| 2 | b | 3 | hh | gg | ff |
| 3 | c | 4 | kk | ll | jj,ii |
| 4 | d | 3 | hh | gg | ff |
| 5 | e | 6 | mm | nn | oo,pp |
+----+------+-------+-------+------+----------+
based on the Relid in T1 table join with FK column in T2 and populate col1 with the least order data, col2 with the next higher order data and col-oth with remaining data comma separated ordered.
Need your help on same.
SELECT id,name,RelId, (select data,rownumber() (partition by data order by order asc) from t2 inner join t1 on t1.relid= t2.FK) from t1
Try following query:
DECLARE #TEMP TABLE
(
Id INT,
Name VARCHAR(10),
RelId INT
)
INSERT INTO #TEMP VALUES (1,'a',2),(2,'b',3),(3,'c',4),(4,'d',3),(5,'e',6)
DECLARE #TEMP1 TABLE
(
Id INT,
Data varchar(10),
FK INT,
[order] INT
)
INSERT INTO #TEMP1 VALUES
(1 ,'aa',2,2),(2 ,'bb',2,3),(3 ,'cc',2,1),(4 ,'dd',2,4),(5 ,'ee',2,5),
(6 ,'ff',3,3),(7 ,'gg',3,2),(8 ,'hh',3,1),(9 ,'ii',4,7),(10,'jj',4,4),
(11,'kk',4,1),(12,'ll',4,3),(13,'mm',6,1),(14,'nn',6,2),(15,'oo',6,3),(16,'pp',6,4)
SELECT
t1.*,
(SELECT Data FROM (SELECT ROW_NUMBER() OVER(ORDER BY t2.[order]) As RowNo,Data FROM #TEMP1 t2 WHERE t2.FK = t1.RelId)t3 WHERE t3.RowNo=1),
(SELECT Data FROM (SELECT ROW_NUMBER() OVER(ORDER BY t2.[order]) As RowNo,Data FROM #TEMP1 t2 WHERE t2.FK = t1.RelId)t3 WHERE t3.RowNo=2),
STUFF((SELECT DISTINCT ',' + Data FROM (SELECT ROW_NUMBER() OVER(ORDER BY t2.[order]) As RowNo,Data FROM #TEMP1 t2 WHERE t2.FK = t1.RelId)t3 WHERE t3.RowNo > 2 FOR XML PATH ('')), 1, 1, '')
FROM
#TEMP t1
Using PIVOT:
DECLARE #t1 TABLE
(
ID INT ,
Name CHAR(1) ,
RelID INT
)
DECLARE #t2 TABLE
(
ID INT ,
Data CHAR(2) ,
RelID INT ,
Ordering INT
)
INSERT INTO #t1
VALUES ( 1, 'a', 2 ),
( 2, 'b', 3 ),
( 3, 'c', 4 ),
( 4, 'd', 3 ),
( 5, 'e', 6 )
INSERT INTO #t2
VALUES ( 1, 'aa', 2, 2 ),
( 2, 'bb', 2, 3 ),
( 3, 'cc', 2, 1 ),
( 4, 'dd', 2, 4 ),
( 5, 'ee', 2, 5 ),
( 6, 'ff', 3, 3 ),
( 7, 'gg', 3, 2 ),
( 8, 'hh', 3, 1 ),
( 9, 'ii', 4, 7 ),
( 10, 'jj', 4, 4 ),
( 11, 'kk', 4, 1 ),
( 12, 'll', 4, 3 ),
( 13, 'mm', 6, 1 ),
( 14, 'nn', 6, 2 ),
( 15, 'oo', 6, 3 ),
( 16, 'pp', 6, 4 );
WITH cte1
AS ( SELECT t1.ID ,
t1.Name ,
t1.RelID ,
t2.Data ,
ROW_NUMBER() OVER ( PARTITION BY t1.ID ORDER BY t2.Ordering ) AS rn
FROM #t1 t1
JOIN #t2 t2 ON t1.RelID = t2.RelID
),
cte2
AS ( SELECT ID ,
Name ,
RelID ,
Data ,
rn ,
STUFF(( SELECT ',' + Data
FROM cte1 ci
WHERE co.ID = ci.ID
AND rn > 2
FOR
XML PATH('')
), 1, 1, '') AS Col3
FROM cte1 co
)
SELECT ID ,
Name ,
RelID ,
[1] AS Col1 ,
[2] AS Col2 ,
Col3
FROM cte2 PIVOT( MAX(data) FOR rn IN ( [1], [2] ) ) p
Output:
ID Name RelID Col1 Col2 Col3
1 a 2 cc aa bb,dd,ee
2 b 3 hh gg ff
3 c 4 kk ll jj,ii
4 d 3 hh gg ff
5 e 6 mm nn oo,pp
Execution plan of my statement
Execution plan of accepted statement:
Which is better? :)

table join where

Table1 t10 (id)
id
---
1
2
table t11(a1,a2,a3)
a1 a2 a3
----------
1 10 a
1 10 b
1 11 b
1 12 c
2 20 d
2 21 e
select * from t10 a,t11 b where a.id = b.a1
how to display
id a1 a2 a3
--------------
1 1 10 a
1 1 10 b //(not display this row)
1 1 11 b //(not display this row)
1 1 12 c //(not display this row)
2 2 20 d
2 2 21 e //(not display this row)
just get t11's random row
maybe display this
id a1 a2
----------
1 1 11 b
1 1 10 a //(not display this row)
1 1 10 b //(not display this row)
1 1 12 c //(not display this row)
2 2 20
2 2 21 //(not display this row)
select a1 as id, a1, min(a2) as a2
from t11
group by a1
will give you:
id a1 a2
----------
1 1 10
2 2 20
This seems almost like he wants something like FIRST/LAST from ms access.
This can ben done (very closely) in Sql Server using
DECLARE #Table TABLE(
id INT,
a1 INT,
a2 INT
)
INSERT INTO #Table (id,a1,a2) SELECT 1, 1, 11
INSERT INTO #Table (id,a1,a2) SELECT 1, 1, 10
INSERT INTO #Table (id,a1,a2) SELECT 1, 1, 12
INSERT INTO #Table (id,a1,a2) SELECT 2, 2, 20
INSERT INTO #Table (id,a1,a2) SELECT 2, 2, 21
SELECT *
FROM #Table t
WHERE a2 = (SELECT TOP 1 a2 FROM #Table WHERE id = t.id AND a1 = t.a1)
This is the answer:
SELECT *
FROM t10 a, (
SELECT * FROM (
SELECT b.*, ROW_NUMBER() OVER(PARTITION BY a10 ORDER BY a10) as rn
FROM t11 b
) WHERE rn =1) b
WHERE a.id = b.a10(+)