I have a table containing user-account permissions and I'm trying to write a query to return one row for each user-account combination.
Here is what I have.
CltKey AcctKey TranTypeID Access
10 2499 10 0
10 2499 11 1
10 2499 12 1
10 2764 10 1
10 2764 11 1
10 2764 12 0
Here is what I'd like to have.
CltKey AcctKey TranTypeID1 Access1 TranTypeID2 Access2 TranTypeID3 Access3
10 2499 10 0 11 1 12 1
10 2764 10 1 11 1 12 0
Or even better something like this.
CltKey AcctKey HasTranTypeID1 HasTranTypeID2 HasTranTypeID3
10 2499 0 1 1
10 2764 1 1 0
I have tried doing a self join, but I keep getting multiple rows for each TranTypeID. One with it equal to 0 and another with it equal to 1. I have also tried using nested "Select" statements, but the performance is horrible. Does anyone have an idea on how to do this?
Thanks.
Edit: Unfortunately, this has to work in SQL 2000.
It's been a while since I used SQLServer 2000, but this will probably work.
select cltkey, acctkey,
max( case when trantypeid = 10 and access = 1
then 1 else 0 end ) as hastrantypeid1,
max( case when trantypeid = 11 and access = 1
then 1 else 0 end ) as hastrantypeid2,
max( case when trantypeid = 12 and access = 1
then 1 else 0 end ) as hastrantypeid3
from table
group by cltkey, acctkey;
If not, try this:
create view has_access as
select cltkey, acctkey,
max( case when trantypeid = 10 and access = 1
then 1 else 0 end ) as hastrantypeid1,
max( case when trantypeid = 11 and access = 1
then 1 else 0 end ) as hastrantypeid2,
max( case when trantypeid = 12 and access = 1
then 1 else 0 end ) as hastrantypeid3
from table;
and then get your results from this
select cltkey, acctkey,
max( hastrantypeid1) as hastrantypeid1,
max( hastrantypeid2 ) as hastrantypeid2,
max( hastrantypeid2 ) as hastrantypeid2
from has_access
group by cltkey, acctkey;
Note that this will tell you a (cltkey, acctkey) has access (of a particular type) if any row for that tuple of (cltkey, acctkey) has access for that particular type. That is, it's essentially a row-wise OR.
If all rows for that tuple must have access for that tuple to have access, that is, if you want a row-wise AND, you'll need to do this:
min( case when trantypeid = 10
then case when access = 1
then 1 else 0 end else null end) as hastrantypeid1,
etc.
SELECT CltKey, AcctKey,
MAX(CASE TrantypeId WHEN 10 THEN Access ELSE NULL END) AS HasTranTypeID1,
MAX(CASE TrantypeId WHEN 11 THEN Access ELSE NULL END) AS HasTranTypeID2,
MAX(CASE TrantypeId WHEN 12 THEN Access ELSE NULL END) AS HasTranTypeID3
FROM PermissionsTable
GROUP BY CltKey, AcctKey
ORDER BY CltKey, AcctKey
;
Use PIVOT - Here is an example: http://msdn.microsoft.com/en-us/library/ms177410.aspx
Related
Select p.pnum,
SUM(CASE WHEN P.NegativeScreen = 'Type99' THEN 1 ELSE 0 END) TotalDetected,
SUM(IIF(P.IsPositive = 1, 1,0)) TotalP,
SUM(CASE WHEN (P.MethId NOT IN (4, 8, 10, 25) THEN 1 ELSE 0 END) Total,
SUM(CASE WHEN (P.MethID IN (34,64) ) THEN 1 ELSE 0 END) TotalVal1,
SUM(CASE WHEN (P.MethID IN (16,64) ) THEN 1 ELSE 0 END) TotalVal2,
SUM(CASE WHEN (P.MethID IN (2,4,6,11,13,14,15,18,21,22,24,28,30,31) OR (P.MethID
= 1 AND P.TotalCount IS NOT NULL)) THEN 1 ELSE 0 END) TotalMethOther,
FROM tbl_plt p
GROUP BY P.PNum
Notice that the above query has all the fields from the tbl_plt table and SUM() is done on the fields.
Notice where I have MethID mentioned above. I need to check if those MethID exist in the tbl_plt table and if they exist in another table called TblOther. If so, tally it up accordingly.
Here is the fields in TblOther Table. Note that in TblOther table, we can have multiple PNums but the MethID will be different. Also note that for not all pNums will have entries in the TblOther table.
ID PNum MethID
1 232 32
2 232 64
3 232 10
4 104 14
5 104 54
6 22 4
7 4 13
I tried with LEFT JOIN with TblOther table but things gets messy as with the left join, it also tallies up incorrectly for places like:
SUM(CASE WHEN P.NegativeScreen = 'Type99' THEN 1 ELSE 0 END) TotalDetected,
SUM(IIF(P.IsPositive = 1, 1,0)) TotalP,
As an example for where I have:
SUM(CASE WHEN (P.MethID IN (34,64) ) THEN 1 ELSE 0 END)
it needs get the count of how many MethID exist in both the tblOther and tbl_plt for where MethID is 34 or 64 for the associated PNum.
It needs to do similarly for other places where MethID is mentioned.
I don't know enough about TblOther or it's join, but I suspect you might need to do the same group by (i.e., PNum) on it before joining on PNum. Then the left join will match either 0 or 1 records. Be sure to account for the null if there is no match.
You could start by getting the list of distinct PNum and MethIDs to use and then do your summing based on that list:
;WITH entries as (
SELECT DISTINCT PNum, MethID
FROM tblOther)
SELECT *
FROM entries
INNER JOIN tbl_plt
ON entries.PNum = tbl_plt.PNum
AND entries.MethID = tbl_plt.MethID
GROUP BY entries.PNum
I am trying to add two dynamic columns HeaderText and IsShowHeader to my table through a stored procedure.
In the first column, the first row must have text as Header1 and after 8 rows text must be Header2, then again after 8 rows text must be Header3 and so on.
In the second column value must be 1, and next 8 rows must have 0, the 9th row value must be 1 again, then the next 8 rows must have 0 like this...
ALTER PROCEDURE [dbo].[SkipRow]
AS
BEGIN
SELECT RID
,FirstName
,LastName
,(CASE WHEN X.[Row#]%9=0 And [X].[Row#]=0 THEN 1 ELSE
0 END)As IsShowHeader
,(COUNT(*) OVER ()) as TotalRows FROM
(
SELECT
*,ROW_NUMBER() OVER(ORDER BY RID) AS [Row#]
FROM Mytable1 WITH(NOLOCK)
)X
End
Output:
HeaderText IsShowHeader
1 Header1 1
2 Null 0
3 Null 0
4 Null 0
5 Null 0
6 Null 0
7 Null 0
8 Null 0
9 Null 0
10 Header2 1
11 Null 0
12 Null 0
13 Null 0
14 Null 0
15 Null 0
16 Null 0
17 Null 0
18 Null 0
19 Header3 1
you already have the [Row#], use the modulus operator % to get every 9 rows
HeaderText = case when ([Row#] - 1) % 9 = 0
then 'Header' + convert(varchar(10), ([Row#] - 1) / 9 + 1)
end,
IsShowHeader = case when ([Row#] - 1) % 9 = 0
then 1 else 0 end
You can try this:
SELECT M.id,
M.HeaderText,
CASE WHEN M.HeaderText IS NOT NULL THEN 1 ELSE 0 END AS IsShowHeader
FROM
(
SELECT P.id,
CASE
WHEN P.HeaderText IS NOT NULL THEN
P.HeaderText + CAST(P.IndexNumber AS VARCHAR(10))
ELSE
NULL
END AS HeaderText
FROM
(
SELECT K.id,
HeaderText,
COUNT(K.HeaderText) OVER (ORDER BY id ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS IndexNumber
FROM
(
SELECT id,
CASE
WHEN (id - 1) % 9 = 0 THEN 'Header' ELSE NULL
END AS HeaderText
FROM dbo.test
) AS K
) AS P
) AS M;
I have illustrate the scenario step by step, however you can rewrite it in a simple way like the answer that #Squirrel posted.
For a SQL Server based report,
Table:
CID Date ID Service Days
1 3/7/2016 1 Individual 3
2 4/5/2016 2 Individual 4
3 5/24/2016 1 Individual 3
4 4/4/2016 4 Group 2
5 4/4/2016 4 Group 2
6 2/18/2016 4 Group 2
7 5/5/2016 5 Group 1
8 5/5/2016 5 Group 1
I used this code:
SELECT
ID,
Service,
COUNT(WHEN Days = 4 THEN 1 END) AS '4Days',
COUNT(WHEN Days = 3 THEN 1 END) AS '3Days',
COUNT(WHEN Days = 2 THEN 1 END) AS '2Days',
COUNT(WHEN Days = 1 THEN 1 END) AS '1Day'
FROM Table T1
GROUP BY
ID,
Service
which gives me this Output:
ID Service 4Days 3Days 2Days 1Day
1 Individual 0 2 0 0
2 Individual 1 0 0 0
4 Group 0 0 3 0
5 Group 0 0 0 2
What I want to do is not count the Group services as separate services for separate individuals, but just as one service per group. A Count Distinct used with the Date or ID could help me do that but I don't know how to make that play with the Individual services where I just wanna count them individually and not using DISTINCT. So the desired output is:
ID Service 4Days 3Days 2Days 1Day
1 Individual 0 2 0 0
2 Individual 1 0 0 0
4 Group 0 0 2 0
5 Group 0 0 0 1
I'll edit the post in case I oversimplified the problem since this is dummy data.
Looks like you could use distinct this way if you wanted:
count(distinct
case when Days = 1 then case when Service = 'Group' then 1 else "Date" end end
) as [1Day]
Depending on your indexing it's possible that introducing another column in the query would change the query plan. I suspect that probably isn't the case though.
If I am not wrong for '2Days' column service type 'Group' count should be '2' if our grouping based on 'Date' column, if so then try this:
SELECT
ID,
Service,
CASE WHEN MAX(t.days) = 4 THEN MAX(t.date) ELSE 0 END AS '4Days',
CASE WHEN MAX(t.days) = 3 THEN MAX(t.date) ELSE 0 END AS '3Days',
CASE WHEN MAX(t.days) = 2 THEN MAX(t.date) ELSE 0 END AS '2Days',
CASE WHEN MAX(t.days) = 1 THEN MAX(t.date) ELSE 0 END AS '1Day'
FROM table T1
OUTER APPLY (SELECT days,
COUNT(DISTINCT(date)) date
FROM Table WHERE days = t1.days GROUP BY days) t
GROUP BY id, service
ORDER BY ID
Based on your last edit, this is the most straight forward way I could think of to handle the query:
with cte as (
select id, service, days
from table t1
where service = 'Individual'
union all
select id, service, days
from table t1
where service = 'Group'
group by id, service, days, date
)
select id,
service,
count(case when days = 4 then 'X' end) as [4Days],
count(case when days = 3 then 'X' end) as [3Days],
count(case when days = 2 then 'X' end) as [2Days],
count(case when days = 1 then 'X' end) as [1Day]
from cte
group by id, service
Here is the Table :
If OBJECT_ID ('tempdb..##SelfCount') Is not null
drop table #SelfCount
create table #SelfCount (CanID int , CanType int)
insert into #SelfCount (CanID, CanType)
values
(1,0),
(2,0),
(1,1),
(2,1),
(1,2),
(1,2),
(1,0)
CanID CanType
1 0
2 0
1 1
2 1
1 2
1 2
1 0
I'm Expecting the result to be like this
CanID Self Spouse Dependent
1 2 1 2
2 1 1 0/NULL --It doesn't matter if it's nUll or 0
I wrote this query
select CanID,
case
When CanType = 0 then count(CanType)
end as [self],
case
when CanType = 1 then count(CanType)
end as [Spouse],
Case
When CanType = 2 then count(CanType)
end as [Dependent]
from #SelfCount
Group by CanID, CanType
But the Result Set is like this :
CanID Self Spouse Dependent
1 2 NULL NULL
2 1 NULL NULL
1 NULL 1 NULL
2 NULL 1 NULL
1 NULL NULL 2
I've tried the Recursive method, If anyone could provide both Recursive as well as Set processing method, it'll be greatly appreciated.
By including the CanType in the group by clause, you're getting a separate result row per distinct value of CanType (and CanId, since it's also contained in the group by clause).
Instead, you should only have CanId in the group by clause, and apply different counts on case expressions:
SELECT CanID,
COUNT (CASE CanType WHEN 0 THEN 1 END) AS [Self],
COUNT (CASE CanType WHEN 1 THEN 1 END) AS [Spouse],
COUNT (CASE CanType WHEN 2 THEN 1 END) AS [Dependent],
FROM #SelfCount
GROUP BY CanID
May be this is what you need:
select CanID,
sum(case when CanType = 0 then 1 else 0 end) as [Self],
sum(case when CanType = 1 then 1 else 0 end) as [Spouse],
sum(case when CanType = 2 then 1 else 0 end) as [Dependent]
from #SelfCount
group by CanID
You can try with PIVOT:
select
CanID,
[0] as Self,
[1] as Spouse,
[2] as Dependent
from tab
pivot
(
count (CanType)
for CanType IN ([0], [1], [2])
) as pvt
Basically, it will group by all columns not declared inside the PIVOT clause (actually, just the CanID) and then create three aggregation COUNT columns, one FOR each CanType IN the list of values. If you need to count more values, just define them in the IN clause and in the SELECT clause.
I'm fairly sure this is an easy thing to do, but I'm a newbie at SQL so be gentle. If I want to write a query, that adds up the total occurences of each process number and stores those values to a new column, what do I do? I thought some mixture of count(distinct ...) could get it down but I'm not sure. See the result table for what I'm looking for.
Order_Table:
order_number process
100 8
100 7
100 7
100 6
100 5
105 6
105 2
105 4
Results:
order_num NumOfEight NumOfSeven NumOfSix NumOfFive NumOfFour NumOfTwo
100 1 2 1 1 0 0
105 0 0 1 0 1 1
Update: I'm using SQL 2005 as a base, but have access to newer versions. Process is a finite set of values.
Assuming SQL Server 2005+ you can use PIVOT
SELECT
order_num, [8] AS NumOfEight, [7] AS NumOfSeven /* etc. etc.*/
FROM
(SELECT order_number AS order_num, process FROM Order_Table) p
PIVOT
(COUNT(process) FOR process IN ([8],[7] /* etc. etc.*/)) pvt
select order_num,
sum(case when process = 8 then 1 else 0 end) as NumOfEight,
sum(case when process = 7 then 1 else 0 end) as NumOfSeven,
sum(case when process = 6 then 1 else 0 end) as NumOfSix
/* Repeat as many times as needed */
from Order_Table
group by order_num