Ordering based on one value of many - sql

I have three SQL tables. Users, Registration Field Values, and Registration Fields.
Name
zip code
favorite food
Sue
55555
sushi
Gary
12345
eggs
Where zip code and favorite food are different registration fields.
The relationship is a user has many registration field values, and those values belong to the registration field.
I'm wondering how I can order my table based on a certain registration field. For example, selecting "favorite food", I would want "eggs" before "sushi".
This is confusing to me because I've only seen ORDER BY for an individual column or series of columns. I can't just ORDER BY registration_field_value.value because it needs to be based on only one of those registration fields.
This is like "ORDER BY field value where the associated field id is 'favorite food'", although I don't want to filter anything out.
I'm using Postgres if that makes a difference.
EDIT, adding a
:

You can use case to order based on specific value.
For eg:
ORDER BY
CASE "favorite food"
WHEN 'eggs' THEN 1
ELSE 2
END
The above query will move row with eggs to start and all other value will be moved to bottom.

Related

Cognos calculate using a different layout section

I have a Cognos report that has four different sections/layouts, each containing their own list. I would like to use one section to perform calculations using another list from another section. Is that possible?
For example:
Section Two contains a list with Name, Week1 Commissions, Week2 Commissions, etc
I want Section One to calculate the first 11 weeks only so the List in Section One would contain
Name, Weeks 1 through 11 total.
What is the best way to make this happen if it's possible.
Section One would contain these columns which would perform the calculations
Name Week 1-11 totals |
Section Two would have these columns
Name Week 1 Week 2 Week 3 .....
Best,
Kev
Yes it is possible, and you can perform all of the logic in one query
There are many ways to do this
For example, the four different sections/layouts, selecting the list, go to properties you can use the same query for each list

DataStudio ::: Count_Distinct Calculation based on two values

I have a data set with two values. The first being User ID, the second being Type.
I previously had a count_distinct on the user ID. However, if a user changes their Type, the metrics we have to show 'Type' of user shows two instances of that once unique user.
I would like some SQL which basically states if the User ID and Type Match, then count that as a unique user. If either one of those change, count that also as a unique user. So that..
1) Jane Doe - Support User
2) Jane Doe - Employee User
3) John Smith - Support User
This would show as 3 unique users, rather than 2 based on a count_unique on just the User ID.
One way to accomplish this is with a calculated field that looks something like the following:
COUNT_DISTINCT(CONCAT(USER_ID, ' - ', TYPE))
You might need to replace USER_ID and TYPE with your field names.

SQL - selecting multiple dates from a table for unique ID's - part 2

