MySQL Left Outer Join and {?} - mysql

I have been tasked with converting some Crystal SQL queries into QlikView and am having trouble deciphering the SQL code as it has been a decade since I last played with this. I'm modelling the data in MySQL workbench prior to importing it into QlikView.
I have posted the code bellow for the sake of completeness although I realize a lot of it is surplus.
The issue I am having is I don't know and cant seem to work out how do duplicate these queries in MySQL workbench as I don't understand what these code segments are doing:
={?APS: ITEM1.ST_Prodcode} and ={?FKE: ITEM1_1.ST_Prodcode} etc.
The above mentioned code appears to me to be calling the next query. Am I right in thinking this?
Pricing
SELECT
`stock_management1`.`st_prodcode`,
`stock_management1`.`st_sdesc`,
`stock_management1`.`st_mstockist`,
`stock_management1`.`APS_rol`,
`stock_management1`.`APS_eoq`,
`stock_management1`.`APS_ms`
FROM
`pricing`.`stock_management`
`stock_management1`
WHERE
(`stock_management1`.`st_mstockist`='BRA'
OR
`stock_management1`.`st_mstockist`='FCS'
OR
`stock_management1`.`st_mstockist`='FKE')
AND
`stock_management1`.`APS_ms`>0
AND
(`stock_management1`.`st_prodcode`>='A'
AND
`stock_management1`.`st_prodcode`<='WZZZZZ999')
EXTERNAL JOIN
stock_management1.st_prodcode={?APS: ITEM1.ST_Prodcode}
AND
stock_management1.st_prodcode={?FCS: stocktake1.S_ProdCode}
AND
stock_management1.st_prodcode={?CENTRAL: Command.mv_PRODCODE}
APS
SELECT `ITEM1`.`ST_SOH`, `ITEM1`.`ST_Prodcode`
FROM `aps`.`ITEM` `ITEM1`
WHERE `ITEM1`.`ST_Prodcode`={?pricing: stock_management1.st_prodcode}
EXTERNAL JOIN ITEM1.ST_Prodcode={?FKE: ITEM1_1.ST_Prodcode}
FKE
SELECT `ITEM1_1`.`ST_SOH`, `ITEM1_1`.`ST_Prodcode`
FROM `iewkelvin`.`ITEM` `ITEM1_1`
WHERE `ITEM1_1`.`ST_Prodcode`={?APS: ITEM1.ST_Prodcode}
EXTERNAL JOIN ITEM1_1.ST_Prodcode={?FCS: ITEM1_2.ST_Prodcode}

