I have Downloaded Document model which contains data regarding the user downloaded documents , the schema of downloaded_documents table is
id document_id document_type user_id
1 444 article 1
2 120 judgment 2
3 444 article 1
4 555 rule 3
5 555 rule 4
Now I want to generate report for all the downloaded documents and it users. And there is no relationship between the downloaded documents and user model, and each document_type in nothing but the each model in our application, so I want a document information also user information also. So How I write a query ?
I have written the following query:
DownloadedDocument.joins("JOIN users ON downloaded_documents.user_id = users.id").select("distinct on (downloaded_documents.document_id, downloaded_documents.user_id, downloaded_documents.document_type) users.email, document_type.created_at").where('created_at >=? and created_at <= ?',Date.parse(from_date), Date.parse(to_date))
But I am not getting the document information. I should add a one more JOIN in the above query which is nothing but JOIN on ------downloaded_documents.document_id = -----.id in the ---- place I need to add document type name, but for ALL I want all models mentioned in the document_type, so how could I write query?
My Article model schema
ID title
444 first title
Judgment schema
ID appellant_name respondent_name case_no
120 ddd gggg abc.123
Rule schema
ID rule_name
555 example
I want a output something like this:
{'judgment' => [{case_no: 'abc.123',email: 'abc.example.com', dcoument_id: 120}], 'article' => [{title: 'first title', email: 'ads#example.com', document_id: 444},{title: 'first_title', email: 'fff#example.com', document_id: 444}]}
I want to generate a report for to list document and its users
Related
I have two tables having following data-
Social_Tbl
ID Name Value
------------------------
1 Facebook FB
2 Orkut OR
3 Google GL
4 Other OT
And Organization_tbl
ID Organization Name
-----------------------------
1 1234 Facebook
2 1234 Google
3 146 Other
4 126 Other
5 126 Facebook
6 77 Google
Here, 'Name' is the foreign key (Not ID).
I want to join these tables and get the 'Name' columns data which does not belong to organization id 1234. As follows-
Name
----
Orkut
Other
Here, 'Orkut' and 'Other' does not belong to 1234 organization.
I tried following query for this-
select * from Social_Tbl st
join Organization_tbl ot
on st.Name = ot.Name
where Organization = 1234
This query fetches Names related to 1234 i.e Facebook and Google. I want result
Orkut and Other. If I replace Organization = 1234 with Organization != 1234 it returns all data from Organization_tbl.
Can somebody help me on this. This should be pretty simple, just npt able to find it out.
Could be done with a subquery:
select st.Name
from Social_Tbl st
where not exists (
select *
from Organization_tbl ot
where st.Name = ot.Name
and ot.Organization = 1234
)
(This also returns names that don't have an entry in Organization_tbl at all.)
Here I am trying to get the record for my products where the # swab location in Main table matches the count of swab locations in swab Table and Users can checked off the Y/N to verify that the description of the locations are correct.
Here is the example of my 2 tables.
tblMainEquipment
Asset_ID EquipmentName Num_SwapLocations Verified
234 Saijimon 2 N
235 Pasquale 3 N
tblMainSwapLocations
Asset_ID Swap_location
234 Particle Cannon
234 RailGun
235 Particle Cannon
I use the following query to count the number of records, i avoided using a having query to combine both tables since it is not updatable.
qryMainSwapLocationCount
SELECT MSL.Asset_ID, Count(Asset_ID) AS [Count]
FROM tblMainSwapLocation AS MSL
GROUP BY MSL.Asset_ID;
This will give me the result of
qryMainSwapLocationCount
Asset_ID count
234 2
234 1
I used the following as a record source for my form to allow users to verify the inputs.
SELECT MEQ.Asset_ID, MEQ.Equipment_Name,MEQ.Num_swapLocations MEQ.Verified
FROM tblMainEquipment AS MEQ, qryMainSwapLocationCount AS MSLC
WHERE (((MEQ.Asset_ID)=[MSLC].[Asset_ID]) AND ((MEQ.Num_SwapLocations)=[MSLC].[Count]);
This result would be
tblMainEquipment
Asset_ID EquipmentName Num_SwapLocations Verified
234 Saijimon 2 N
However this record set is not editable. Is there any reasons for this?
I think you should put your table tblMainEquipment as your recordsource and bring all the fields from that on to your form:
Then insert an unbound textbox (perhaps close to your Num_SwapLocations field for easy comparison):
Then in this new textbox, put the following in the ControlSource:
=DCount("ASSET_ID","tblMainSwapLocations","ASSET_ID=" & [Asset_ID])
Then open your form and it should count the number of records in table tblMainSwapLocations that have the same Asset_ID as the record currently showing:
You'll then be able to update the Verified field in your tblMainEquipment table.
I have two tables
tblSTATUS
StatusID|PlanID|Description|EmailSubject|EmailFrom|EmailTo||Comment
1 8 Approved aaa
2 7 Rejected bbb
3 7 Rejected ccc
4 42 Rejected ccc
tblSTATUSREASON
PlanID|REASONS
7 failed
7 not eligible
42 not eligible
when i send email to particular person if their plan is (only) rejected it stores in table tblstatusreason the reasons for rejected and PlanID used which is dependent on tblStatus.
i should retrieve evrything in grid view C# code using stored procedures and displaying it for user according to description.
Now my problem is i can retrieve and display all the other columns but i dont know how to display rReasons so i want to select [REASONS] from tblSTATUSREASON where for that particluar description = rejected from tblSTATUS and also i dont want to change my tables/columns.I need sql stored procedure for this particular thing
You need also where
select R.PlanID,REASONS
FROM tblSTATUSREASON R
INNER JOIN tblSTATUS S
ON R.PlanID = S.PlanID
AND description = 'rejected'
I am dealing with one table(3+ million rows,SQL Server)
I need to filter results according to the two columns below:
<code>
...FromID| ToID |Column5|....
...1001 2001
...1002 2020
...1003 5000
...1001 3000
...2001 1001
</code>
Now User1 can access records with FromID or ToId 1001.
FromID|ToID
1001|2001
1001|3000
2001|1001
User2 can access records with FromID or ToID 1002,1003,3000
FromID|ToID
1002|2020
1003|5000
1001|3000
What is the most efficient way to do this ?
Do i need to create a view for each user ?(this is working on enterprise,user count will be
max 100 )
Thanks.
PS. My very first question. O.o
Your access criteria seem to be fairly arbitrary. User1 gets 1001, user2 gets 1002, 1003, and 3000, and I assume users 3 through 99 have arbitrary access as well. In that case, I recommend that you create a table, call it useraccess for this example:
user |accessID
---------------
user1|1001
user2|1002
user2|1003
user2|3000
... |...
Now when you want to know what rows a user has, you can do this:
SELECT t.FromID, t.ToID, [[other columns you care about]]
FROM yourtable t
JOIN useraccess a ON t.FromID = a.accessID OR t.ToID = a.accessID
WHERE a.user = 'user2'
You can either run that query dynamically or you can create a view based on it. The usual tradeoffs between views and direct queries will apply as usual.
Edit: I just saw your note that you already have a UserRights table, so you already have step 1 completed.
My application let users to send files to each other. Regular users can edit their contacts, change password, etc. In addition, admin users can add/remove users and view the log of what happened. My question is how to store this log in MySQL database ?
I thought to store the log like this:
log_id time user_id action_type description
------ ---- ------- ---------------- ----------------------------------------
1 .... 4 User added Added new user: alex
2 .... 1 Contact added Added contact Paul to group Family
3 .... 1 User removed Removed user: gabrielle
4 .... 3 Files sent Sent files 3,5,7,14 to contacts 2,4,8
5 .... 8 Group added Added new group: Family
6 .... 8 Password changed
7 .... 8 First Name changed Changed First Name from Michael to Misha
What type would be the best for action_type ? Since new action_types may be added in future, I thought that ENUM won't be a good choice. So I thought to make it VARCHAR(..), like description.
Is this seems reasonable ?
I will be happy to hear any comments / suggestions.
If you're concerned about adding additional action types, make a separate table to store your action types and and join it to your logs table with a foreign key:
logs table:
log_id time user_id action_type_id description
------ ---- ------- ---------------- -----------------------------------
1 .... 4 1 Added new user: alex
2 .... 1 2 Added contact Paul to group Family
...
action_types table:
id name
--- ---------------
1 User added
2 Contact added
.....