|
Chris Quinn wrote: The servers are all behind the same firewall
And you are positive that that statement means that the traffic in fact doesn't go through the firewall? (Versus firewall rules that 'should' allow it to behave as though the firewall wasn't impacting it.)
|
|
|
|
|
Hi Guys,
I want to write a query in Oracle/SQL Server (doesn’t make a lot of difference)
I have 3 tables:
EMSINFO (EMSINFOID, DEVICEID, DATE, PlateNumber)
Devices (DEVICEID, INOUT)
Roads (ROADID, ROADNAME)
EMSINFO Contains the enters and exits of vehicles and it’s captured by a device
We have the device id,
And roads has the lists of all the roads in the zone
based on the INOUTwe know whether its enter or exit
Now I want to select PlateNumber, total time in the zone (enter time – exit time) and
Details of when entered and when exited in a text format in one record, because each vehicle can enter and exit multiple times in a particular day
Return Fields of store procedure:
PlateNo: Plate number
text: Enter from (road name) at (time)
Exited from (road name) at ( time)
Duration: Total Duration of being in the zone
My question is generating that text or enters and exits
I appreciate the help
Mr.K
|
|
|
|
|
There is no info on which road the device controls.
Should possibly be:
Devices (DEVICEID, INOUT, ROADID)
|
|
|
|
|
Yes, that's right, neglected to mention it, there is a ROADIDin Device table, now do you have any solution for this problem?
|
|
|
|
|
There are a number of solutions to this, you can use a CTE or 2 queries.
Select all the records using an inner join on the foreign keys and order by platenumber road and date.This should give you the reads for each plate so you can get the max/min for each plate/road and do a simple timespan function on the dates.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Try something like this:
WITH Entries AS (
SELECT PlateNumber
,DATE
,RoadName
FROM EMSINFO e
,Devices d
,Roads r
WHERE e.deviceID = d.DeviceID
AND d.RoadID = r.RoadID
AND d.InOut = 'IN'
)
,Exits AS (
SELECT e.PlateNumber
,e2.DATE EntryTime
,e2.RoadName EntryRoad
,Min(DATE) ExitTime
FROM Entries e2
,EMSINFO e
,Devices d
WHERE e2.platenumber = e.platenumber
AND e.deviceID = d.DeviceID
AND d.InOut = 'OUT'
AND e.DATE > e2.DATE
GROUP BY e.PlateNumber
,e2.DATE
,e2.RoadName
)
SELECT e.PlateNumber
,EntryTime
,EntryRoad
,ExitTime
,r.RoadName ExitRoad
FROM Exits e
,EMSINFO e2
,devices d
,Roads r
WHERE e.platenumber = e2.platenumber
AND e.ExitTime = e2.DATE
AND e2.DeviceID = d.DeviceID
AND d.RoadID = r.RoadID
There are probably better implementations to your problem, Search for sql+islands+and+gaps[^].
"Islands and Gaps" is the name of this classic problem.
|
|
|
|
|
Thanx, I will check it out
|
|
|
|
|
hi to all
i want to use a sub report in rdlc
i use a store procedure for data sourse of subreport that no contain any parameter
and when i want to run main reort i get this error!!!!?
An error occurred during local report processing.
Value cannot be null. Parameter name: value
i dont have any parameter in my sub report
and this errot occuered when i have sub report in main report.
my main report is ok.
thank for any help
|
|
|
|
|
Hi Experts, in an existing sp at our company I find this:
SET @MonthFrom=(@Month%12)-1
where @Monthis a calling parameter and @MonthFrom is declared within the sp.
Can someone please explain what "%12" means?
Thank you, Bernd
modified 7-Jan-14 2:11am.
|
|
|
|
|
|
Hi Peter, thx for your help. I could no determine that from the code and without a clue I was unable to find it in the internet. Now I wonder what sense this makes? None?!?
Greets Bernd
|
|
|
|
|
You are welcome. Please do one thing, according to CP instruction : "If your problem was answered then edit your message and add "[Solved]" to the subject line of the original post, and cast an approval vote to the one or several answers that really helped you." Thanks.
|
|
|
|
|
Hi to all
How can I write a query that get me this result:
My table is :
ColumnA ColumnB ColumnC ColumnD
A1 B1 Can be any thing Can be any thing
A1 B2 Can be any thing Can be any thing
A1 0 Can be any thing Can be any thing
A1 0 Can be any thing Can be any thing
A3 1 Can be any thing Can be any thing
I want to get this result after run a query:
ColumnA ColumnB ColumnC ColumnD
A1 B1 Can be any thing Can be any thing
A1 0 Can be any thing Can be any thing
A1 0 Can be any thing Can be any thing
A3 1 Can be any thing Can be any thing
If ColumnA have B1 and also have a row with B2 I want to just get A1 B1 Row
How can I do this
Thanks for any idea
|
|
|
|
|
How should the query behave if you have a row with B2 but none with B1? Should the row with B2 be included or not?
|
|
|
|
|
I'm not very good at SQL, but I did write this for SQL Server 2012
I'm trying to write TSQL to get all the stuff sold on a date, distinct list of stuff based on part number, and just add up the total qty sold, and the subtotal based on price for each item.
I'm not sure if I should loop the @Temptable results using WHERE partnumber
I'm stuck here, not even sure what words to use to search for learning. I could use a pointer or suggested method to use here.
DECLARE <a href="/Members/Year">@Year</a>AS int;
DECLARE <a href="/Members/month">@Month</a>AS int;
DECLARE <a href="/Members/Day">@Day</a>AS int;
SET <a href="/Members/Year">@Year</a>= 2013;
SET <a href="/Members/month">@Month</a>= 8;
SET <a href="/Members/Day">@Day</a>= 29;
DECLARE @startDate AS DATE;
DECLARE @stopDate AS DATE;
SET @startDate = DATETIMEFROMPARTS(@Year, <a href="/Members/month">@Month</a> <a href="/Members/Day">@Day</a> 0, 0, 0, 0);
SET @stopDate = DATETIMEFROMPARTS(@Year, <a href="/Members/month">@Month</a> <a href="/Members/Day">@Day</a> 23, 59, 59, 999);
DECLARE <a href="/Members/temp">@Temp</a>TABLE(
PartNumber VarChar(80),
ThumbNail VarChar(250),
Qty INT,
Cost Decimal,
Price Decimal
)
INSERT INTO <a href="/Members/temp">@Temp</a>(PartNumber, ThumbNail, Qty, Cost, Price)
SELECT PartNumber, ThumbNail, Qty, Cost, Price From CompletedOrdersCart WHERE OrderDate > @startDate AND OrderDate < @stopDate
06-CRL10, /images/06-cr10.jpg, 1, 6.45, 10.50
06-CRL10, /images/06-cr10.jpg, 1, 6.45, 10.50
06-CRL10, /images/06-cr10.jpg, 1, 6.45, 10.50
06-CRL10, /images/06-cr10.jpg, 1, 6.45, 10.50
33-8114, /images/33114.jpg, 1, 3.45, 6.00
33-8114, /images/33114.jpg, 1, 3.45, 6.00
33-8114, /images/33114.jpg, 1, 3.45, 6.00
33-8114, /images/33114.jpg, 1, 3.45, 6.00
DECLARE @SummaryItems TABLE(
PartNumber VarChar(80),
ThumbNail VarChar(250),
Qty INT,
Cost Decimal,
Price Decimal,
SubTotal Decimal
)
INSERT INTO @SummaryItems (PartNumber, ThumbNail, Cost, Price)
SELECT DISTINCT(PartNumber), ThumbNail, Cost, Price FROM <a href="/Members/temp">@Temp</a>
06-CRL10, /images/06-cr10.jpg, null, 6.45, 10.50, null
33-8114, /images/33114.jpg, null, 3.45, 6.00, null
SELECT PartNumber, Thumbnail, Qty, Cost, Price, SubTotal FROM @SummaryItems;
|
|
|
|
|
I'm not sure I understood you correctly but try this:
SELECT PartNumber
,ThumbNail
,Qty
,Cost
,Price
,SUM(Qty * Price) subtotal
From CompletedOrdersCart
WHERE OrderDate BETWEEN @startDate AND @stopDate
GROUP BY PartNumber
,ThumbNail
,Qty
,Cost
,Price
And tell us if it gets you the result you want.
It looks to me like you're thinking procedurally. But SQL is a fourth generation programming language that takes care of that for you.
Refresh your Set theory instead.
Also remember, a temptable is always the last resort.
I also don't believe the Thumbnail belongs in a transaction table, I would keep it in a Parts table.
|
|
|
|
|
Thanks Jorgen!
I tried using sum, but I was told via error that I needed a group by
I'll try it Sunday morning and let you know.
The thumbnail was just to show the picture of the item.
Normally, i would just do the math in asp.net, but I thought I would expand my knowledge of SQL.
|
|
|
|
|
jkirkerx wrote: The thumbnail was just to show the picture of the item.
Yes, but you're storing multiple instances of the same item which isn't very efficient when the database grows. It also gives you many records to update when you change the picture.
Put it in a different table with an ID and join[^] it in to the base query
jkirkerx wrote: Normally, i would just do the math in asp.net, but I thought I would expand my knowledge of SQL.
In this case it's the right place to do it. SQL excels at storing, filtering and aggregation of data. So any math that comes with the aggregation belongs to the database (as a general but not specific rule, there are always exceptions).
|
|
|
|
|
I didn't think of that on the thumbnails. That took me awhile to understand just now.
My skill levels at the time 7 years ago were weak, But the lesson is something I need to implement.
Let me try the Get Clause now, will let you know how it works out.
|
|
|
|
|
That's close to exactly what I wanted. I had no idea that SQL was so, I'm not sure what word to use here, but in so few lines, it was able to produce the result I wanted. So the Group By Clause replaced the Distinct I was using to just produce to 2 items I wanted.
Now I just need to add up the total Qty for each item. The SubTotal is correct, but the Qty is still just 1, should be like 7 for the first line.
Let me see if I can solve this part myself. I may have a question
Thanks!
DECLARE <a href="/Members/Year">@Year</a>AS int;
DECLARE <a href="/Members/month">@Month</a>AS int;
DECLARE <a href="/Members/Day">@Day</a>AS int;
SET <a href="/Members/Year">@Year</a>= 2013;
SET <a href="/Members/month">@Month</a>= 8;
SET <a href="/Members/Day">@Day</a>= 29;
DECLARE @startDate AS DATE;
DECLARE @stopDate AS DATE;
SET @startDate = DATETIMEFROMPARTS(@Year, <a href="/Members/month">@Month</a> <a href="/Members/Day">@Day</a> 0, 0, 0, 0);
SET @stopDate = DATETIMEFROMPARTS(@Year, <a href="/Members/month">@Month</a> <a href="/Members/Day">@Day</a> 23, 59, 59, 999);
DECLARE @SummaryItems TABLE(
PartNumber VarChar(80),
ThumbNail VarChar(250),
SDescription VarChar(250),
Qty INT,
Cost Decimal,
Price Decimal,
SubTotal Decimal
)
INSERT INTO @SummaryItems (PartNumber, ThumbNail, SDescription, Qty, Cost, Price, SubTotal)
SELECT PartNumber, ThumbNail, SDescription, Qty, Cost, Price, SUM(Qty * Price) From CompletedOrdersCart
WHERE OrderDate > @startDate AND OrderDate < @stopDate
GROUP BY PartNumber, Thumbnail, SDescription, Qty, Cost, Price
SELECT PartNumber, Thumbnail, SDescription, Qty, Cost, Price, SubTotal FROM @SummaryItems;
This is what I got returned, the Qty just needs to be 7, so 7 * 44.00 = 308.00
PN, Thumbnail, Description, Qty, Cost, Price, SubTotal
06-10, /Images/thumbnail/06-10.jpg, 10 inch Continuious Blade, 1, 22.00, 44.00, 308.00
FYI:
I tried between, but I would of had to deduct a day, and add a day for start and stop.
|
|
|
|
|
Where does the QTY come from, and why should it be 7 ? if it's the result of a different group by, you can get it with a CTE[^], and then join it back in to your query.
CTEs are useful when you need to return two calculated values based on different group by statements.
Christian Graus
My new article series is all about SQL !!!
|
|
|
|
|
The partnumber and thumbnail has changed since the original post, I believe you can safely assume the database contents or some other circumstances has also changed.
|
|
|
|
|
The Qty comes from like a shopping cart history table. So all cart items sold on the requested report date may contain 20 orders for the same item, so that day, 7 people ordered the same item of qty 1, so the qty would be 7. 7 was the result I was looking for to make sure the math was right.
My explanation below is crude, but it should represent the thought. I have this bad feeling about this, I think your going to rip me a new one here
Shopping Cart History
date ------- PN ---------- Qty
08/29/2013 - 8AM 06-CR10 1
08/29/2013 - 9AM 06-CR10 1
08/29/2013 - 10AM 06-CR10 1
08/29/2013 - 11AM 12-ZZZ1 2
So the Qty would be 3
the result set would be
Items requiring fulfillment today
PN --------Qty
06-CR10 3
12-ZZZ1 2
modified 6-Jan-14 12:28pm.
|
|
|
|
|
I believe you can remove also the last temptable:
DECLARE <a href="/Members/Year">@Year</a>AS int;
DECLARE <a href="/Members/month">@Month</a>AS int;
DECLARE <a href="/Members/Day">@Day</a>AS int;
SET <a href="/Members/Year">@Year</a>= 2013;
SET <a href="/Members/month">@Month</a>= 8;
SET <a href="/Members/Day">@Day</a>= 29;
DECLARE @startDate AS DATE;
DECLARE @stopDate AS DATE;
SET @startDate = DATETIMEFROMPARTS(@Year, <a href="/Members/month">@Month</a> <a href="/Members/Day">@Day</a> 0, 0, 0, 0);
SET @stopDate = DATETIMEFROMPARTS(@Year, <a href="/Members/month">@Month</a> <a href="/Members/Day">@Day</a> 23, 59, 59, 999);
SELECT PartNumber, ThumbNail, SDescription, sum(Qty) Qty, Cost, Price, SUM(Qty * Price) SubTotal From CompletedOrdersCart
WHERE OrderDate > @startDate AND OrderDate < @stopDate
GROUP BY PartNumber, Thumbnail, SDescription, Cost, Price
And just like the thumbnail, the SDescription belongs in a different table, for the same reasons
jkirkerx wrote: I tried between, but I would of had to deduct a day, and add a day for start and stop.
My bad,
WHERE OrderDate BETWEEN @StartDate AND @StopDate is the same as
WHERE OrderDate >= @startDate AND OrderDate <= @stopDate
not
WHERE OrderDate > @startDate AND OrderDate < @stopDate
|
|
|
|
|
I think I ended up doing that, and was amazed at how it worked.
Let me check,
This is the SQL in the asp.net function I used for testing. I kept the DateTimeFromParts because I stored the order date as a timestamp, and not a date, and had to be more precise with the hours minutes and seconds. I was originally BETWEEN, but I could not get a result, and it took me hours to figure it out, plus I had upgraded to SQL Server 2012 at the time. Then the timestamp dawned on me, and I spent another hour figuring out how to be more precise. I'd rather use between, and not use anything specific to 2012.
I SUM the Qty to get 7, and it worked on the test data set, not sure if I was just lucky to get that value, I need to find more data on different days to fully check it.
"DECLARE @startDate AS DATE; " & _
"DECLARE @stopDate AS DATE; " & _
"SET @startDate = DATETIMEFROMPARTS(@Year, @Month, @Day, 0, 0, 0, 0); " & _
"SET @stopDate = DATETIMEFROMPARTS(@Year, @Month, @Day, 23, 59, 59, 999); " & _
"DECLARE @SummaryItems TABLE(" & _
"PartNumber VarChar(80), " & _
"ThumbNail VarChar(250), " & _
"SDescription VarChar(250), " & _
"Qty INT, " & _
"Cost Decimal, " & _
"Price Decimal, " & _
"SubTotal Decimal " & _
") " & _
"INSERT INTO @SummaryItems (PartNumber, ThumbNail, SDescription, Qty, Cost, Price, SubTotal) " & _
"SELECT PartNumber, ThumbNail, SDescription, SUM(Qty), Cost, Price, SUM(Qty * Price) From CompletedOrdersCart " & _
"WHERE OrderDate > @startDate AND OrderDate < @stopDate " & _
"GROUP BY PartNumber, Thumbnail, SDescription, Qty, Cost, Price " & _
"SELECT PartNumber, Thumbnail, SDescription, Qty, Cost, Price, SubTotal FROM @SummaryItems;"
modified 6-Jan-14 12:44pm.
|
|
|
|
|