Trying to get use_labels=True to work on this query below...but the library keeps spitting errors....i'm doing this complicated join and need to see the labels on the columns to try and grab the data. I know i shouldn't be using an ORM session to execute() but frankly, idc at this point and just want to get it to work. I have given up on ORM...doesnt work with the query below:
data = DBSession.execute(text("SELECT \
* \
FROM \
art_packs \
LEFT OUTER JOIN \
art_pack_pieces AS art_pack_pieces_1 ON art_packs.art_pack_id = art_pack_pieces_1.art_pack_id \
LEFT OUTER JOIN \
art_pack_pieces_artists AS art_pack_pieces_artists_1 ON art_pack_pieces_1.art_pack_piece_id = art_pack_pieces_artists_1.art_pack_piece_id \
LEFT OUTER JOIN \
users ON art_pack_pieces_artists_1.artist_id = users.id \
LEFT OUTER JOIN \
art_pack_pieces as art_pack_pieces_2 ON art_packs.cover_id = art_pack_pieces_2.art_pack_piece_id \
LEFT OUTER JOIN \
art_pack_pieces_likes as art_pack_pieces_likes_1 ON art_pack_pieces_1.art_pack_piece_id = art_pack_pieces_likes_1.art_pack_piece_id AND art_pack_pieces_likes_1.artist_id = " + str(artistID) + " \
WHERE art_packs.art_pack_id = " + id, use_labels=True)).fetchall()
Query ain't the safest..but the int's are already validated so nothing wrong with what i'm doing i suppose.
Thanks.
Related
I need to translate this query which works in SQL Server to MySQL. I've circled around it at least 50 times and am stumped and cannot even find a starting point.
The issues in a nutshell:
It has not one but two FULL OUTER joins which MySQL does not do. I
know this can be emulated some how using UNION in combination with LEFT and RIGHT joins.
It joins the tables on multiple conditions and I am not sure how to add exclusion
criteria in the ON if doing a typical RIGHT JOIN
SELECT COALESCE(ca.reporting_period, s_p.reporting_period, s_uy.reporting_period) AS reporting_period,
COALESCE(ca.state, s_p.state, s_uy.state) AS state,
COALESCE(ca.servicer, s_p.servicer, s_uy.servicer) AS servicer,
COALESCE(ca.product, s_p.product, s_uy.product) AS product,
COALESCE(ca.product_group, s_p.product_group, s_uy.product_group) AS product_group,
COALESCE(ca.portfolio, s_p.portfolio, s_uy.portfolio) AS portfolio,
COALESCE(ca.channel, s_p.channel, s_uy.channel) AS channel,
ca.Gross,
ca.Costs,
ca.commission,
ca.commissionable,
s_p.WAVG_placed_numerator,
s_p.place_balance AS WAVG_placed_denominator,
SUM(s_uy.cum_gross) AS gross_uy_num,
SUM(s_uy.cum_netnet) AS netnet_uy_num,
SUM(s_uy.cum_commission) AS servicer_uy_num,
SUM(s_uy.num_accounts) AS uy_den,
ca.costs_recovered
# INTO adhoc_work.RevenueReport_Legal
FROM adhoc_work.Cash ca
FULL OUTER JOIN adhoc_work.Summary_placed s_p
ON ca.reporting_period = s_p.reporting_period
AND ca.state = s_p.state
AND ca.servicer = s_p.servicer
AND ca.product = s_p.product
AND ca.product_group = s_p.product_group
AND ca.portfolio = s_p.portfolio
AND ca.channel = s_p.channel
FULL OUTER JOIN adhoc_work.Summary_uy s_uy
ON ca.reporting_period = s_uy.reporting_period
AND ca.state = s_uy.state
AND s_p.state = s_uy.state
AND ca.servicer = s_uy.servicer
AND s_p.servicer = s_uy.servicer
AND ca.product = s_uy.product
AND s_p.product = s_uy.product
AND ca.product_group = s_uy.product_group
AND s_p.product_group = s_uy.product_group
AND ca.portfolio = s_uy.portfolio
AND s_p.portfolio = s_uy.portfolio
AND ca.channel = s_uy.channel
AND s_p.channel = s_uy.channel
WHERE COALESCE(ca.reporting_period,s_p.reporting_period,s_uy.reporting_period) BETWEEN DATE_FORMAT(TIMESTAMPADD(MONTH,-13,NOW(3)),'%Y%m%d') AND DATE_FORMAT(NOW(3),'%Y%m%d')
GROUP BY COALESCE(ca.reporting_period,s_p.reporting_period,s_uy.reporting_period)
,COALESCE(ca.state,s_p.state,s_uy.state)
,COALESCE(ca.servicer,s_p.servicer,s_uy.servicer)
,COALESCE(ca.product,s_p.product,s_uy.product)
,COALESCE(ca.product_group,s_p.product_group,s_uy.product_group)
,COALESCE(ca.portfolio,s_p.portfolio,s_uy.portfolio)
,COALESCE(ca.channel,s_p.channel,s_uy.channel)
,ca.gross
,ca.Costs
,ca.commission
,ca.commissionable
,s_p.WAVG_placed_numerator
,s_p.place_balance
,ca.costs_recovered;
Actually, your query doesn't quite do a full outer join, because a condition like this:
ca.state = s_uy.state
requires that ca.state be matched. That is, the outer join is turned to an inner join.
So, to answer your question more theoretically, the simplest method is probably to generate all the rows by using UNION among the the table and then use left join:
FROM (SELECT reporting_period, . . .
FROM adhoc_work.Cash ca
UNION -- on purpose to remove duplicates
SELECT reporting_period, . . .
FROM adhoc_work.Summary_placed
UNION -- on purpose to remove duplicates
SELECT reporting_period, . . .
FROM adhoc_work.Summary_uy
) x LEFT JOIN
adhoc_work.Cash ca
ON x.reporting_period = ca.reporting_period AND
. . . LEFT JOIN
adhoc_work.Summary_placed s_p
ON x.reporting_period = s_p.reporting_period AND
. . . LEFT JOIN
adhoc_work.Summary_uy s_uy
ON x.reporting_period = s_uy.reporting_period AND
. . .
I have a broken Access query and the reason it is broken is that one of the tables it is referencing doesn't exist so I can't see the query in design view - problem is I normally work in SQL Server and some of the formatting is bizarre to me - I need to take out all references to 'RMS_Import' table - any ideas?
Thanks!
SELECT Temp_LockedList.DEPARTMENT, PO_EmployeeName.Employee, PO_EmployeeName.BU, PO_EmployeeName.[PO Type], Temp_LockedList.STOCK_CATEGORY, Temp_LockedList.PO_NUMBER, Temp_LockedList.MATERIAL, Temp_LockedList.DESCRIPTION, Temp_LockedList.PO_ISSUE_DATE, Date()-[PO_ISSUE_DATE] AS [Days Since Issue], Temp_LockedList.PO_REQ_SHIP_DATE, IIf([LT] Is Null,IIf([dbo_articlenumbers].[LeadTime] Is Null,[rms_info].[leadtime],[dbo_articlenumbers].[LeadTime]),[LT]) AS [Prod LT], Temp_LockedList.PO_ORDERED_QUANTITY, Temp_LockedList.PO_BALANCE_QUANTITY, Temp_LockedList.Color, Temp_LockedList.ORIGINAL_SEASON, Temp_LockedList.CURRENT_SEASON, Temp_LockedList.LAUNCH_DATE, Temp_LockedList.WORKING_NUMBER, Temp_LockedList.IB_LOCK, Temp_LockedList.ORDER_PRIORITY_INDICATOR, Temp_LockedList.RANGE_TYPE_DESC, IIf([ActivationStatus] Is Null,[Lifecycle1] & "/" & [LO_Track1],[ActivationStatus]) AS [Activation Status / Prod Stat / Dev- LO], IIf([SignoffDate] Is Null,[RMS_Info].[Buy Ready],[SignoffDate]) AS [Buy Ready Date/ Sign off Date], RMS_Import.long_descr, RMS_Import.ret_from, RMS_Import.ret_to, IIf([MinOfCreateDate] Is Null,Date(),[MinOfCreateDate]) AS [First Shown on rpt], IIf(IsNull([LockedList - 2].[US PO #]),Null,"x") AS [In prior rpt]
FROM ((((((Temp_LockedList LEFT JOIN dbo_ArticleNumbers ON Temp_LockedList.MATERIAL = dbo_ArticleNumbers.ArticleNumber) LEFT JOIN PO_EmployeeName ON (Temp_LockedList.PO_NUMBER = PO_EmployeeName.PO_NUMBER) AND (Temp_LockedList.PO_ITEM_NUMBER = PO_EmployeeName.PO_ITEM_NUMBER) AND (Temp_LockedList.MATERIAL = PO_EmployeeName.MATERIAL)) LEFT JOIN RMS_Info ON Temp_LockedList.MATERIAL = RMS_Info.Article) LEFT JOIN [LockedList - 2] ON (Temp_LockedList.PO_NUMBER = [LockedList - 2].[US PO #]) AND (Temp_LockedList.MATERIAL = [LockedList - 2].Article)) LEFT JOIN [LockedList - 4] ON (Temp_LockedList.PO_NUMBER = [LockedList - 4].[US PO #]) AND (Temp_LockedList.MATERIAL = [LockedList - 4].Article)) LEFT JOIN RMS_Import ON Temp_LockedList.MATERIAL = RMS_Import.artnr) LEFT JOIN exceptions ON Temp_LockedList.PO_NUMBER = exceptions.[SAP PO NUMBER]
ORDER BY Temp_LockedList.DEPARTMENT, Temp_LockedList.PO_NUMBER, Temp_LockedList.MATERIAL;
A quick and dirty GUI way would be to just make a table RMS_Import edit the query, use the editor to remove it from the query, save the query and then delete the RMS_Import table
Manually you should be able to just remove the text LEFT JOIN RMS_Import ON Temp_LockedList.MATERIAL = RMS_Import.artnr from the query string - it's flanked by parenthesis so it's easy to single out
In both cases you will have unreferenced columns that will prompt for user input each time until you remove them from the query editor columns
I have a MySQL Command and it looks like this
UPDATE Variance as VAR INNER JOIN receiving as REV ON (VAR.ItemCode = REV.ItemCode)
SET VAR.Receiving = REV.QtyPack * REV.PCS + REV.QtyStan;
and its working perfectly.
now my question here is how can I apply this criteria to mysql command?
1.Receiving.Date is Between 2 Dates
2.Receiving.Status = "Posted"
3.If Receiving.QtyPack and Receiving.QtyStan = "0" or "NULL" then Var.Receiving will become "0"
I hope you dont downvote this.
Please do not hesitate to ask question or clarification and I will edit my post
I think you can just add the conditions directly into the query:
UPDATE Variance VAR INNER JOIN
receiving REV
ON VAR.ItemCode = REV.ItemCode
SET VAR.Receiving = COALESCE(REV.QtyPack * REV.PCS + REV.QtyStan, 0)
WHERE rev.Status = 'Posted' AND
rev.date between $date1 and $date2;
The COALESCE() converts NULL values to 0, which is part of the third condition.
I met some problem that wired me the whole day. I'm using flask/flask-sqlalchemy/postgresql, and I want to do this:
published_sites = msg_published.query \
.filter( and_(*filter_clause) ) \
.group_by( msg_published.site_id ) \
.order_by( order_clause ) \
.paginate( page_no, per_page, error_out = False )
but in mysql, it is OK, and in postgresql it is wrong and ask for the other fields besides site_id either in a group by clause or in a aggregation function, I know that postgresql is stricter on SQL than mysql , so I must select the site_id in the query object of msg_published, but in pure sqlalchemy I can do like this:
published_sites = session.query( msg_published.site_id ) \
.filter( and_(*filter_clause) ) \
.group_by( msg_published.site_id ) \
.order_by( order_clause ) \
.paginate( page_no, per_page, error_out = False )
and in flask-sqlalchemy, how to get it work?
You're most of the way there. to do in PostgreSQL what MySQL allows requires a subselect.
published_sites_ids = session.query( msg_published.site_id ) \
.filter( and_(*filter_clause) ) \
.group_by( msg_published.site_id ) \
.order_by( order_clause ) \
.paginate( page_no, per_page, error_out = False )
published_sites = session.query(msg_published) \
.filter(msg_published.id.in_(published_sites_ids))
When I run the SQL script to export data out of MySQL database, using command line, I get the above error.
The SQL query works fine when run in phpMyAdmin, but just when run from command line throws an error.
Here is the command line I am using:
cat my_export | mysql -uxyzuser -pabcpassword mydb > export072911.txt
The code in my_export is as follows:
SELECT CONCAT( custfirstname, ' ', custlastname ) AS fullname, custcompany, \
SPACE( 10 ) AS custtitle, custaddressone, custaddresstwo, custcity, custstate, \
custzip, SPACE( 10 ) AS dummy, custphone, SPACE( 10 ) AS custfax, custemail, \
event_id, SPACE( 10 ) AS ticket1, SPACE( 10 ) AS ticket2, \
SPACE( 10 ) AS ticket3, SPACE( 10 ) AS ticket4, orderdate, b.quantity, \
FROM order_master a \
LEFT JOIN order_detail b ON b.order_master_id = a.id \
LEFT JOIN customer c ON c.email = a.custemail \
WHERE a.orderdate > '2010-12-01'\
AND a.event_id = '30' \
AND a.orderstatus = 'O' \
AND b.litype = 'ITEM' \
AND b.reftag = 'PKG' \
ORDER BY a.orderdate DESC;
You can safely delete all the backslashes and use input redirection rather than piping. The backslashes are needed if you are working with the SQL as a shell variable, but not for piping or redirection.
mysql -uxyzuser -pabcpassword mydb < my_export > export072911.txt
UPDATE After a quick test of my own, it looks like the pipe works just as well as input redirection as long as the backslashes are removed.