create pivot table with 2 dimensions - sql

I have 2 tables:
CREATE TABLE loans
(
loan_id int,
client_id int,
loan_date date
);
CREATE TABLE clients
(
client_id int,
client_name varchar(20),
gender varchar(20)
);
INSERT INTO CLIENTS
VALUES (1, arnold, 'male'),
(2, lilly, 'female'),
(3, betty, 'female'),
(4, tom, 'male'),
(5, jim, 'male');
INSERT INTO loans
VALUES (1, 1, '20220522'),
(2, 2, '20220522'),
(3, 3, '20220525'),
(4, 4, '20220525'),
(5, 1, '20220527'),
(6, 2, '20220527'),
(7, 3, '20220601'),
(8, 1, '20220603'),
(9, 2, '20220603'),
(10, 1, '20220603');
It is necessary to calculate by years the number of contracts for loans in the context of the serial number of the contract and sex.
Output should be:
sex
1 contract, 2022
2 contract, 2022
3 contract, 2022
male
2
2
1
female
4
1
1
1 contract, 2 contract - Its client contract serial number.
Probably need to apply crosstab here, but it does not apply to CTE.
I would like to have auto-completion of the serial number and year in the columns, because the period includes several years and a large number of contracts
with cte as
(
select
l.client_id,
loan_date,
extract(year from loan_date) as year,
client_name,
gender,
row_number() over (partition by l.client_id order by loan_date asc) as serial_number_contact
from
loans l
inner join
clientc on l.client_id = c.client_id
)
select
gender,
year,
serial_number_contact,
count(*) as count_loan
from
cte
group by
gender, year, serial_number_contact
order by
serial_number_contact, year

;with cte as(
select gender,
row_number() over (partition by l.client_id order by l.client_id,l.loan_date) serial
from loans l
inner join clients c
on c.client_id=l.client_id)
select gender
,sum(case when serial=1 then 1 else 0 end) as "1 Contract 2022"
,sum(case when serial=2 then 1 else 0 end) as "2 Contract 2022"
,sum(case when serial=3 then 1 else 0 end) as "3 Contract 2022"
from cte group by gender
Test
http://sqlfiddle.com/#!17/0d0b93/3

Related

How can distribute one table data to another table

