how to join table group by count - sql-server

I have query_1:
select id,
count(case when no01 ='B' then 1 END) +
count(case when no02='B' then 1 END) +
count(case when no03='B' then 1 END) as Count_All
From tabel_a
where date ='20150201'
group by id
and also I have query_2:
select id, COUNT (*) from tabel_b
where ids <> 'T' and idt ='C'
group by id
How can I join query_1 with query_2 via id?

You could try using sub selects. Something like
SELECT *
FROM (
select id,
count(case when no01 ='B' then 1 END) + count(case when no02='B' then 1 END) + count(case when no03='B' then 1 END) as Count_All
From tabel_a
where date ='20150201'
group by id
) a INNER JOIN
(
select id,
COUNT (*) cnt
from tabel_b
where ids <> 'T'
and idt ='C'
group by id
) b ON a.ID = b.ID
Seeing as you specified SQL Server, you could also make use of a Common Table Expression.
Something like
;WITH a AS (
select id,
count(case when no01 ='B' then 1 END) + count(case when no02='B' then 1 END) + count(case when no03='B' then 1 END) as Count_All
From tabel_a
where date ='20150201'
group by id
)
, b as (
select id,
COUNT (*) cnt
from tabel_b
where ids <> 'T'
and idt ='C'
group by id
)
SELECT *
FROm a INNER JOIN
b ON a.ID = b.ID

Related

Switching rows with columns to generate a summary report in SQL Server

I have this query that geneartes data in format also shown:
SELECT TOP (10)
{ FN CONCAT({ FN CONCAT(dbo.BILL_INFO.BILL_NUMBER, '-')}, REPLICATE('0', 2 - LEN(RTRIM(dbo.BILL_INFO.PAY_MODE_ID)))
+ RTRIM(dbo.BILL_INFO.PAY_MODE_ID))} AS Id,
dbo.BILL_INFO.PAY_MODE_ID,
dbo.MASTER_PAY_MODE.NAME AS PAY_MODE
FROM dbo.BILL_INFO
INNER JOIN dbo.MASTER_PAY_MODE
ON dbo.BILL_INFO.PAY_MODE_ID = dbo.MASTER_PAY_MODE.ID
ORDER BY dbo.BILL_INFO.BILL_DATE DESC;
I need to transpose the result to this format:
I can do this using excel and a pivot but is there a way using SQL query?
Sample Data
Id |BILL_DATE |PAY_MODE
0000056-1002-18-10|2018-11-26 14:03:03.553|Bank Transfer
0001199-1002-18-05|2018-11-26 13:58:25.763|Credit Card
0000162-1030-18-05|2018-11-26 13:55:40.590|Credit Card
0001198-1002-18-05|2018-11-26 13:49:39.013|Credit Card
0001859-1030-18-04|2018-11-26 13:44:23.333|Free
0004443-1002-18-03|2018-11-26 13:42:27.550|Debit
0001532-1031-18-03|2018-11-26 13:36:23.010|Debit
0001916-1002-18-04|2018-11-26 13:33:23.157|Free
0001915-1002-18-04|2018-11-26 13:32:45.653|Free
0001914-1002-18-04|2018-11-26 13:30:35.580|Free
0004442-1002-18-03|2018-11-26 13:24:11.730|Debit
0004441-1002-18-03|2018-11-26 13:22:35.020|Debit
0004440-1002-18-03|2018-11-26 13:12:01.920|Debit
0004439-1002-18-03|2018-11-26 13:10:06.483|Debit
0001197-1002-18-05|2018-11-26 13:07:19.673|Credit Card
0001196-1002-18-05|2018-11-26 13:02:31.527|Credit Card
0004438-1002-18-03|2018-11-26 13:00:01.000|Debit
0001003-1030-18-03|2018-11-26 12:57:42.630|Debit
0001531-1031-18-03|2018-11-26 12:56:33.210|Debit
0001913-1002-18-04|2018-11-26 12:54:41.077|Free
Update
I have taken the solution provided by #[Tim Biegeleisen] as follows:
SELECT
MONTH(bi.BILL_DATE) AS [Month],
MAX(CASE WHEN m.NAME = 'Cheque' THEN bi.PAY_MODE_ID END) AS Cheque,
MAX(CASE WHEN m.NAME = 'Cash' THEN bi.PAY_MODE_ID END) AS Cash,
MAX(CASE WHEN m.NAME = 'Bank Transfer' THEN bi.PAY_MODE_ID END) AS [Bank Transfer],
MAX(CASE WHEN m.NAME = 'Credit Card' THEN bi.PAY_MODE_ID END) AS [Credit Card],
MAX(CASE WHEN m.NAME = 'Debit' THEN bi.PAY_MODE_ID END) AS Debit,
MAX(CASE WHEN m.NAME = 'Free' THEN bi.PAY_MODE_ID END) AS Free
FROM dbo.BILL_INFO bi
INNER JOIN dbo.MASTER_PAY_MODE m
ON bi.PAY_MODE_ID = m.ID
WHERE YEAR(bi.BILL_DATE) = 2018
GROUP BY
MONTH(bi.BILL_DATE) ORDER BY MONTH(bi.BILL_DATE)
You may do so via a pivot query, perhaps something alone these lines:
SELECT
YEAR(bi.BILL_DATE) + '-' + MONTH(bi.BILL_DATE) AS Date,
COUNT(CASE WHEN m.NAME = 'Cheque' THEN 1 END) AS Cheque,
COUNT(CASE WHEN m.NAME = 'Cash' THEN 1 END) AS Cash,
COUNT(CASE WHEN m.NAME = 'Bank Transfer' THEN 1 END) AS [Bank Transfer],
COUNT(CASE WHEN m.NAME = 'Credit Card' THEN 1 END) AS [Credit Card],
COUNT(CASE WHEN m.NAME = 'Debit' THEN 1 END) AS Debit,
COUNT(CASE WHEN m.NAME = 'Free' THEN 1 END) AS Free
FROM dbo.BILL_INFO bi
INNER JOIN dbo.MASTER_PAY_MODE m
ON bi.PAY_MODE_ID = m.ID
GROUP BY
YEAR(bi.BILL_DATE) + '-' + MONTH(bi.BILL_DATE);
Note that I am grouping the date column by month and year, because a given month appearing in the BILL_INFO table could belong to more than one year, in general.

