I am creating a generic key/value data store in MS SQL Server. I have a UserDecimalData table defined as follows
create table [UserDecimalData] (
[UserID] nvarchar(64) not null,
[ValueKey] uniqueidentifier not null,
[Value] decimal null
);
alter table [UserDecimalData]
add constraint PK_UserDecimalData primary key clustered ([UserID], [Date], [ValueKey]);
and as an example, here is some data that uses a GUID ending with 0001 to indicate an individual's score in a game, and a GUID ending with 0002 to indicate which team a user is on.
UserID ValueKey Value
Dave 00000000-0000-0000-0000-000000000001 35
Dave 00000000-0000-0000-0000-000000000002 1
Phil 00000000-0000-0000-0000-000000000001 35
Phil 00000000-0000-0000-0000-000000000002 1
Pete 00000000-0000-0000-0000-000000000001 35
Pete 00000000-0000-0000-0000-000000000002 2
I can easily find the score for an individual by searching for their UserID + ValueKey ending with 0001. I can also find out which team an individual is in by searching for their UserID + the value of the row where the ValueKey ends with 0002.
My question is, how can I get a list of team scores? In this case team 1 would have 70 and team 2 would have 35.
Here is one method:
select sum(udd.value)
from UserDecimalData udd
where udd.ValueKey like '%1' and
exists (select 1
from UserDecimalData udd2
where udd2.ValueKey like '%2' and
udd2.UserId = udd.UserId
);
select v1.Value as Team, sum(v2.value) as TeamScore
from userdecimaldata v1
join userdecimaldata v2 on v2.UserId = v1.UserId and v2.[Date] = v1.[Date]
where v1.ValueKey = #clankey and v2.ValueKey = #scoreKey
group by v1.Value
OUTPUT
Team TeamSccore
1 70
2 35
--first way
with commands as (
select UserID as UserID, Value as command
from UserDecimalData
where ValueKey like '%2'
)
select c.command, sum(Value)
from UserDecimalData u
left join commands c on u.UserID = c.UserID
where ValueKey like '%1'
group by command
GO
--second way
select c.command, sum(Value)
from UserDecimalData u
left join (
select UserID as UserID, Value as command
from UserDecimalData
where ValueKey like '%2'
) c on u.UserID = c.UserID
where ValueKey like '%1'
group by command
GO
--results
--1 70
--2 35
select u1.Value, sum(u0.Value)
from UserDecimalData u1
left join UserDecimalData u0 on u0.UserID = u1.UserID and u0.ValueKey like '%1'
where u1.ValueKey like '%2'
group by u1.Value
GO
Related
My Tables look like this
Table 1 Table 2
Users Options
id name id user_id option
------- --- -------- --------
1 Donald 1 1 access1
2 John 2 1 access2
3 Bruce 3 1 access3
4 Paul 4 2 access1
5 Ronald 5 2 access3
6 Steve 6 3 access1
Now, i want to select join these to find a user which has only access1
If i do something like
select t1.id,t1.name,t2.id,t2.user_id,t2.option
from table1 t1, table2 t2
where t1.id=t2.user_id
and option='access1';
This does not give me unique results, as in the example i need only user_id=3 my data has has these in hundreds
I also tried something like
select user_id from table2 where option='access1'
and user_id not in (select user_id from table2 where option<>'access1')
There have been other unsuccessful attempts too but i am stuck here
You can do this using a EXISTS subquery (technically, a left semijoin):
SELECT id, name
FROM table1
WHERE EXISTS(
SELECT * FROM table2
WHERE table1.id = table2.user_id
AND table2.option = 'access1'
)
If you want only users that have access1 and not any other access, add NOT EXISTS (a left anti-semi-join; there's a term to impress your colleagues!):
AND NOT EXISTS (
SELECT * FROM table2
WHERE table1.id = table2.user_id
AND table2.option <> 'access1'
)
bool_and makes it simple
with users (id,name) as ( values
(1,'donald'),
(2,'john'),
(3,'bruce'),
(4,'paul'),
(5,'ronald'),
(6,'steve')
), options (id,user_id,option) as ( values
(1,1,'access1'),
(2,1,'access2'),
(3,1,'access3'),
(4,2,'access1'),
(5,2,'access3'),
(6,3,'access1')
)
select u.id, u.name
from
users u
inner join
options o on o.user_id = u.id
group by 1, 2
having bool_and(o.option = 'access1')
;
id | name
----+-------
3 | bruce
If you want the user that has only access1, I would use aggregation:
select user_id
from table2
group by user_id
having min(option) = max(option) and min(option) = 'access1';
WITH users(id,name) AS ( VALUES
(1,'Donald'),
(2,'John'),
(3,'Bruce'),
(4,'Paul'),
(5,'Ronald'),
(6,'Steve')
), options(id,user_id,option) AS ( VALUES
(1,1,'access1'),
(2,1,'access2'),
(3,1,'access3'),
(4,2,'access1'),
(5,2,'access3'),
(6,3,'access1')
), user_access_count AS (
SELECT op.user_id,count(op.option) AS access_count
FROM options op
WHERE EXISTS(
SELECT 1 FROM options
WHERE option = 'access1'
)
GROUP BY op.user_id
)
SELECT u.id,u.name
FROM users u
INNER JOIN user_access_count uac ON uac.user_id = u.id
WHERE uac.access_count = 1;
We can have users with multiple valueIDs. In SQL Server I am trying to pull all users that have valueID of 3 but do not have valueID of 1 and 2. So Table A would have a user column and a valueID column.
How do I write a SQL query to do this?
Also, any good resources to get my SQL query skills up to par?
Much appreciated.
Try:
SELECT user
FROM A outer
WHERE valueID = 3
AND NOT EXISTS
(
SELECT 1
FROM A inner
WHERE
(
valueID = 1
OR valueID = 2
)
AND inner.user = outer.user
)
Here's one more.... (all users with valueid = 3 and nothing else)
SELECT [USER]
FROM A
GROUP BY [USER]
HAVING MAX(CASE WHEN valueid = 3 THEN 0 when valueid IN (1,2) THEN 1 END) = 0;
This query will return all users that have values (3 and 1) or (3 and 2) or just 3
select user
from mytable t1
where valueId = 3
and (select count(distinct valueId)
from mytable t2 where t2.user = t1.user
and valueId IN (1,2)) < 2
A simple way to get what you asked if you meant users with a value of 3 except those which also have values of 1 and 2
Select s.UserID From SomeTable s
Where s.UserID NOT In
(select s1s.UserID From SomeTable s1s
inner join SomeTable s2s On s1s.UserID = s2s.UserID
Where s1s.ValueId = 1 And s2s.ValueId = 2)
Or
Select s.UserID From SomeTable s
Left Join
(select s1s.UserID From SomeTable s1s
inner join SomeTable s2s On s1s.UserID = s2s.UserID
Where s1s.ValueId = 1 And s2s.ValueId = 2) both
Where both.userID is null
I'm not really sure what your table structure is, but it's likely the below query should work depending on column names...
SELECT user, valueID
FROM users
WHERE valueID = 3
I have a rather confusing SQLite query that I can't seem to quite wrap my brain around.
I have the following four tables:
Table "S"
sID (string/guid) | sNum (integer)
-----------------------------------
aaa-aaa 1
bbb-bbb 2
ccc-ccc 3
ddd-ddd 4
eee-eee 5
fff-fff 6
ggg-ggg 7
Table "T"
tID (string/guid) | ... other stuff
-----------------------------------
000
www
xxx
yyy
zzz
Table "S2TMap"
sID | tID
-------------------
aaa-aaa 000
bbb-bbb 000
ccc-ccc xxx
ddd-ddd yyy
eee-eee www
fff-fff 000
ggg-ggg 000
Table "temp"
oldID (string/guid) | newID (string/guid)
------------------------------------------
dont care fff-fff
dont care ggg-ggg
dont care zzz
What I need is to be able to get the MAX() sNum that exists in a specified "t" if the sID doesn't exist in the temp.NewID table.
For example, given the T '000', '000' has S 'aaa-aaa', 'bbb-bbb', 'fff-fff', and 'ggg-ggg' mapped to it. However, both 'fff-fff' and 'ggg-ggg' exist in the TEMP table, which means I need to only look at 'aaa-aaa' and 'bbb-bbb'. Thus, the statement would return "2".
How would I go about doing this?
I was thinking something along the lines of the following for selecting s that don't exist in the "temp" table, but I'm not sure how to get the max of the seat and only do it based on a specific 't'
SELECT s.sID, s.sNum FROM s WHERE NOT EXISTS ( SELECT newID from temp where tmp.newID = s.sID)
Thanks!
Give this a try:
select max(s.sNum) result from s2tmap st
join s on st.sId = s.sId
where st.tId = '000' and not exists (
select * from temp
where temp.newId = st.sId)
Here is the fiddle to play with.
Another option, probably less efficient would be:
select max(s.sNum) result from s2tmap st
join s on st.sId = s.sId
where st.tId = '000' and st.sId not in (
select newId from temp)
The following query should give you a list of Ts and their max sNums (as long as all exist in S and S2TMap):
SELECT t.tID, MAX(sNum)
FROM S s
JOIN S2TMap map on s.sID=map.sID
JOIN T t on map.tId=t.tID
LEFT JOIN temp tmp on s.sID=tmp.newID
WHERE tmp.newID IS NULL
You were close, you just had to join on S2TMap and then to T in order to restrict the result set to a given T.
SELECT MAX(s.sNum)
FROM s
INNER JOIN S2TMap m on m.sID = s.sID
INNER JOIN t on t.tID = m.tID
WHERE t.tID = '000'
AND NOT EXISTS (
SELECT newID FROM temp WHERE temp.newID = s.sID
)
I am wanting to return columns representing various users for a CSV file for part of our database.
The base tables look something like this:
ProviderId ProviderName
1 Test
2 FooBar
UserId UserName
1 Mike
2 Bob
3 John
ProviderId UserId
1 1
2 2
1 3
I want the results to look like this:
ProviderName UserName1 UserName2 ...
Test Mike John
FooBar Bob
So far, I tried using pivot for this, but it only supports Ints (and thus only the Ids):
SELECT ProviderId, [1],[2],[3],[4] FROM (
SELECT p.ProviderId, u.UserId, ROW_NUMBER() OVER(PARTITION BY p.ProviderId ORDER BY p.ProviderId ASC, u.UserId ASC) as Row
FROM Provider p
left join ProviderUser pu on p.ProviderId=pu.ProviderId
left join [User] u on pu.UserId = u.UserId
) as MyTable
PIVOT
(
SUM(UserId) FOR Row IN ([1],[2],[3],[4])
) as PivotTable
ProviderId 1 2 3 4
1 2 15 18 22
2 17 23 NULL NULL
However, I need to be able to take the usernames (and other text details) out of user child rows and append them as columns per their provider parent.
Any suggestions for making this work?
Thanks
Ok, if you want to get a dynamic number of columns, then you are gonna have to use dynamic sql. For that, please take a look at this link first. Then, you can try this:
DECLARE #Users NVARCHAR(MAX), #Query NVARCHAR(MAX)
SET #Users = ''
SELECT #Users = #Users + '[UserName' + CAST(UserId AS VARCHAR) +'],'
FROM Users
ORDER BY UserId
SET #Users = LEFT(#Users,LEN(#Users)-1)
SET #Query = '
SELECT ProviderName, '+#Users+'
FROM ( SELECT B.ProviderName, C.UserName, ''UserName''+CAST(A.UserId AS VARCHAR) UserAlias
FROM ProviderUsers A
JOIN Provider B
ON A.ProviderId = B.ProviderId
JOIN Users C
ON A.UserId = C.UserId) T
PIVOT(MIN(UserName) FOR UserAlias IN ('+#Users+')) AS PT'
EXEC sp_executesql #Query
this is my sql problem - there are 3 tables:
Names Lists ListHasNames
Id Name Id Desc ListsId NamesId
=-------- ------------ ----------------
1 Paul 1 Football 1 1
2 Joe 2 Basketball 1 2
3 Jenny 3 Ping Pong 2 1
4 Tina 4 Breakfast Club 2 3
5 Midnight Club 3 2
3 3
4 1
4 2
4 3
5 1
5 2
5 3
5 4
Which means that Paul (Id=1) and Joe (Id=2) are in the Football team (Lists.Id=1), Paul and Jenny in the Basketball team, etc...
Now I need a SQL statement which returns the Lists.Id of a specific Name combination:
In which lists are Paul, Joe and Jenny the only members of that list ? Answer only Lists.Id=4 (Breakfast Club) - but not 5 (Midnight Club) because Tina is in that list, too.
I've tried it with INNER JOINS and SUB QUERIES:
SELECT Q1.Lists_id FROM
(
SELECT Lists_Id FROM
names as T1,
listhasnames as T2
WHERE
(T1.Name='Paul') and
(T1.Id=T2.Names_ID) and
( (
SELECT count(*) FROM
listhasnames as Z1
where (Z1.lists_id = T2.lists_Id)
) = 3)
) AS Q1
INNER JOIN (
SELECT Lists_Id FROM
names as T1,
listhasnames as T2
WHERE
(T1.Name='Joe') and
(T1.Id=T2.Names_ID) and
(
(SELECT count(*) FROM
listhasnames as Z1
WHERE (Z1.Lists_id = T2.lists_id)
) = 3)
) AS Q2
ON (Q1.Lists_id=Q2.Lists_id)
INNER JOIN (
SELECT Lists_Id FROM
names as T1,
listhasnames as T2
WHERE
(T1.Name='Jenny') and
(T1.Id=T2.Names_ID) and
(
(SELECT count(*) FROM
listhasnames as Z1
WHERE (Z1.Lists_id = T2.lists_id)
) = 3)
) AS Q3
ON (Q1.Lists_id=Q3.Lists_id)
Looks a little bit complicated, uh? How to optimize that?
I need only that Lists.Id in which specific names are in (and only these names and nobody else). Maybe with SELECT IN?
Regards,
Dennis
SELECT ListsId
FROM ListHasNames a
WHERE NamesId in (1, 2, 3)
AND NOT EXISTS
(SELECT * from ListHasNames b
WHERE b.ListsId = a.ListsId
AND b.NamesId not in (1, 2, 3))
GROUP BY ListsId
HAVING COUNT(*) = 3;
Edit: Corrected thanks to Chris Gow's comment; the subselect is necessary to exclude lists that have other people on them.
Edit 2 Corrected the table name thanks to Dennis' comment
Using Carl Manaster's solution as a starting point I came up with:
SELECT listsid
FROM listhasnames
GROUP BY listsid HAVING COUNT(*) = 3
INTERSECT
SELECT x.listsid
FROM listhasnames x, names n
WHERE n.name IN('Paul', 'Joe', 'Jenny')
AND n.id = x.namesid
Updated:
select a.ListsId from
(
--lists with three names only
select lhn.ListsId, count(*) as count
from ListHasNames lhn
inner join Names n on lhn.NamesId = n.Id
group by lhn.ListsId
having count(*) = 3
) a
where a.ListsId in (select ListsId from ListHasNames lhn where NamesId = (select NamesId from names where Name = 'Paul'))
and a.ListsId in (select ListsId from ListHasNames lhn where NamesId = (select NamesId from names where Name = 'Joe'))
and a.ListsId in (select ListsId from ListHasNames lhn where NamesId = (select NamesId from names where Name = 'Jenny'))
I was just solving a problem recently that may work well for your case as well. It may be overkill.
I took the approach of creating a list of candidate associations that may be the correct solution, and then using a cursor or queue table to go through the likely correct solutions to do full validation.
In my case this was implemented by doing like
select
ParentId
count(*) as ChildCount
checksum_agg(checksum(child.*) as ChildAggCrc
from parent join child on parent.parentId = child.parentId
Then you can compare the count and aggregate checksum against your lookup data (i.e. your 3 names to check for). If no rows match, you are guaranteed to have no matches. If any row matches you can then go through and do a join of that specific ParentId to validate if there are any discrepancies between the row sets.
Clear as mud? :)