Thanks for comments imran & ralfbecher.
I found that the {? implies WHERE followed by the Database: table.field
Simple as that really.

Related

SQLAlchemy Join using PyBigquery to filter results

Using a SQLAlchemy class, I'm trying to generate a query that resembles
SELECT
DISTINCT(non_unique_key)
FROM
`tablename`,
UNNEST(tasks_dns) AS dns
WHERE
create_date_utc = TIMESTAMP("2020-12-31T23:59:59")
AND dns LIKE "%whatever%"
Being an implicit join using unnest(), I don't have a clue how to construct my statement.
Using a combination of .label() and moving the unnest() call around, I've managed to move the unnest clause to either the SELECT or WHERE clauses, but not in the FROM.
For example,
session.query(Table.non_unique_key).filter(func.unnest(Table.dns) != '').filter(Table.create_date == "2021-04-22")
leaves me with
SELECT `tablename`.`non_unique_key` AS `tablename_non_unique_key`
FROM `tablename`
WHERE unnest(`tablename`.`tasks_dns`) IS NOT NULL AND `tablename`.`create_date_utc` = %(create_date_utc_1)s
So far, using join() has just caused exceptions around not having a column to join on (which while yes, I understand what that means, I'm not sure how to get around that since an unnest is basically doing an expansion of a nested data type that doesn't have a column to join on.. which is probably where my ignorance around how to properly use SQLAlchemys join() method comes in)
Is this just a SQLAlchemy / BigQuery dialect issue at this point? Or am I just a dunce? I know the dialect library is still infant, but even with Postgres, I would have thought that this should be a somewhat common query pattern?
After some additional digging, I've figured it out
Model().query().select_from(func.unnest(Model.col1).alias("whatever")).filter()....

phpmyadmin SQL query multiple tables

I have two tables.
(1) compressors
(2) crankcase_heaters
I'm trying to create a SQL query to do:
Select the compressor.VOLTAGE and compressor.WATT of each compressor.PART_NUMBER
Find the crankcase_heater.PARTNO that has the same voltage and watts.
Add that value into a new field on the compressor table called "CRANKHTR"
Essentially this query will reproduce my compressors table but will have another 'column' called "CRANKHTR".
I'm completely lost on where to even start with this. I tried using the phpmyadmin SQL Query builder but i have no idea where to begin.
Without seeing the exact data structure, it sounds like you need a simple INNER JOIN:
SELECT
`cp`.`VOLTAGE`,
`cp`.`WATT`,
`ch`.`PARTNO` as CRANKHTR
FROM
`compressor` cp
INNER JOIN `crankcase_heaters` ch ON ch.VOLTAGE = cp.VOLTAGE AND ch.WATT = cp.WATT

MultipleObjectsReturned error in django admin - but there is no duplication in database

I'm having a problem in the Django admin. I'm using version 1.5.5
I have a Booth model which has a foreign key to my AreaLayout model, which then goes back through another few models with foreign and many2many keys. My model code can be seen on pastebin. The initial indication of the problem in admin is that AreaLayouts are being duplicated in the select dropdown in Booth admin. The MultipleObjectsReturned error (traceback) is being raised when I then try to save a new booth. I was able to trace this back to the SQL query that django is creating to grab the AreaLayout list:
SELECT `venue_arealayout`.`id`, `venue_arealayout`.`name`, `venue_arealayout`.`description`, `venue_arealayout`.`area_id`, `venue_arealayout`.`additional_notes`
FROM `venue_arealayout`
INNER JOIN `venue_area` ON (`venue_arealayout`.`area_id` = `venue_area`.`id`)
INNER JOIN `venue_venue` ON (`venue_area`.`venue_id` = `venue_venue`.`id`)
INNER JOIN `venue_venue_venue_type` ON (`venue_venue`.`id` = `venue_venue_venue_type`.`venue_id`)
INNER JOIN `venue_venuetype` ON (`venue_venue_venue_type`.`venuetype_id` = `venue_venuetype`.`id`)
WHERE (`venue_arealayout`.`id` = 66 )
This query produces a duplicate in MySQL when I run it there. Removing the final 2 JOINs results in a single result being returned (which is the desired result), whereas removing only the last join still results in duplication.
I tried running that query with SELECT * in place of selecting specific fields and the two results in that case are almost equal. The difference is that the venue in question has multiple venuetypes and I'm getting a result for each of those. Is there any way I can tell django not to include those joins for these queries, or is there a way I can get distinct results as far as AreaLayouts go?
I think you're being caught by the bug reported in ticket 11707. One of the comments mentions that it can cause a MultipleObjectsReturned exception.
All I can suggest is that you stop using limit_choices_to. Your models are fairly complex, so I can't immediately see which one is causing the problem.

No tables used?

I've been working with this bit of code for some time now, and now, after some tinkering, I thought I finally got it to work, then it gives me this error:
Error Number: 1096
No tables used
SELECT *
and this is the code
SELECT start.*, posts.did, COUNT(posts.pid)
FROM akia_starting_posts AS start
JOIN posts
ON posts.did = start.did
JOIN akia_users AS users
ON users.username = start.username
I'm pretty sure at the * my start is being used, so what is it talking about? It couldn't be any other code in the file, since when I take out this bit of code, it starts working.
Try to add backticks on sql string that has the word start on it. To begin try to use the one table in question first like.
SELECT 'start.*' FROM akia_starting_posts as 'start'
and if it works then you may incorporate it in fullscale join like the one you posted
with bacticks used.

Joomla 1.6 menu problem in mysql (execution time < 10 min)

I found that code in the bottom executes more than 10 minutes on my server. The server itself is quite good and I cannot find any suitable explanation for that.
I am using Joomla 1.6.3 the data is migrated from Joomla 1.5.23 using jUpgrade and MySQL client version is 5.1.45.
SELECT a.*,COUNT(DISTINCT m1.id) AS count_published,COUNT(DISTINCT m2.id) AS count_unpublished,COUNT(DISTINCT m3.id) AS count_trashed
FROM `j16_menu_types` AS a
LEFT JOIN `j16_menu` AS m1 ON m1.menutype = a.menutype AND m1.published = 1
LEFT JOIN `j16_menu` AS m2 ON m2.menutype = a.menutype AND m2.published = 0
LEFT JOIN `j16_menu` AS m3 ON m3.menutype = a.menutype AND m3.published = -2
GROUP BY a.id
ORDER BY a.id asc;
I would be very pleased if someone could help me since I am in big trouble :)
P.s. I have downloaded db and checked it on my computer - still the same, execution time is terrible. Is there any way to solve this problem? Or maybe to remove this sql part without significant changes in administration of joomla?
Well it was done quite faster on my PC but still the result is far from one which would satisfy my.
EDIT
Well, I've found a reported bug of this problem. http://joomlacode.org/gf/project/joomla/tracker/?action=TrackerItemEdit&tracker_item_id=24868
And the solution is:
CREATE INDEX idx_menu_published ON j16_menu (published);
However, I am not sure how this will affect administration. I there is anyone who could briefly tell how this part works and should I edit Joomla core code or just use the above code on mysql once. I am wondering if I should index table everytime when I edit menu.
A database / table Index is just a kind of yellow pages if you don't mind the comparison. It updates automatically. If this Index fixes your problem there is nothing else you need to do or do again. You can't break anything either - so just give it a try.