I am using SSRS Report Builder.
My query returns permits for each user:
user1 permit1
user1 permit2
user1 permit3
user2 permit2
user2 permit4
...
How can I create a report like this?
USER PERMITS
------------------------------------
user1 permit1 permit2 permit3
user2 permit2 permit4
...
Or even better:
USER PERMIT1 PERMIT2 PERMIT3 PERMIT4
-----------------------------------------------
user1 x x x
user2 x x
...
The difficulty is that the column "Permit" is not a group of set values. We will add new permits and remove frequently.
Thanks for any assistance.
You're looking for the Matrix Control. In this control you'll group rows on the first (user) column, and group columns on the second (permit) column. In the cell you can check the value of the permit, and if it's set you plot an 'x'. Schematically the control will look like this:
---------+-----------------------------------------------
| | [Permit] |
|---------+-----------------------------------------------
| [User] | =Iif(Fields!Permit.Value Is Nothing, "", "x") |
---------+-----------------------------------------------
Related
This is my sqlfiddle
http://sqlfiddle.com/#!9/e443688/2
If I have a list of values:
a#test.com
b#test.com
c#test.com
And my table has a column called email I can run a query like
select email from datatable where email in ('a#test.com', 'b#test.com', 'c#test.com')
to find whether they exist.
However, if my list of values consists of at least 2 columns:
a#test.com, manager
b#test.com, editor
c#test.com, editor
And my table has two columns email and job_title, how do I run a query to check if there are rows that match the values I am searching for?
I don't want to know how many there are. I do want to know which value group exists.
Ideally if my query can return back something like this
email | job_title | exists
------|-----------|-------
a#test.com | manager | 1
b#test.com | editor | 0
c#test.com | editor | 1
Or just
email | job_title
------|-----------
a#test.com | manager
c#test.com | editor
That's good enough
What 'marekful' suggested is a pretty good option. If you are looking for an alternative way, here's another option:
select email
from datatable
where (email = 'a#test.com' and job_title = 'manager')
or (email = 'b#test.com' and job_title = 'editor')
or (email = 'c#test.com' and job_title = 'editor');
I used #marekful comment and concatenate the two values.
Also #Strawberry's suggestion works as well.
Here's the fiddle. http://sqlfiddle.com/#!9/e443688/3
And the query was
select id from emails where (email,job_title) in(('a#test.com','manager'),('b#test.com','editor'),('c#test.com','editor'))
For some reason, I am unable to export a table of subscribers from my phpList (ver. 3.0.6) admin pages. I've searched on the web, and several others have had this problem but no workarounds have been posted. As a workaround, I would like to query the mySQL database directly to retrieve a similar table of subscribers. But I need help with the SQL command. Note that I don't want to export or backup the mySQL database, I want to query it in the same way that the "export subscribers" button is supposed to do in the phpList admin pages.
In brief, I have two tables to query. The first table, user contains an ID and email for every subscriber. For example:
id | email
1 | e1#gmail.com
2 | e2#gmail.com
The second table, user_attribute contains a userid, attributeid, and value. Note in the example below that userid 1 has values for all three possible attributes, while userid's 2 and 3 are either missing one or more of the three attributeid's, or have blank values for some.
userid | attributeid | value
1 | 1 | 1
1 | 2 | 4
1 | 3 | 6
2 | 1 | 3
2 | 3 |
3 | 1 | 4
I would like to execute a SQL statement that would produce a row of output for each id/email that would look like this (using id 3 as an example):
id | email | attribute1 | attribute2 | attribute3
3 | e3#gmail.com | 4 | "" | "" |
Can someone suggest SQL query language that could accomplish this task?
A related query I would like to run is to find all id/email that do not have a value for attribute3. In the example above, this would be id's 2 and 3. Note that id 3 does not even have a blank value for attributeid3, it is simply missing.
Any help would be appreciated.
John
I know this is a very old post, but I just had to do the same thing. Here's the query I used. Note that you'll need to modify the query based on the custom attributes you have setup. You can see I had name, city and state as shown in the AS clauses below. You'll need to map those to the attribute id. Also, the state has a table of state names that I linked to. I excluded blacklisted (unsubscribed), more than 2 bounces and unconfirmed users.
SELECT
users.email,
(SELECT value
FROM `phplist_user_user_attribute` attrs
WHERE
attrs.userid = users.id and
attributeid=1
) AS name,
(SELECT value
FROM `phplist_user_user_attribute` attrs
WHERE
attrs.userid = users.id and
attributeid=3
) AS city,
(SELECT st.name
FROM `phplist_user_user_attribute` attrs
LEFT JOIN `phplist_listattr_state` st
ON attrs.value = st.id
WHERE
attrs.userid = users.id and
attributeid=4
) AS state
FROM
`phplist_user_user` users
WHERE
users.blacklisted=0 and
users.bouncecount<3 and
users.confirmed=1
;
I hope someone finds this helpful.
I have two related tables, results and userID.
results looks like this:
+----+--------+--------+
| ID | userID | result |
+----+--------+--------+
| 1 | abc | 124 |
| 2 | abc | 792 |
| 3 | def | 534 |
+----+--------+--------+
userID looks like this:
+----+--------+---------+
| id | userID | name |
+----+--------+---------+
| 1 | abc | Angela |
| 2 | def | Gerard |
| 3 | zxy | Enrico |
+----+--------+---------+
In results, the userID field is a lookup field; it stores userID.id but the combo box has userID.userID as its choices.
When I try to enter data into results by setting the userID combo box and entering a value for result, I get this error message:
You cannot add or change a record because a related record
is required in table `userID`.
This is strange, because I'm specifically selecting a value that's provided in the userID combo box.
Oddly, there are about 100 rows of data already in results with the same value for userID.
I thought this might be a database corruption issue, so i created a blank database and imported all the tables into it. But I still got the same error. What's going on here?
Both tables include a text field named LanID. You are using that field in this relationship, which enforces referential integrity:
The problem you're facing is due to the Lookup field properties. This is the Row Source:
SELECT [LanID].ID, [LanID].LanID FROM LanID ORDER BY [LanID];
But the value which gets stored (the Bound Column property) is the first column from that SELECT statement, which is the Long Integer [LanID].ID. So that number will not satisfy the relationship, which requires results.LanID = [LanID].LanID.
You must change the relationship or change the Lookup properties so both reference the same field value.
But if it were me, I would just eliminate the Lookup on the grounds that simple operations (such as this) become unnecessarily confusing when Lookup fields are involved. Make results.LanID a plain numeric or text field. If you want some kind of user-friendly drop-down for data entry, build a form with a combo or list box.
For additional arguments against Lookup fields, see The Evils of Lookup Fields in Tables.
If you are using a parameter query, make sure you have them in the same order as the table you are modifying and the query you have created. You might have one parameter inserting the conflicting data. Parameters are used in the order they are created...not the name of the parameter. I had the same problem and all I had to do was switch the order they were in so they matched the query. This is an old thread, so I hope this helps someone who is just now having this problem.
I have a scenario where I need to insert the data into table temporarily and later on approval or confirmation, make it permanent. The data will be inserted by a user and approval or denial needs to be done by Super User.
What I think of now is to have two different but identical tables (temporary and main) and the user will insert the data into temp table. After confirmation of Super User, the data will be moved to main table. But the problem comes when a database contains very large number of tables then this process will become more complex.
EDIT : This implies to CREATE EDIT & DELETE commands.
Is there any simpler or better approach of doing this?
Please suggest.
Using a version table (related to comment):
The idea here is to have a version table; when your user changes a piece of information the new version is stored in this table along with the related ID.
Then all you need to do is join on the PersonID and select the most recent accepted version.
This means the user can make as many updates as they want but they won't show until the super user accepts them, it also means the data is never destroyed (stored in the version table) and they don't need to implement rollback as it's already there!
See: http://sqlfiddle.com/#!3/cc77f/4
People Table:
ID | Age Etc... (Info That Doesn't Change)
-----------------------
1 | 12
2 | 16
3 | 11
People Version Table:
VersionID | PersonID | Name | Approved
-----------------------
1 | 1 | Stevz | FALSE
2 | 1 | Steve | TRUE
3 | 2 | James | TRUE
4 | 3 | Jghn | FALSE
5 | 3 | John | TRUE
Example table SQL
CREATE TABLE People
(
id int identity primary key,
age int
);
CREATE TABLE PeopleVersion
(
versionId int identity primary key,
peopleId int,
name varchar(30),
approved varchar(30)
);
Example Query
SELECT * FROM People p
INNER JOIN PeopleVersion v ON p.id = v.peopleID
WHERE v.approved = 'TRUE'
ORDER BY versionId DESC
A further insight:
You could even have three states of Approved; null meaning no admin has chosen yet, TRUE meaning it was accepted and FALSE meaning it was rejected
You could show the user the most recent from null and true, show the admin all three and show the other users of the site only versions that were true
Old Comments
Could you just add a field called approved to the table and then hide anything without the approval flag set to TRUE?
It could default to FALSE and only the super user would be able to see items with the flag set to FALSE
E.g.
Name | Age | Approved
-----------------------
Steve | 12 | FALSE
James | 16 | TRUE
John | 11 | FALSE
The user would only see James, but the SuperUser would see all three listed
Alternatively using your temporary and main tables is the other way of looking at this problem, though this may lead to problems as everything get's larger
The easiest approach is a flag within the table marking an entry either approved or not-yet approved.
Then just change the retrieving logic to only show entries where that flag is set to approved.
I have a table, user_quotes, with the fields quotes_id, quotes_user, quotes_desc, quotes_date, quotes_status, and quotes_location. In this quotes_user allows duplication entries. When I execute my query I am trying to avoid duplication entries of quotes_user. So I executed the query like this,
select distinct quotes_user from user_quotes;
This query returns only the quotes_user field. How can I retrieve all other records using distinct quotes_user.
I have tried with these following,
select distinct quotes_user, quotes_desc, quotes_date, quotes_status from user_quotes;
It's not avoiding the duplication of quotes_user.
If I use,
select distinct quotes_user, * from user_quotes;
I am getting mysql error,
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '* FROM user_quotes.
How can I fetch all records using select distinct of a single column in a same table? I am storing the email address in the field. The datatype is varchar.
Note: please don't suggest me other types like group by or other. I need to know how I can retrieve the records by only using distinct.
In addition to what has already been said, it should be stressed that the DISTINCT keyword can't give you distinct results for an individual column when you're selecting multiple columns. You will get distinct rows of, in your case, 4 columns. What you're observing is the expected, standard behaviour of DISTINCT.
You say you want to retrieve the other fields, but you haven't specified how SQL is to know which values to retrieve for the other fields, for each distinct value of quotes_user.
To show you want I mean, consider this example:
+-------------+---------------+
| quotes_user | email_address |
+-------------+---------------+
| user1 | email1 |
| user1 | email2 |
| user2 | email3 |
| user2 | email4 |
| user2 | email5 |
| user3 | email6 |
+-------------+---------------+
Now, if you just wanted quotes_user, the output would obviously be:
+-------------+
| quotes_user |
+-------------+
| user1 |
| user2 |
| user3 |
+-------------+
But if you wanted the other fields as well, you'd need to decide whether, for example, to have email1 or email2 for the user1 row.
Perhaps what you want is to concatenate the values of the other fields together. In that case, I would suggest using the GROUP_CONCAT aggregate function with GROUP BY quotes_user.
I'm not sure why you want to avoid using GROUP BY, though. Perhaps if you could explain that, we could help more.
It sounds like quotes_user should be a foreign key like user_id to presumably your users table. You could then query user_quotes by user_id returning all the quotes for that user. Your front end could then format all the quotes for each user nicely, it doesn't really sound like a MySql issue.