insert into a table based on condition in another - mysql

I have trying to build a table from a condition existing in another table. If the condition exists I want to pull data from a different table to populate the new table. Here is the SQL statements I am using, however it is populating the new table with the wrong data.
INSERT INTO 2014TranstarPriceTnum (tran_no)
SELECT Tran_no
FROM Trans_Types_Updated
WHERE 2014TranstarPriceTtype.`tran_type`=Trans_Types_Updated.`Tran_Type`;
I am looking in 2014TranstarPriceTtype for a Trans Type that exists for the and pull all the associated tran_no's from Trans_Types_Updated and populate 2014TranstarTnum. However, my script resulted in the Tran_Type populated into the tran_no field.
2014TranstarPriceTtype
Tprice tran_type make core note Updated
650 125C BUICK 250 (P.H.B.) 2014-01-07
650 200C BUICK 250 (P.H.B.) 2014-01-07
850 2004R BUICK 350 (P.H.B.) 2014-01-07
650 350 Chev 250 (P.H.B.) 2014-01-07
Trans_Type_Updated
Tran_No Tran_Type Make Eng_size
T1014AA 125C BUICK 2.5
T1006AA 125C BUICK 2.5
T1363AA 2004R BUICK 5.0
T1365AA 2004R BUICK 5.0
T1310AA 200C BUICK 3.8 (231)
T1318AA 200C BUICK 5.0
T1427CA 350C CHEVROLET 5.0
T1427AA 350C CHEVROLET 5.0
Results in 2014TranstarPriceTnum
tran_no Tprice tran_type make eng_size core_tote note updated
125C \N \N \N \N \N \N \N
200C \N \N \N \N \N \N \N
2004R \N \N \N \N \N \N \N
350C \N \N \N \N \N \N \N
I would appreciated any help.
Thanks,
Tony Cripps

