I have a table truck_data with columns truck_no, diesel_filled, source, destination, amount etc
I want to fetch only truck_no and diesel_filled in such way that it will show the details of diesel_filled in each truck through out the month..
Please tell me the SQL query for that - I had tried this query but it's not working
SELECT
truck_no, diesel_filled, date
FROM
truck-data
ORDER BY
date
Please help me out
Thanks in advance
I want output like this
truck_no 1 2 3 4 5 6 7 8 9 etc(date from 1 to 31))
---------------------------------------------------------------------------
xyz 25 22 33 33 22 22 22 0 0 (diesel filled in truck order by date)
pqr 25 25 22 11 22 00 22 55 22
abc 21 15 12 14 13 00 22 00 00
It's still quite unclear what you want. But I think you are mixing presentation and data
SELECT truck_no,diesel_filled,date
FROM truck-data
ORDER BY date
Will give you all rows ordered by date. Presumably you whant some filter on that to only show a specific month.
SELECT truck_no,diesel_filled,date
FROM truck-data
WHERE date >= 2014-10-05 AND date < 2014-11-01 ORDER BY date
To get all the rows for october this year. If date here is a DateTime column.
Then you could change it to:
SELECT truck_no,sum(diesel_filled),Day(date)
FROM truck-data
WHERE date >= 2014-10-05 AND date < 2014-11-01
GROUP BY truck_no
ORDER BY date"
That would give you only one row per day and truck. If disel_filled is a numeric value of some kind.
Then in your UI you would have to do the presentation in any way you prefer. For example in some kind of pivot table like your description above.
You could of course do that in SQL as well, but usually that is a job for the presentation layer.
If you really want to do in SQL you could look into: http://technet.microsoft.com/en-us/library/ms177410(v=sql.105).aspx
If you'r using MsSql.
Related
This is my first post here, and the first problem i havent been able to find a solution to on my own. I have a MainTable that contains the fields: Date, MinutesActiveWork (And other not relevant fields). I have a second table that contains the fields: ID, id_Workarea, GoalOfActiveMinutes, GoalActiveFrom.
I want to make a query that returns all records from MainTable, and the active goal for the date.
Exampel:
Maintable (Date = dd/mm/yyyy)
ID Date ActvWrkMin WrkAreaID
1 01-01-2019 45 1
2 02-01-2019 50 1
3 03-01-2019 48 1
GoalTable:
ID id_Workarea Goal GlActvFrm
1 1 45 01-01-2019
2 2 90 01-01-2019
3 1 50 03-01-2019
What i want from my query:
IDMain Date ActvWrkMin Goal WrkAreaID
1 01-01-2019 45 45 1
2 02-01-2019 50 45 1
3 03-01-2019 48 50 1
The query that i have now is really close to what i want. But the problem is that the query outputs all goals that is less than the date from MainTable (It makes sense why, but i dont know what criteria to type to fix it). Like so:
IDMain Date ActvWrkMin Goal WrkAreaID
1 01-01-2019 45 45 1
2 02-01-2019 50 45 1
3 03-01-2019 48 45 1 <-- Dont want this one
3 03-01-2019 48 50 1
My query
SELECT tblMain.Date, tblMain.ActiveWorkMins, tblGoal.Goal
FROM VtblSumpMain AS tblMain LEFT JOIN (
SELECT VtblGoalsForWorkareas.idWorkArea, VtblGoalsForWorkareas.Goal, VtblGoalsForWorkareas.GoalActiveFrom (THIS IS THE DATE FIELD)
FROM VtblGoalsForWorkareas
WHERE VtblGoalsForWorkareas.idWorkArea= 1) AS tblGoal ON tblMain.Date > tblGoal.GoalActiveFrom
ORDER BY tblMain.Date
(I know i could do this pretty simple with Dlookup, but that is just not fast enough)
Thanks for any advice!
For this, I think you have to use the nested query as I mention below.
select tblMain.id,tblMain.Date,tblMain.ActvWrkMin, tblMain.WrkAreaID,
(select top 1 Goal
from GoalTable as gtbl
where gtbl.id_workarea = 1
and tblmain.[Date] >= gtbl.glActvFrm order by gtbl.glActvFrm desc) as Goal
from Maintable as tblMain
Check the below image for the result which is generated from this query.
I hope this will solve your issue.
On my dataset I select information from four different years sorted by date and how many subscriptions I had on said date, which looks something like this:
Date Year Subs Day
15/09/2014 2015 57 1
16/09/2014 2015 18 2
17/09/2014 2015 16 3
14/09/2015 2016 10 1
15/09/2015 2016 45 2
16/09/2015 2016 28 3
12/09/2016 2017 32 1
13/09/2016 2017 11 2
14/09/2016 2017 68 3
24/08/2017 2018 23 1
25/08/2017 2018 53 2
26/08/2017 2018 13 3
What I'm trying to do is create an 'Year' Column Group to align them horizontally, but when I do that, this is the result:
result
Expected result:
expected result
Is this achievable in SSRS? I've tried removing the group =(Details), which gives me the desired result, except it only returns one line of information.
Any insight aprreciated.
By default, the Details group causes you to get one row per row in the dataset. In your case, I would suggest grouping the Rows by the Day column and create a column group by Year.
First, create the two groups and add columns inside the column group.
Then, add a row outside and above the Day row group. Place the headings here and then delete the top row. It should look like this:
Now these 4 columns will repeat to the right for each year and you will get rows based on the number of days in your dataset.
I have a table arrivals like this:
HHMM Car
---- ---
0001 01
0001 02
0001 03
0002 04
...
0029 20
0029 21
0030 22
...
0059 56
I need to know how many cars arrived at each range of 30 minutes.
I wrote a query like this:
WITH
PREVIOUS_QUERIES AS
(
-- in fact, "PREVIOUS_QUERIES" represent a sequence of queries where I get
-- the timestamp HHMM and convert it in a range where HOURS = HH and
-- MINUTES_START can be 0 (if MM<30) or 30 (if MM>=30).
),
INTERVALS AS
(
SELECT
TO_CHAR(HOURS,'FM00')||':'||TO_CHAR(MINUTES_START,'FM00')||' - '
||TO_CHAR(HOURS,'FM00')||':'||TO_CHAR(MINUTES_START +29,'FM00') AS INTERVAL,
CAR
FROM
PREVIOUS_QUERIES
)
SELECT
INTERVAL,
COUNT (DISTINCT (CAR)),
FROM INTERVALS
GROUP BY INTERVAL
ORDER BY INTERVAL
;
My query produces the following results.
Interval Cars
------------- ----
00:00 - 00:29 21
00:30 - 00:59 35
01:00 - 01:29 41
02:30 - 02:59 5
03:00 - 03:29 12
03:30 - 03:59 13
...
That means, if there are no arrivals in some interval, my query doesn´t show a line with Cars=0. I need these rows in my results:
01:30 - 01:59 0
02:00 - 02:29 0
How could I add these rows? Can it be done with a change on my query, or
should this query be completely rewritten?
What I imagine is that I should generate the 48 ranges of 30 minutes from '00:00-00:29' to '23:30-23:59' and then use them as a parameter for SELECT, but I don´t know how to do it.
You could just select those 48 values into another CTE, something like 'cteIntervals', and then adjust the final query to be something like:
SELECT
I.Interval
, NVL(Q.Cars, 0) Cars
FROM
cteIntervals I
LEFT JOIN
(
-- Your current query
) Q
ON I.Interval = Q.Interval
This has the effect of creating a template for the final query to fit into.
For a slightly more dynamic solution you could look into producing cteIntervals using a recursive CTE, or you could store the values in a table, etc.
I wrote a crosstab "A" in Access which selects 2 text columns and 2 number columns, month and posts from another table "B".
An excerpt of the code is like this...
Select B.xx...
From B
Group by B.xx...
Pivot B.Month;
...which works fine if the months belong to the same year, but since the months are on a rolling basis, when the year carries over, for e.g. month are {October, November, December, January, February}, the order just stays as B.Month ASC {01, 02, 10, 11, 12} and I want it to be pivoted in order like {10 11 12 01 02}
UPDATE: I fixed the main problem, but now I want to
1. change the order to 10/2013 to 3/2014
2. change the headings to 10 to 3
TOPIC SUB-TOPIC 1/2014 10/2013 11/2013 12/2013 2/2014 3/2014
language english 86 88 90 82 88 90
language french 70 77 75 79 82 80
mathematics geometry xx xx xx xx xx xx
mathematics calculus xx xx xx xx xx xx
Create an expression that includes both the year and the month, and use it in the pivot.
something like Pivot (year & format(month,"00"))
or, if you have the complete date in a field, something like `Pivot Format (Date,"yyyy/mm/dd")
as a reminder, Year, Date and Month may be reserved words and you may need different field names - not sure, but likely
I have an excel spreadsheet which contains data like this
Month Value1 FY11Count FY12COunt
----------------------------------------------
jul xxx 23 39
aug yy 33 49
..etc
I am using ODBC to query this data for a dashboard application. I would like to order this data by fiscal year (july,aug....etc till Jun) .
can i add another pseudo column say MonthNo and use some sort of logic to say when Month=Jul then 1, aug then 2..etc and order by that pseudocolumn?
Are there any other suggestions better than this?
not sure if this is not what you mean by "pseudocolumn", but SQL way is this:
order by case Month
when 'jul' then 1
when 'aug' then 2
...
end
ORDER BY CASE
WHEN Month(application_receiving_date) = 1 THEN 13
WHEN Month(application_receiving_date) = 2 THEN 14
WHEN Month(application_receiving_date) = 3 THEN 15
ELSE Month(application_receiving_date) END