How to pivot table from multiple column to multiple rows

CodeDt CodeHeader Item Qty Type Remark Attachment
LK4-033502 RK-K-LK4-032438 IA01001023 2.00 TPR002 2 1.jpeg
LK4-033502RK RK-K-LK4-032438 IA01001023RK 2.00 NULL IA01001023 NULL
Above is my data from Sql server table (using 2008 R2). I want to make it one row only. Here is my expected result:
CodeDt CodeHeader Item NewItem Qty
LK4-033502 RK-K-LK4-032438 IA01001023 IA01001023RK 2.00
How can I achieve that? Here is the relation:
Row 1 Item = Row 2 Remark,
Row 1 Code DT = Row 2 CodeDt+'RK'
There are several solutions
1) Using JOIN. It assumes that Type field is null for rows with CodeDt+'RK'
select
a.CodeDt, a.CodeHeader, a.Item, b.Item, a.Qty
from
myTable a
join myTable b on a.Item = b.Remark
where
a.Type is not null
2) Conditional aggregation
select
max(case when rn = 1 then CodeDt end)
, CodeHeader
, max(case when rn = 1 then Item end)
, max(case when rn = 2 then Item end)
, max(case when rn = 1 then Qty end)
from (
select
*, rn = row_number() over (partition by CodeHeader order by CodeDt)
from
myTable
) t
group by CodeHeader

Need to add a subquery in a CASE expression that is part of an aggregate function

