Hello I have the following query:
SELECT *
FROM ams.TestResultHiPot_Archive,ams.unit u
WHERE timestamp >='3/16/2017 20:39 ' AND timestamp <= '3/17/2017, 20:39' AND LOWER(line_code)=LOWER('aac04')
AND LOWER(unitmodelnumber) like LOWER('%%%') AND unitmodelnumber != 'VTI' and u.serial_num=unitserialnumber and u.date_deleted is null
this table has many fields so I want to stay away from hard coding each field AND also this structure works for multiple tables.
The only issue I am having is some of the comparison items are in the units table so I want to visit that tablet to compare and if it matches then include that record in my result from the ORIGINALTEST RESULTS ARCHIVE table NOT the units table.
the main issue I am seeing is that the select * is causing both tables to return all of their fields.
Is there a way to use select * ,and still compare 2 tables but get the columns ONLY from one table ?
I have tried right join, left join, inner join but nothing seems to work, they all return all the columns from both tables, maybe I have done it incorrectly, or maybe this can't be done?
I also thought maybe doing like a query that selects all the table fields and then storing them in an array and passing that array as my select parameters, that way I am passing the exact needed parameters without hardcoding (since I would always consult the table) but that seems like it would take longer since pgsql is slower. Any suggestions are greatly appreciated.
select ams.TestResultHiPot_Archive.*
Related
I'm thinking of switching to using temp tables and vba.
I want to do this. I have multiple tables, in these tables may or may not have fields with items that have a one to many or one to one relationship. I know what those relationships are (and will create multiple queries accordingly). What I'm hunting for is each value that DOES NOT EXIST in every other table. To make an example:
Say we have 3 single column tables, table 1 is {x, y, z}, table 2 is {a, x, z}, and table 3 is {a,b,x,y,z}, the result will be b for t3 (yes I need to know where the error is). Pretty much, I want to use the unequal wizard but for 3 or more tables.
I may want to look for any item that exists in some but not all other tables. If you want to speak on that, it would be helpful, but I think that is strictly in the vba realm.
I think the challenge here is the open-endedness of the problem you are trying to solve. Varying column names, table names, and uniqueness thresholds across all tables would make it a bit more difficult. In the way I show below, I don't think it would be the most efficient, query-wise, but would be relatively easy to script. The following code assumes values in the tables are unique within each table.
There are 3 queries total:
qry_001_TableValues_ALL
SELECT Table1.MyValue, "Table1" AS Source
FROM Table1
UNION
SELECT Table2.MyValue, "Table2" AS Source
FROM Table2
UNION SELECT Table3.MyValue, "Table3" AS Source
FROM Table3;
qry_002_TableValues_Unique:
SELECT qry_001_TableValues_ALL.MyValue
FROM qry_001_TableValues_ALL
GROUP BY qry_001_TableValues_ALL.MyValue
HAVING (((Count(qry_001_TableValues_ALL.MyValue))=1));
qry_003_TableValues_UniqueWithSource:
SELECT qry_002_TableValues_Unique.MyValue, qry_001_TableValues_ALL.Source
FROM qry_002_TableValues_Unique INNER JOIN qry_001_TableValues_ALL
ON qry_002_TableValues_Unique.MyValue = qry_001_TableValues_ALL.MyValue;
The first table is the one you would need to script out if columns\tables changed. It is looking across all tables and creating a unique list of values from the specified field. The second query looks to look up the Source table name against the original unique value query for all values which have a count of 1, post aggregation. This means of all tables involved, there is only one instace of the values returned, and it joins against the original unique value list again to determine what the source table is. You can script a change to the HAVING clause here to see if there are x tables which contain the value. The final query is simply the one you run to give you the final report of the values you are looking for and where they reside.
Hope this is in the ballpark of what you are trying to do.
I have a mysql table that has a column "my_set", a non-unique key, and another column "my_element", a unique key. Each my_sel value can correspond to multiple my_element values, whereas each my_element corresponds to only one my_set.
All values of theese two columns are integers unsigned (11).
Starting from a my_element value, in a single query and without nested selects, I need to find all other my_elements that has same my_set.
The solution I would think was nested select
select my_element
from table
where my_set = (
select my_set
from table
where my_element = <elementValue>
)
But, as I explained, I'd like to find a better, maybe faster way to do it without subselect, as performance is being an issue due to the huge number of similar queries in the db scheduled maintenance phase.
Also, better db structure advice could be appreciated, but currently db refactoring is not allowed.
I am not 100% sure what you are asking, but I will try to answer from what I understood.
I think you need to use self join to get all the elements related to given element( which are related by same my_set). Try the below query.
select t2.my_element
from table t1
join table t2 on t1.my_set = m2.my_set and t2.my_element <> t1.my_element
where t1.my_element = "element";
If it does not work. Create a sql fiddle with sample data, that would make it easy for us.
You can try the below query. If i understand it correctly, use the left outer join between two tables to get the ordered set of set and element.
select
mysetid,
myelementid
from
tableset a
left outer join
tableset b
on
a.setid = b.setid
order by
a.setid,b.elementid
Thanks.
I'm trying to figure out the best way to get data from a MySQL database and process it. I have 2 tables 'objects', and 'objects_metadata'. rows in the objects_metadata table belong to rows in the objects table and the link is defined by a 'parent_id' column in objects_metadata that corresponds to an 'id' column in objects. (SQLFiddle below).
The Scenario
When I search against these tables I'm always looking for rows from the objects table. I sometimes have to query the objects_metadata table to get the right results. I do this by defining boundaries such as "hasMetadataWithValue". This boundary would run the following query by itself:
SELECT * FROM objects
INNER JOIN objects_metadata ON objects.id=objects_metadata.parent_id
WHERE objects_metadata.type_id = ? AND objects_metadata.value = ?
Another example boundary "notSelf" would use a query such as:
SELECT * FROM objects WHERE objects.id != ?
My scenario caters for multiple boundaries at a time. For a row from the objects table to be selected it MUST pass all boundaries. (i.e. if each boundary query was run independently the row would appear in every set of results)
I'm wondering if anyone has any thoughts on the best way to do this?
Use each boundary's query as a subquery in a single query on the database (my original goal)
Run each boundary's query as a full query and then use PHP to process the results
I would prefer to make the database do most of the work and spit out the results simply to avoid running a bunch of queries instead of a single one. Here's the tricky part, I've tried to create a full query using subqueries, but I'm not getting the hang of it at all. My latest attempt is below:
SELECT * FROM objects
WHERE type_id = 7
AND confirmed = 1
AND (SELECT * FROM objects WHERE objects.id != 1)
AND (SELECT * FROM objects LEFT JOIN objects_metadata ON objects.id=objects_metadata.parent_id WHERE objects_metadata.type_id = 8 AND objects_metadata.value ='male')
LIMIT 0,20
I can see that the way I'm trying to use these subqueries is obviously wrong, but I can't figure out what the right way is.
SQL Fiddle is here
Any insights into the best way of doing this would be much appreciated.
I think you can just put those 'boundaries' inside your joined query.
SELECT
*
FROM objects LEFT JOIN objects_metadata
ON objects.id = objects_metadata.parent_id
WHERE
objects_metadata.type_id = 8
AND objects.confirmed=1
AND ( objects.id!=1 )
AND ( objects_metadata.type_id=8 AND objects_metadata.value='male' )
LIMIT 0,20
SQL Fiddle: http://sqlfiddle.com/#!2/0ee42/34
Just mind the same column names for both tables, so you have to specify the exact table as well (e.g., objects_metadata.type_id = 8). If I completely misunderstand your question let me know! :)
I'm a novice SQL programmer and have been banging my head against this all morning, so please bear with me. My situation is this: I have a table of SKUs that need to be sent to our eCommerce website. Each of these SKUs has a 'quantity', an 'active' value, and a 'discontinued' value. This was easy enough to handle when we were dealing with one SKU at a time, but now I have to send kits, which contain one or more SKUs.
For example, if my Kit's ID is 000920_001449_001718_999999 (a combination of four SKUs) I need to collect data for the entire set of SKUs like so:
Here's the logic I need to incorporate:
If any of the SKUs have null or WEBNO as an IsActive value, the entire kit must return WEBNO. Otherwise, return WEBYES.
If any of the SKUs have null or '1' as an IsDiscontinued value, the entire kit must return IsDiscontinued = '1'. Otherwise, return a 0.
My code is a bit of a mess, but here's what I've managed so far:
SELECT
CASE WHEN 'WEBNO' in
(
SELECT IsActive
FROM #SkusToSend as Sending
RIGHT JOIN
(
SELECT * FROM [eCommerce].[dbo].[Split] (
'000920_001449_001718_999999'
,'_')
) as SplitSkus
on Sending.SKU = SplitSkus.items
) THEN 'WEBNO'
ELSE 'WEBYES'
END
My question is this: Is it possible to write a statement that parses through my example table, returning only one row of 'IsActive' and 'IsDiscontinued'? I've tried using GROUP BY and HAVING statements on those fields, but always get multiple rows returned.
The code I have handles the WEBNO value, but not NULL, and doesn't even start to take into consideration the IsDiscontinued field yet. Is there a concise way to parse this together, or a better way to handle this type of problem?
I think a combination of ISNULL and MIN / MAX should do the trick:
SELECT
MIN(ISNULL(sending.IsActive, 'WEBNO')) AS IsActive,
MAX(ISNULL(sending.IsDiscontinuted, 1)) AS IsDiscontinuted
FROM
(
SELECT * FROM [eCommerce].[dbo].[Split] (
'000920_001449_001718_999999'
,'_')
) AS SplitSkus
LEFT JOIN #SkusToSend AS Sending
AS Sending.SKU = SplitSkus.items
I think this would be easier if you had a working example of some sample data in those tables. From guessing it looks like you have a table function splitting a string apart and giving multiple rows. You have some temp table that right joins to that so that is taking the function and essentially returning all rows it gets even if there are nulls in the temp table. This could return multiple rows as if you have a condition where you expect a single entity on a left or right join and there is a null at times you will get multiples. Or if you have a value repeated you will get multiples. You would have to ensure that you get one one result I am believing from your
Case when 'WEBNO' in
(
As while the logic may be correct to return the 'WEBNO' answer, it may be repeating the row result multiple times as the engine may interpret 'this happened' once, twice, three times. You could alleviate this by potentially doing a
'Select Distinct IsActive'
Which will make the expression return only a single result that is distinct for that column return.
Again this would be easier if we could see examples of what data those objects contained but this would be my guess.
I have multiple select statements from different tables on the same database. I was using multiple, separate queries then loading to my array and sorting (again, after ordering in query).
I would like to combine into one statement to speed up results and make it easier to "load more" (see bottom).
Each query uses SELECT, LEFT JOIN, WHERE and ORDER BY commands which are not the same for each table.
I may not need order by in each statement, but I want the end result, ultimately, to be ordered by a field representing a time (not necessarily the same field name across all tables).
I would want to limit total query results to a number, in my case 100.
I then use a loop through results and for each row I test if OBJECTNAME_ID (ie; comment_id, event_id, upload_id) isset then LOAD_WHATEVER_OBJECT which takes the row and pushes data into an array.
I won't have to sort the array afterwards because it was loaded in order via mysql.
Later in the app, I will "load more" by skipping the first 100, 200 or whatever page*100 is and limit by 100 again with the same query.
The end result from the database would pref look like "this":
RESULT - selected fields from a table - field to sort on is greatest
RESULT - selected fields from a possibly different table - field to sort on is next greatest
RESULT - selected fields from a possibly different table table - field to sort on is third greatest
etc, etc
I see a lot of simpler combined statements, but nothing quite like this.
Any help would be GREATLY appreciated.
easiest way might be a UNION here ( http://dev.mysql.com/doc/refman/5.0/en/union.html ):
(SELECT a,b,c FROM t1)
UNION
(SELECT d AS a, e AS b, f AS c FROM t2)
ORDER BY a DESC