I just wanna say that this forum has been really helping me a lot in my current work.
I am in need of another help in writing an sql query for our ms access database. The idea is to make a union query for all the tables (January to December) to get the unique ID numbers and get their "Item" values per month as a column in the output table.
An example could be seen below. If the ID cannot be found in the table, the value will be returned null.
This seemed easy to do in excel but we would like to do it in our backend. I've only gotten as far as writing down the UNION query for all the tables but that's how far I got.
Thanks in advance for the help.
Table 1: January
| ID | Item |
| 1 | Apple |
| 2 | Salad |
| 3 | Grapes |
Table 2: February
| ID | Item |
| 1 | Apple |
| 2 | Grapes |
| 4 | Grapes |
Output Table:
| ID | January | February |
| 1 | Apple | Apple |
| 2 | Salad | Grapes |
| 3 | Grapes | NULL |
| 4 | NULL | Grapes |
One way is with union all and group by:
select id, max(January) as January, max(February) as February
from (select id, item as January, NULL as February from January
union all
select id, NULL, item from February
) jf
group by id;
If id exist only once per table why not use Inner Join instead? It will automatically null out the column if it does not exist on the other table but exist on the first one
SELECT id, Jan.Item,Feb.Item
FROM January Jan
INNER JOIN February Feb
on Jan.id=Feb.id
Related
Okay, so I have a table which has 3000 rows. Each row is unique, however some fields are duplicated.
I want to update the rows with fields that are duplicated. I'm having a hard time putting this into words, so I'm sorry if I'm getting it all wrong.
For example, the table looks like this:
+----+--------+--------+-----------+-------+-------+------+--+
| id | name | place | day | time1 | time2 | dupe | |
+----+--------+--------+-----------+-------+-------+------+--+
| 1 | George | Garden | Sunday | 12:00 | 13:00 | 0 | |
| 2 | George | House | Monday | 15:00 | 18:00 | 0 | |
| 3 | David | School | Wednesday | 15:00 | 18:00 | 0 | |
| 4 | Stan | Church | Sunday | 12:00 | 13:00 | 0 | |
+----+--------+--------+-----------+-------+-------+------+--+
I'd like to run a mysql query that checks the table for duplicate names in the name field, and marks the dupe field as 1 if they are a duplicate.
So the dupe fields in row 1 and 2 should be '1' and the rest should be '0'.
Thank you for any help you might be able to provide! I hope I explained it right.
You can use EXISTS in a CASE statement:
select
t.*,
case
when exists (
select 1 from tablename
where id <> t.id and name = t.name
) then 1
else 0
end dupe
from tablename t
You seem to only care about the name field. So:
update t join
(select name
from t
group by name
having count(*) >= 2
) dups
on t.name = dups.name
set dups = 1;
The title is a bit messy, but here's an example
suppose we have table:
| name | room |
=================
| John | 4 |
| John | 6 |
| John | 9 |
| Smith | 4 |
| Smith | 6 |
| Brian | 4 |
| Brian | 6 |
| Brian | 9 |
I want to select John and Brian because they both have exactly rooms 4, 6 and 9, but not Smith, since he doesn't have the room 9. (If we had another person who ONLY has room 4 and 6, then it'd select that other person as well as Smith).
I know I need to do some kind of correlated query, but I'm not sure how to actually get it to do something like
for a check for b
If you want groups of names that share the exact same rooms, I would recommend group_concat():
select rooms, group_concat(name) as names
from (select name, group_concat(room order by room) as rooms
from t
group by name
) n
group by rooms;
If you want only combinations with more than one name, then add having count(*) > 1 to the outer select.
Say I have a relation grades about students' grades like this:
| ID | semester | Year | course_id | grade |
|------+----------+------+-----------+-------+
| 1018 | Fall | 2002 | 272 | A+ |
| 107 | Fall | 2002 | 274 | B |
| 111 | Fall | 2002 | 123 | C |
/* a lot of data here */
|------+----------+------+-----------+-------+------------|
I wanna group by course_id and count its grades like this:
| course_id | semester | year | A+ | A- | B+ | B- | C+ | D+ | D- | else | sum |
| 1 | Fall | 2009 | 11 | 8 | 10 | 1 | 1 | 1 | 1 | 1 | 34 |
| 2 | Fall | 2009 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 8 |
I already figured out one solution but seems not satisfying to me:
/* use sum function */
select course_id, semester, year,
sum(if(grade = 'A+', 1, 0)) as 'A+',
sum(if(grade = 'A-', 1, 0)) as 'A-',
/* multiple lines */
from grades
group by course_id, semester, year;
I wonder if there is a more built-in way to make it, because my above solution is kinda of tricky and not general.
Can anyone offer better idea?
p.s.: yes it's a school assignment, and I want to seek more solutions:)
It will be appreciated if give me more hints.
There is no "simpler" way to do this. Well, actually, I would simplify the logic (assuming MySQL) to:
select course_id, semester, year,
sum( grade = 'A+') as `A+`,
sum(i grade = 'A-') as `A-`,
/* multiple lines */
from grades
group by course_id, semester, year;
However, that is probably not what you are looking for. A SQL query returns a fixes set of columns, with their names and types fixed. If you want the columns to be based on the actual values, then you cannot readily use a simple SQL statement.
You can use dynamic SQL, but that is even more complicated than your SQL query.
I have a table for users like this
id | username | name | join_date
-----+-------------+--------+-------------
1 | asdasd | name1 | timestamp
2 | asdas1 | name2 | timestamp
3 | asdas2 | name3 | timestamp
4 | asdas3 | name4 | timestamp
Each user needs to pay for a monthly subscription. Needs to take the report if user paid or not.
So I created another table for adding month data ( admin will add each month manually )
id | month_number | year
----+----------------+---------
1 | 02 | 2017 // Case Feb 2014
3 | 03 | 2017 // Case Mar 2014
how to connect these two tables ?
The easiest way would be to just join them inside queries.
The easiest way for that would be to just put it inside the where clause.... SELECT payment.month_number, payment.year, users.username FROM users, payment WHERE (users.id = payment.id) AND (users.username = asdasd)
You will need a table to track the subscriptions (second table is not needed actually). subscriptions can have user id as Foreign Key, subscription_date and amount columns, e.g.:
id | user_id | subscription_date | amount
----+-----------+-------------------+--------
1 | 2 | 2017-03-01 | 100
1 | 1 | 2017-02-01 | 100
if possible Please add the column user_id to the month_date table
and, try this
SELECT *
FROM users u
LEFT JOIN month_date m
ON u.id = m.user_id
I have a table that has 2 columns with data like this (from 1950 to 2015):
| Year | Count |
| 1994 | 10 |
| 1994 | 49 |
| 1994 | 2 |
| 1995 | 13 |
| 1995 | 6 |
I want my query result to be:
| Year | Count |
| 1994 | 61 |
| 1995 | 19 |
Things I have tried:
I began with a simple query like SELECT SUM(Count) FROM 'population' WHERE 'Year' = '1994' which was fine to bring a specific year but I wanted to fill an array with the population of every year in the database.
Doing something like SELECT Year, SUM(Count) FROM 'population' is closer to what I want except it just shown the first year only.
I'm not sure what terms I need to search up to get close to my answer. Union? I tried applying it but I just blerghed.
Try to use
SELECT year,SUM(Count) FROM 'population' group by year