How can I limit my query to only return the first result found for each ID? - tsql

I'm trying to select only the first result for each PlayerID.
So I tried this query using DISTINCT :
SELECT DISTINCT CharacterName, PlayerID, Class, Race, Alignment, Weapon
FROM CharacterTable
But it is still giving me all rows for each character like this:
CharacterName PlayerID Class Race Alignment Weapon
----------------------------------------------------------------------------
Racx 55 Fighter Human Chaotic Good Sword
Racx 55 Fighter Human Chaotic Good Spear
Racx 55 Fighter Human Chaotic Good Dagger
Elix 41 Cleric Orc Lawful Good Mace
Elix 41 Cleric Orc Lawful Good Club
Marra 97 Wizard Elf Neutral Staff
Lyrss 76 Thief Human Chaotic Good Dagger
Lyrss 76 Thief Human Chaotic Good Knife
Lyrss 76 Thief Human Chaotic Good Dart
Lyrss 76 Thief Human Chaotic Good Bow
I only want the first result found for each character.
Is this possible?
Thanks!

Does this help?
SELECT * FROM
(
SELECT *
,RANK() OVER
(PARTITION BY PLAYERID ORDER BY Weapon DESC) AS Rank FROM TEST003
) AA
WHERE Rank =1

Related

Select difference depending on criteria

Hope you can help me with one select statement.
I need to select only the records based on criteria but only when duplicates are detected. The criteria is No_Type='Custom'
Example of table data:
Product | Component | Detail | Stock | No_Type
-------------------------------------------------------
Brick Powder Grain 12 General
Brick Water Plain 34 General
Brick Additives A95 54 General
Brick Powder Grain 67 Custom
Brick Water Plain 55 Custom
Brick Additives A95 43 Custom
Box Wood Oak 1 General
Box Nails Steel 2 General
Result is based on detecting duplicate values for columns (Product, Component, Details) and if detected, select the records where under column No_Type is stated with 'Custom'.
What I would need as a result is:
Product | Component | Detail | Stock | No_Type
-------------------------------------------------------
Brick Powder Grain 67 Custom
Brick Water Plain 55 Custom
Brick Additives A95 43 Custom
Box Wood Oak 1 General
Box Nails Steel 2 General
There is no ID fields in the table, only what is here.
I'm using Postgresql.

reshape and merge in stata

I have three data sets:
First, called education.dta. It contains individuals(students) over many years with their achieved educations from yr 1990-2000. Originally it is in wide format, but I can easily reshape it to long. It is presented as wide under:
id educ_90 educ_91 ... educ_00 cohort
1 0 1 1 87
2 1 1 2 75
3 0 0 2 90
Second, called graduate.dta. It contains information of when individuals(students) have finished high school. However, this data set do not contain several years only a "snapshot" of the individ when they finish high school and characteristics of the individual students such as backgroung (for ex parents occupation).
id schoolid county cohort ...
1 11 123 87
2 11 123 75
3 22 243 90
The third data set is called teachers.dta. It contains informations about all teachers at high school such as their education, if they work full or part time, gender... This data set is long.
id schoolid county year education
22 11 123 2011 1
21 11 123 2001 1
23 22 243 2015 3
Now I want to merge these three data sets.
First, I want to merge education.dta and graduate.dta on id.
Problem when education.dta is wide: I manage to merge education and graduation.dta. Then I make a loop so that all the variables in graduation.dta takes the same over all years, for eksample:
forv j=1990/2000 {
gen county j´=.
replace countyj´=county
}
However, afterwards when reshaping to long stata reposts that variable id does not uniquely identify the observations.
further, I have tried to first reshape education.dta to long, and thereafter merge either 1:m or m:1 with education as master, using graduation.dta.
However stata again reposts that id is not unique. How do I deal with this?
In next step I want to merge the above with teachers.dta on schoolid.
I want my final dataset in long format.
Thanks for your help :)
I am not certain that I have exactly the format of your data, it would be helpful if you gave us a toy dataset to look at using dataex (and could even help you figure out the problem yourself!)
But to start, because you are seeing that id is not unique, you need to figure out why there might be multiple ids in any of the datasets. Can someone in graduate.dta or education.dta appear more than once? help duplicates will probably be useful to explore the data in this way.
Because you want your dataset in long format I suggest reshaping education.dta to long first, then doing something like merge m:1 id using "graduate.dta" (once you figure out why some observations are showing up more than once) and then, finally something like merge 1:1 schoolid year using "teacher.dta" and you will have your final dataset.

pig merge lists without key

In Apache Pig 0.15, I have two simple lists (WITHOUT id/primary key, etc.) that I want to merge together to create one list of tuples with two columns. Example:
Names
-----
Peter
John
Anne
Ages
-----
45
23
44
I want to end up with:
Names Age
---------------
Peter 45
John 23
Anne 44
I know I can use RANK on both lists and then JOIN, but that looks way too costly as I have millions of entries in these lists. I kind of want to do a JOIN with "merge" without having a join parameter...
Any idea about how to do this efficiently in Apache Pig?
If you do not care about the mapping between Age and Name then you can try cross-join between two relations. Post Cross join group by names and retain anyone out of it. However IMO, this may be more costlier ( rather resource intensive) than the RANK approach you mentioned above.