Hmm, not that clear, but I think you want something like that (not sure of your table names, you've got different versions).
INSERT INTO 2014TranstarPriceTnum (tran_no)
SELECT ttu.Tran_no
FROM Trans_Type_Updated ttu
INNER JOIN 2014TranstarPriceTtype tpt on tpt.tran_type = ttu.tran_type
see SqlFiddle

Related

MYSQL INTO OUTFILE

I wrote a MySQL query for exporting data into text file.
Query runs successfully but result did not match my expectations.
I want to result without spacing between columns.
select
sample_export_record1_2013.*,
sample_export_record2_2013.*
from
sample_export_record1_2013
inner join
sample_export_record2_2013
INTO OUTFILE 'C:/Windows/temp/a20.txt'
FIELDS TERMINATED BY ""
LINES TERMINATED BY '\r\n'
STARTING BY '0'
Currently output is generated by this query like this :
01 181 2 0 P 5701 13.01.A.01 9664 0 94003048 94002596 0 3 20130101 20130106 000000001223 20130106 \N \N \N 2 1 258972658 9664 426407407 75 20050917 1 1 Reijtest \N 2 Reijtest \N o 1 2402LS \N 130 \N NL \N \N \N
I want the output without space between columns - just look like
0118120P570113.01.A.0196640940030489400259603201301012013010600000000122320130106\N\N\N212589726589664426407407752005091711 Reijtest\N2Reijtest\No12402LS\N130\NNL\N\N\N
Just use concat, SELECT CONCAT(rec1, rec2,rec3) as 'results' FROM...

Grouping and Total on one Column Values

I have this group in a table. Where I need to display one value on the top and rest according to its alphabetical order.
Table
Column1 Value#1 Value#2
Alpha 12 26
Beta 65 745
Gamma 987 87
Pie 7 2
Non-Beta 132 426
Zeta 112 266
How can I display it in the below format...
Table
Column1 Value#1 Value#2
Non-Beta 132 426
Alpha 12 26
Pie 7 2
Zeta 112 266
Total 263 720
Beta 65 745
Gamma 987 87
Total 1057 832
I could display Non-Beta on the top by using the below expression in the sorting tab in the group Property.::
=IIF(Fields!Column1.Value = "Non-Beta", "A" + Fields!Column1.Value, "B" + Fields!Column1.Value)
But how do I Display it according to the above format. Values Come from one column (Column1)
EDIT
This is the output I got after the operation that Ian specified...
Thank you
You can set up a group based on an expression like this:
=IIf(Fields!Column1.Value = "Beta" or Fields!Column1.Value = "Gamma"
, "Group2"
, "Group1")
i.e. if Beta or Gamma, include in one group; everything else gets grouped together.
Include a Header and a Footer row with the group.
Apply the sorting expression to the Tablix as required.
The report will look something like this in the designer:
End results looks like your requirements:

Conditional Sorting according to one value and then alphabetically

I have this group in a table. Where I need to display one value on the top and rest according to its alphabetical order.
Table
Column1 Value#1 Value#2
Alpha 12 26
Beta 65 745
Gamma 987 87
Pie 7 2
Non-Beta 132 426
Zeta 112 266
I want to sort it like this(Can anyone also tell me the real use of this other than Viewing Purpose)
Table
Column1 Value#1 Value#2
Non-Beta 132 426
Alpha 12 26
Beta 65 745
Gamma 987 87
Pie 7 2
Zeta 112 266
So the Non-Beta has to be displayed on the top and rest according to alphabetical order.
Edit
Thank you very much for the below reply Chris, Really appreciate and yes it works.
I have one more question from the over table format itself...How can I display it in the below format...
Table
Column1 Value#1 Value#2
Non-Beta 132 426
Alpha 12 26
Pie 7 2
Zeta 112 266
Total 263 720
Beta 65 745
Gamma 987 87
Total 1057 832
Thank you
Select the table, right-click the table handle, select Tablix Properties and select the Sorting tab. Press the Add button then click the fx button to open the expression editor. Enter the following expression:
=IIF(Fields!Column1.Value = "Non-Beta", "A" + Fields!Column1.Value, "B" + Fields!Column1.Value)
All we are doing is prefixing the special value field with something so it comes before the other fields.

MySQL left join returning wrong number of results

hoping someone can help me with this issue. I have a table with the following data:
site_id type key data
2 organic-keywords-last-month (not provided) 9064
2 organic-keywords-last-month bmi 345
2 organic-keywords-last-month bmi kalkulator 445
2 organic-keywords-last-month grove pannekaker 678
2 organic-keywords-last-month grove vafler 976
2 organic-keywords-last-month lapper 475
2 organic-keywords-last-month melk.no 624
2 organic-keywords-last-month ostesuffle 361
2 organic-keywords-last-month scones 697
2 organic-keywords-last-month sunne pannekaker 658
2 organic-keywords-last-month sunne vafler 484
2 organic-keywords-this-month (not provided) 10034
2 organic-keywords-this-month bmi kalkulator 659
2 organic-keywords-this-month grove pannekaker 721
2 organic-keywords-this-month grove vafler 857
2 organic-keywords-this-month lapper 515
2 organic-keywords-this-month melk.no 587
2 organic-keywords-this-month ostesuffle 433
2 organic-keywords-this-month scones 626
2 organic-keywords-this-month smultringer 401
2 organic-keywords-this-month sunne pannekaker 566
2 organic-keywords-this-month sunne vafler 407
What I want to do is query for all the organic-keywords-this-month, and left join them to the organic-keywords-last-month, so that I get a list of the 11 keywords from this month, their performance, and their performance last month.
I am using the following query:
SELECT
ktm.key AS keyword,
ktm.data AS `this month`,
klm.data AS `last month`
FROM
data AS ktm
LEFT JOIN data AS klm ON
(ktm.key = klm.key AND ktm.type = 'organic-keywords-this-month'
AND klm.type = 'organic-keywords-last-month')
WHERE
ktm.site_id = 2
AND klm.site_id = 2
ORDER BY
`this month` ASC
Which I think should return 11 rows, as there are 11 rows of type 'organic-keywords-this-month', yet when I run it it only return 10 rows, and looks more like it is inner joining the tables rather than left join.
Anyone have any ideas?
I think the query is very close... but put your join condition on the types being the same too, and only put the QUALIFIER of the type in the WHERE clause associated with THIS MONTHs data
SELECT
ktm.key AS keyword,
ktm.data AS this_month,
klm.data AS last_month
FROM
data AS ktm
LEFT JOIN data AS klm
ON klm.site_id = ktm.site_id
AND klm.key = ktm.key
AND klm.type = 'organic-keywords-last-month'
WHERE
ktm.site_id = 2
AND ktm.type = 'organic-keywords-this-month'
ORDER BY
this_month ASC
By applying the "AND ktm.type = 'organic...', that will properly qualify WHAT YOU WANT NOW.
However, the LEFT JOIN will also explicitly match based on "AND ktm.type = klm.type" to the clause. Notice I adjusted to similarly apply your "same site ID" as last month too.
I would ensure the data table has an index on (site_id, type, key)
Your WHERE requires a particular value in klm, therefore the query is not returning your 11th line that doesn't even have any klm values.
Take the klm half of the WHERE out and put it in the LEFT JOIN.
Like Paul Gregory says. I think that should be accepted as an answer. I would just add that the site values should be compared inside the join 'on' clause if and only if you want to restrict comparisons of the key values to those from the same site, which it seems you want. In either case, if you want to restrict results to ktm.site = 2 it's ok to have that in either the join on clause, along with a ktm.site = klm.site, or the where clause, just don't put klm.site in the where clause.
If you seperate your table into two tables, one for this-month, one for last-month, you'll notice a slight discrepancy in the key column. The last-month group has a key called 'bmi' which is missing from this-month, and this-month has a key called 'smultringer', which is missing from last-month. Because of the lack of a match, which you specified in klm.key = ktm.key, those rows will be left out of the join, giving you only 10 rows instead of 11.
Edit: I'm not saying to actually seperate the table, I meant to do that mentally to compare the groups.

Choosing terms to insert into database that allows record to be queried easy later

I have a text file that I parse each month and insert the data into my database. The text file looks like this.
------------------------------------------------------------------------------------------------------------------------------------------------------------
BLK. NO 410 | 1 2| 3 4 5 6 7 | 8 9| 10 11 12 13 14 |15 16| 17 18 19 20 21 |22 23| 24 25 26 27 28 |29 30| 31 1 2
BLK 88.35 |SA SU| MO TU WE TH FR |SA SU| MO TU WE TH FR |SA SU| MO TU WE TH FR |SA SU| MO TU WE TH FR |SA SU| MO TU WE
CR. 88.35 |194 / 159 | |194 / 159 | |194 / 159 | |194 / 159 | |239
OFF 14 : : VER DFW TRC DFW X : : VER DFW TRC DFW X : : VER DFW TRC DFW X : : VER DFW TRC DFW X : : CUU DFW
C/O 06.00 TAFB 216.15 194=/1410/1055/0740, 159=/0900/1700/1400, 239=/1845/1945/0155
EM3 25.55 ERD 42.00 EM4 20.40
-----------------------------------------------------------------------------------------------------------------------------
This text file has from 100 to 300 of these records (we refer to it as a "line of flying") and I use it to determine my flight schedule each month. I am parsing this file just fine but the company added a new wrinkle.
This "line" can be for a pilot or a flight attendant. Until recently each line could be for a captain or First Officer. But now some files have captain lines and first officer lines. The flight attendant file could have 1 or two flight attendant positions or more depending on the type of aircraft flown.
I need to create a new column in my database that reflects this new information. Once the lines are saved to the database, they are only searched, never updated again. I don't want to add a new table just to keep track of this information. I want to add a new column to the existing table. Here is my strategy.
When I parse the text file, I will keep track of the positions available. If it is for Captain, I will save "CA" to the database. If it is first officer, I will save "FO". If it is for both a captain and First officer, I will save "CAFO". For flight attendants I will save a number "1" for 1 flight attendant poistion. For each additional poistion, I will just increment it one number. This would allow for expansion later if larger aiplanes required more flight attendant poistions.
So to recap, I will save one the following for each database record.
"CAFO"
"CA"
"FO"
"1"
"2"
Now I will need to access the database and retrieve these records for my web program. If a captain wants to retreive records He will want only captain lines which would be "CAFO" or "CA". And just the opposite for first officers.
When I do the query, I found that I could use the mySQL command SUBSTRING to just grab the first 2 or last 2 characters.
for captain lines
SELECT Line_number,html_text FROM lines WHERE SUBSTRING(Crew_type,1,2)="CA"
This would just get the first two beginning characters from the record and it would match both cases.
For First officers I start at the end of the string and grab last two characters.
SELECT Line_number,html_text FROM lines WHERE SUBSTRING(Crew_type,-2)="FO"
For flight attendants I could compare the number.
If a flight attendant only wanted lines with 1 position.
SELECT Line_number,html_text FROM lines WHERE Crew_type = "1"
Or if they want all possible flight attendant positions
SELECT Line_number,html_text FROM lines WHERE Crew_type >= "1"
I would set the column type to ENUM with the values I listed above.
My question is there a better way to approach this problem? I will only be inserting once, but will be retreiving a lot. Will the SUBSTRING command slow down the queries too much?
SELECT Line_number,html_text FROM lines WHERE SUBSTRING(Crew_type,1,2)="CA"
will be a performance killer, as it calculates the substring for all records, not only those used. You want
SELECT Line_number,html_text FROM lines Crew_type in ("CA", "CAFO")
Ditto for FO.
So somewhere in your PHP iw would define
$matches=array(
'CA'=>'"CA","CAFO"',
'FO'=>'"FO","CAFO"',
'FA'=>'"1","2"'
);
and then use
$match=$matches[$crew_type];
$sql="SELECT Line_number,html_text FROM lines Crew_type in ($match)";
with $crew_type being one of 'CA','FO','FA'