I have 2 Tables in Access and I am trying to build a LookUp Query. (I am new to SQL)
Dogs
ID
DogName
Type (int) Either 1,2,3,4
ClassResults
ID
ClassEntered (int) 1-24
DogName
So in my lookup I am trying to find all the dogs from the DOGS table that if the ClassEntered is less than 12 select all the dogs with a TYPE 1 or 2.
Dogs Data Sample:
ID DogName Type
0 AAA 1
1 BBB 3
2 CCC 1
3 DDD 2
4 EEE 4
ClassResults Data Sample:
ID ClassEntered DogName
0 6 ?????
So, the Drop Downlist for the DogName should be Showing:
0, AAA, 1
2, CCC, 1
3, DDD, 2
SELECT DISTINCT Dogs.DogName FROM Dogs, ClassResults
WHERE (IIf([ClassResults].[ClassEntered] < 10,[Dogs.Type]<3,[Dogs.Type]>2)) ORDER BY Dogs.DogName;
SELECT DISTINCT Dogs.DogName FROM Dogs, ClassResults
WHERE (IIf([ClassResults].[ClassEntered] < 10,[Dogs.Type]<3,[Dogs.Type]>2)) ORDER BY Dogs.DogName;
I hope that makes sense.
Stephan
It's better to use joins:
select * from dogs d
left join ClassResults c on d.dogname=c.dogname
where c. ClassEntered<12 and d.type in (1,2)
If you want to pick data from two different tables, you have to utilize joins.
Related
I have 2 queries that looks like this.
QUERY 1
SELECT a.utype, DATE_TRUNC(b.date, DAY) AS date, (b.noofspec)
FROM `table1`as a
JOIN `table2` as b
ON a.joinkey = b.joinkey
GROUP BY 1, 2, 3
ORDER BY 2;
QUERY 2
SELECT a.utype, DATE_TRUNC(b.date, DAY) AS date, SUM(b.noofspec)
FROM `table1`as a
JOIN `table2` as b
ON a.joinkey = b.joinkey
GROUP BY 1, 2
ORDER BY 2;
I am expecting that (b.noofspec) of QUERY 1 would somehow have a TOTAL with same value as SUM((b.noofspec). However, it returned a completely different value.
My tables looks like this
TABLE B
gameid
date
noofspecs
id
1
2121-04-06 00:01
50
ID1
2
2121-04-06 00:01
50
ID2
3
2121-04-06 00:01
58
ID3
4
2121-04-06 00:01
100
ID4
5
2121-04-06 00:01
555
ID5
6
2121-04-06 00:01
55
ID6
7
2121-04-06 00:01
17
ID7
TABLE A
gameid
date
game
action
id
specid
1
2121-04-06 00:01
DOTA
to host
ID1
ID58
1
2121-04-06 00:02
LOL
to join1
ID11
ID24
1
2121-04-06 00:03
DOTA
like
ID1
ID87
1
2121-04-06 00:04
DOTA
to host
ID1
ID99
2
2121-04-06 00:01
CSGO
to host
ID2
ID47
2
2121-04-06 00:06
DOTA
to join1
ID21
ID09
2
2121-04-06 00:10
DOTA
to join2
ID22
ID23
3
2121-04-06 00:01
LOL
to host
ID3
ID77
What happens is that a user hosts a game(host), another user joins(join1, join2) and others can watch(counted in watch), spectators can send stars to the game if they want. just like in fb gaming and twitch. sender is listed in the specid, rcvr is in id which means that rcvr can also be not the host.
In Table B, gameid is unique. And also displays the host of the game.
In Table A, gameid returns activity results for a specific time.
For example, in row 1 of table a, viwer id58 sent star to host on 2121-04-06 00:01 for gameid 1 which is a dota game. the game column specifies what game is hosted so game id 1 is a dota game. but you may notice that in row 2 of table a, the game listed is LOL for the same game. that is because the game type listed is the game that gamer is streaming. in this case, ID11 is a LOL streamer who joined a DOTA game.
gameid
date
game
action
id
specid
1
2121-04-06 00:01
DOTA
to host
ID1
ID58
1
2121-04-06 00:02
LOL
to join1
ID11
ID24
The result I am expecting is that I get a SUM of all the views of each type of game per day. I used gameid as the joining key but I think it complicates the result because of the duplicate entries in gameid tablea. I also tried with the using id as a joining key but the problem is the result is also aggregating the join userids.
One more problem I find is that in id column of table a, when someone send a like to the game, it goes to the host. so i have two unique columns if i group them and filtered joins. one for the like and one for the tohost.
Your two queries are very different; your first query will return a set of values that is distinct, your second will sum a set that is not distinct
Let us demonstrate with simpler data:
A.X, A.Y
1, 100
2, 200
B.X
1
1
1
2
2
Let us join the tables:
SELECT A.X, A.Y, B.X
FROM A JOIN B ON A.X = B.X
The result:
1, 100, 1
1, 100, 1
1, 100, 1
2, 200, 2
2, 200, 2
The first two columns come from A, the third from B
Joining B in has caused A’s rows to repeat. They would still repeat even if you didn’t select B; the repetition is caused by the join
This is all fine until you want to sum up Y
SELECT A.X, SUM(A.Y) FROM A JOIN B ON A.X = B.X GROUP BY A.X
1, 300
2, 400
The repetitions are included in the sum
So why was it different to your first query?
Your first query grouped all the columns, effectively squishing the generated duplicates
SELECT A.X, A.Y, B.X
FROM A JOIN B ON A.X = B.X
GROUP BY A.X, A.Y, B.X
The select and from lines above are the original query I first wrote in this answer; they generate 5 rows, 3 rows of 1,100.1 and 2 rows of 2,200,2
The group by squishes them back to only unique rows, the sum of Y would hence be very different because there are no duplicated values taking part in the sum
I have two tables:
TypeTable
TypeId PersonClassificationId
----------------------
1 1
1 2
1 3
2 1
2 2
PersonClassificationTable
PersonClassificationId Capacity
----------------------
1 2
2 2
3 2
I need to select such TypeId that in the entire TypeTable table do not have at least one PersonClassificationID specified in PersonTable.
So, if PersonTable has 1, 2, 3, then TypeId = 2 should be selected, because there is no record in TypeTable:
TypeId PersonClassificationId
----------------------
2 3
How can I do that?
It is undesirable to use cursors : )
I think that you can do what you want by generating all possible combinations of types and classifications, and then filter on those that do not exist in the mapping table:
select t.TypeId, pc.PersonClassificationId
from (select distinct TypeId from TypeTable) t
cross join PersonClassificationTable p
where not exists (
select 1
from TypeTable t1
where t1.TypeId = t.TypeId and t1.PersonClassificationId = p.PersonClassificationId
)
I have table below:
SerialNumber Name Product
1 aaa a
2 bbb b
3 ccc c
I would like to convert to table below:
serialNumber PropertyName value
1 Name aaa
1 Product a
2 Name bbb
2 Product b
3 Name ccc
3 Product c
How can i achieve this in SSIS 2012?
You have Pivot and Unpivot Data Flow Transformations, but even simpler it will be for you to use Multicast, to make two streams out of one. Then in one stream you'll be passing Names and values, an in the other Products and values. Then use Union All to join the streams again.
I have a table A that contains the following columns
ID(PK) id_1
1 4
2 10
3 15
4 4
Now Im trying to create a table B with columns such that
ID(PK) Description id_1_a_id (composite foreign key(ID,id_1))
1 Apple (1,4)
2 Orange (2,10)
3 Banana (3,15)
4 dog (4,4)
5
Does this design make sense? Or is there a better way to do this? (SQL rookie)
Composite foreign keys are common and useful, but you don't have one. If you did, it would look like this.
ID(PK) Description A_id id_1
--
1 Apple 1 4
2 Orange 2 10
3 Banana 3 15
4 dog 4 4
But you wouldn't ordinarily do that, either. Ordinarily, you'd reference a unique set of columns in table A. The unique set of columns is just the single column A.ID. So your table would usually look like this.
ID(PK) Description A_id
--
1 Apple 1
2 Orange 2
3 Banana 3
4 dog 4
You wouldn't usually duplicate the values of A.id_1 in table B. If you need the values from A.id_1, write a query with a JOIN.
select B.ID, B.Description, A.ID, A.id_1
from B
inner join A on A.ID = B.A_id;
If the only unique constraint you have in your tables is on the ID numbers, you're doing it wrong. But that's a different question.
What's the best way to get a sublist of things?
I have two tables:
create table A (
id int primary key
)
create table B (
id int primary key,
aid int foreign key references A( id ),
sort_key int
)
I want to get a list of objects A, with subobjects B, but only the top five of B.
Let's say A is people, and B is type of food, with sort_key being how much a person likes that food. How do I get everybody (or some people) and their top 5 food?
On the previous comment if it's an INT you can't put non numerics in there.
Assuming the following data:
a
--
id
1
2
3
b
------------------------
id aid sort_key
1 1 1
2 1 2
3 2 1
4 3 1
5 1 3
6 1 4
7 1 5
8 1 6
9 2 2
10 2 3
The following query in MySQL would give you this:
SELECT a.*,
(SELECT GROUP_CONCAT(id) AS ids FROM b AS bi WHERE bi.aid = a.id ORDER BY sort_key LIMIT 5) AS ids
FROM a
Result:
id ids
1 1,2,5,6,7,8
2 3,9,10
3 4
This query assumes the sort key is one based, rather than zero:
SELECT a.name
b.food
FROM A a
JOIN B b ON b.aid = a.id
WHERE b.sortkey <= 5
ORDER BY a.name, b.sortkey