DB2 9 Fundamentals

Given the following two tables:
NAMES
NAME NUMBER
---------- -------
Wayne Gretzky 99
Jaromir Jagr 68
Bobby Orr 4
Bobby Hull 23
Mario Lemieux 66
POINTS
-----------------------------
NAME POINTS
---------- ------
Wayne Gretzky 244
Bobby Orr 129
Brett Hull 121
Mario Lemieux 189
Joe Sakic 94
How many rows would be returned using the following statement?
SELECT name FROM names, points
Can someone explain why the answer is 25?
Thanks in advance for any help provided
I guess this instruction is equivalent to a cross join in standard SQL. Hence the number of records returned is 5 records in names * 5 records in points = 25.
Also known as the "Cartesian Product"
"The Cartesian product, also referred to as a cross-join, returns all the rows in all the tables listed in the query. Each row in the first table is paired with all the rows in the second table. This happens when there is no relationship defined between the two tables."
from:
http://www.dba-oracle.com/t_garmany_9_sql_cross_join.htm

Group Summary Involving dividing in Crystal Reports

I have a Crystal Report with about 9 different groups and wanted to be able to have a summary that involves dividing one value by the other.
For example (table):
Period Company Division Region State City Store Employee Sales Goal
01-2012 Ab Works Northern NW OR Ball 888 Foo 100 150
01-2012 Ab Works Northern NW OR Ball 888 Choo 175 120
02-2012 Ab Works Southern SE GA Chip 743 Bar 34 75
02-2012 Perrywee Northern NC MN Fedd 147 Bill 80 64
03-2012 Perrywee Eastern FE WV Wood 98 Tim 75 250
01-2012 Perrywee Eastern FE WV Wood 92 Karl 84 250
01-2012 Perrywee Western W CA Fub 398 Fubar 175 170
My output then looks like this:
Company Jan2012 Feb2012 Mar2012 ... YTD-Avg
Ab Works 70.3% 87.3% 87.5% 84.3%
Perrywee 93.5% 97.3% 130.3% 90.4%
Each will allow drilling down to the group below it and the value is calculated by the sum of sales for the group divided by the sum of goals for the group. As there are 14 periods and 8 groups I want to avoid having to create ~115 formuals.
Is there an easy way to accomplish this?
I've done something similar but not using Percentages. The way I thought it out is almost painful to do, but you will have drilldown capabilities. I'll post what I've come up with but if someone can modify this to make it easier, by all means.
First, create the 8 groups you want use and hide them. You will need 14 formulas to create columns for your periods. For your first period, the formula (and following periods) should look like this:
IF {table.period} = "01-2012" THEN
IF DrillDownGroupLevel = 0 THEN
SUM({table.sales}, {table.group1}) / SUM({table.goal}, {table.group1})
ELSE IF DrillDownGroupLevel = 1 THEN
SUM({table.sales}, {table.group2}) / SUM({table.goal}, {table.group2})
ELSE IF DrillDownGroupLevel = 2 THEN
SUM({table.sales}, {table.group3}) / SUM({table.goal}, {table.group3})
ELSE IF DrillDownGroupLevel = 3 THEN
SUM({table.sales}, {table.group4}) / SUM({table.goal}, {table.group4})
...
ELSE
SUM({table.sales}, {table.group8}) / SUM({table.goal}, {table.group8})
ELSE
0
What this formula does is identify what period it is, then determine what group level it is on. It then Sums up the Sales and Goal considering what the group is suppose to be on that level.
For line IF {table.period} = "01-2012" THEN, I'd set up as a parameter so it will work for multiple years.
I would rather use a function to determine what group level I was on, but all I know is DrillDownGroupLevel. It is useful but flawed if you try to do something fancy with the groups (conditional surpression) don't do a simple drilldown.
Change {table.group1} to what ever field you used for that group level. i.e. {table.company}
A drawback on this formula is every time you drill down, the DrillDownGroupLevel will change. The new level you reveal will be correct, but previous levels that still show will change.
Hope this helps.
Steps:
insert a cross-tab; company for row totals; period for column totals (set group options to 'for each month'); add sales and goal to summarized fields
preview report
right click a goal cell, select 'Embedded Summary', then 'Insert Embedded Summary'
switch to design mode (cross-tab expert); ensure order (top to bottom) of fields are #Embedded Summary, Sum of Sales, Sum of Goal
go to preview mode; right click Embedded Summary field and choose 'Edit Calculation Formula'; add the following:
local numbervar n:=GridValueAt (CurrentRowIndex, CurrentColumnIndex, 1);
local numbervar d:=GridValueAt (CurrentRowIndex, CurrentColumnIndex, 2);
if d<>0 then n/d*100
suppress the sales and goal fields
I haven't (yet) found a way to remove the extra space, however.
Sample: