|Gdn_Database|Query|select * from GDN_Permission Permission where RoleID = :RoleID and JunctionTable is null order by RoleID asc limit 1
I don't know how to solve this, I'm not a programmer and the host I use is 000webhost
I tried to see in phpmyadmin or something like that but I didn't understand anything
Related
I am making a suggestions application in relation to school.
I have a table with users and a table with suggestions that share the key userId.
Every suggestions has the unique key of sugId and a duplicateable userId.
How can I select all the entries from the user with the most entries?
I resolved my issue with a small hack.
select * from suggestions
where userId = (
select userId from suggestions
group by userId
order by count(sugId) desc limit 1
)
order by TimeStamp asc;
Just posting it here in case it is needed.
**edit
sugId had to be userId
I want to see which user has received the most highfives using a SQL query. My table looks like following, id | uid | ip. Now, I want to count the amount of rows a uid has, but it has to be unique with the ip. So nobody can give multiple highfives to a person.
I searched around online, and I couldn't find anything about this. If anyone could help me with this, I would be grateful.
you can try like below
select ip, count(distinct uid) from table t
group by ip
SELECT uid,COUNT(ip)NoOfVotes FROM
(SELECT uid,ip,Serial=ROW_NUMBER() OVER(PARTITION BY ip,uid ORDER BY uid) FROM
dbo.tbl_user)A
WHERE Serial=1
GROUP BY uid
I think this will give you perfect vote counting. Using Row Number actively remove duplicates from same ip,same uid. Voting to multiple uid from same ip is allowed in this query.
The Mistake
Originally, there was a one-one relationship between Orgs and Servers where the key of Server was simply an OrganizationId, well this was a pretty bad design as business logic changed and now multiple Orgs can have the same server. Before we made the changes, we just duplicated servers for each org, so multiple Orgs would have Servers with the same Subdomains. Below is the current setup.
Requirements
First off, this is unfortunately on prod with a lot of data, so deleting the whole database with the correct model is kind of off the table.
What we would like to do is now remove duplicate Servers on distinct Subdomains, for example if Org1 and Org2 had Ser1 and Ser2 both with the subdomain "test", we would make the FK Org.Server_Id be the lowest occurrence of a server with that domain, in this case Ser1, so that for both Org1 and Org2 their servers would be Ser1. Below is a high tech excel example:
Things we have tried
We were able to get as far as getting Org.Server_Id to be the correct value based on Server.OrganizationId via:
UPDATE Organization
SET Server_Id = t.Id
FROM(
SELECT Id, OrganizationId
FROM Server
) t
WHERE t.OrganizationId = Organization.Id
but whenever we try and go further, we get stuck because we cant use ORDER BY in the inner FROM to try and grab the first occurrence in some aggregate way.
This is finally what we got to, but of course it doesn't work because we cant access t inside the inner from, and I also don't think this is even the correct path to be following:
UPDATE Organization SET Server_Id = t.Id
FROM
(
SELECT Id, OrganizationId
FROM (
SELECT TOP(1) Subdomain, Id, OrganizationId
FROM Server
WHERE Subdomain = t.Subdomain
) a
) t
WHERE t.OrganizationId = Organization.Id
I can't say I completely understand everything you have going on, but in the past when I have needed to grab the first entry of duplicate information I used a Partition function in the inner query. I don't know how you want to order the results but it would look something like this:
(
SELECT ROW_NUMBER() OVER (PARTITION BY column1, column2, etc... ORDER BY columnX DESC/ASC) As row_num, Id, OrganizationId
FROM Server
) t
WHERE t.OrganizationId = Organization.Id AND row_num = 1
That would be essentially the same thing as what you tried to do in your second code block (I believe). The column1 and column2 would be the set of columns of duplicate data that you want to collapse into one entry and columnX would be the column to order the results by. By having row_num = 1 in the WHERE statement, you would only get back the first result for each unique column1, column2, etc.. combo from the inner query.
After using the partition suggested by #user2731076, we were able to modify our query to this:
UPDATE Organization SET Server_Id = t.Id
FROM
(
SELECT ROW_NUMBER() OVER (PARTITION BY Subdomain ORDER BY [Server].Id ASC) As row_num, [Server].Id, OrganizationId, Subdomain
FROM Organization
INNER JOIN [Server] ON [Server].OrganizationId = [Organization].Id
) t
WHERE t.Subdomain IN(SELECT Subdomain FROM Server WHERE OrganizationId = Organization.Id) AND row_num = 1
The issue we had with our code:
t.OrganizationId = Organization.Id
Was that since there was always a Server associated with an Org, it would just set the value of Org.Server_Id to what it was already set to. So what we wanted to find the first instance of is the row_num = 1 of the Server that had a subdomain similar to the subdomain of the current Org's server. This required the Inner Join to grab it from the partition, and to grab it from the current org via the IN statement in the WHERE clause, so we could do t.Subdomain = subdomain for our Org.
There is probably a more efficient way to do this, and we will look into it in the future.
I am designing a web application and I need to give the administrators and moderators the right to allow and deny other users access to the application. I am thinking of having a table with the following columns:
OperationType (Ban / Access regained).
BannedUser
User (admin/mod that gave access or banned another user)
EventDate
Reason (optional)
I can just have a table, storing all banned users, but I want to keep track of what is actually happening in the app and make sure that the administrators and moderators are not misbehaving as well.
So, If my table doesn't include an OperationType column, a list of all the banned users could be retrieved as simple as writing the following query:
select BannedUser from UserBan;
But if I leave the table with an OperationType column, as shown above, my simple select query could become something like this:
select o3.BannedUser
from
(
select o1.BannedUser, max(o1.EventDate) EventDate
from UserBan o1
group by o1.BannedUser
) o2, UserBan o3
where o3.EventDate = o2.EventDate and
o3.BannedUser = o2.BannedUser and
o3.OperationType = 1
Assume that OperationType = 1 is ban.
So, can someone give me a better solution for my case? :)
I have a some issues with a SQL query I'm working on, I'm sorry that I don't have any work-in-progress to show because nothing that I have tried until now have worked out too well, so I am hoping that someone will be able to point me in the right direction.
Tables:
Computers:
[SN][PN][ComputerName][Model][OS][Architecture][RAM][CPU]
Logons:
[SN][Username][Timestamp]
Info:
It works this way, every time a user logs on to a computer the computer info gets updated to the computer table and the username and timestamp gets inserted to the logons table.
Result
The result I am trying to acheive is the following:
[SN][PN][ComputerName][Model][OS][Architecture][RAM][CPU]**[Primary User]**
It should be only one row for each computer
The Primary User field should be based from the 5 latest logons and being the username with the most recurrences in those 5.
So I think that wraps It up, I hope someone here is able to at least point me in the right direction as every result google have to offer now show up as red.
It's a bit RBAR but something like the following should do it.
SELECT [SN],
[PN],
[ComputerName],
[Model],
[OS],
[Architecture],
[RAM],
[CPU],
O.[Username] AS [Primary User]
FROM Computers C
OUTER APPLY (SELECT TOP 1 [Username]
FROM (SELECT TOP (5) *
FROM Logons L
WHERE L.[SN] = C.[SN]
ORDER BY [Timestamp] DESC) Last5Users
GROUP BY [Username]
ORDER BY Count(*) DESC,
Max([Timestamp]) DESC) O
Looks like you want to define a computed column. Check out this question: Creating a computed column in SQL Server 2008