I have two tables one containing the details of the user and another containing the details of user's tasks. I need to return one row for each user containing the username and the task ids.
The query I have is as follows
select
w.user
, (select '; ' +taskid from tasks d where d.guid = w.guid FOR XML PATH(''))
from taskuser w
where w.type = 'pm0'
group by w.user, w.tid
The output I am getting is
username
TaskId
Frane
PM0-10003
Jordan
PM0-10004
Jordan
PM0-10005
Jordan
PM0-10006
What I want is
Username
TaskId
Frane
PM0-10003
Jordan
PM0-10004, PM0-10005, PM0-10006
Related
I have a database which has two tables, namely:
Boats
user_id|boat_id|sails
1 1 <sails><id135986><sailinfo>A2</sailinfo><sailtype>Main</sailtype></id135986><id185764><sailinfo>#3</sailinfo><sailtype>Jib</sailtype></id185764></sails>
1 2 <sails><id10230><sailinfo>A2</sailinfo><sailinfo>Main</sailinfo></id10230><id20000><sailinfo>#5</sailinfo><sailinfo>Genoa</sailinfo></id20000></sails>
2 3 <sails><id43567><sailinfo>A2</sailinfo><sailinfo>Main</sailinfo></id43567><id24503><sailinfo>#5</sailinfo><sailinfo>Genoa</sailinfo></id24503></sails>
Records
user_id|boat_id|location |sails
1 1 San Francisco <sails><id135986>id135986</id135986><id185764>id185764</id185764></sails>
1 2 Chicago <sails><id10230>id10230</id10230></sails>
1 2 Chicago <sails><id20000>id20000</id20000></sails>
1 2 New York <sails><id10230>id10230</id10230><id20000>id20000</id20000></sails>
2 3 Bermuda <sails><id43567>id43567</id43567></sails>
The idea behind this structure is that if the records "sailinfo" and "sailtype" are updated in the table Boats, the Records table is unaffected because it is linked to the other one by the id tag.
Now, I have an application where the user can choose a sailinfo (e.g. A2) and based on this input a query should be generated to retrieve the location where that particular sailinfo has been used. The results should be dispayed in tabular format where the first column ("Location") contains the relevant locations and the second column should have the "sailtype" associated with "A2" as header and A2 as entry. For instance, suppose the user with user_id=2 inputs "A2" for boat_id=2, this is what should be returned:
location | Jib
Chicago A2
New York A2
I have tried using the following SQL statement but it didn't work:
DECLARE #boat_id VARCHAR(50);
SET #boat_id = (SELECT Boats.sails.value('local-name(/sails[1]/*[1])','nvarchar(50)')
FROM Boats
WHERE boat_id = 88
AND Boats.sails.value('(/sails/*/sailinfo)[1]', 'nvarchar(50)') = 'A2');
DECLARE #record_id varchar(50);
SET #record_id = (SELECT Records.sails.value('local-name(/sails[1]/*[1])', 'nvarchar(50)')
FROM Records
WHERE boat_id = 2
AND Records.sails.value('local-name(/sails[1]/*[1])', 'nvarchar(50)') = #boat_id );
SELECT
[location], Boats.sails.value('(/sails/id10230/sailinfo)[1]', 'nvarchar(50)') AS 'Jib'
FROM
Boats
FULL JOIN
Records ON Records.sails.value('local-name(/sails[1]/*[1])', 'nvarchar(50)') = #boat_id
WHERE
Boats.boat_id = 2;
Of course since I am picking from table Boats the above returns NULL for "location" and just a single row for "Jib":
Sample query result
I hope the above description is clear enough.
Your help would be greatly appreciated!
That statement does not work at first because in your data you do non have 'boat_id = 88' and then you do not have 'id28108'
Code below returns what you want:
SELECT
[location], Boats.sails.value('(/sails/id10230/sailinfo)[1]', 'nvarchar(50)') AS 'Jib'
FROM
#tbl_Boats as Boats
FULL JOIN
#tbl_Records as Records
ON Records.sails.value('local-name(/sails[1]/*[1])', 'nvarchar(50)')
= Boats.sails.value('local-name(/sails[1]/*[1])','nvarchar(50)')
WHERE Records.sails.value('(/sails/id10230)[1]', 'nvarchar(50)') is not null;
P.S. Something looks wrong. Try to store some values in columns, outside of XML. That will work much faster and will be easier to understand/troubleshoot.
I have a table that looks as follows:
Grade Name
______ ________
1 John
2 Jerry
3 Jack
4 Ben
5 Bill
I have a form with two combo boxes named Combo1 and Combo2. Combo1 has three values- 1,2,3. Combo2 has three values - John, Jerry, All
And I set up the following query:
Select *
From Persons
WHERE
Persons.ID > Forms![Form1]![Combo1]
AND (Persons.Name = Forms![Form1]![Combo2]) OR Forms![Form1]![Combo2]= 'All');
I want the user to be able to select either one or all the Names based on the criteria for Grades but it is not giving me the desired result.
Try this minor modification:
Select
*
From
Persons
WHERE
Persons.ID > Forms![Form1]![Combo1]
AND
(Persons.Name = Forms![Form1]![Combo2] OR Forms![Form1]![Combo2]= 'All');
I work on a help-desk and am doing some analysis of PC repair tickets.
I am needing to dump data from our call log system that returns history of tickets for issues on computers where they were recently repaired by another team. We are simply trying to improve QA on deployed machines and this data will help.
I have the query for the analysis of tickets, but I am wanting to return the ticket number of the last PC repair case.
My current query is as follows:
SELECT
CallLog.CallID,
CallLog.CustID,
Subset.Rep_num,
Subset.FirstName,
Subset.LastName,
CallLog.OpndetailCat,
CallLog.Tracker_Full,
CallLog.RecvdDate,
FROM
heatPrd.dbo.CallLog CallLog,
heatPrd.dbo.Subset Subset
WHERE
CallLog.CallID = Subset.CallID AND
CallLog.RecvdDate>='2015-10-01' AND
CallLog.OpnAreaCat='back from repair'
ORDER BY
CallLog.CallID DESC
This returns
CallID CustID Rep_num FirstName LastName OpndetailCat Tracker_Full
2182375 1234 Sarah Doe Missing Email Folde
2181831 1235 JENNIFER Doe ZOTHER
2180815 1236 123 Jason Smith ZOTHER
2180790 1237 124 DARCY Doe Wrong Proxy Config
2180787 1239 125 Jason Smith ZOTHER
I want to add a column to the query that would return something to the effect of
select max(callid)
from calllog
where calltype = 'in_for_service_pc' and custid = '1234'
where calltype = 'in_for_service_pc' resides on the CallLog table and custID would pull from the query result.
This is a lot of info so i hope my request is clear.
Disclaimer: Data resides in SQL Server 2000 so some of the newer commands may not work.
Something like this should be pretty close.
SELECT
cl.CallID,
cl.CustID,
s.Rep_num,
s.FirstName,
s.LastName,
cl.OpndetailCat,
cl.Tracker_Full,
cl.RecvdDate,
x.MaxCallID
FROM heatPrd.dbo.CallLog cl
JOIN heatPrd.dbo.Subset s ON cl.CallID = s.CallID
left join
(
select max(cl2.callid) as MaxCallID
, cl2.custid
from calllog cl2
where cl2.calltype = 'in_for_service_pc'
group by cl2.custid
) x on x.custid = cl.custid
WHERE cl.RecvdDate >= '2015-10-01' AND
cl.OpnAreaCat = 'back from repair'
ORDER BY cl.CallID DESC
I'm currently working on a query where I need to pull a list of items, as well as the date they were entered into the system and the name and user ID of the person who entered them.
My Items table contains the columns CreateDate, CreatedBy (this is the person's user ID), LastChanged (this is a date), and LastChangedBy (also a user ID, being as the person who entered the item isn't always the same person who created it).
I have the Items table joined with a User table so I can pull the first and last name of the users who match the CreatedBy and LastChangedBy user IDs. The way I have the query written now, I've concatenated the two fields so they end up displayed as "LastName, FirstName" in a single column. I also want to only pull results for a certain user, and this user just needs to be the last person who changed the item - it doesn't matter whether or not they created it.
My query looks something like this:
SELECT
i.ItemName, i.CreateDate,
(u.LastName + ', ' + u.FirstName) as [Created By Name],
i.CreatedBy, i.LastChanged,
(u.LastName + ', ' + u.FirstName) as [Last Changed By Name],
i.LastChangedBy
FROM
Items i
JOIN
Users u ON i.CreatedBy = u.UserID
WHERE
i.LastChangedBy = 0001;
My issue though is that since I have the exact same piece of code to pull the user's name both times, it's pulling the same name regardless of whether the user IDs are different in the CreatedBy and LastChangedBy columns. So I end up with results that look like this:
ItemName CreateDate Created By Name CreatedBy LastChanged Last Changed By Name LastChangedBy
Item A 12/01/2015 Smith, John 0001 12/03/2015 Smith, John 0001
Item B 12/02/2015 Smith, John 0001 12/04/2015 Smith, John 0001
Item C 12/02/2015 Doe, Jane 1002 12/05/2015 Doe, Jane 0001
So even though John Smith was the last person to change Item C, it's still displaying Jane Doe's name there because (I assume) the strings of code to display the user's name is the same in both instances and it's just pulling the same name both times.
Is there a way to pull the first and last names from the User table twice in the same query but make sure they correspond with the correct user IDs?
I think you are missing a join in your query:
SELECT i.ItemName, i.CreateDate,
(uc.LastName + ', ' + uc.FirstName) as [Created By Name],
i.CreatedBy, i.LastChanged,
(ucb.LastName + ', ' + ucb.FirstName) as [Last Changed By Name],
i.LastChangedBy
FROM Items i LEFT JOIN
Users uc
ON i.CreatedBy = uc.UserID LEFT JOIN
Users ucb
ON i.LastChangedBy = ucb.UserId
WHERE i.LastChangedBy = 0001;
You need to join twice to get both users.
This Query Dose NOT work in Access 2000
SELECT (Members.First_Name + " " + Members.Last_Name)AS Member,
(SELECT Friend_E_Mail,
FROM Friends,Members WHERE My_E_Mail = ? and Friend_E_Mail <> ?
UNION ALL
SELECT My_E_Mail,FROM Friends,Members
WHERE Friend_E_Mail = ?and My_E_Mail <> ?) AS E_Mail ,
(Members.First_Name) AS Name
FROM Members,Friends
Sample Tables
Members(all VARCHAR) SOME DATA
First_Name Alester Jude Carl Jones
Last_Name A B C J
FRIENDS(ALL VARCHAR)
My_E_Mail Alester#lam.com Alester#lam.com Alester#lam.com
Friend_E_Mail jude#lam.com carl#lam.com jones#lam.com
Desired Output if ("?" in above query is: jones#lam.com)
+--------------+-----------+------------+
|Member |E_Mail | Name |
+---------------------------------------+
Alester A Alester#lam.com Alester
Desired Output if("?" in above query is: Alester#lam.com)
+--------------+-----------+------------+
|Member |E_Mail | Name |
+---------------------------------------+
Jude B jude#lam.com Jude
carl C carl#lam.com Carl
Jones J jones#lam.com Jones
PS the "?" are query string parameters that im passing in the "?" i know that works fine.
MY QUESTION IS : i keep getting this error "Operation Not Allowed in Sub Query"
is their a work around query i can use without using a stored procedure or using multiple queries since this needs to be ONE SINGLE QUERY!?
Thanks.
You can´t return more then 1 row in a select subquery.
To this case you have to use the CASE expression.
You have to do something like this:
SELECT (m.First_Name + " " + m.Last_Name) AS Member,
case
when f1.my_email is null
then f2.my_e_mail
else f1.friend_email
end as email,
m.First_Name AS Name
from members m
left outer join friends f1
on m.email = f1.my_e_mail
and f1.friend_e_mail = ?
left outer join friends f2
on m.email = f2.friend_e_mail
and f2.my_e_mail = ?;
Thys way it will return a row for each friend.
You can see that the names of the fields are not so good, try to refactory it.
my_e_mais isn´t so representative.