View all data from a GROUP BY statement - asp.net

I will demonstrate a simple example of my issue instead of viewing the long sql query from my project.
My example is something like:
Select DeliveryAddressID, PickupDate, TotalWeight, count(DeliveryAddressID) AS Packages
From DeliveryDB
Where Date1 >= '10-12-2013' AND Date2 <= '15-12-2013'
Group By PickupDate, DeliveryAddressID
My result will be something like:
http://prntscr.com/1iksoe
What I want to achieve is to get this DeliveryAddressID for ALL 4 packages but still keep the total numbers. This would be something like this.
What I want as expected result:
http://prntscr.com/1iksr7
The reason I want to do this is because in this way i will be able to track the barcode for each package and also be able to track more details.
Cheers :-)

In most databases, you can do this using window functions:
Select DeliveryAddressID, PickupDate, TotalWeight,
count(DeliveryAddressID) over (partition by PickupDate, DeliveryAddressID) AS Packages
From DeliveryDB
Where Date1 >= '10-12-2013' AND Date2 <= '15-12-2013';
This will return the total on all four lines.
EDIT:
You can handle TotalWeight (which was not part of the original question) the same way:
Select DeliveryAddressID, PickupDate,
count(DeliveryAddressID) over (partition by PickupDate, DeliveryAddressID) AS Packages,
CEILING(SUM(CASE When (Weight <> 0 AND Volumen <> 0) AND Weight >= (Volumen * #zeroVoluFac)
Then Weight
end) over (partition by PickupDate, DeliveryAddressID)
) as TotalWeight
From DeliveryDB
Where Date1 >= '10-12-2013' AND Date2 <= '15-12-2013';

I can't do quite that, but would you accept total lines after the details instead of zero on the first N lines and the total on the last, like this:
IdCode Weight PickupDate
----------- ----------- -------------------------
12345 5 2013-01-07 00:00:00.0
12345 10 2013-01-07 00:00:00.0
12345 15 2013-01-07 00:00:00.0
12345 20 2013-01-07 00:00:00.0
(4 rows)
Sum(Weight) Count(IdCode)
----------- -------------
50 4
If that's of any use to you, the SQL I used was (sorry about the non-matching column names)
Select IdCode, Weight, PickupDate
From DeliveryDB
Group By PickupDate, IdCode
ORDER BY PickupDate, IdCode
COMPUTE sum(Weight), count(IdCode) by PickupDate, IdCode
The COMPUTE gives totals or other aggregates at the breaks in Group By.

Related

Analytical function issue

I am working on a history handling issue. I am writing a query to update the wrong entries for the start_date. The data in the table is as below:
Subs_is subs_cd number start_dt end_dt
ABC 100 7854 10/8/2015 3/9/2015
ABC 100 58742 10/9/2015 20/09/2015
ABC 100 1278 23/09/2015 30/09/2015
ABC 100 4785 15/10/2015 25/10/2015
I want the start_date to be previous row end_date when the number changes.
can anyone please help me with this.
regards,
Amit
Seems to be a simple LAG (which is not implemented in Teradata, but easy to rewrite):
-- lag(start_date) -- not implemented
-- over (partition by Subs_is, subs_cd
-- order by start_dt
-- previous row's value
max(start_dt)
over (partition by Subs_is, subs_cd
order by start_dt
rows between 1 preceding and 1 preceding)

Iteration for a non-sequential column

can some one help me...
I have to create,for each "Costumer", a iterator for a non-sequential ID to update the "version" column.
I need a cursor or something else?
Can i get some help?
Example:
ID COSTUMER VERSION
12 ANNA 1
24 ANNA 4
25 ANNA 5
60 ANNA 11
I want to correct the version to be sequential
You could use code something like this:
begin
for r in ( select id, row_number() over (partition by name order by version) as rn
from costumer
)
loop
update costumer
set version = r.rn
where id = r.id;
end loop;
end;
/
The partition by is there because I have assumed you want to have the sequence start from 1 for 'ANNA', then start from 1 again for customer 'JANE' etc. If not you can remove that part.
Here's the way to do it via a single MERGE statement:
MERGE INTO costumer tgt
USING (SELECT ID,
costumer,
VERSION,
ROWID row_id,
row_number() OVER (PARTITION BY costumer ORDER BY VERSION) new_version
FROM costumer) src
ON (tgt.rowid = src.rowid)
WHEN MATCHED THEN
UPDATE SET tgt.version = src.new_version;

Calculating occupany level between a date range

I'm having trouble trying to wrap my head around how to write this query to calculate the occupancy level of a hotel and then list the results by date. Consider the following type of data from a table called reservations:
Arrival Departure Guest Confirmation
08/01/2015 08/05/2015 John 13234
08/01/2015 08/03/2015 Bob 34244
08/02/2015 08/03/2015 Steve 32423
08/02/2015 08/02/2015 Mark 32411
08/02/2015 08/04/2014 Jenny 24422
Output Data would ideally look like:
Date Occupancy
08/01/2015 2
08/02/2015 4
08/03/2015 2
08/04/2015 1
08/02/2015 0
And the query should be able to utilize a date range as a variable. I'm having trouble getting the obviously hardest piece of how to both get the count per night and spitting it out by date.
You can generate a list of dates first. In Oracle you can do this by using connect by. This will make a recursive query. For instance, to get the next 30 days, you can select today and keep connecting until you've got the desired number of days. level indicates the level of recursion.
select trunc(sysdate) + level - 1 as THEDATE
from dual
connect by level <= 30;
On that list, you can query the number of reservations for each day in that period:
select THEDATE,
(select count(*)
from reservations r
where r.Arrival >= THEDATE and
r.Departure < THEDATE) as RESERVATIONCOUNT
from
( select trunc(sysdate) + level - 1 as THEDATE,
from dual
connect by level <= 30)
Instead of getting a fixed number of dates, you can also get another value there, for instance, to get at least 30 days in the future, but further if there are reservations for later..:
select THEDATE,
(select count(*)
from reservations r
where r.Arrival >= THEDATE and
r.Departure < THEDATE) as RESERVATIONCOUNT
from
( select trunc(sysdate) + level - 1 as THEDATE,
from dual
connect by
level <= greatest(30, (select trunc(max(DEPARTURE) - sysdate)
from reservations)))

sqlite query comparing data in different rows

i have the following table where i have the date( not a primary key) and rating ('A' being the highest grade):
date rating
03-10-2010 C
03-09-2010 C
03-08-2010 B
03-07-2010 B
03-06-2010 B
03-05-2010 B
03-04-2010 A
I need to make a query where i compare the rating in order to return the result for each 'date'.
For example. considering the date 03-10-2010, i want to know when the last rating downgrade happened. if the downgrade was 1 day ago return '1' as result, if it was 2 days ago return '2' and if was older than 3 days return 0.
And i would do the same query for each date, getting an array with the results.
i'm stuck trying to do this and i have no more ideas how to do it. Anyone can help me please?
thanks.
You want the difference, in days, between the date of each record and the date of the record before the last downgrade.
When you have a specific record, the record before the last downgrade is the record that
has a higher rating than this record, and
has a lower date than this record, and
is the latest record of those.
In SQL, this can be done with a correlated subquery:
SELECT date,
rating,
(SELECT date
FROM MyTable AS downgrade
WHERE downgrade.date < MyTable.date
AND downgrade.rating < MyTable.rating
ORDER BY date DESC
LIMIT 1) AS downgrade_date
FROM MyTable
date rating downgrade_date
---------- ---------- ----------
2010-03-04 A
2010-03-05 B 2010-03-04
2010-03-06 B 2010-03-04
2010-03-07 B 2010-03-04
2010-03-08 B 2010-03-04
2010-03-09 C 2010-03-08
2010-03-10 C 2010-03-08
To compute the difference, convert the date into a numeric value.
You can then use this value for further computations:
SELECT date,
rating,
CASE
WHEN days <= 3 THEN days
ELSE 0
END AS whatever
FROM (SELECT date,
rating,
julianday(date) -
julianday((SELECT date
FROM MyTable AS downgrade
WHERE downgrade.date < MyTable.date
AND downgrade.rating < MyTable.rating
ORDER BY date DESC
LIMIT 1)) AS days
FROM MyTable)

Getting All the record of particular month - Building SQL Query

I need some help to build SQL Query. I have table having data like:
ID Date Name
1 1/1/2009 a
2 1/2/2009 b
3 1/3/2009 c
I need to get result something like...
1 1/1/2009 a
2 1/2/2009 b
3 1/3/2009 c
4 1/4/2009 Null
5 1/5/2009 Null
6 1/6/2009 Null
7 1/7/2009 Null
8 1/8/2009 Null
............................
............................
............................
30 1/30/2009 Null
31 1/31/2009 Null
I want query something like..
Select * from tbl **where month(Date)=1 AND year(Date)=2010**
Above is not completed query.
I need to get all the record of particular month, even if some date missing..
I guess there must be equi Join in the query, I am trying to build this query using Equi join
Thanks
BIG EDIT
Now understand the OPs question.
Use a common table expression and a left join to get this effect.
DECLARE #FirstDay DATETIME;
-- Set start time
SELECT #FirstDay = '2009-01-01';
WITH Days AS
(
SELECT #FirstDay as CalendarDay
UNION ALL
SELECT DATEADD(d, 1, CalendarDay) as CalendarDay
FROM Days
WHERE DATEADD(d, 1, CalendarDay) < DATEADD(m, 1, #FirstDay)
)
SELECT DATEPART(d,d.CalendarDay), **t.date should be (d.CalendarDay)**, t.Name FROM Days d
LEFT JOIN tbl t
ON
d.CalendarDay = t.Date
ORDER BY
d.CalendarDay;
Left this original answer at bottom
You need DATEPART, sir.
SELECT * FROM tbl WHERE DATEPART(m,Date) = 1
If you want to choose month and year, then you can use DATEPART twice or go for a range.
SELECT * FROM tbl WHERE DATEPART(m,Date) = 1 AND DATEPART(yyyy,Date) = 2009
Range :-
SELECT * FROM tbl WHERE Date >= '2009-01-01' AND Date < '2009-02-01'
See this link for more info on DATEPART.
http://msdn.microsoft.com/en-us/library/ms174420.aspx
You can use less or equal to.
Like so:
select * from tbl where date > '2009-01-01' and date < '2009-02-01'
However, it is unclear if you want month 1 from all years?
You can check more examples and functions on "Date and Time Functions" from MSDN
Create a temporary table containing all days of that certain month,
Do left outer join between that table and your data table on tempTable.month = #month.
now you have a big table with all days of the desired month and all the records matching the proper dates + empty records for those dates who have no data.
i hope that's what you want.

Resources