I hope I can explain this properly.
This is what the result of the table looks like
Content_IDs of 4,5,6 are NEWS. As long as there is one boolean of true the column IsContentUpdated for NEWS, I'd like the distinct NEWS boolean to be true otherwise false.
Ultimately what I'd just want to get is
I tried with a temp table with a count of IsContentUpdated but it didn't work very well.
I'm kind of convinced I shouldn't need a temp table but am having a brain cramp trying to think what I can do here.
Any help is greatly appreciated. Thanks!
not sure I understand well, but if what you wanna get is what you show, it should be
SELECT Page_ID, MAX(CAST(IsContentUpdated AS INT))
FROM Table
GROUP BY Page_ID
This will give you a distinct list of the Page_IDs and whether or not any of them have a IsContentUpdated = 1:
select distinct Page_ID, max(IsContentUpdated)
from myTable
group by Page_ID
Related
sorry in advance for beeing stupid :)
I have the following tables
I want to search for different values and then get a group_id back that contains all the values I'm looking for. Is that possible, or does the structure have to be changed for this?
For example if you only need to select group_id for which value A, B and C all are available you can use group by with aggregation like below:
SELECT group_id FROM envelope_characteristic
WHERE value IN ('A','B','C');
GROUP BY GROUP_ID
HAVING COUNT(DISTINCT VALUE)=3
As far as I can tell your existing structure should be OK.
You could write a query that goes like this:
SELECT group_id FROM envelope_characteristic WHERE value IN (...);
It'll return all the group_ids that contain the values you're searching for.
I am new with the MS access SQL and the following is what I would like to do
in my database table
I have the following data
basically no duplicate, I tried "Distinct" but given my result is not one column only so I can't do that. I research online and look like it might need to use inner join but I only have one table only.
Appreciate if anyone can help me up..
Thanks
Regards,
Marc
You should be able to run the following query:
SELECT DISTINCT name, birthday
FROM [table];
select DISTINCT column1, column2, ...
from TABLENAME;
Replace columns and table name with respective values.
like this
select DISTINCT name, birthday from TABLENAME;
I need to select from all users that are not present in a set of other sub queries:
SELECT user_id
FROM (<userSubQuery1>)
WHERE
user_id NOT IN (<badUserSubQueryA>) AND
user_id NOT IN (<badUserSubQueryB>) AND
user_id NOT IN (<badUserSubQueryC>)
Only I need to do the NOT IN filters in many different queries where the userSubQuery and the badUserSubQueries may be different. E.g.:
SELECT user_id
FROM (<userSubQuery2>)
WHERE
user_id NOT IN (<badUserSubQueryB>) AND
user_id NOT IN (<badUserSubQueryC>) AND
user_id NOT IN (<badUserSubQueryD>)
All the sub queries, both the ones I'm selecting from and the once used in the NOT IN are complex so I don't want to duplicate the code for the NOT IN sub queries which are often the same (badUserSubQueryB and badUserSubQueryC in my example).
I could achieve this with dynamic sql but I'd rather not if I can avoid it. Is it possible?
How about storing all the bad user IDs in an indexed temporary table from which you can filter?
How about creating a view for each complex subquery that you want to reuse?
I would have a crack at it like this:
Try to think of this in reverse logic like this
SELECT user_id
FROM (<userSubQuery1>)
MINUS
(
SELECT useri_id
FROM <badUserSubQueryB>
UNION
SELECT useri_id
FROM <badUserSubQueryC>
UNION
SELECT useri_id
FROM <badUserSubQueryD>
)
Let me know how it goes
Cheers ,
Alex
I have a bunch of records that I want to paginate by reverse modified date. From what I can tell, using a simple query pulls the same sub-set of records and then sorts only those by date.
Something like this:
SELECT * FROM items WHERE status='1' ORDER BY modified_date DESC LIMIT start,count
Would an index help me? Or am I going to have to figure out some way to code this perhaps adding a column that somehow keeps track of modified_date in reverse order?
An index on (status, modified_date) might help. See indexes dos and donts.
This query looks good for me. It does the following:
Find records with status = 1
Order them by modified_date DESC
Return count records starting from start
So if you need to show first page, you should set start=0. For second page it'll be start=count, for third start=2*count and so on.
Or do you have performance problems with this?
Its a hack and I haven't tried it because I can't reproduce your problem and may not perform well but this may work for you
SELECT *
FROM (SELECT *
FROM items
WHERE status = '1'
ORDER BY modified_date desc ) t
LIMIT START, COUNT
You can add a column that holds a TIMESTAMP then set the auto-update property on it, so that it is properly updated. Finally, create an index for this column. Your query will look something like this:
SELECT *
FROM items
WHERE status='1'
ORDER BY 'timestamp' DESC
LIMIT start,count
For examples I don't know how many rows in each table are and I try to do like this:
SELECT * FROM members
UNION
SELECT * FROM inventory
What can I put to the second SELECT instead of * to remove this error without adding NULL's?
Put the columns names explicitly rather than *, and make sure the number of columns and data types match for the same column in each select.
Update:
I really don't think you want to be UNIONing those tables, based on the tables names. They don't seem to contain related data. If you post your schema and describe what you are trying to achieve it is likely we can provide better help.
you could do
SELECT *
from members
UNION
SELECT inventory.*, 'dummy1' AS membersCol1, 'dummy2' AS membersCol2
from inventory;
Where membersCol1, membersCol12, etc... are the names of columns from members that are not in inventory. That way both queries in the union will have the same columns (Assuming that all the columns in inventory are the same as in members which seems very strange to me... but hey, it's your schema).
UPDATE:
As HLGEM pointed out, this will only work if inventory has columns with the same names as members, and in the same order. Naming all the columns explicitly is the best idea, but since I don't know the names I can't exactly do that. If I did, it might look something like this:
SELECT id, name, member_role, member_type
from members
UNION
SELECT id, name, '(dummy for union)' AS member_role, '(dummy for union)' AS member_type
from inventory;
I don't like using NULL for dummy values because then it's not always clear which part of the union a record came from - using 'dummy' makes it clear that the record is from the part of the union that didn't have that record (though sometimes this might not matter). The very idea of unioning these two tables seems very strange to me because I very much doubt they'd have more than 1 or 2 columns with the same name, but you asked the question in such a way that I imagine in your scenario this somehow makes sense.
Are you sure you don't want a join instead? It is unlikely that UNOIN will give you what you want given the table names.
Try this
(SELECT * FROM members) ;
(SELECT * FROM inventory);
Just add semicolons after both the select statements and don't use union or anything else. This solved my error.
I don't know how many rows in each table
Are you sure this isn't what you want?
SELECT 'members' AS TableName, Count(*) AS Cnt FROM members
UNION ALL
SELECT 'inventory', Count(*) FROM inventory
Each SELECT statement within the MySQL UNION ALL operator must have the same number of fields in the result sets with similar data types
Visit https://www.techonthenet.com/mysql/union_all.php