I'm trying to change the header of a column based on a variable
Currently I have
SELECT
(CASE
WHEN GROUPING(CASE ##Role
WHEN 2 THEN Processor
WHEN 3 THEN Reviewer
END) = 1
THEN 'Total'
ELSE (CASE ##Role
WHEN 2 THEN Processor
WHEN 3 THEN Reviewer
END)
END) AS 'User',
COUNT(EntityId) AS 'Tickets Processed'
FROM
table
WHERE
conditions
GROUP BY
CASE ##Role
WHEN 2 THEN Processor
WHEN 3 THEN Reviewer
END WITH ROLLUP
Right now this returns the data I need for the correct role, however is there a way to change the second column's header based on the variable to something like
COUNT(EntityId) AS CASE ##Role
WHEN 2 THEN 'Tickets Processed'
WHEN 3 THEN 'Tickets Reviewed'
END
EDIT:
Sample of current result:
##Role = 2 or ##Role = 3
Both return:
User Tickets Processed
-----------------------------
Steve 1
Gerald 3
John 1
Paul 2
Peter 5
Total 12
Desired result:
##Role = 2
User Tickets Processed
-----------------------------
Steve 1
Gerald 3
John 1
Paul 2
Peter 5
Total 12
##Role = 3
User Tickets Reviewed
-----------------------------
Steve 1
Gerald 3
John 1
Paul 2
Peter 5
Total 12
Sample data
EntityID Processor Reviewer
----------------------------------
1 Peter Bob
2 Peter Paul
3 Peter Bob
4 John Paul
5 Peter Bob
6 Peter Bob
...
You can either use dynamic sql, or you can split the logic based on the ##role variable:
IF ##Role = 2 THEN {do Query A}
ELSE {do Query B}
But you definitely cannot base the column alias on the value of a variable in the context of a non-dynamic query.
Related
Let's say I have a table that looks like that:
Name Category Subject Score
Alice 1 Math 2
Alice 1 Biology 3
Bob 2 Math 4
Bob 2 Biology 2
I would like to leave just one occurence of Score in each batch of Category for each Name and set rest to zero, so the result would be:
Alice 1 Math 2
Alice 1 Biology 0
Bob 2 Math 4
Bob 2 Biology 0
Is it possible to do?
I wanted to create a new column (Group ID) on the basis of following conditions:
If the DOB and first three letters of Name are same, then it must fall is same Group ID.
Name
DOB
Group ID
Anny
18-01-1922
0
Anny Scott
01-01-1950
1
Annie
01-01-1950
1
David
14-02-1950
2
David Kern
15-02-1951
3
William Perry
15-02-1953
4
Kenneth Field
15-02-1953
5
This how I want to create the groups
I have used the following code, to create the group ID for name (If first three letters are matched)
df['Group ID Name']=df.groupby(df['name'].str[:3]).ngroup()
The following code is used to create the group ID for DOB (If two records have the same DOB)
df['Group ID DOB']=df.groupby('Date of Birth').ngroup()
I want to use both the condition to create the Group ID, please help me out for the same.
Add multiple columns in list and also for correct ordering sort=False:
df['Group ID Name'] = df.groupby(['DOB',df['Name'].str[:3]], sort=False).ngroup()
print (df)
Name DOB Group;ID Group ID Name
0 Anny 18-01-1922 0 0
1 Anny Scott 01-01-1950 1 1
2 Annie 01-01-1950 1 1
3 David 14-02-1950 2 2
4 David Kern 15-02-1951 3 3
5 William erry 15-02-1953 4 4
6 Kenneth Field 15-02-1953 5 5
I have a sql table like:
ID Name Balance
1 Peter 324.5
2 Michael 122.7
3 Peter 788.3
4 Mark 45.7
5 Ralph 333.5
6 Thomas 563.2
7 Ralph 9685.1
8 Peter 2444.5
9 Susi 35.2
10 Andrew 442.5
11 Susi 2424.8
Is it possible to write a while loop in sql, where you could add a whole new column with integer numbers (for example 1....3) for each duplicate names (3 times Peter, 2 times Susi, 2 times Ralph)? For the non duplicate names it should be a value of 0.
So the final table should look like this:
ID Name Balance Value
1 Peter 324.5 1
2 Michael 122.7 0
3 Peter 788.3 1
4 Mark 45.7 0
5 Ralph 333.5 2
6 Thomas 563.2 0
7 Ralph 9685.1 2
8 Peter 2444.5 1
9 Susi 35.2 3
10 Andrew 442.5 0
11 Susi 2424.8 3
You wouldn't want to use a while loop for this. Just use window functions:
select t.*, count(*) over (partition by name) as cnt
from t;
This provides the total count for each name. If you want an incremental value, you can use row_number():
select t.*, row_number() over (partition by name order by id) as seqnum
from t;
This would enumerate the rows for each name, so every name would have a "1" value, some would have "2" and so on.
I'm having a bit of trouble finding the best way to group users together based off of their attributes.
Here's a simplified version of some user data:
ID | First Name | Last Name | Address | Email | Phone 1 | Phone 2 | Phone 3 |
-----------------------------------------------------------------------------
1 Bob Smith Addy 1 a#a 1 2 3
2 Susan Q Addy 2 b#b 4 5 6
3 Robert Smith a#a 1
4 Susie Quinn Addy 2
5 Ryan Foo Addy 3 c#c
6 Pat Bar Addy 4
7 Patrick Bar Addy 4 1 2 3
From that, I can tell that the grouped items will look like:
1,3 (Matched on email)
2,4 (Matched on address)
5 (no matches)
6,7 (Matched on Address and last name)
1,7 (Matched on all three phone numbers)
I'm able to get this far with the stored procedure I've written. This results in 2.6 million results. Now, the next step I need to take is to merge these into non-duplicate groups... which should look something like this:
1,3,6,7
2,4
5
I've tested out a cursor query but, after 30 minutes of execution, it hadn't finished. So my question is this: what's the best/most efficient way to turn my 1 to 1 matches into one to many matches?
I am working with MS Access 2007. I have 2 tables: Types of Soda, and Likeability.
Types of Soda are: Coke, Pepsi, Dr. Pepper, and Mello Yellow
Likeability is a lookup with these options: Liked, Disliked, No preference
I know how to count the number of Cokes or Mello Yellows in the table using DCount("[Types]", "[Types of Soda]", "[Types]" = 'Coke')
I also know how to count the number of Liked, Disliked, No preference.
("[Perception]", "[Likeability]", "[Perception]" = 'Liked')
But, what if I need to count the number of "Likes" by Type.
i.e. the table should look like this:
Coke | Pepsi | Dr. Pepper | Mello Yellow
Likes 9 2 12 19
Dislikes 2 45 1 0
No Preference 0 12 14 15
I know in Access I can create a cross tab queries, but my tables are joined by an ID. So my [Likeability] table has an ID column, which is the same as the ID column in my [Types] table. That's the relationship, and that's what connects my tables.
My problem is that I don't know how to apply the condition for counting the likes, dislikes, etc, for ONLY the Types that I specify. It seems like I first have to check the [Likeability] table for "Likes", and cross reference the ID with the ID in the [Types] table.
I am very confused, and you may be too, now. But all I want to do is count the # of Likes and Dislikes for each type of soda.
Please help.
Its not really clear (to me anyway) what your tables look like so lets assume the following
tables
Soda
------
Soda_ID (Long Integer (Increment))
Soda_Name (Text(50)
Perception
------
Perception_ID (Long Integer (Increment))
Perception_Name (Text(50)
Likeability
-----------
Likeability_ID (Long Integer (Increment))
Soda_ID (Long Integer)
Perception_ID (Long Integer)
User_ID (Long Integer)
Data
Soda_Id Soda_Name
------- ---------
1 Coke
2 Pepsi
3 Dr. Pepper
4 Mello Yellow
Perception_ID Perception_Name
------------- ---------
1 Likes
2 Dislikes
3 No Preference
Likeability_ID Soda_ID Perception_ID User_ID
-------------- ------- ------------- -------
1 1 1 1
2 2 1 1
3 3 1 1
4 4 1 1
5 1 2 2
6 2 2 2
7 3 2 2
8 4 2 2
9 1 3 3
10 2 3 3
11 3 3 3
12 4 3 3
13 1 1 5
14 2 2 6
15 2 2 7
16 3 3 8
17 3 3 9
18 3 3 10
Transform query You could write a query like this
TRANSFORM
Count(l.Likeability_ID) AS CountOfLikeability_ID
SELECT
p.Perception_Name
FROM
Soda s
INNER JOIN (Perception p
INNER JOIN Likeability l
ON p.Perception_ID = l.Perception_ID)
ON s.Soda_Id = l.Soda_ID
WHERE
p.Perception_Name<>"No Preference"
GROUP BY
p.Perception_Name
PIVOT
s.Soda_Name;
query output
Perception_Name Coke Dr_ Pepper Mello Yellow Pepsi
--------------- ---- ---------- ------------ -----
Dislikes 1 1 1 3
Likes 2 1 1 1