For example, let us consider this table:
In this image consists of rows of 8 where names like Mike,Glenn,Daryl,Shane and Patricia is included with their respective ages
Id
Name
Age
1
Mike
25
2
Glenn
19
3
Glenn
19
4
Daryl
56
5
Shane
30
6
Shane
30
7
Patricia
16
Now I want to insert the type of query that will show the names without repetitions like This, not like This
EDIT: I entered the data from first picture. The request is to list the names without duplicates, as shown in the second and third picture but I will not convert them to text.
DISTINCT specifies removal of duplicate rows from the result set.
SELECT DISTINCT Name
FROM tablename
see: use DISTINCT in SELECT
You can use GROUP BY to achieve it.
SELECT * FROM your_table
GROUP BY your_table.name
ORDER BY id
With the data you gave, the result from this query will be:
id
name
age
1
Mike
25
2
Glenn
19
4
Deryl
56
5
Shane
30
7
Patricia
16
In the past I entried my data month wise due to lack of my knowledge in the month name table. But now I easily filter them, in my database, there are 6 months (Oct - Feb) table are there with same row name month-wise data can I put all the data in a table, for manually put them in a file little bit difficult for me because of id,
so Please suggest to me to make it easily
for example, the October 2018 table is this
id user_name date nota veet tree location
1 milon 10/10/2018 43 12 111 bandel
2 kadir 11/10/2018 12 34 76 katwa
3 javed 22/10/2018 33 56 92 sirampur
4 milon 29/10/2018 55 21 78 salar
november 2018 table is
id user_name date nota veet tree location
1 milon 10/11/2018 13 12 71 Rampurhat
2 kadir 11/11/2018 12 24 76 katwa
3 javed 12/11/2018 53 30 62 kandi
4 milon 24/11/2018 55 27 58 salar
now I want SQL table like this
id user_name date nota veet tree location
1 milon 10/10/2018 43 12 111 bandel
2 kadir 11/10/2018 12 34 76 katwa
3 javed 22/10/2018 33 56 92 sirampur
4 milon 29/10/2018 55 21 78 salar
5 milon 10/11/2018 13 12 71 Rampurhat
6 kadir 11/11/2018 12 24 76 katwa
7 javed 12/11/2018 53 30 62 kandi
8 milon 24/11/2018 55 27 58 salar
You can create a new table (for example user_locations). Then you can copy data from first table and then copy data from second table. If you have ID column with auto-increment, and if you do not specify the ID column in select, than the ID will be assigned automatically and there will not be collision of IDs. Example of SQL for selecting from one table and inserting to another table:
INSERT INTO user_locations (user_name, date, nota, veet, tree, location)
SELECT user_name, date, nota, veet, tree, location FROM user_locations_november_2018
... however I am not sure that I understand you correctly. What exactly are you trying to do? Move data from all tables to one table (this is the one I answer). Or just select data from all the tables (in which case the UNION from other answer is correct). Or to select all data and put them into a text file?
select * from table1 Union Select * from Table2
use The SQL UNION Operator
I'm just stuck with this issue atm and I'm not 100% sure how to deal with it.
I have a table where I'm aggregating data on week
select week(create_date),count(*)
from user
where create_date > '2015-02-01'
and id_customer between 9 and 17
group by week(create_date);
the results that I'm getting have missing values in the count, as shown below
5 334
6 376
7 394
8 405
9 504
10 569
11 709
12 679
13 802
14 936
15 1081
16 559
21 1
24 9
25 22
26 1
32 3
34 1
35 1
For example here from 16 to 21 there a obviously 4 values missing I would like these values to be included and count to be 0. I want this because I want the weeks to be matching with other metrics as we are outputting them in an excel file for internal analysis.
Any help would be greatly appreciated.
The problem is that an sql query cannot really produce data that is not there at all.
You have 3 options:
If you have data for each week in your entire table for the period you are querying, then you can use a self join to get the missing weeks:
select week(t1.create_date), count(t2.id_customer)
from customer t1
left join customer t2 on t1.id_customer=t2.id_customer and t1.create_date=t2.create_date and t2.id_customer between 9 and 17
where t1.create_date > '2015-02-01'
group by week(t1.create_date)
If you have missing weeks from the customer table as whole, then create a helper table that contain week numbers from 1 or 0 (depending on mysql config) to 53 and do a left join to this helper table.
Use a stored procedure that loops through the results of your original query and inserts the missing data in the resultset using a temporary table and then returns the extended dataset as result.
The problem is that there is no data matching your criteria for the missing weeks. A solution will be to join from a table that has all week numbers. For example if you create a table weeknumbers with one field weeknumber containing all the numbers from 0 to 53 you can use something like this
select weeknumber,count(user.*)
from weeknumbers left join user on (weeknumbers.weeknumber=week(user.create_date)
and user.create_date > '2015-02-01'
and user.id_customer between 9 and 17)
group by weeknumber;
Additionaly you might want to limit the week numbers you do not want to see.
The other way is to do it in the application.
So Here is My data
ID C1 C2 C3
6 Digit 2 6,8,10,12
12 Digit 3 15
15 127 Digit 2 6,7,8,9,10,11,12,13
68 140,141 Digit 11 85,86,87,88,167,168,158,159
73 1 Digit 11 85,86,87,88,169,170
76 Digit 11 85,86,87,91,164,165,166,167,168
99 Digit 11 20,27,85,86,87
106 Digit 1 1,2
111 Digit 11 85,86,87,88
112 Digit 11 85,86,87,88
135 Digit 11 85,86,87
and my condition string is (2,6,15,37,42,52,62,65,79,85,94,100,104,107,113,124,131)
Now,I want to exclude row 3,4,5 if the values 127,140,141,1 are not in the list condition. I tried Not in , but no avail. I think I might be missing something basic, but just cant get it.
It's better not to store multiple values in a column if possible. Then it's easier to do queries like this.
You cannot use "IN" or "NOT IN" because they are looking for a list of separate items. But C3 is just one item that happens to have commas in it.
Try this:
SELECT * FROM
(SELECT ID, C1, C2, CONCAT('|',REPLACE(C3,',','|'),'|') as C3 FROM `table` WHERE `C3` ) as t1
WHERE t1.C3 NOT LIKE "|127|" AND t1.C3 NOT LIKE "|140|" AND t1.C3 NOT LIKE "|141|" AND t1.C3 NOT LIKE "|1|"
You could avoid the "|" and just concat "," to the start and end.
Or you could fix your database schema so that it actually acts like a Normalized Relational Database.
Every column that contains multiple values should be separated out into its own table.
There should be no column C3 in your table above. Instead, you should have a table, some_other_data:
At this point, I see that C3=6 is related to more than one record in the main table. Therefore, you actually need a third, linking table, in addition to some_other_data. See below.
`some_other_data`
id
6
8
10
12
15
`main_table_to_some_other_data_link`
some_other_data_id | main_table_id
6 6
8 6
10 6
12 6
15 12
6 15
etc. You can see that the linking table can contain duplicates of either value. But your other two tables would have completely unique ids.
I think you're trying to solve the wrong problem.
(I'm assuming you can change your table structure. If you can't someone else will need to address your question.)
The long lists of comma-separated data are a flag that they have a one-to-many relationship with ID.
For example, make the data in C3 its own table:
ID MainID C3
================
1 6 6
2 6 8
3 6 10
4 6 12
5 12 15
6 15 6
7 15 7
8 15 8
9 15 9
10 15 10
11 15 11
12 15 12
13 15 13
// and so forth //
So ID is the primary key of the new table, MainID is the foreign key that refers to the record in your primary table, and C3 is the data in C3.
Each separate value of C3 now has its own record.
Now, you're in a position to use something like
Select * from MainTable
Inner Join NewTable
On MainTable.ID = NewTable.MainID
Where NewTable.C3 Not In (2,6,15,37,42,52,62,65,79,85,94,100,104,107,113,124,131);
If you can, pulling out the one-to-many relationships into their own tables will make things easier for you.
I'm first time trying to use dlookup function in access as below but I get blank output.
select dlookup ("quantity","test","series > 2000") from test
This is test table
id series quantity
1 1000 25
2 2000 33
3 3000 44
4 4000 55
5 5000 66
6 6000 77
I thought the above query will display all records from the table below which has series more than 2000 i.e. as below but it displays blank result.
id series quantity
3 3000 44
4 4000 55
5 5000 66
6 6000 77
I'm not sure if my syntax is incorrect or anything else. I already double checked my syntax from various sources though.
DLookup() returns a single value, which is not what I think you want. Just put your selection constraint in the WHERE clause.
SELECT id, series, quantity
FROM test
WHERE series > 2000;