Gordon got me pretty straight on my former question and now I've learned a lot more :). my query now looks like this:
select ident, min(input_date) as 'date requested',
min(case when op='u' and user IN (lists tech's names)) as 'date assigned'
max(case when status='closed' then date end) as 'date completed'
max(case when op='u' then user end) as 'tech'
from table
so this give me all that i want except the column that's labelled as 'tech'. for the majority of the results, i get the correct results but for a handful it gives me either the wrong tech or the 'user'
so I've added a field so you can better understand - it's field called 'role'. when i run this query, i never want the person listed as 'user' in the role column to be listed as 'tech'. in query above, it lists the user instead of tech and it's always in cases where the user updated their request. so if you look at the table, in ident 1, tom is user and harry is tech. harry assigned it to himself on 2/2; however, tom updated request (to give more info) after harry assigned it. running the above query, will result in tom being listed as the tech in the tech field. my guess is b/c of max and tom is after harry alphabetically.
so i have 5 tech's to track (only 2 listed in table - bertha & harry). if i add an IN clause so that it reads like this:
max(case when op='u' and user IN ('bertha','harry',etc) then user end) as 'tech'
the results is it always gives me the last tech alphabetically rather than the one who assigned it/closed it.
my goal is to have the tech who assigns & closes it to be listed. if the request has not been assigned then the 'tech' field should read null.
the date fields all have time stamps so i could go about it that way and i guess i could target their role as well.
i know that max is probably the problem as the tech could assign it then the user could update but if i put IN statement in there I don't get why it lists a tech who's not associated w/that ident.
finally, the other scenario, in ident 2, bertha actually takes the assignment away from harry so i need to say bertha; however, harry will always be the one reported in field - probably due to max()
thanks

Multi-Row Per Record SQL Statement

I'm not sure this is possible but my manager wants me to do it...
Using the below picture as a reference, is it possible to retrieve a group of records, where each record has 2 rows of columns?
So columns: Number, Incident Number, Vendor Number, Customer Name, Customer Location, Status, Opened and Updated would be part of the first row and column: Work Notes would be a new row that spans the width of the report. Each record would have two rows. Is this possible with a GROUP BY statement?
Record 1
Row 1 = Number, Incident Number, Vendor Number, Customer Name, Customer Location, Status, Opened and Updated
Row 2 = Work Notes
Record 2
Row 1 = Number, Incident Number, Vendor Number, Customer Name, Customer Location, Status, Opened and Updated
Row 2 = Work Notes
Record n
...
I don't think that possible with the built in report engine. You'll need to export the data and format it using something else.
You could have something similar to what you want on short description (list report, group by short description), but you can't group by work notes so that's out.
One thing to note is that the work_notes field is not actually a field on the table, the work_notes field is of type journal_input, which means it's really just a gateway to the actual underlying data model. "Modifying" work_notes actually just inserts into sys_journal_field.
sys_journal_field is the table which stores the work notes you're looking for. Given a sys_id of an incident record, this URL will give you all journal field entries for that particular record:
/sys_journal_field_list.do?sysparm_query=name=task^element_id=<YOUR_SYS_ID>
You will notice this includes ALL journal fields (comments + work_notes + anything else), so if you just wanted work notes, you could simply add a query against element thusly:
/sys_journal_field_list.do?sysparm_query=name=task^element=work_notes^element_id=<YOUR_SYS_ID>
What this means for you!
While you can't separate a physical row into multiple logical rows in the UI, in the case of journal fields you can join your target table against the sys_journal_field table using a Database View. This deviates from your goal in that you wouldn't get a single row for all work notes, but rather an additional row for each matched work note.
Given an incident INC123 with 3 work notes, your report against the Database View would look kind of like this:
Row 1: INT123 | markmilly | This is a test incident |
Row 2: INT123 | | | Work note #1
Row 3: INT123 | | | Work note #2
Row 4: INT123 | | | Work note #3

using MS Access 2010, editing a form, 3 tables, one table is "one to many" and want to have query across one row

Example:
(using a comma, to show columns & "A_", "B_", "C_" to show tables)
I currently have...
A_John, A_Doe, B_MemStartDate, C_Date,
A_John, A_Doe, B_MemStartDate, C_Date,
A_John, A_Doe, B_MemStartDate, C_Date,
I would like Table "C" columns to be pulled by Course but shown as the "Date" column... in other words, example, where Course = 1, use Date... etc...
The table would have each member's course history.
MemberID, CourseID, Date
JohnDoe, Course1, 10-10-13
JohnDoe, Course2, 10-11-13
JohnDoe, Course3, 10-12-13
The table C is the one to many of course, with the goal to show the date that the course was taken as I would title the columns with the 3 different courses I want to show... (I want to pull only 3 different courses)
I would like to have them in a row...
A_John, A_Doe, B_Start Date, C_Course1Date, C_Course2Date, C_Course3Date
Sorry for the lack of experience in my question, but I usually get by with "copy/paste"...LOL
Keeping in mind I am using Access... can I do this?
Clarifying... sorry don't know how to do tables in basic html so have commas
Have this. Pulling from 3 different tables where the member# is unique and joined.
MEMBER, STARTDATE, STATUS, COURSE, COURSEDATE
JohnSmith, 08-01-2013, Active, Workshop1, 10-20-2013
JohnSmith, 08-01-2013, Active, Workshop2, 10-13-2013
JohnSmith, 08-01-2013, Active, Workshop3, 10-28-2013
LaraBentt, 12-01-2012, Inactive, Workshop1, 02-20-2012
LaraBentt, 12-01-2012, Inactive, Workshop2, 02-13-2012
LaraBentt, 12-01-2012, Inactive, Workshop3, 02-28-2012
Want this...
MEMBER, STARTDATE, STATUS, WORKSHOP1, WORKSHOP2, WORKSHOP3
JohnSmith, 08-01-2013, Active, 10-20-2013, 10-13-2013, 10-28-2013
LaraBentt, 12-01-2012, Inactive, 02-20-2012, 02-13-2012, 02-28-2012
Tables columns are basically like this...
Table 1 - tblMember (one - one)
MEMBER, STARTDATE
Table 2 - tblRegStatus (one - one)
MEMBER, STATUS
Table 3 - tblCourses (one to many)
MEMBER, COURSE, COURSEDATE
Hope this explains it better!
Your amendment to your question made it much more clear.
One of the problems you are going to encounter is if you have someone that has registered for the same course, twice. There has to be the understanding that you are only showing the most recent registration per person. Now that we understand that caveat - lets look at how you can build this.
The simplest way of building this is to change the query type from Select to Crosstab. This is done through the Access interface.
For each of the fields in the query, set the Total and Crosstab values to:
MemberName: Total = *Group By* Crosstab = *Row Heading*
StartDate: Total = *Group By* Crosstab = *Row Heading*
Status: Total = *Group By* Crosstab = *Row Heading*
CourseName: Total = *Group By* Crosstab = *Column Heading*
CourseDate: Total = *Max* Crosstab = *Value*
This will result in query results such as:
MemberName StartDate Status Biology English Math
---------- --------- ------ ------- ------- ----
John 10/1/2013 Active 11/14/2013 12/1/2013
Matthew 9/1/2013 Inactive 1/1/2013
Peter 8/7/2013 Active 1/1/2013 4/1/2013
Sam 1/14/2013 Inactive 11/14/2013
William 5/19/2013 Active 1/1/2013 4/1/2013
The advantage to the crosstab is that it will automatically add columns as there is data.
You have to construct your query (the joins) to give you the data that you want however. For example - do you want to show members that have no courses on the list? Do you want to show courses that have no enrollment?
Microsoft has a page on using crosstab queries:
http://office.microsoft.com/en-us/access-help/make-summary-data-easier-to-read-by-using-a-crosstab-query-HA010229577.aspx
If you want to hard code your three columns, you can also manually pivot the data. Again - keep in mind that we are using the Max aggregate, so only the latest courses will show. With a manual pivot you also need to remember that it will be limited to the data you are requesting.
Take your select query, right click and click "Totals" to show the Totals row. You can also click on the Totals icon in the ribbon under Query Tools / Design.
Set up your three columns:
MemberName: Total = *Group By*
StartDate: Total = *Group By*
Status: Total = *Group By*
Now add in a separate field for each course you wish to include. In the "Field" definition enter the following.
Science: IIf([CourseName]="Science",[CourseDate],Null)
Biology: IIf([CourseName]="Biology",[CourseDate],Null)
Math: IIf([CourseName]="Math",[CourseDate],Null)
English: IIf([CourseName]="English",[CourseDate],Null)
Set the "Total" definition value to "Max".
That should get you the results you are looking for.