I have the following code below...the query works, but I'm looking for a better way to search though an entire column for a specific criteria. I think my question is going to require a loop, I'm just not sure how to perform it.
The last line of code states '
Where [dbIdwWhseLC].[dbo].[tbItemTxt].[sTxt] like '%258912.pdf
The value 258912.pdf is the value in
[IDEAUrlBot].[dbo].[IDEA Project Tracker].[Filename]
I would like to try and create a method where the query reads one value in sTxt, then compares the whole column to Filename. If it finds the value, then display sTxt, if not, go to the next value in sTxt and begin searching each value in Filename.
Please let me know if you need additional information. Thanks in advance.
Select [dbIdwWhseLC].[dbo].[tbItemTxt].[nItemId]
, [sTxtType]
, [IDEAUrlBot].[dbo].[tbl_IDWItems].[nUrlId]
, [IDEAUrlBot].[dbo].[tbl_Urls].[sUrl]
, [sTxt]
, [Filename]
, [dbIdwWhseLC].[dbo].[tbItemTxt].[vUpdateDt]
From [dbIdwWhseLC].[dbo].[tbItemTxt]
Left Join [IDEAUrlBot].[dbo].[tbl_IDWItems] on [dbIdwWhseLC].[dbo].[tbItemTxt].[nItemid] = [IDEAUrlBot].[dbo].[tbl_IDWItems].[nItemid]
Join [IDEAUrlBot].[dbo].[tbl_Urls] on [IDEAUrlBot].[dbo].[tbl_IDWItems].[nUrlId] = [IDEAUrlBot].[dbo].[tbl_Urls].[nUrlId]
Join [IDEAUrlBot].[dbo].[IDEA Project Tracker] on [IDEAUrlBot].[dbo].[tbl_IDWItems].[nUrlId] = [IDEAUrlBot].[dbo].[IDEA Project Tracker].[UrlId]
Where [dbIdwWhseLC].[dbo].[tbItemTxt].[sTxt] like '%258912.pdf'
If I understand you correctly, it ought to be possible to do this:
select itemTxt.[nItemId]
, [sTxtType]
, idwItems.[nUrlId]
, urls.[sUrl]
, [sTxt]
, [Filename]
, itemTxt.[vUpdateDt]
From [dbIdwWhseLC].[dbo].[tbItemTxt] as itemTxt
Left Join [IDEAUrlBot].[dbo].[tbl_IDWItems] as idwItems
on itemTxt.[nItemid] = idwitems.[nItemid]
Join [IDEAUrlBot].[dbo].[tbl_Urls] as urls
on idwItems.[nUrlId] = urls.[nUrlId]
Join [IDEAUrlBot].[dbo].[IDEA Project Tracker] projTracker
on itemText.[nUrlId] = projTracker.[UrlId]
Where itemTxt.[sTxt] like '%258912.pdf' -- not sure you intend this to remain
and projTracker.[FileName] = itemTxt.[sTxt]
But that's so simple that there must be some aspect to what you're looking for that's not clear to me.
Do you want to stop searching after you find a match between [FileName] and [sTxt]? If you want to return exactly one record, you can just change the first line to
select top 1 itemTxt.[nItemId]
... and add an ORDER BY clause to the end to control how the results are sorted and therefore which one is the "top 1".
Do you need to use wildcards when matching [FileName] and [sTxt]? It's not clear to me from the description which column would have the full path (or file name) and which would have just "258912.pdf", but you could change my last line to:
itemTxt.[sTxt] like ('%' + projTracker.[FileName])
If you need something more complex, like the first record from itemTxt.[sTxt] that matches projTracker.[FileName] for every record in projTracker, please say so in the comments.
If none of this is along the lines of what you need, you'll need to elaborate on what it is you do need. Please add more detail to your question, such as an example of what the output should look like or what you plan to do with it.
Related
LoadTableQuery.Qry = #"
SELECT `orders`.`ordID`,
`orders`.`OrderType`,
`prt`.`Name`,
`orders`.`prtID`,
`orders`.`ReqRef`,
`orders`.`DateOrdered`,
`orders`.`orderETA`,
`orders`.`DateRcvd`,
`orders`.`Status`,
`orders`.`ShipCost`,
`orders`.`Duties`,
`orders`.`Currency`,
`orders`.`Conv`,
`orders`.`Terms`,
`orders`.`Shipping`,
`orders`.`BuyerID`,
`orders`.`ShipTo`,
`orders`.`Notes2`,
`orders`.`Notes3`,
`orders`.`legacyID`,
`prt`.`Address`,
`prt`.`City`,
`prt`.`StateProv`,
`prt`.`PostalZip`,
`prt`.`Tel`,
`prt`.`Contact`,
`prt`.`Email`,
`prt`.`OrderTaxPc`
FROM `n2000`.`orders` left join `n2000`.`prt`
on (orders.prtID = prt.prtID)
prtID is a unique identifier which I'm using to pull the data I need about the party (their name, address, city..). The way it's written above works, accomplishing what I need it to do. getting and inserting the data into my table.
The issue is, I need a SECOND set of party data, based on orders.ShipTo instead of orders.prtID . ShipTo is really just a different prtID.
I'd like to be able to just run it again with
on (orders.ShipTo = prt.prtID).
I've tried using UNION, but that creates a new row, instead of adding columns to my current row. Which is where I'm looking to pull my data from.
The best way to understand it is, I've expanded prtID to add the Name, Address, City.. column data. I'd like to expand ShipTo as well. I don't know how to do that though without simply overwriting the prtID expansion.
The solution was actually quite simple. Hope this helps someone else
Solution
The solution was actually quite simple. Alias the different tables and do a double left join
Solution
I have a table of texts from various dates. Each is indexed by service, variation, and page and sub-page ids.
I need to fetch all entries for a given service, page and sub-page. i.e. each variation! BUT If the particular specific subpage doesn't exist, I need it to fetch the first subpage for that page, rather than nothing for that variation.
This is my code -
SELECT * FROM frames f
LEFT JOIN varients v ON f.varient_id = v.varient_id AND f.service_id = v.service_id
ẀHERE f.service_id = :sid
AND f.frame_id = :fid
AND (f.subframe_id = :subid
OR f.subframe_id = (
select min(subframe_id) from frames ff
ẀHERE ff.service_id = f.service_id
AND ff.varient_id = f.varient_id AND ff.frame_id = f.frame_id
)
)
GROUP BY f.service_id, f.varient_id, f.frame_id
ORDER BY f.service_id, v.varient_date, f.frame_id, f.subframe_id
but as often as not this just gives the minimum rather than the specific, even when the specific value exists. I'm pretty sure that the OR isn't what I need..
I've tried working with UNION as per some other answers, but since I want more than a single result, I can't seem to work it out!
Thanks for any help ..
OK. I managed to achieve what I wanted. Two days of messing with this, and finally work it out after posting a question. I was helped by one of the 'related' questions, which whilst not giving me an answer, made me think about it a different way -
I basically turned the whole thing inside out: As i only wanted one result per variation, I used that as the primary table. Then Joined the 'frames' table twice, once for the specific value, and once for the first value (found using a search for MIN().) Used IFNULL to return the second record if the first wasn;t found. An extra IS NOT NULL check against the second record within the WHERE to avoid returning anything where there is nothing stored against the variation record.
SELECT v.service_id, v.varient_id, IFNULL(f.frame_id,ff.frame_id)as frame_id,
IFNULL(f.subframe_id,ff.subframe_id) as subframe_id , IFNULL(f.frameunique, ff.frameunique) as frameunique, IFNULL(f.frame_content,ff.frame_content) as framecontent
FROM varients v
LEFT JOIN `frames` f ON `f`.`varient_id` = `v`.`varient_id` AND `f`.`service_id` = `v`.`service_id` AND `f`.`frame_id` = 698 AND `f`.`subframe_id` = 0004
LEFT JOIN `frames` ff ON `ff`.`varient_id` = `v`.`varient_id` AND `ff`.`service_id` = `v`.`service_id` AND `ff`.`frame_id` = 698
AND `ff`.`subframe_id` = (select min(`subframe_id`) from `frames` `fff` where `fff`.`service_id` = `v`.`service_id`
AND `fff`.`varient_id` = `v`.`varient_id` AND `fff`.`frame_id` = 698 )
where `v`.`service_id` = 3 AND ff.frameunique IS NOT NULL
ORDER BY `f`.`service_id`, `v`.`varient_date`, `f`.`frame_id`, `f`.`subframe_id`
Posting this in case it helps anybody else. It still needs tidying but it works. Thanks for the comments. :)
I have a nested repeated structure, the repeated structure is of variable length. For example, it could be a person object with a repeated structure that holds cities the person has lived in. I'd like to find the last item in that list say to find current city person lives in. Is there an easy way to do this, I tried looking around jsonpath functions but I'm not sure how to use it with "within". Any help please?
1) You can use LAST and WITHIN
SELECT
FIRST(cell.value) within record ,
LAST(cell.value) within record
FROM [publicdata:samples.trigrams]
where ngram = "! ! That"
2) or if you want something more advanced you can use POSITION
POSITION(field) - Returns the one-based, sequential position of field within a set of repeated fields.
You can check the samples from trigrams (click on Details to see the unflatten schema)
https://bigquery.cloud.google.com/table/publicdata:samples.trigrams?pli=1
And when you run POSITION, you get the ordering of that field.
SELECT
ngram,
cell.value,
position(cell.volume_count) as pos,
FROM [publicdata:samples.trigrams]
where ngram = "! ! That"
Now that you have the position, you can query for last one.
Here is the query I have to call each time a new item is added to the database :
UPDATE `cas_phocagallery`,`cas_phocagallery_categories`
SET cas_phocagallery.description=concat(
cas_phocagallery_categories.title,'<br />',cas_phocagallery.description)
WHERE cas_phocagallery.catid = cas_phocagallery_categories.id;
It works fine except that it produces a duplicate each time I run the query.
I tried to add distinct but I get an error and I am not sure this is the right thing to do in this case.
Thank you for your cooperation, I have been looking at all kinds of ways all day with no success.
When you have a unique key, it should produce an error when inserting a duplicate value and is expected behaviour. I think what you should be looking at is on duplicate key update where you can specify what to do if a duplicate is found.
Every time you run the query, you add the title to the current description. If you run it three times in a row, you will get
title <-- added by 3rd
title <-- added by 2nd
title <-- added by 1st
description (original)
Is that what you mean? That is exactly what the query is designed to do. Maybe you need another column to keep the "base_description", so that each time, "description" gets to be "title" + br + "base_description"
EDIT
You can try something like this, which would work 95% of the time, until the title changes in which case you will get the historical titles built up. It also doesn't work when the title contains the % symbol and may work funny if it contains the _ character.
UPDATE cas_phocagallery, cas_phocagallery_categories
SET cas_phocagallery.description=concat(
cas_phocagallery_categories.title,'<br />',cas_phocagallery.description)
WHERE cas_phocagallery.catid = cas_phocagallery_categories.id
AND NOT cas_phocagallery.description
LIKE concat(cas_phocagallery_categories.title, '%')
Can't really comment on Joomla or how it does things or why this would be necessary (to merge title into description). What you should really do is in some SELECT statement in some module, show the result of the CONCAT instead of persisting (pushing/mangling) it into description.
SELECT cas_phocagallery.*, concat(cas_phocagallery_categories.title,
'<br />',
cas_phocagallery.description) as FullDescription
FROM cas_phocagallery
LEFT JOIN cas_phocagallery_categories
ON cas_phocagallery.catid = cas_phocagallery_categories.id
I want to use CakePHP to pull an array of photos from a database, sorted by photo title (0, 1, 2, 3...) My query currently looks something like:
$ss_photos = $this->Asset->find('all',array(
'conditions'=>array('kind'=>'photo'),
'order'=>'title'
));
Unfortunately the titles seem to be in string format, leading to an undesirable sort order (2.jpg after 19.jpg, etc). Is there a quick way to cast 'title' as an int for ordering purposes within a Cake query of this type?
Not sure if this is "recommended practice", but on a first pass it seems to work:
$ss_photos = $this->Asset->find('all',array(
'conditions'=>array('kind'=>'photo'),
'order'=>'Asset.title + 0'
));
Any opinions?
The solution is to create a hidden column which is responsible for orders in your example image names should be: 00002.jpg, 00019.jpg - this way the order will work properly.
If the results are not too many, I think it's easier to sort them in PHP (if you use it of course :)) See this natsort() you just need to extract a list of images and to sort them.