I want to extract a list o players and a list of clubs where it has played, separated by commas.
SELECT DISTINCT ?playerLabel
(GROUP_CONCAT(?teamLabel ; separator=',') as ?teams)
WHERE {
?player wdt:P106 wd:Q937857 .
?player wdt:P2574 ?team
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
GROUP BY ?playerLabel
I have two problems:
I don't get a list of teams for each player, only the name, and variable ?teams empty.
If I don't use GROUP CONCAT and GROUP BY I obtain the team id, but I prefer the label of team.
For example 2 players...:
playerLabel teams
Cristiano Ronaldo Sporting Portugal, Manchester U, Real Madrid, Juventus, Manchester U
Leo Messi Barcelona, PSG
At least I need the Concat and group by, even with code...
thanks
You use P2574, which is "National-Football-Teams.com player ID". While National-Football-Teams.com lists all teams a player played for, this data is not accessible through the Wikidata Query Service. But Wikidata itself has a dedicated property for sports team member: P54.
So write ?player wdt:P54 ?team instead of ?player wdt:P2574 ?team.
Additionaly, you need to add ?team rdfs:label ?teamLabel . filter (lang(?teamLabel)='en') to be able to use ?teamLabel in GROUP_CONCAT().
Thus, the full working query looks like this (restricted to US players to avoid query time outs):
SELECT DISTINCT ?playerLabel (GROUP_CONCAT(?teamLabel ; separator=',') as ?teams)
WHERE {
?player wdt:P106 wd:Q937857 .
?player wdt:P27 wd:Q30 .
?player wdt:P54 ?team .
?team rdfs:label ?teamLabel . filter (lang(?teamLabel)='en')
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
GROUP BY ?playerLabel
Just like the following example:
// ...
#defgroup group_1 test_group_1
#{
#### title 1 #####
#}
#ref group_1
// ...
I used Doxygen GUI frontend to generate the html page, but it didn't show correctly, just is empty!
I show the another example that has the same problem:
#ref group_1
#defgroup group_1 test_group_1
#{
#### title 1 #####
#}
#ref group_2
#defgroup group_2 test_group_2
#{
#### title 2 #####
#}
And it's no doubt that in my case it only show group_1 reference!
But when I put all the group after the ref,it correctly shows all refs, like the following:
#ref group_1
#ref group_2
#defgroup group_1 test_group_1
#{
#### title 1 #####
#}
#defgroup group_2 test_group_2
#{
#### title 2 #####
#}
I want to put each group together with the corresponding ref just the second example shows, but I don't know why and how to solve!
thanks in advance!
I'm pretty new to the PIVOT function and I have been trying to figure this out for the past day and a half so I thought I would create an account after lurking for so long and just ask.
I have a table with the layout as follows:
AsOfDt AcctNum MntYr Dt Category Count
4/15/2015 12345 Jan-15 1/18/2015 Registered User 1
4/15/2015 12346 Feb-15 2/7/2015 New Registration User 1
4/15/2015 12347 Jan-15 1/27/2015 Unique Account 1
4/15/2015 12348 Jan-15 1/24/2015 Registered User 1
This is the end result I am trying to achieve
MntYr Account Population New Registration User Registered User Unique Account
Jan 2015 330984 12 26212 26311
Feb 2015 331897 2953 58702 58894
Mar 2015 343561 950 29498 29638
Apr 2015 343181 675 8845 8916
Grand Total 1349623 4590 123257 123759
Here is the Query that I currently have built:
WITH BaseQuery AS (
SELECT
MntYr
,Category
,[Count]
FROM [dbo].[rpt_gen_WebPortal_TestingData]
)
SELECT [MntYr]
,'Account Population'
,'Unique Account'
,'Registered User'
,'New Registration User'
FROM BaseQuery
pivot (sum([count]) for MntYr
in ("Jan 2015", "Feb 2015", "Mar 2015", "Apr 2015" )
) AS Pivoting
My first question:
I am getting an error for my MntYr column in the second SELECT statement, "Invalid column name 'MntYr'." I really don't understand why this is throwing an error. What am I doing wrong with trying to pull that column when I explicitly name it in my BaseQuery pull?
My second question:
I would also like to create a calculated field based upon the percentage of (Unique Account / Account Population), but I'm not quite sure how to go about calculated fields in a PIVOT function. Any ideas on how to get started with this one?
Any and all help would be much appreciated!
Thanks.
Your pivot clause was wrong. You also don't need a CTE. Try this:
SELECT
MntYr
,[Account Population]
,[Unique Account]
,[Registered User]
,[New Registration User]
,case
when isnull([Account Population],0) = 0 then 0
else 100 * [Unique Account] / [Account Population]
end Pct
FROM (
SELECT
MntYr
,Category
,[Count]
FROM [dbo].[rpt_gen_WebPortal_TestingData]
) BaseQuery
pivot (sum([Count]) for Category
in ([Account Population]
,[Unique Account]
,[Registered User]
,[New Registration User] )
) AS Pivoting
I very new to SQL Queries and am having issue with Getting MAX of Column Speedo below is my current query
SELECT
tblVehicle.Rego
,tbljob.Speedo
,tbljob.DateEntered
from tblJob
INNER JOIN tblVehicle
on tbljob.Vehicle_ID = tblVehicle.ID
where JobType_ID = 2
order by Rego
Which outputs
Rego Speedo DateEntered
000JKC 147729 2010-05-10 10:56:55.040
000JKC 150145 2010-06-02 13:57:15.470
000JKC 169553 2011-01-06 12:24:09.143
000JKC 155149 2010-07-21 14:58:20.777
000JKC 157882 2010-09-17 16:39:48.480
000JKC 165660 2010-11-29 08:20:39.453
000JKC 164339 2010-11-18 16:19:19.213
000JKC 155149 2010-08-11 06:16:28.180
000RQD 65 2011-11-21 09:27:31.693
000RQD 6978 2012-03-06 15:18:06.987
000RQD 6766 2012-02-24 13:28:47.603
000RQD 12130 2012-08-28 09:29:18.027
000RQD 12145 2012-08-27 14:57:17.330
000RQD 21550 2013-04-15 19:30:30.160
000RQD 23640 2013-05-22 16:16:33.880
Now i would just like the MAX(Speedo) of Each Rego Only.
The MAX() function returns the largest value of the selected column.
SELECT
tblVehicle.Rego
, MAX( tbljob.Speedo )
,tbljob.DateEntered
from tblJob
INNER JOIN tblVehicle
on tbljob.Vehicle_ID = tblVehicle.ID
where JobType_ID = 2
order by Rego
You need to use GROUP BY
SELECT
tblVehicle.Rego
,MAX(tbljob.Speedo) as MaxSpeedo
,MAX(tbljob.DateEntered) as MaxDate
from tblJob
INNER JOIN tblVehicle
on tbljob.Vehicle_ID = tblVehicle.ID
where JobType_ID = 2
order by Rego
GROUP BY Rego
Note: This may not achieve the result you want if the Speedo is wound back, and the last date does not have the maximum for Speedo. In that scenario you will see the maximum speedo, and the maximum date, but they are coming from different rows.
When ever you edit a User that has_one Profile, Active admin DELETE the profile and re create it again, which makes the avatar image to be deleted by paperclip
How can i make active admin just update the nested form without deleting and re-creating it.
here is my ActiveAdmin:User Code
ActiveAdmin.register User do
menu :priority => 2
## Controller
controller do
def update
if params[:user][:password].blank?
params[:user].delete("password")
params[:user].delete("password_confirmation")
end
if params[:user][:profile_attributes][:avatar].blank?
params[:user][:profile_attributes].delete("avatar")
end
super
end
def permitted_params
params.permit user: [:username, :email, :password, :password_confirmation, :approved, role_ids: [], profile_attributes: [:first_name, :last_name, :date_of_birth, :avatar]]
end
end
#Scopes
scope :all, default: true
scope :admins
scope :editors
scope :members
## Filters
filter :username
filter :email
## Index
index do
column :id
column :first_name do |user|
user.profile.first_name unless user.profile.nil?
end
column :last_name do |user|
user.profile.last_name unless user.profile.nil?
end
column :username
column :email
column :role, :sortable => false
default_actions
end
## Form
form :html => {:multipart => true} do |f|
f.inputs "User Account Details" do
f.input :username
f.input :email
f.input :roles
f.input :approved
f.input :password, hint: "leave it blank if you don't want to change it"
f.input :password_confirmation
end
f.inputs "Profile Details", for: [:profile, f.object.profile || f.object.build_profile] do |profile_form|
profile_form.input :first_name
profile_form.input :last_name
profile_form.input :date_of_birth, as: :date_select, start_year: Date.today.year-80, end_year: Date.today.year-13
profile_form.input :avatar, :as => :file, :hint => profile_form.template.image_tag(profile_form.object.avatar.url(:small))
end
f.actions
end
end
and here is what the console shows, notice that it deleted the profile record and re create it again which makes the paperclip gem to delete the already updated image.
Started PATCH "/admin/users/1" for 127.0.0.1 at 2013-08-02 01:29:09 +0200
Processing by Admin::UsersController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"m9tBWsM+Jq0CWmSu2kCxCMn8xV3pgYQ0iFz5K9X5EjY=", "user"=>{"username"=>"admin", "email"=>"admin#example.com", "role_ids"=>["", "", "1", "2"], "approved"=>"1", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "profile_attributes"=>{"first_name"=>"Moe", "last_name"=>"Hassan", "date_of_birth(1i)"=>"1935", "date_of_birth(2i)"=>"4", "date_of_birth(3i)"=>"4", "id"=>"58"}}, "commit"=>"Update User", "id"=>"1"}
User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1
(0.3ms) SELECT COUNT(*) FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id" WHERE "users_roles"."user_id" = $1 AND (((roles.name = 'admin') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL))) [["user_id", 1]]
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", "1"]]
Unpermitted parameters: id
Unpermitted parameters: utf8, _method, authenticity_token, commit, id
Role Load (0.4ms) SELECT "roles".* FROM "roles" WHERE "roles"."id" IN (1, 2)
Role Load (0.3ms) SELECT "roles".* FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id" WHERE "users_roles"."user_id" = $1 [["user_id", 1]]
(0.1ms) BEGIN
(0.1ms) COMMIT
Profile Load (0.2ms) SELECT "profiles".* FROM "profiles" WHERE "profiles"."user_id" = $1 ORDER BY "profiles"."id" ASC LIMIT 1 [["user_id", 1]]
(0.1ms) BEGIN
[AWS S3 200 1.692562 0 retries] head_object(:bucket_name=>"assets.goldpricenetwork.dev",:key=>"public/avatars/58/original-901301_3903423203601_1063661495_o.jpg")
[AWS S3 200 0.305978 0 retries] head_object(:bucket_name=>"assets.goldpricenetwork.dev",:key=>"public/avatars/58/small-901301_3903423203601_1063661495_o.jpg")
SQL (0.3ms) DELETE FROM "profiles" WHERE "profiles"."id" = $1 [["id", 58]]
[paperclip] deleting public/avatars/58/original-901301_3903423203601_1063661495_o.jpg
[AWS S3 204 0.261193 0 retries] delete_object(:bucket_name=>"assets.goldpricenetwork.dev",:key=>"public/avatars/58/original-901301_3903423203601_1063661495_o.jpg")
[paperclip] deleting public/avatars/58/small-901301_3903423203601_1063661495_o.jpg
[AWS S3 204 0.255899 0 retries] delete_object(:bucket_name=>"assets.goldpricenetwork.dev",:key=>"public/avatars/58/small-901301_3903423203601_1063661495_o.jpg")
(2.2ms) COMMIT
(0.1ms) BEGIN
SQL (0.5ms) INSERT INTO "profiles" ("created_at", "date_of_birth", "first_name", "last_name", "updated_at", "user_id") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id" [["created_at", Thu, 01 Aug 2013 23:29:12 UTC +00:00], ["date_of_birth", Thu, 04 Apr 1935], ["first_name", "Moe"], ["last_name", "Hassan"], ["updated_at", Thu, 01 Aug 2013 23:29:12 UTC +00:00], ["user_id", 1]]
(0.3ms) COMMIT
Redirected to http://localhost:3000/admin/users/1
Completed 302 Found in 2552ms (ActiveRecord: 5.8ms)
any solution?
Try permitting :id attribute for profile_attributes
profile_attributes: [:id , ...]
And try remove this monkeypatch :
if params[:user][:profile_attributes][:avatar].blank?
params[:user][:profile_attributes].delete("avatar")
end
I had the same problem. You need to use semantic_fields_for for nested attributes.
Here is my solution:
f.inputs "User Fundamentals" do
...
end
if params[:action] == 'edit'
f.inputs "User Profile" do
f.semantic_fields_for :profile do |profile_form|
profile_form.inputs :first_name,:last_name, etc..
...
elsif params[:action] == 'new'
f.inputs :name => "User Profile", :for => [Profile.new] do |profile_form|
profile_form.inputs :first_name,:last_name, etc..
...
Hope this may solve your problem.