I have 2 tables, the first one of them is orderbook has a field namely
easy_order with datatype of tinyint(1) and the primary key is id.
The second table name is execution including the fields of buy_order_id and sell_order_id. These 2 fields are ref. from the id key from the orderbook table.
I would like to write a SQL to find all the rows from the execution table whose buy_order_id OR sell_order_id row from the orderbook table has the easy_order column value of 1.
I use MySQL database.
I would write it this way, avoiding repetition and dual nested selects:
select execution.*
from execution
inner join orderbook on
orderbook.id in (execution.buy_order_id, execution.sell_order_id) and
orderbook.easy_order = 1;
(You may prefer conceptually to put orderbook.easy_order = 1 in a where clause. It will produce the same results, so it's a matter of preference.)
I make it work The SQL is,
SELECT * FROM execution WHERE buy_order_id IN (SELECT id FROM orderbook WHERE easy_order=1)
OR
sell_order_id IN (SELECT id FROM orderbook WHERE easy_order=1);
Related
Edit2:
I like to get all values from 2 tables WHERE the value of highlight='1'
highligh is a column present in both tables. Both tables have an unique structure.
I want to get all data from both tables where highlight='1'
SELECT * FROM painting, instaview WHERE highlight='1'
...both give me the following error:
"Column 'highlight' in where clause is ambiguous"
I searched a lot and found a lot about this error but only complicated JOIN causes that are different than my case. i know the column is in both tables but I want the results of those 2 table queries joined.
Edit1:
As requested the structure of both tables:
Column Type Comment
id int(6) Auto Increment
category varchar(3)
filename varchar(30)
title varchar(200)
material varchar(200)
year varchar(4)
highlight tinyint(1)
active tinyint(1)
You need to say from which table, you are querying the highlight.
Can you try like this ?
SELECT * FROM painting JOIN instaview ON painting.highlight='1'
I did not know about the "qualify", but this joined my data in more columns, I just wanted the results of both tables. After more searching I found the keyword: UNION
SELECT * FROM painting WHERE highlight=1
UNION
SELECT * FROM instaview WHERE highlight=1
This did the trick!
(I like to excuse myself for not being very clear in my question, for me this was all very new)
If you want highlight='1' in both tables, and don't want to specify a table, you may use
SELECT *
FROM painting
JOIN instaview USING (highlight)
WHERE highlight='1'
The fields used in USING are "combined", and tablename qualification not needed.
PS. This is "bad practice" - specify tablenames for all fields when there are more than one table in whole query source (even when some fieldname is unique over data source - it is present in one table only).
My Access Database is slow when finding non-matching records
SELECT
RT3_Data_Query.Identifier, RT3_Data_Query.store, RT3_Data_Query.SOURCE,
RT3_Data_Query.TRAN_CODE, RT3_Data_Query.AMOUNT,
RT3_Data_Query.DB_CR_TYPE, RT3_Data_Query.status,
RT3_Data_Query.TRAN_DATE, RT3_Data_Query.ACCEPTED_DATE,
RT3_Data_Query.RECONCILED_DATE
FROM
RT3_Data_Query
LEFT JOIN Debit_AO_Query ON RT3_Data_Query.[Identifier] = Debit_AO_Query.[Identifier]
WHERE
(((Debit_AO_Query.Identifier) Is Null));
I'm doing a query of two queries I created. The last query is just to compare these two queries and show what is missing between them which is what i posted above. I'm matching an identifier between the two queries which looks like this 583005-01-20185804.33 which is a combination of store, date and amount.
Here is a link to the database:
https://wetransfer.com/downloads/15f912909fbe2ea0a5111e44b953d11a20190808195913/db9912
The query is slow because you don't use indexes on tables and join on concated fields (Identifier is Location & Date & Total)!
Each table needs a primary key or it is not a table! That should be an autonumber for the beginning!
Indexing:
Add a field called id to each table, datatype autonumber and make it PK.
Add a key for the fields compared in the join and the where clause (set all index properties (primary, unique, ignore) to no)!
for table RT3_Data (because it is huge create a copy first, then delete the data, or creating index will fail onMaxLocksPerFile):
store
AMOUNT
TRAN_DATE
after that reimport data from copy with query:
INSERT INTO RT3_DATA
SELECT [Copy Of RT3_DATA].*
FROM [Copy Of RT3_DATA];
for table Debit_AO:
Location
Total
Date (should be renamed as Date() is a VBA-Function)
Now change the queryRT3_Data_Query Without Matching Debit_AO_Queryto:
SELECT RT3_Data.store
,RT3_Data.SOURCE
,RT3_Data.TRAN_CODE
,RT3_Data.AMOUNT
,RT3_Data.DB_CR_TYPE
,RT3_Data.STATUS
,RT3_Data.TRAN_DATE
,RT3_Data.ACCEPTED_DATE
,RT3_Data.RECONCILED_DATE
FROM RT3_Data
LEFT JOIN Debit_AO
ON RT3_Data.[store] = Debit_AO.[Location]
AND RT3_Data.[AMOUNT] = Debit_AO.[Total]
AND RT3_Data.[TRAN_DATE] = Debit_AO.[DATE]
WHERE (
(
Debit_AO.Location IS NULL
AND Debit_AO.Total IS NULL
AND Debit_AO.[Date] IS NULL
)
);
Now the query executes in less than 10 seconds and for sure there are more optimizations (e.g composite index).
I have a table that has Act ID, and another table that has Act ID, percentage complete. This can have multiple entries for different days. I need the sum of the percentage added for the Act ID on the first tableZA.108381.080
First table
Act ID Percent Date
ZA.108381.110 Total from 2 table
ZA.108381.120
ZA.108476.020
ZA.108381.110 25% 5/25/19
ZA.108381.110 75 6/1/19
ZA.108381.120
ZA.108476.020
This would be generally considering not good practice. Your primary key should be uniquely identifiable for that specific table, and any other data related to that key should be stored in separate columns.
However since an answer is not a place for a lecture, if you want to store multiple values in you Act ID column, I would suggest changing your primary key to something more generic "RowID". Then using vba to insert multiple values into this field.
However changing the primary key late in a databases life may cause alot of issues or be difficult. So good luck
Storing calculated values in a table has the disadvantage that these entries may become outdated as the other table is updated. It is preferable to query the tables on the fly to always get uptodate results
SELECT A.ActID, SUM(B.Percentage) AS SumPercent
FROM
table1 A
LEFT JOIN table2 B
ON A.ActID = B.ActID
GROUP BY A.ActID
ORDER BY A.ActID
This query allows you to add additional columns from the first table. If you only need the ActID from table 1, then you can simplify the query, and instead take it from table 2:
SELECT ActID, SUM(Percentage) AS SumPercent
FROM table2
GROUP BY ActID
ORDER BY ActID
If you have spaces other other special characters in a column or table name, you must escape it with []. E.g. [Act ID].
Do not change the IDs in the table. If you want to have the result displayed as the ID merged with the sum, change the query to
SELECT A.ActID & "." & Format(SUM(B.Percentage), "0.000") AS Result
FROM ...
See also: SQL GROUP BY Statement (w3schools)
I need to insert identifiers (numbers) into a temporary table which satisfy several conditions.
I use insert into select structure.
One of the conditions is the next. There are tables received_posts_1(id,post_id), received_posts_2(id,post_id)...
Each selected identifier is part of a table name with received posts. I need to add an and part into the where clause of the next form.
and not exists(select 1 from CURRENT_RECEIVED_POSTS_TABLE where id = device_id and post_id = post_id_)
The insertion is in while cycle. The stop condition is a needed count of identifiers to be inserted.
Having multiple tables with the same structure is generally a sign of a poor database design. In general, it is much better to have a single table, with columns that distinguish what you are trying to do.
One approach is to create such a table using a view:
create view received_posts
select 1 as which, r.* from received_posts_1 r union all
select 2, r.* from received_posts_2 r union all
. . .;
You can then use this table in your query.
A more efficient method is to repeat the exists, with the right conditions:
not exists(select 1
from received_posts_1
where id = device_id and post_id = 1) and
not exists(select 1
from received_posts_2
where id = device_id and post_id = 2) and
. . .
As mentioned in the comments, you can use dynamic SQL if you know which post table you need for a particular invocation of the query.
I am writing an SQL query in order to fetch data, which is to be populated in the scrollable table .
Content id's of the contents which are going to be compared(Content1, Content2, Content3 .. in the image above) will be the input to the query.
Since, for comparison minimum 2 items are required, the number of id's passed to the query will always be 2 or greater than 2.
Following is the SQL of 3 tables, from which the required data is to be fetched.
Following table contains the Paramter names that are to be compared:
CREATE TABLE tbl_content_commons (
id integer PRIMARY KEY,
content_common_code char(20) NOT NULL,
content_common_name char(100) NOT NULL // The comparison label
)
Following table contains the code of comparison label(content_common_name char) in the table above and the Content id of the contents(which would be passed as the parameter for the query)
CREATE TABLE tbl_comparison_values (
id integer PRIMARY KEY,
tbl_content_common_id integer NOT NULL,// ID's of the Contents under comparison
userneed_comparison_label_id integer NOT NULL,// ID of comparison label in the table above
value char(50) NOT NULL// Value corresponding to a comparison label - if it exists for a given content id
)
Finally, the table containing the name of the contents(Content1, Content2..) whose id's were passed as parameters to the query
CREATE TABLE userneed_comparison_labels (
id integer PRIMARY KEY,
name char(50) NOT NULL// Name of the content whose id's are passed through queries. content ID in the table above
)
I have made enough of efforts in writing a query to fetch data that would help me populate the table shown in the attached image, but did not succeed. I can show the queries I have written, but since it again prolongs the question, I am not posting here.
Any guidance or help as to how to proceed, writing this SQL query would be much appreciated.
How about this...
select c.content_common_name,
l.name,
v.value
from userneed_comparison_labels l
left join tbl_comparison_values v on l.id = v.userneed_comparison_label_id
left join tbl_content_commons c on c.id = v.tbl_content_common_id
where c.id in (1, 2, 3)
See SQL Fiddle for more detail.
Choose SQLLite (SQL.js) version. Click cancel if it asks if you want to use WebSQL.