I have a query that's supposed to pull all reservations from one table and then sum the Quantity column from a linked table. I've set a right join on the query but for some reason, it's only showing reservations that have at least one quantity record. The query below should return 4 rows, 3 with a sum of zero quantity but is only returning a single row. Any ideas?
SELECT
tblReservations.Subaccount,
SUM(tblBilling_Detail.Quantity)
FROM tblBilling_Detail
RIGHT JOIN tblReservations
ON tblBilling_Detail.Linked_Account = tblReservations.Subaccount
WHERE Usage_Start
BETWEEN tblReservations.Reservation_Start
AND tblReservations.Reservation_End
GROUP BY tblReservations.Subaccount
It was the WHERE clause. All fixed.
Related
How to select two records from multiple records first where the date field is minimum and second where the date field is maximum.
I will try to explain you in brief:- I have a records_history table where there can be multiple records for one Employee. I want to select only two records for one employee based on max and minimum date . How can i achieve this?
Currently what is done for this scenario is first select all records from Employee then stored it into a temp table the apply cursor for the temp table and get the distinct employee record and then they are selecting the max and min data records from the temp table. but it is taking to much time for processing.
Please suggest a way out for this problem.?
As I can understand you want max and min value for one column based on employee ID.
Select max(column_name) as 'Max', min(column_name) as 'Mini' from table_name group by EmployeeID having EmployeeID='EmployeeID'
Please accept the answer if it helps
I am trying to sum my column named target where measured_component is equal to a specific condition and add it to my table but am having trouble. Ultimately I want to add 4 new rows for the 4 conditions to my current table with all the columns null except for the time_value which would be the month for each total based on the condition.
I am using the below query.
select sum(TARGET) as TARGET_TOTAL
from REF_targets
where MEASURED_COMPONENT ='dispatch'
or MEASURED_COMPONENT='acknoweledge'
or MEASURED_COMPONENT= 'DRIVE'
or MEASURED_COMPONENT= 'ENROUTE'
group by TIME_VALUE
When I have the conditions grouped, I get a crazy number for my sum, but if I create separate queries I get the correct total.
select time_value
, sum(TARGET) as TARGET_TOTAL
from REF_targets
where MEASURED_COMPONENT ='dispatch'
group by TIME_VALUE
I cant select all with this query because I keep getting an error saying that I need to add ALL the columns to the group by which ultimately gives me a mirror of the data I already have for target just in a new column.
Please help,
Thanks!
You get a large number because you don't put MEASURED_COMPONENT in the GROUP BY. This should give you sum for each MEASURED_COMPONENT.
select TIME_VALUES, MEASURED_COMPONENT, sum(TARGET) as TARGET_TOTAL
from REF_targets
where MEASURED_COMPONENT ='dispatch'
or MEASURED_COMPONENT='acknoweledge'
or MEASURED_COMPONENT= 'DRIVE'
or MEASURED_COMPONENT= 'ENROUTE'
group by TIME_VALUES, MEASURED_COMPONENT
Okay it has been quite some time since I have used SQL Server very intensively for writing queries.
There has to be some gotcha that I am missing.
As per my understanding the following two queries should return the same number of duplicate records
SELECT COUNT(INVNO)
, INVNO
FROM INVOICE
GROUP BY INVNO
HAVING COUNT(INVNO) > 1
ORDER BY INVNO
SELECT DISTINCT invno
FROM INVOICE
ORDER BY INVNO
There are no null values in INVNO
Where could I be possible going wrong?
Those queries will not return same results. First one will only give you INVNO values that have duplicates, second will give all unique INVNO values, even if they appear only once in entire table.
the group by query will filter our all the single invoices while the distinct will simply pick one from every invoice. First query is a subset of the second
In addition to what Adam said, the GROUP BY will sort the data on the GROUPed columns.
I am creating a Query which is taking data from multiple other databases through DBlinks.
One of the table "ord" in a Query is immensely large , say more than 50 million rows.
Now, I want to create query and I want to traverse the data and retrieve the required data based on Partitions defined in t1.
i.e if ord has 50 partitions in it with 1 million records each, I want to run my whole query on first partition get the result ,then move to 2nd, 3rd and so on. . upto last partition.
How can I do that?
Please consider the Sample Query where from local DB I am accessing all the remote DBs using DB links.
This Query list all the orders which are active.
Select ord.order_no,
ord.customer_id,
ord.order_date,
cust.customer_id,
cust.cust_name,
adr.street,
adr.city,
adr.state,
ship.ship_addr_street,
ship.ship_addr_city,
ship.ship_addr_state,
ship.ship_date
from order ord#ordDB
inner join customer#custDB cust on cust.customer_id = ord.customer_id
inner join address#adrDB adr on adr.address_id = cust.address_id
inner join shipment#shipDB ship on ship.shipment_id = ord.shipment_id
where ord.active = 'true';
Now there is a feild "partition_key" defined in this table, and each key is associated with say 1 million rows and I want to restructure the query so that at one time we take one partition from Order and run this whole query on that partition and move to next partition until table is not completed.
Please help me to create sample query.
I have two tables, for example ONE and TWO, my tables are bigger and with different structures, but have some fields that coincide.
How can I get the table QUERY?
I can do it with just one table WITH GROUP and SUM, but how can I add the last column in QUERY? Is it possible?
SELECT Date, SUM(Apples), SUM(Oranges) FROM One
GROUP BY Date
SELECT Date, one.place, SUM(Apples), SUM(Oranges), SUM(Grapes)
FROM One left join Two on one.place = two.place and one.[Date] = two.[Date]
GROUP BY Date
SELECT one.Date, one.place, SUM(Apples), SUM(Oranges), SUM(Grapes)
FROM One left join Two on one.place = two.place and one.date = two.date
GROUP BY one.date, one.place
Will return all places in first table and the correct counts