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.
Related
I have a table "Bed" and a table "Component". Between those two I have a m:n relation and the table "BedComponent", where I store the Bed-ID and the Component-ID.
Every Component has a price. And now I want to write a select-statement that gives me the sum of prices for a certain bed.
This is what I have:
SELECT Bed.idBed, Bed.name, SUM(src.price) AS summe, Bed.idCustomer
FROM Bed,
(SELECT price
FROM dbo.Component AS C
WHERE (C.idComponent IN
(SELECT idComponent
FROM dbo.BedComponent AS BC
WHERE 1 = BC.idBed))) AS src
GROUP BY dbo.Bed.idBed, dbo.Bed.name, dbo.Bed.idCustomer;
This statement works. But of course I don't want to write the bed-ID hard coded into my select as it will always calculate the price for bed 1. Instead of the "1" i want to have the current bed-id.
I work with MS SQL Server
Thanks for your help.
I think you want:
select b.idBed, b.name, SUM(src.price) AS summe, b.idCustomer
from bed b join
bedcomponent bc
on b.idBed = bc.idBed join
component c
on c.idComponent = bc.idComponent
group by b.idBed, b.name, b.idCustomer;
The idCustomer looks strange to me in the select and group by, but I don't know what you are trying to achieve.
Also note the use of table aliases, which make the query easier to write and to read.
I don´t know if the title is correct but basicaly I need this(result of query):
r_e_s_id person_id
89074 161704
89074 161703
89095 161708
89095 161707
68651 129884
68651 129883
81512 161074
81512 161073
To be inserted/updated(in case that there will be r_e_s_id values in table) into table COMM_PROP which has cols - r_e_s_id, perid1, perid2...like this:
r_e_s_id perid1 perid2
89074 161704 161703
89095 161708 161707
68651 129884 129883
81512 161074 161073
How can I do this? I´m using Oracle 11g.
Thank you for any help!!!
Considering only 2 values i.e. 1 max and 1 min per r_e_s_id, Try this:-
SELECT r_e_s_id, MAX(person_id) AS perid1, MIN(person_id) AS perid2
FROM YOUR_TABLE
GROUP BY r_e_s_id;
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.
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
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.