I have a database where a userID exists multiple times in a table, data structure is like so
userid event rank
mm11 hurdles first
mm11 sock fourth
mm11 three fifteen
rr99 run seven
rr99 swim second
In access, I want to return only one row per userID and it does not matter how the event data is parsed. I tried to concatenate the event data but my query still returns an individual row for each userid.
This is the SQL from my access query - what should I change so that I only have one row per userID returned?
Select userID, [event] & ", " & [rank] As [TestConcat]
From eventdata
Group by userID;
EDIT
My desired returned results are like this
auserid eventInfo
mm11 hurdles, first, sock, fourth, three, fifteen
rr99 run, seven, swim, second
Do you need anything other than the userID? If not what is below will return the userIDs you are after.
SELECT userID
FROM eventdata
GROUP BY userID
ORDER BY userID;
Related
I have created a members SQLite table that, when queried, only returns the first row. The table has 11 fields, most are TEXT and 9 and 10 are DATE. Here is a snapshot of the data:
https://i.stack.imgur.com/5NYqC.png
The following view query, along with many simpler variations of it, only returns the first record:
CREATE VIEW test_monthly_filter AS
SELECT first_name,
last_name,
email,
join_date,
type_code,
status,
nac_num,
nac_bill,
change_date,
comments
FROM member
WHERE status = 'A' OR
change_date = '10/15/18';
Any ideas on what is causing this? Thanks!
You have data quality issues, if status = 'A' only returns one row, that means only one row satisfies the criteria of status = 'A'.
You can try something like:
WHERE status LIKE '%A%'
If that returns the rows you are interested in that means you probably have leading or trailing spaces.
I am using SQL Server and I have one table generated I just need to create another table from the below generated table that will have the following details:
Price (total price of the repeated UserId)
Number (Unique number with respect to UserId)
UserId (unique)
Please ignore first column it is repeated. Consider it as a one column i.e. only one data of 67.
After a while I have read and got the solution.
It's simple
Select sum(Price) as Price, Number, u.UserId
from the table Group by UserId, Number
there are two tables 1) participant and 2) logindatetime.
for first time login datetime value along with other user data like,Name, location, contact number, email gets inserted into participant table having datatime column...for any subsequent login of the same user we insert datetime value into logindatetime column to keep the records of how many times the user logged in....now i have to show all the login time (first login time and subsequent login time) in a single column along with name, location, contact I number, email of the same user.
(I do have an identity in participant table).
Have tried following query:
select a.firstname as 'Name', a.Email as 'Email', a.Address1 as 'Location',
a.MobileNo as 'Contact', COALESCE(a.datetime, b.datetime) as DateTime
from eventonline.participant a, eventonline.logindatetime b
where a.Id = b.Rid";
but it show first login time multiple times.
You need to do something like this to fetch the first and then the other logons separately:
select a.firstname as Name, a.Email, a.Address1 as Location,
a.MobileNo as Contact, a.datetime
from eventonline.participant a
union all
select a.firstname as Name, a.Email, a.Address1 as Location,
a.MobileNo as Contact, b.datetime
from eventonline.participant a
join eventonline.logindatetime b on a.Id = b.Rid
It might be easier just to add the first logon to logindatetime
JamesZ's answer gives the solution but a further note on why your approach isn't working. You're joining the two tables and using coalesce(a.DateTime, b.DateTime) to display login time. If the user has logged in before, a.DateTime has a non-null value. coalesce(x,y) only uses y if x is null. But that's not the real problem. The first login-time needs a record of its own in the logindatetime table. If the users logs in 5 times, your logindatetime will have 4 rows and that's all you'll see when joining the two tables. You need to either save the first login time as a row in logindatetime, or use a UNION to force that first login time to be added as an extra row.
I faced the same issue but according to James Z's answer I solved my problem:
select a.start_date as start, a.end_date as end, a.rooms_id as roomid from maintenance a UNION ALL SELECT b.check_in as start, b.check_out as end, b.room_id AS roomid from reservations b
The result of the query from both tables are here:
You could use joins in your sql statement, i just learnt them 3 days ago and they are very useful!
it would look something like this:
SELECT * FROM table1
LEFT JOIN table2 ON table1.column1 = table2.column1
WHERE table1.column1 = '$yourVar';
table1.column1 could be the id, and the corresponding id in the second table can be the id that links it to the first table (Foreign key), it will retrieve the data of the first table and bring all the data of the second table as well that would match the on the ON criteria
I have this table
sessionid | serviceid | requesttimestamp | etc..
My aim is to get a serviceid for logging in and logging out which for arguments sake we will say is 2 and 3. So when the two records have a log in and log out then I want to make sure the sessionid is the same and then datediff the timestamp in order to find out how long the user have been logged in. I also want a condition that if the serviceid for logging in exists for one and not exists for logging out then to skip that record in the results. I wondered if there was a magic way to combine this in one query, so far i have
Declare #sessionStart DATETIME , #sessionStart2 as DATETIME
set #sessionStart = (select requesttimestamp from logentry where serviceid=151 and sessionid = '1234')
set #sessionStart2 = (select requesttimestamp from logentry where serviceid=202 and sessionid = '1234')
select datediff(mi, #sessionStart, #sessionStart2 );
This brings me back the required result without the null checking, I have been looking into simple case statements but I am attempting to make this into one query if possible. Any pointers in the right direction would be great
Kinda confusing explanation there. :) But to rephrase, the way I understand it is you just want the total minutes for each session's logon time. Is your sessionid unique? Also, you've tagged MYSQL and SQL Server, while your syntax is SQL Server.
IE, something like this, then?
SELECT L1.sessionid, DATEDIFF(MI, L1.requesttimestamp, L2.requesttimestamp) LOG_TIME_IN_MINUTES
FROM logentry L1
JOIN logentry L2 ON L2.sessionid = L1.sessionid
WHERE L1.requesttimestamp IS NOT NULL AND L2.requesttimestamp IS NOT NULL
AND L1.serviceid = 2 AND L2.serviceid = 3
I've a scenario in Sql where I've following schema.
If I have 3 items in the Item table then one unique combination of all the items will be assigned to a user. For ex:
Items:
1
2
3
Then combinations will be: {1}, {2}, {3}, {1,2}, {1,2,3}, {1,3}, {2,3) all are unique combinations.
Any of these combination will be assigned to a single user.
Now I want to find out given combination belongs to which user, how can I find that? For ex: I'll pass items list {2,3} then it should return the userid who is having that combination from the table UserItemCombinations. {2,3} is passed as comma separated string to a SP. I've taken 3 items as example, this table may contain n number of items. Users number will be dependent on the number of combinations. For ex: For three items there are 7 combinations so 7 users will be there user table.
UserItemCombinations will have one row for each user-item, and one user can have only one combination, so if the query combination is {2,3}
select userid from user where userid not in
(select distinct userid from UserItemCombinations where itemid in
(select itemid from item where itemid not in (2,3));
If there's not too much updates in UserItemCombinations and the performance of the desired query is critical enough, you would make additional field in User table, i.e. Items and create SP that fills in those values per every user. Stored proc will select sorted items per each user in loop and concatenate them into one string to put in User.Items field. You can also make trigger on UserItemCombinations for INSERT, UPDATE, DELETE and recalc the value again.
You may also create index on that field.
select userid from UserItemCombinations where itemid in (2,3) and itemid not in
(select itemid from Item where itemid not in (2,3));