Querying a table to get values based on no of digits of a parameter? - ms-access

Considering the following table
I have a large table from which I can query to get the following table
type no of times type occurs
101 450
102 562
103 245
111 25
112 28
113 21
Now suppose I wanted to get a table which shows me the sum of no of times type occurs
for type starting with 1 then starting with 10,11,12,13.......19 then starting with 2, 20,21, 22, 23...29 and so on.
Something like this
1 1331 10 1257
11 74
12 ..
13 ..
.. ..
2 ... 20 ..
21 ..
Hope I am clear
Thanks

You really have two different queries:
SELECT [type]\100 AS TypePart, Count(t.type) AS CountOftype
FROM t
GROUP BY [type]\100;
And:
SELECT [type]\100 AS TypePart, [type] Mod 100 AS TypeEnd,
Count(t.type) AS CountOftype
FROM t
GROUP BY [type]\100, [type] Mod 100;
Where t is the name of the table.

Here on the first query i am getting something like this
utypPart CountOftype
1 29
2 42
3 46
4 50
5 26
6 45
7 33
9 1
it is giving me how many utyp are starting with 1 2 and so on
but whai i want is the sum of no of times those types occur for the utyp .

Related

MySQL: Get count for each range

There is mysql Ver 8.0.18 value_table as:
value count
1 3
11 1
12 2
22 5
31 1
34 3
35 1
40 3
46 7
What is query to get a total count for each dozen (1-10 - first dozen,11-20 - second , etc..)
as:
1 3
2 3
3 5
4 8
5 7
Query should be flexible, so when some records added to value_table , for example
51 2
62 3
so, it is not necessary to change a query by adding new range (51-60 - 6-th dozen, etc.)
I think you just want division and aggregation:
select min(value), sum(count)
from t
group by floor(value / 10);
To be honest, I'm not sure if the first column should be min(value) or floor(value / 10) + 1.

join same type table data in a table in sql

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

CSV, convert multiple rows in to comma delimited list grouped by another column's value

I have a CSV file with two columns. Column 1 contains a group ID and Column 2 contains an item ID.
Here's some sample data (copied out of excel)
- 5 154
- 5 220
- 5 332
- 5 93
- 5 142
- 5 471
- 5 164
- 5 362
- 5 447
- 5 1697
- 5 170
- 6 173
- 6 246
- 6 890
- 6 321
- 6 421
- 6 1106
- 6 5
- 6 253
- 6 230
- 6 551
- 8 2155
- 8 2212
- 8 2205
- 8 2211
- 8 2165
- 8 2202
- 8 1734
- 8 2166
- 8 2129
I need to reformat this so that I have just one row for each group ID and Column 2 contains a comma delimited list of item IDs.
So it should look something like this
-5 154,220,332,93,142,471,164362,447,1697,170
-6 173,246,890,321,421,1106,5,253,230,551
-8 2155,2212,2205,2211,2165,2202,1734,2166,2129
I'm happy to import the CSV in to Excel / Numbers in order to reformat. Or even in to a temp MySQL database if a SELECT query can achieve this.
Thank you for your help!
I feel something like this is best solved with R and reshape.
But here you go in Excel:
assuming
group keys in column A
item keys in column B
unique group keys in column D (I guess you can do this manually)
enter into E2:
=INDEX($B:$B,SMALL(IF($A$2:$A$50=$D2,ROW($A$2:$A$50),""),COLUMN()-COLUMN($D2)))
and press CTRL+SHIFT+ENTER to enter it as an array formula. Now you can copy cell E2 to F2:P2 and E3:P4.
result:

select all rows when its values smaller than specify number?

i have a table :
a b c
1 10 1001
7 6 54
56 2000 31
1200 5 9
4 10 20
2 65 20
how can i select rows with column's value of this row smaller than 1000. i want to get this
a b c
7 6 54
4 10 20
2 65 20
mysql query still get all value :
SELECT a,b,c FROM test
where a <'1000' or b<'1000' or c<'1000'
It sounds like you would like to pull a row where there is NO column greater than 1000 in that row, if that is correct then you need to us AND instead of OR.
SELECT a,b,c FROM test
where a <'1000' AND b<'1000' AND c<'1000'
Hope that helps!

Mysql search for exact number in field

in my MySql DB, i have column containing that kind of value
63 61 57 52 50 47 46 44 43 34 33 27 23 22 21 10 5 3 2 1
Those numbers are separated by tab.
Impossible to get the good result for a simple query that would aid something like this
SELECT * FROM mytable WHERE mycolumn = 63
I'm not sure if "=" is the good method, i've also tried LIKE, IN and even FIND_IN_SET
I need some help :)