Please correct the query.
It is working but it gives the wrong result, I have database tables that look like this.
[3rdi_EventsRolePrice] :-EventID, RoleID, RolePrice
[3rdi_EventsRolePrice]:- FirstName, LaastName And EventID
I want to get FirstName, LastName, RoleID by joining these two, and I am passing an event value as a parameter which is 13 in my case just for getting result.
SELECT ep.FirstName, ep.LastName, erp.RoleID
from [3rdi_EventParticipants] ep,[3rdi_EventsRolePrice] erp
WHERE ep.EventID==erp.EventID and erp.EventID='13'
I want to match where these two things "ep.EventID==erp.EventID" are equal, and their value is also 13. My query is also working syntaxically correct but I get a thoroughly wrong result.
SELECT
ep.FirstName,
ep.LastName,
erp.RoleID
FROM [3rdi_EventParticipants] ep
INNER JOIN [3rdi_EventsRolePrice] erp
ON ep.EventID = erp.EventID
WHERE erp.EventID='13'
I think it will work..
ep.EventID==erp.EventID to ep.EventID=erp.EventID
Related
What do I have to change to get different results from different names.The table should give me the debts of each of them, this is calculated by the amount and the price of the drink. Now it should show all the names with the corresponding invoice that happens after the select
%sql select name, sum(getraenk.preis*schulden.menge) schulden from schulden \
join person on (fk_person = person.id)\
join getraenk on (fk_getraenk = getraenk.id)\
where name like ("dani")
Edit: it should spend all the names with their debts, that is:
dani = 8.5
michael = 12.5
...
Just in case your problem is very simple, you should be able to see all names and values with an SQL that looks like this:
select name, getraenk.preis*schulden.menge schulden
from schulden
join person on (fk_person = person.id)
join getraenk on (fk_getraenk = getraenk.id)
Note that I removed the where clause... this was the part that limited it to one name.
You also don't need the sum clause here unless you are doing a group by
Have you considered simply using GROUP BY name at the end of this query?
https://www.w3schools.com/sql/sql_groupby.asp
This will give you the sum of total debt for all names in your table which sounds like the result you are looking for.
You're missing
GROUP BY name
in the query.
I am new to writing queries in SQL, and struggling to get the datas from table.
I have a table called "address" in DB. Here, I have more columns and specifically from two column I need to get the datas filtered out. Both are integers
frequency_type_id
date_of_invoice_id
I need to get rows only where frequency_type_id = 1 and date_of_invoice_id=2, and frequency_type_id = 2 and date_of_invoice_id=3, frequency_type_id = 3 and date_of_invoice_id=1.
I am trying with the following query, but its not filtering properly,
SELECT address_id, company_name,
invoice_batch_billing, frequency_type_id, date_of_invoice_id
FROM pls.address where invoice_style='Consolidated'
and frequency_type_id in
(SELECT frequency_type_id from pls.address where frequency_type_id=1 and date_of_invoice_id=1);
Hope someone assist.
Try below query:
SELECT address_id, company_name,
invoice_batch_billing, frequency_type_id, date_of_invoice_id
FROM pls.address where invoice_style='Consolidated'
and ((frequency_type_id=1 and date_of_invoice_id=2) or (frequency_type_id=2 and date_of_invoice_id=2) or (frequency_type_id=3 and date_of_invoice_id=2))
Try this:
SELECT
address_id,
company_name,
invoice_batch_billing,
frequency_type_id,
date_of_invoice_id
FROM
pls.address
WHERE
frequency_type_id IN (1,2,3)
AND
date_of_invoice_id=2
If I have understand your question correctly, this will suffice your requirement.
I've got the code below which displays the location_id and total number of antisocial crimes but I would like to get the location_name from a different table called location_dim be output as well. I tried to find a way to UNION it but couldn't get it to work. Any ideas?
SELECT fk5_location_id , COUNT(fk3_crime_id) as TOTAL_ANTISOCIAL_CRIMES
from CRIME_FACT
WHERE fk1_time_id = 3 AND fk3_crime_id = 1
GROUP BY fk5_location_id;
You want to use join to lookup the location name. The query would probably look like this:
SELECT ld.location_name, COUNT(cf.fk3_crime_id) as TOTAL_ANTISOCIAL_CRIMES
from CRIME_FACT cf join
LOCATION_DIM ld
on cf.fk5_location_id = ld.location_id
WHERE cf.fk1_time_id = 3 AND cf.fk3_crime_id = 1
GROUP BY ld.location_name;
You need to put in the right column names for ld.location_name and ld.location_id.
you need to find a relationship between the two tables to link a location to crime. that way you could use a "join" and select the fields from each table you are interested in.
I suggest taking a step back and reading up on the fundamentals of relational databases. There are many good books out there which is the perfect place to start.
I have these two subqueries that I got working
(
SELECT GLOBAL_USERS.ID AS USER_ID, GLOBAL_USERS.USER_ID AS USERNAME,
GLOBAL_USERS.DEPARTMENT AS DEPARTMENT,
GLOBAL_USERS.FIRST_NAME AS FIRSTNAME, GLOBAL_USERS.LAST_NAME AS LASTNAME,
GLOBAL_USERS.TITLE AS TITLE,
USER_FIRST_ENTITLEMENTS.ENTITLEMENT_NAME AS ENTITLEMENTNAME,
USER_FIRST_ENTITLEMENTS.APPLICATION_NAME AS APPLICATIONNAME
FROM GLOBAL_USERS
INNER JOIN USER_FIRST_ENTITLEMENTS
ON GLOBAL_USERS.ID=USER_FIRST_ENTITLEMENTS.USER_ID AND
(USER_FIRST_ENTITLEMENTS.APPLICATION_NAME='MY APPLICATION NAME'
AND USER_FIRST_ENTITLEMENTS.ENTITLEMENT_NAME LIKE '%\fis\%')
) join1
(
SELECT DISTINCT injoin1.ID,injoin2.APPLICATION_ROLE_ID, injoin2.NAME FROM
(
SELECT GLOBAL_USERS.ID,USER_SECOND_ENTITLEMENTS.APPLICATION_ROLE_ID
FROM GLOBAL_USERS
INNER JOIN USER_SECOND_ENTITLEMENTS
ON GLOBAL_USERS.ID=USER_SECOND_ENTITLEMENTS.USER_ID
) injoin1,
(
SELECT USER_SECOND_ENTITLEMENTS.APPLICATION_ROLE_ID,SECOND_ENTITLEMENT_DEFINITIONS.NAME
FROM USER_SECOND_ENTITLEMENTS
INNER JOIN SECOND_ENTITLEMENT_DEFINITIONS
ON USER_SECOND_ENTITLEMENTS.APPLICATION_ROLE_ID=SECOND_ENTITLEMENT_DEFINITIONS.ID
) injoin2
WHERE injoin1.APPLICATION_ROLE_ID=injoin2.APPLICATION_ROLE_ID
) join2
essentially, what I need to do now is see if every join1.ENTITLEMENTNAME for each join1.USER_ID exists for that same join2.ID in join2.NAME. If it doesn't exist then I need to have a row that says join1.USERNAME join1.DEPARTMENT, join1.FIRSTNAME, join1.LASTNAME, join1.TITLE, join1.ENTITLEMENTNAME, join1.APPLICATIONNAME
I, at the same time, need to see if every join2.NAME for each join2.ID exists for that same join1.USER_ID in join1.ENTITLEMENTNAME
I am a little lost on how to do this, but I am sure it is with some type of join.
Notes: comparing join1.ENTITLEMENTNAME's with join2.NAME's and the userids are in join1.USER_ID and join2.ID.
I'm not worried about efficiency or speed, I just need functionality, so a simple answer will suffice.
Extra brownie points if you help me with the regex. The join2.NAME's are stored as themselves, but the join1.ENTITLEMENTNAME's are stored as "/fis/" I'm not exactly sure how to filter on that so it would be helpful if anyone could explain.
Thanks in advance!
Have you looked into Oracle's MINUS command? It is like the inverse of a UNION. It subtracts similarities between two queries so that the result set is the difference between the two. It sounds like it's exactly what you need. Just take your first set and MINUS the second set.
I think the below code is what you described, but you might need to modify it to fit your needs exactly.
SELECT
GLOBAL_USERS.ID AS USER_ID,
GLOBAL_USERS.USER_ID AS USERNAME,
GLOBAL_USERS.DEPARTMENT AS DEPARTMENT,
GLOBAL_USERS.FIRST_NAME AS FIRSTNAME,
GLOBAL_USERS.LAST_NAME AS LASTNAME,
GLOBAL_USERS.TITLE AS TITLE,
USER_FIRST_ENTITLEMENTS.ENTITLEMENT_NAME AS ENTITLEMENTNAME,
USER_FIRST_ENTITLEMENTS.APPLICATION_NAME AS APPLICATIONNAME
FROM GLOBAL_USERS
INNER JOIN USER_FIRST_ENTITLEMENTS
ON GLOBAL_USERS.ID=USER_FIRST_ENTITLEMENTS.USER_ID
AND ( USER_FIRST_ENTITLEMENTS.APPLICATION_NAME='MY APPLICATION NAME'
AND USER_FIRST_ENTITLEMENTS.ENTITLEMENT_NAME LIKE '%\fis\%')
MINUS
SELECT
GLOBAL_USERS.ID AS USER_ID,
GLOBAL_USERS.USER_ID AS USERNAME,
GLOBAL_USERS.DEPARTMENT AS DEPARTMENT,
GLOBAL_USERS.FIRST_NAME AS FIRSTNAME,
GLOBAL_USERS.LAST_NAME AS LASTNAME,
GLOBAL_USERS.TITLE AS TITLE,
USER_SECOND_ENTITLEMENTS.ENTITLEMENT_NAME AS ENTITLEMENTNAME,
USER_SECOND_ENTITLEMENTS.APPLICATION_NAME AS APPLICATIONNAME
FROM GLOBAL_USERS
INNER JOIN USER_SECOND_ENTITLEMENTS
ON GLOBAL_USERS.ID=USER_SECOND_ENTITLEMENTS.USER_ID
AND ( USER_SECOND_ENTITLEMENTS.APPLICATION_NAME='MY APPLICATION NAME'
AND USER_SECOND_ENTITLEMENTS.ENTITLEMENT_NAME LIKE '%\fis\%')
As far as the regex goes, '%\/fis\/%' should do the trick. You needed to escape the '/'.
here's my problem: I have an SQL query that makes 4 calls to a lookup table to return their values from a list of combinations in another table. I finally got this working, and for some reason, when I run the query without DISTINCT, I get a ton of data back, so I'm guessing that I'm either missing something or not doing this correctly. It would be really great if this would not only work, but also return the list alphabetically by the first colour name.
I'm putting my SQL here I hope I've explained this well enough:
SELECT DISTINCT
colour1.ColourID AS colour1_ColourID,
colour1.ColourName AS colour1_ColourName,
colour1.ColourHex AS colour1_ColourHex,
colour1.ManufacturerColourID AS colour1_ManufacturerColourID,
colour2.ColourID AS colour2_ColourID,
colour2.ColourName AS colour2_ColourName,
colour2.ColourHex AS colour2_ColourHex,
colour2.QEColourID2 AS colour2_QEColourID2,
colour3.ColourID AS colour3_ColourID,
colour3.ColourName AS colour3_ColourName,
colour3.ColourHex AS colour3_ColourHex,
colour3.QEColourID3 AS colour3_QEColourID3,
colour4.ColourID AS colour4_ColourID,
colour4.ColourName AS colour4_ColourName,
colour4.ColourHex AS colour4_ColourHex,
colour4.QEColourID4 AS colour4_QEColourID4,
Combinations.ID,
Combinations.ManufacturerColourID AS Combinations_ManufacturerColourID,
Combinations.QEColourID2 AS Combinations_QEColourID2,
Combinations.QEColourID3 AS Combinations_QEColourID3,
Combinations.QEColourID4 AS Combinations_QEColourID4,
Combinations.ColourSupplierID,
ColourSuppliers.ColourSupplier
FROM
ColourSuppliers INNER JOIN
(
colour4 INNER JOIN
(
colour3 INNER JOIN
(
colour2 INNER JOIN
(
colour1 INNER JOIN Combinations ON
colour1.ColourID=Combinations.ManufacturerColourID
) ON colour2.ColourID=Combinations.QEColourID2
) ON colour3.ColourID=Combinations.QEColourID3
) ON colour4.ColourID=Combinations.QEColourID4
) ON ColourSuppliers.ColourSupplierID=Combinations.ColourSupplierID
WHERE Combinations.ColourSupplierID = ?
Thanks
Steph
It looks as though you've probably got multiple records for each set of four colour combinations in the Combinations table - posting the structure of the table might help us to work it out.
Adding the clause order by colour1.ColourName to the end of the query should sort it alphabetically by the first colour name.
My guess (and it is a guess because your SQL query is very wide!) is that you're getting the cartesian product.