I need to add the number of records in which the status is set to 'reopened'. But the 'reopened' status has several IDs.
This is the subquery that will Id the 'reopen' statuses:
SELECT (CASE WHEN s.sr_status_recid = 1 THEN 1 ELSE 0 END) AS Reopened
from v_rpt_service s
where vsrv.sr_status_recid in
(select distinct SR_Status_RecID from SR_Status where [Description] like '%Re-opened%'))
This is the main query that the above query needs to be a part:
SELECT DATEPART(WK, vsrv.date_entered) as WkNumber,
COUNT(vsrv.TicketNbr) AS OpenedIssues, --total ticket count
SUM(CASE WHEN vsrv.Closed_Flag = 1 THEN 1 ELSE 0 END) AS ClosedIssues, --sum of tickets with closed_flag = 1
(SELECT SUM(CASE WHEN s.sr_status_recid = 1 THEN 1 ELSE 0 END)
from v_rpt_service s
where vsrv.sr_status_recid in
(select distinct SR_Status_RecID from SR_Status where [Description] like '%Re-opened%')) AS ReopenedIssues,
SUM(CASE WHEN vsrvy.Surveys_Completed = 1 THEN 1 ELSE 0 END) AS SurveysCompletedWithConnectWise, -- Surveys_Completed flag in view is 1
SUM(CASE WHEN Source = 'Portal' THEN 1 ELSE 0 END) AS IssueLoggedPortal,
SUM(CASE WHEN Source = 'Email Connector' THEN 1 ELSE 0 END) AS IssueLoggedEmai
FROM v_rpt_service vsrv LEFT OUTER JOIN v_rpt_SurveysByTicket vsrvy ON vsrv.TicketNbr = Vsrvy.SR_Service_RecID
WHERE vsrv.company_name <> 'XYZ Test Company' AND vsrv.date_entered BETWEEN '01/01/2016' AND '10/07/2016'
GROUP BY DATEPART(WK, vsrv.date_entered)
ORDER BY WkNumber
How can I have a subquery that uses a CASE statement and the CASE statement is aggregated?
You can use CROSS APPLY
SELECT DATEPART(WK, vsrv.date_entered) as WkNumber,
COUNT(vsrv.TicketNbr) AS OpenedIssues, --total ticket count
SUM(CASE WHEN vsrv.Closed_Flag = 1 THEN 1 ELSE 0 END) AS ClosedIssues, --sum of tickets with closed_flag = 1
SUM(CountReopen.YesNo) AS NumberOfReopen,
SUM(CASE WHEN vsrvy.Surveys_Completed = 1 THEN 1 ELSE 0 END) AS SurveysCompletedWithConnectWise, -- Surveys_Completed flag in view is 1
SUM(CASE WHEN Source = 'Portal' THEN 1 ELSE 0 END) AS IssueLoggedPortal,
SUM(CASE WHEN Source = 'Email Connector' THEN 1 ELSE 0 END) AS IssueLoggedEmail
FROM v_rpt_service vsrv
LEFT OUTER JOIN v_rpt_SurveysByTicket vsrvy
ON vsrv.TicketNbr = Vsrvy.SR_Service_RecID
CROSS APPLY (
SELECT IIF(COUNT(*) > 0,1,0) YesNo
FROM SR_Status
where [Description] like '%Re-opened%'
AND SR_Status_ID = vsrv.sr_status_recid
) CountReopen(YesNo)
WHERE vsrv.company_name <> 'XYZ Test Company'
AND vsrv.date_entered BETWEEN '01/01/2016' AND '10/07/2016'
GROUP BY DATEPART(WK, vsrv.date_entered)
ORDER BY WkNumber

Create new columns depending on the columns in a table