I have two tables Orderapproval and stock:
Table 1: Orderapproval
itemcode
approvalqty
1
25
2
10
Table 2: stock
itemcode
stockqty
batch
date
1
5
aa
2021-02-01
1
10
bb
2021-02-10
1
15
cc
2021-02-02
2
5
dd
2021-02-01
2
20
aa
2021-02-05
we have stock batch-wise.
when we get an order request for an item
we are trying to pick up items from the old batch first & so on
How can I get a result set like this:
itemcode
qty
batch
date
1
5
aa
2021-02-01
1
15
cc
2021-02-02
1
5
bb
2021-02-10
2
5
dd
2021-02-01
I am trying as......
Declare #StockTable table
(ItemCode int,BranchCode int,Qty int)
Insert Into #StockTable values
(1, 101, 5),
(1, 102, 10),
(1, 101, 15),
(2, 102, 5),
(2, 103, 20)
Declare #Orderapproval table (ItemCode int,Qty int)
Insert Into #Orderapproval values
(1,25),
(2,10)
;with T1 as (
Select A.*
,ToDist = cast(D.Qty as int),
running_total = SUM(D.Qty) OVER (PARTITION BY A.ItemCode,BranchCode ORDER BY BranchCode DESC
ROWS BETWEEN UNBOUNDED PRECEDING
AND CURRENT ROW)
From #StockTable A
Join #Orderapproval D on A.ItemCode=D.ItemCode )
, T2 as (
Select *, prev_running_total = LAG(Qty,1,0) OVER (PARTITION BY ItemCode ORDER BY BranchCode DESC) From T1
)
select *,
CASE WHEN prev_running_total >= ToDist THEN 0
WHEN running_total > ToDist THEN ToDist - prev_running_total
ELSE qty
END 'qty'
from T2;
I didn't get the right output.
Please suggest a better way
Try this:
SELECT s.itemcode
, s.stockqty AS qty
, s.batch
, s.date
FROM stock AS s
INNER JOIN Orderapproval AS oa
ON s.stockqty <= oa.approvalqty
If required, insert resultset into new table
I try it on SQLite 3.30
Schema
CREATE TABLE stock (
id INT,
itemcode INT,
stockqty INT,
batch CHAR,
date DATE
);
INSERT INTO stock (id, itemcode, stockqty, batch, date) VALUES (1, 1, 5, 'aa', '2021-02-01');
INSERT INTO stock (id, itemcode, stockqty, batch, date) VALUES (2, 1, 10, 'bb', '2021-02-10');
INSERT INTO stock (id, itemcode, stockqty, batch, date) VALUES (3, 1, 15, 'cc', '2021-02-02');
INSERT INTO stock (id, itemcode, stockqty, batch, date) VALUES (4, 2, 5, 'dd', '2021-02-01');
INSERT INTO stock (id, itemcode, stockqty, batch, date) VALUES (5, 2, 20, 'aa', '2021-02-05');
CREATE TABLE orderapproval (
id INT,
itemcode INT,
approvalqty INT
);
INSERT INTO orderapproval (id, itemcode, approvalqty) VALUES (1, 1, 25);
INSERT INTO orderapproval (id, itemcode, approvalqty) VALUES (2, 2, 10);
Query
with a as ( SELECT *, sum(stockqty) over (partition by itemcode order by date ) as acc FROM stock)
select a.itemcode, case when acc<=approvalqty then stockqty else approvalqty-(acc-stockqty) end as qty, batch, date from a
inner join orderapproval o
on a.itemcode = o.itemcode
where qty > 0
order by a.itemcode, date
Result
itemcode
qty
batch
date
1
5
aa
2021-02-01
1
15
cc
2021-02-02
1
5
bb
2021-02-10
2
5
dd
2021-02-01
2
5
aa
2021-02-05
db-fiddle
SQL CODE---(Tested)
Select stock.itemcode
,stock.stockqty AS qty
,stock.batch,stock.date
from stock
join orderapproval on stock.stockqty <= orderapproval.approvalgty
group by stock.batch;

unexpected output with group by

Here is the DDL and DML:
create table fb_customers (
customer_id int,
customer_name varchar,
product_bought varchar);
insert into fb_customers values
(1, 'james', 'A'),(2, 'james', 'B'), (3, 'james', 'A'), (4, 'james', 'C'), (5, 'ada', 'A'), (6, 'ada', 'A'), (7, 'Tom', 'B'), (8, 'Leo', 'C');
when I select customer_name and product_bought with group by:
select customer_name, product_bought from fb_customers
group by customer_name, product_bought;
the result is surprisingly not grouped by name automatically - I thought with group by the result should be grouped by customer_name and product_bought but we can see james is separated and scattered instead of grouped together.
Now I'd like to find the customers who bought both A and B, and here is my query:
select customer_name from fb_customers
group by customer_name, product_bought
having sum(case when product_bought = 'A' then 1
when product_bought = 'B' then 1
else 0
end) = 2;
and the result is surprising include ada who should not be there:
where am I wrong with these two questions? Thanks
You can try the below - DEMO Here
select customer_name from fb_customers
where product_bought in ('A','B')
group by customer_name
having count(distinct product_bought)=2
please see the below photo
you in your query writed for every customer_name every where product_bought = 'A' then +1 and every where product_bought = 'B' then +1 that in customer_name whit name's ada
ada A +1
ada A +1
since ada have tow 'A' that result sum in query is 2
sum for ada=>1+1=2
and see james
james A +1
james B +1
james A +1
james C 0
since james have tow 'A' and one 'B' that result sum in query is 3
sum for james=>1+1+1=3
query result is ada since just sum ada is 2
and if you want use from your code you can edite as below
select customer_name from fb_customers
group by customer_name
having sum(case when product_bought = 'A' then 1
else 0
end) >= 1
and
sum(case when product_bought = 'B' then 1
else 0
end) >= 1

SQL Server: matching two tables and results in the order of proper column

I have two tables JOB and EMP; structure and values are like this:
CREATE TABLE JOB
(
JOBID SMALLINT UNIQUE NOT NULL,
JOBNAME CHAR(15)
);
CREATE TABLE EMP
(
EMPID SMALLINT,
JOBID SMALLINT,
SAL SMALLINT,
CITYID SMALLINT,
YEAR SMALLINT,
STATUS CHAR(1)
);
INSERT INTO JOB(JOBID, JOBNAME)
VALUES (1, 'DEVELOPMENT'),
(2, 'DEVELOPMENT'),
(3, 'TESTING'),
(4, 'TESTING'),
(7, 'TESTING'),
(9, 'RESEARCH'),
(8, 'HR');
INSERT INTO EMP (EMPID , JOBID, SAL, CITYID, YEAR, STATUS)
VALUES (100, 1, 1000, 10, 2015, 'A'),
(200, 2, 2000, 10, 2015, 'A'),
(300, 1, 2500, 20, 2015, 'A'),
(400, 3, 1000, 10, 2016, 'A'),
(500, 6, 3000, 10, 2015, 'E'),
(600, 8, 1000, 30, 2015, 'A'),
(700, 8, 2000, 10, 2015, 'E'),
(800, 9, 1500, 10, 2015, 'A');
I want to display all jobname count and avg salaries; for the jobname if jobid's not exists then display 0
For the given input cityid , YEAR and STATUS ( Emp table), take all jobid for each jobname (from job table ) and match in Emp table, if exists display count (count of jobid present in Emp table) and avgsal else 0 for count and avgsal. And Sal is calculated based on Status. If Status is 'A' Sal goes to Status-A-Sal else Status-E-Sal. And for each matched i.e non zero record put 'X' in another field
Output should be like this for Cityid's 10 and 20 year 2015. Results should be displayed first for Status 'A' and then Status 'E'. Added status type field in result .
Cityid Status-type jobname count STATUS sal
--------------------------------------------------------------
10 STATUSA development 2 X 1500
10 STATUSA TESTING 0 0
10 STATUSA RESEARCH 1 X 1500
10 STATUSA HR 0 0
10 total 3 0
10 STATUSE development 0 0
10 STATUSE TESTING 0 0
10 STATUSE RESEARCH 0 0
10 STATUSE HR 1 X 2000
10 total 1 2000
20 STATUSA development 1 X 2500
20 STATUSA TESTING 0 0
20 STATUSA RESEARCH 0 0
20 STATUSA HR 0 0
20 total 1 2500
20 STATUSE development 0 0
20 STATUSE TESTING 0 0
20 STATUSE RESEARCH 0 0
20 STATUSE HR 0 0
20 total 0 0
How to bring the results one status after another ?
I've tried like this but its throwing
SELECT C.CITYID AS CITYID,
CASE WHEN P.STATUS ='A' THEN 'STATUSA' ELSE 'STATUSE' END AS STATUS_TYPE ,
COALESCE(J.JOBNAME, 'TOTAL') AS JOBNAME,
COUNT(CASE WHEN P.STATUS ='A' THEN P.CITYID END ) AS COUNT ,
COALESCE(AVG(CAST(CASE WHEN P.STATUS = 'A' THEN P.SAL END AS DECIMAL(13,2)))/12 , 0) AS "AVG SAL",
COUNT(CASE WHEN P.STATUS ='E' THEN P.CITYID END ) AS COUNT ,
COALESCE(AVG(CAST(CASE WHEN P.STATUS = 'E' THEN P.SAL END AS DECIMAL(13,2)))/12 , 0) AS "AVG SAL"
FROM JOB1 J
CROSS JOIN
(SELECT DISTINCT CITYID
FROM EMP1 B WHERE CITYID = 10
) C
LEFT JOIN EMP1 P ON P.JOBID = J.JOBID
AND P.CITYID = C.CITYID and
YEAR = 2015
GROUP BY ROLLUP(C.CITYID, J.JOBNAME );
ERROR:
Column 'EMP1.STATUS' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
IF OBJECT_ID('tempdb..#JOB') IS NOT NULL
DROP TABLE #JOB
CREATE TABLE #JOB
(
JOBID SMALLINT UNIQUE NOT NULL,
JOBNAME CHAR(15)
);
IF OBJECT_ID('tempdb..#EMP') IS NOT NULL
DROP TABLE #EMP
CREATE TABLE #EMP
(
EMPID SMALLINT,
JOBID SMALLINT,
SAL SMALLINT,
CITYID SMALLINT,
YEAR SMALLINT,
STATUS CHAR(1)
);
INSERT INTO #JOB(JOBID, JOBNAME)
VALUES (1, 'DEVELOPMENT'),
(2, 'DEVELOPMENT'),
(3, 'TESTING'),
(4, 'TESTING'),
(7, 'TESTING'),
(9, 'RESEARCH'),
(8, 'HR');
INSERT INTO #EMP (EMPID , JOBID, SAL, CITYID, YEAR, STATUS)
VALUES (100, 1, 1000, 10, 2015, 'A'),
(200, 2, 2000, 10, 2015, 'A'),
(300, 1, 2500, 20, 2015, 'A'),
(400, 3, 1000, 10, 2016, 'A'),
(500, 6, 3000, 10, 2015, 'E'),
(600, 8, 1000, 30, 2015, 'A'),
(700, 8, 2000, 10, 2015, 'E'),
(800, 9, 1500, 10, 2015, 'A');
;with cteJobDict as (
select
distinct
j.JOBNAME
from
#JOB j
)
,cteStatusDict as(
select
distinct STATUS
from
#EMP e
),cteCityDict as (
select
distinct CITYID
from
#EMP
)
,cteJobStatusCityMatrix as(
select
*
from
cteJobDict
cross apply cteStatusDict
cross apply cteCityDict
)
,cteEmpWithJobName as (
select
e.*
,j.JOBNAME
from
#EMP e
join #JOB j on j.JOBID=e.JOBID
), cteData as (
SELECT
m.CITYID
,CASE WHEN m.STATUS ='A' THEN 'STATUSA' ELSE 'STATUSE' end as [Status-type]
,CASE WHEN m.STATUS ='A' THEN 1 ELSE 3 end as [Status-order]
,m.JOBNAME
,count(distinct e.EMPID) count
,iif(count(distinct e.EMPID)>0,'X','') status
,isnull(avg(e.sal),0) sal
FROM
cteJobStatusCityMatrix m
left join cteEmpWithJobName e on e.CITYID=m.CITYID and e.STATUS=m.STATUS and e.JOBNAME=m.JOBNAME and e.YEAR=2015
where
m.CITYID in (10,20)
group by
m.CITYID
,m.STATUS
,m.JOBNAME
union
SELECT
m.CITYID
,'total' as [Status-type]
,CASE WHEN m.STATUS ='A' THEN 2 ELSE 4 end as [Status-order]
,null
,count(distinct e.EMPID) count
,iif(count(distinct e.EMPID)>0,'X','') status
,isnull(avg(e.sal),0) sal
FROM
cteJobStatusCityMatrix m
left join cteEmpWithJobName e
on e.CITYID=m.CITYID
and e.STATUS=m.STATUS
and e.JOBNAME=m.JOBNAME
and e.YEAR=2015 -- here goes year
where
m.CITYID in (10,20) -- here goes cityid
group by
m.CITYID
,m.STATUS)
select
CITYID
,[Status-type]
,JOBNAME
,count
,status
,sal
from
cteData
order by
CITYID
,[Status-order]

Accumulating previous rows with grouping

I have this table on MS SQL Server
Customer Month Amount
-----------------------------
Tom 1 10
Kate 1 60
Ali 1 70
Tom 2 50
Kate 2 40
Tom 3 80
Ali 3 20
I want the select to get accumulation of the customer for each month
Customer Month Amount
-----------------------------
Tom 1 10
Kate 1 60
Ali 1 70
Tom 2 60
Kate 2 100
Ali 2 70
Tom 3 140
Kate 3 100
Ali 3 90
Noticing that Ali has no data for the month of 2
and Kate has no data for the month of 3
I have done it but the problem is that for the missing month for each customer no data shows
i.e. Kate has to be in month 3 with 100 amount
and Ali has to be in Month 2 with 70 amount
declare #myTable as TABLE (Customer varchar(50), Month int, Amount int)
;
INSERT INTO #myTable
(Customer, Month, Amount)
VALUES
('Tom', 1, 10),
('Kate', 1, 60),
('Ali', 1, 70),
('Tom', 2, 50),
('Kate', 2, 40),
('Tom', 3, 80),
('Ali', 3, 20);
select * from #myTable
select
SUM(b.Amount),a.Customer, a.Month
from
#myTable a
inner join
#myTable b
on a.Customer = b.Customer and
a.Month >= b.Month
group by
a.Customer, a.Month
Use window function
select Customer, Month,
sum(Amount) over (partition by customer order by month) Amount
from table t
So, you want some kind of look up tables which has possible months with customers.
with cte as
(
select * from (
select Customer from table
group by Customer)c
cross join (values (1),(2),(3))a(Months)
) -- look-up table
select c.Customer, c.Months,
sum(t.Amount) over (partition by c.Customer order by c.Months) Amount
from cte c left join table t
on t.Month = c.Months and t.Customer = c.Customer
Result :
Customer Months Amount
Tom 1 10
Kate 1 60
Ali 1 70
Tom 2 60
Ali 2 70
Kate 2 100
Ali 3 90
Kate 3 100
Tom 3 140
with cte as
(select *
from
(select distinct customer from myTable ) c
cross join ( values (1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12)) t(month))
select cte.customer, cte.month,
sum(myTable.amount) over (partition by cte.customer order by cte.month) as cumamount
from cte left join myTable
on cte.customer = myTable.customer and cte.month = myTable.month
order by cte.month, cte.customer desc
to be clear(in answer Amount and AmountSum)
DECLARE #myTable TABLE(Customer varchar(50), Month int, Amount int);
INSERT INTO #myTable(Customer, Month, Amount)
VALUES
('Tom', 1, 10),
('Kate', 1, 60),
('Ali', 1, 70),
('Tom', 2, 50),
('Kate', 2, 40),
('Tom', 3, 80),
('Ali', 3, 20);
DECLARE #FullTable TABLE(Customer varchar(50), Month int, Amount int);
INSERT INTO #FullTable(Customer, Month, Amount)
SELECT c.Customer, m.Month, ISNULL(mt.Amount, 0)
FROM (SELECT DISTINCT [Month] FROM #myTable) AS m
CROSS JOIN (SELECT DISTINCT Customer FROM #myTable) AS c
LEFT JOIN #myTable AS mt ON m.Month = mt.Month AND c.Customer = mt.Customer
SELECT t1.Customer, t1.Month, t1.Amount, (t1.Amount + ISNULL(t2.sm, 0)) AS AmountSum
FROM #FullTable AS t1
CROSS APPLY (SELECT SUM(Amount) AS sm FROM #FullTable AS t WHERE t.Customer = t1.Customer AND t.Month < t1.Month) AS t2
ORDER BY Month, Customer
Do you want get the sum amount every month for each customer , what ever the customer has transaction in that month?
In following script, If you have a customer table, you can join the customer table, do not need use (SELECT DISTINCT Customer FROM #myTable)
declare #myTable as TABLE (Customer varchar(50), Month int, Amount int);
INSERT INTO #myTable(Customer, Month, Amount)
VALUES
('Tom', 1, 10),
('Kate', 1, 60),
('Ali', 1, 70),
('Tom', 2, 50),
('Kate', 2, 40),
('Tom', 3, 80),
('Ali', 3, 20),
('Jack', 3, 90);
SELECT c.Customer,sv.number AS Month ,SUM(CASE WHEN t.Month<=sv.number THEN t.Amount ELSE 0 END ) AS Amount
FROM master.dbo.spt_values AS sv
INNER JOIN (SELECT DISTINCT Customer FROM #myTable) AS c ON 1=1
LEFT JOIN #myTable AS t ON t.Customer=c.Customer
WHERE sv.type='P' AND sv.number BETWEEN 1 AND MONTH(GETDATE())
GROUP BY sv.number,c.Customer
ORDER BY c.Customer,sv.number
----------
Customer Month Amount
-------------------------------------------------- ----------- -----------
Ali 1 70
Ali 2 70
Ali 3 90
Jack 1 0
Jack 2 0
Jack 3 90
Kate 1 60
Kate 2 100
Kate 3 100
Tom 1 10
Tom 2 60
Tom 3 140
Try this the table name is "a".
Using a combination of Cte and sub query. Tried it out in MSSQL2008R2
with cte as
(
select * from (
select Customer from a
group by Customer)c
cross join (values (1),(2),(3),(4),(5),(6),(7),(8),(9), (10),(11),(12))a(Months)
)
select Customer,Months,
(select SUM(total) from
(select customer , month , sum(amount)as total from a group by customer,
month) as GroupedTable
where GroupedTable.customer= cte.customer and GroupedTable.month<= cte.Months) as total
from cte
Group by Customer,Months
order by Customer,Months
try this:
create table #tmp (Customer VARCHAR(10), [month] INT ,Amount INT)
INSERT INTO #tmp
SELECT 'Tom',1,10
union all
SELECT 'Kate',1,60
union all
SELECT 'Ali',1,70
union all
SELECT 'Tom',2,50
union all
SELECT 'Kate',2,40
union all
SELECT 'Tom',3,80
union all
SELECT 'Ali',3,20
;WITH cte1 AS (
SELECT [month], ROW_NUMBER() OVER(order by [month] desc) rn
FROM (SELECT DISTINCT [month] as [month] FROM #tmp) a
)
, cte2 AS (
SELECT customer, ROW_NUMBER() OVER(order by customer desc) rn
FROM (SELECT DISTINCT customer as customer FROM #tmp) b
)
SELECT t2.Customer,t2.[month],ISNULL(t1.Amount,0) As Amount
into #tmp2
from #tmp t1
RIGHT JOIN
(select [month],customer from cte1
cross apply
cte2) t2 ON t1.customer=t2.customer and t1.[month]=t2.[month]
order by t2.[month]
SELECT Customer,[Month] ,SUM (Amount) OVER(partition by customer order by customer ROWS UNBOUNDED PRECEDING ) as Amount
FROM #tmp2
order by [month]
drop table #tmp
drop table #tmp2
I think this does what you want
declare #myTable as TABLE (Customer varchar(50), Month int, Amount int);
INSERT INTO #myTable (Customer, Month, Amount)
VALUES
('Tom', 1, 10),
('Kate', 1, 60),
('Ali', 1, 70),
('Tom', 2, 50),
('Kate', 2, 40),
('Tom', 3, 80),
('Ali', 3, 20);
select dts.Month, cts.Customer, isnull(t.Amount, 0) as Amount
, sum(isnull(t.Amount, 0)) over(partition by cts.Customer order by dts.Month) as CumAmt
from ( select distinct customer
from #myTable
) cts
cross join ( select distinct Month
from #myTable
) dts
left join #myTable t
on t.Customer = cts.Customer
and t.Month = dts.Month
order by dts.Month, cts.Customer;
Month Customer Amount CumAmt
----------- -------------------------------------------------- ----------- -----------
1 Ali 70 70
1 Kate 60 60
1 Tom 10 10
2 Ali 0 70
2 Kate 40 100
2 Tom 50 60
3 Ali 20 90
3 Kate 0 100
3 Tom 80 140
Try Sum Over Partition By
https://learn.microsoft.com/en-us/sql/t-sql/functions/sum-transact-sql
This will help you get the idea how to accumulate. If the code i use in postgresql like this
Select sum(amount) over(partition by customer, month)
This should do it for you. Also here is a link to the Microsoft docs regarding aggregation functions.
https://learn.microsoft.com/en-us/sql/t-sql/functions/aggregate-functions-transact-sql
Example:
SELECT
Customer, Month, SUM(Amount) as Amount
FROM myTable
GROUP BY Customer, Month
ORDER BY Customer, Month

SQL - Combination of Distinct & Count in a table

Need a simple query to summarize result from a table where 3 columns are present:
Order ID, Category & Brand.
The summary should contain order ID, distinct count of category and distinct count of brand belonging to the order ID.
Sample Data:
orderno product brand
1 A Z
1 A X
1 B Y
2 C X
2 B X
3 C X
3 B Y
Expected Result:
orderno product brand
1 2 3
2 2 1
3 2 2
Sample Data & Summary
Use DISTINCT of COUNT to get your expected result:
select orderno, count(distinct product) as product, count(distinct brand) as brand
from testtable
group by orderno
Sample execution with the given data
declare #test1 table (orderno int, product varchar(2), brand varchar(2))
insert into #test1 (orderno, product, brand) values
(1, 'A', 'Z'),
(1, 'A', 'X'),
(1, 'B', 'Y'),
(2, 'C', 'X'),
(2, 'B', 'X'),
(3, 'C', 'X'),
(3, 'B', 'Y');
select orderno, count(distinct product) as product, count(distinct brand) as brand
from #test1
group by orderno
Result:
orderno product brand
1 2 3
2 2 1
3 2 2
Try this,.
select orderno,count(DISTINCT product) product,COUNT(DISTINCT brand) brand
from data
GROUP by orderno
The output.,
orderno product brand
----------- ----------- -----------
1 2 3
2 2 1
3 2 2