I have a quote_logs table where there are groups of logs(about 8 logs in each group). Every group has quote_log_group field which has the same value for the group(for example 1530599717507).
I need SQL statement to get all quote logs, sorted by the net_charge field in each group.
Rows in the table look like this:
Expected sample output:
*id* *net_charge* *quote_log_group*
5 - 110 - group1
7 - 120 - group1
2 - 130 - group1
4 - 140 - group1
3 - 150 - group1
11 - 110 - group2
15 - 120 - group2
12 - 130 - group2
13 - 140 - group2
14 - 150 - group2
You can order the query by quote_log_group to get all the rows in the same group one of the other and then by net_charge:
SELECT *
FROM quote_logs
ORDER BY quote_log_group, net_charge
Related
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.
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:
Ok, I have this table
ControlID - Version - Type - Rel ID - Parent Rel ID
1 - 2 - 11 - 212 - 5
1 - 2 - 44 - 542 - 5
1 - 2 - 47 - 742 - 5
2 - 2 - 11 - 200 - 4
2 - 2 - 14 - 565 - 4
2 - 2 - 20 - 700 - 4
As showed in the above table, we have data in 2 blocks (Note: Rel ID is unique):
Block 1
ControlID - Version - Type - RelID - ParentRelID
1 - 2 - 11 - 212 - 5
1 - 2 - 44 - 542 - 5
1 - 2 - 47 - 742 - 5
Block 2
ControlID - Version - Type - RelID - ParentRelID
2 - 2 - 11 - 200 - 4
2 - 2 - 14 - 565 - 4
2 - 2 - 20 - 700 - 4
Ok, now user will enter any relID & the system will show the the Rel ID that has type=11 in the same block of entered relID.
Ex: if user enters 700, then it will know that 700 belongs to block 2 & then it will search the type=11 in block 2 & print out 200
Ex2: if user enters 742, then it will know that 742 belongs to block 1 & then it will search the type=11 in block 1 & print out 212
This is my query but I think it is too long & may be slow.
Select relID from mytable where controlID = (select controlID from mytable where relID=700)
and
version= (select version from mytable where relID=700)
and parentRelID= (select parentRelID from mytable where relID=700)
and type=11
The above query works but too long & maybe slow, can you make it shorter & make it run faster?
Why are you also selecting on version and ParentRelId ?? Do they also determine the Block? i.e., can there be different blocks that have the same RelID but have different versions and/or different parentRelIDs ?
If not, try this:
Select * From table t
Where type = 11
And ControlId =
(Select ControlId
From table
Where RelId = #RelId)
or ....
Select * From table t
Where type = 11
and exists (Select * from table
Where relId = #RelId
and ControlId = t.ControlId)
SELECT t1.RelId FROM table t1
INNER JOIN table t2
ON t1.ControlId = t2.ControlId
WHERE t2.RelId = 700
AND t1.type = 11
I need help trying to find a efficient method of adding data into a table only if the data has changed.
I have the first table which is LatestResults and only stores the latest results ( 1 for each team number)
TeamNumber(Primary) TeamScore TeamWorkUnits RecordDateTime
1 - 500 - 600 - 2012-09-22 08:20:00
2 - 400 - 6 - 2012-09-22 08:20:00
3 - 90 - 15 - 2012-09-22 08:20:00
4 - 1 - 0 - 2012-09-22 08:20:00
The other table which is called HistoricResults stores a copy of very result in case I need it for statistics in the future.
HisotryKey(Auto-Primary) TeamNumber TeamScore TeamWorkUnits RecordDateTime
1 - 1 500 - 600 - 2012-09-22 08:20:00
2 - 2 -400 - 6 - 2012-09-22 08:20:00
3 - 3- 90 - 15 - 2012-09-22 08:20:00
4 - 4 - 1 - 0 - 2012-09-22 08:20:00
5 - 1 490 - 600 - 2012-09-21 08:20:00
6 - 2 -300 - 6 - 2012-09-21 08:20:00
7 - 3- 40 - 15 - 2012-09-21 08:20:00
8 - 4 - 0 - 0 - 2012-09-21 08:20:00
So I want to merge the first table into the second only if the data TeamScore or TeamWorkUnits has changed since the last record by RecordDateTime.
Meaning if TeamScore and TeamWorkUnits are the same you keep the oldest entry in the History table.
I would by far prefer do this totally within MySQL for speed if possible.
Currently I need to check about 100,000 records per hour
Thanks in advance for your help.
You can try this approach:
insert into HistoricResults
(teamNumber, TeamScore, TeamWorkUnits, RecordDateTime)
select
lr.teamNumber,
lr.TeamScore,
lr.TeamWorkUnits,
NOW()
from
LatestResults lr
where
exists (
select
*
from
HistoricResults hs
where
hs.teamNumber = lr.teamNumber and
(hs.TeamScore != lr.TeamScore or hs.TeamWorkUnits != lr.TeamWorkUnits)
and hs.RecordDateTime = (select max(RecordDateTime)
from HistoricResults hs1
where hs1.teamNumber = lr.teamNumber)
order by RecordDateTime desc
)
Here is how it should works: subquery within exist is looking in HistoricResults for the most recent records with TeamScore or TeamWorkUnits different from LatestResults table record. If such record is found, then external select selects appropriate records from LatestResults and such records are finally inserted into HistoricResults.
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 .