I have code in the this following example.
legacy_id phone_type phone_number
1 f 1234567890
1 b 1233854100
1 f 4110256565
2 f 0707070770
2 b 7895120044
I want the data to end up like the following.
legacy_id f_phone_number_1 b_phone_number_1 f_phone_number_2
1 1234567890 1233854100 4110256565
2 0707070770 7895120044
My initial approach works but I was hoping there is a more efficient what of doing this.
Select fill.legacy_id, max(fill.f_phone_number_1),max(fill.b_phone_number_1),max(fill.f_phone_number_2)
from
(
Select
a.legacy_id as legacy_id, a.phone_type as phone_type,
case
when a.phone_type = 'F' then a.phone_number and
dense_rank() over (partition by a.legacy_id, a.phone_type order by a.legacy_id, a.phone_type, a.phone_number) = 1
else null
end as f_phone_number_1,
case
when a.phone_type = 'F' then a.phone_number and
dense_rank() over (partition by a.legacy_id, a.phone_type order by a.legacy_id, a.phone_type, a.phone_number) = 2
else null
end as f_phone_number_2,
case
when a.phone_type = 'b' then a.phone_number and
dense_rank() over (partition by a.legacy_id, a.phone_type order by a.legacy_id, a.phone_type, a.phone_number) = 1
else null
end as b_phone_number_1
from table a
group by a.legacy_id, a.phone_type, a.phone_number
) fill
group by fill.legacy_id
Is there a more efficient way of approaching this?
If you don't need to go dynamic, a conditional aggregation should do the trick
Declare #YourTable table (legacy_id int,phone_type varchar(25),phone_number varchar(25))
Insert Into #YourTable values
(1,'f','1234567890'),
(1,'b','1233854100'),
(1,'f','4110256565'),
(2,'f','0707070770'),
(2,'b','7895120044')
Select legacy_id
,f_phone_number_1 = max(case when phone_type='f' and RowNr=1 Then phone_number else '' end)
,b_phone_number_1 = max(case when phone_type='b' and RowNr=1 Then phone_number else '' end)
,f_phone_number_2 = max(case when phone_type='f' and RowNr=2 Then phone_number else '' end)
,b_phone_number_2 = max(case when phone_type='b' and RowNr=2 Then phone_number else '' end)
From (
Select *
,RowNr=Row_Number() over (Partition By legacy_id,phone_type Order By (Select NULL) )
From #YourTable
) A
Group By legacy_id
Returns
legacy_id f_phone_number_1 b_phone_number_1 f_phone_number_2 b_phone_number_2
1 4110256565 1233854100 1234567890
2 0707070770 7895120044

SQL Server - How to display master details data in columns

I have two tables, to be concise let’s call them TableA and TableB. This is the schema:
TableA
ID – int
Name varchar(50)
TableB
ID – int
TableA_Fk – int
Value varchar(50)
Each record in table A can have at most 9 records in table B. I want to be able to retrieve the data in a columnar form:
TableA-Name, TableB-Value1, … TableB-Value9
Is this possible using queries? Thanks!
You could do something like:
SELECT rank() OVER (ORDER BY tableA_FK) as rank, tableA_fk, value
INTO #temp
FROM TableB b
ORDER BY rank
SELECT a.Name,
CASE WHEN t.rank = 1 THEN t.Value ELSE NULL END AS TableB-Value1,
CASE WHEN t.rank = 2 THEN t.Value ELSE NULL END AS TableB-Value2,
CASE WHEN t.rank = 3 THEN t.Value ELSE NULL END AS TableB-Value3,
.... (etc.)
FROM TableA a
INNER JOIN #temp t ON a.Id = t.tableA_fk
You need Sql Server 2005 or up.
Sorry, but I don't have Sql Server (or the time) to test this well. Hope this gives you an idea and helps.
You will require a LEFT JOIN and a PIVOT table
This should do it, in addition to be DBRM independant.
SELECT A.Name
, SUM(CASE WHEN B.Value = 1 THEN 1 ELSE NULL END) AS B_Value_1
, SUM(CASE WHEN B.Value = 2 THEN 2 ELSE NULL END) AS B_Value_2
, SUM(CASE WHEN B.Value = 3 THEN 3 ELSE NULL END) AS B_Value_3
, SUM(CASE WHEN B.Value = 4 THEN 4 ELSE NULL END) AS B_Value_4
, SUM(CASE WHEN B.Value = 5 THEN 5 ELSE NULL END) AS B_Value_5
, SUM(CASE WHEN B.Value = 6 THEN 6 ELSE NULL END) AS B_Value_6
, SUM(CASE WHEN B.Value = 7 THEN 7 ELSE NULL END) AS B_Value_7
, SUM(CASE WHEN B.Value = 8 THEN 8 ELSE NULL END) AS B_Value_8
, SUM(CASE WHEN B.Value = 9 THEN 9 ELSE NULL END) AS B_Value_9
FROM A
INNER JOIN B ON B.TableA_FK = A.ID
GROUP BY A.Name
ORDER BY A.Name

Resources