Hello, I'm trying to find the most optimal way to perform a trickyquery. I'm hoping this is some sort of standard problem that has beensolved before, but I'm not finding anything too useful so far. I havea solution that works (using subqueries), but is pretty slow.Assume I have two tables:[Item]ItemID int (Primary Key)ItemSourceID intItemUniversalKey uniqueidentifierPrice int[Source]ItemSourceIDPriorityI'm looking for a set of ItemIDs that match a query to the Price(something like Price < 30), with a unique ItemUniversalKey, taking thefirst item with each key according to Source.Priority.So, given Item rows like this:1 2 [key_one] 152 2 [key_two] 253 1 [key_one] 15and Source rows like this:1 12 2I want results like this:2 2 [key_two] 253 1 [key_one] 15Row 1 in Item would be eliminated because it shares an ItemUniversalKeywith row 3, and row 3's Source.Priority is lower than row 1.Help!?
Given any date, how can I change that date to be one of the next 3 year cycle dates. These are the fixed cycle dates (always on Sep 1 on those set years) 9/1/1997 9/1/2000 9/1/2003 etc.
For example, if the supplied date was January 4, 1998, it would convert to September 1, 2000 October 31, 2000 -> September 1, 2000 Decemb 12, 2005 -> September 1, 2006 January 1, 1995 -> September 1, 1997
I have tricky question related to date fields. here i explain with a simple scenario.
Consider a table A with about 100000 patient stay records. the table has facilitypatientkey, staybegineffectivedate, stayendeffectivedate columns. There can a more than one record for a patient since same patient can stay and get discharged more than once. For example
facilitypatientkey stays from '1/1/2000' to '5/25/2000' facilitypatientkey stays from '12/20/2000' to '3/15/2001'
I want to return the latest end date of each patient. This is pretty straightforward we can just group by facilitypatientkey and return max(enddate) like below
Select facilitypatientkey,maX(stayendeffectivedate) AS MaxEndDate from dw_patient_stay where facilitypatientkey IN (Select facilitypatientkey from dw_patient_stay) group by facilitypatientkey order by facilitypatientkey
but the tricky part is, if the latest end date is 'null' then i want to return as 'null' for that patient.
For example, facilitypatientkey stays from '1/1/2000' to '5/25/2000' facilitypatientkey stays from '12/20/2000' to '3/15/2001' facilitypatientkey stays from '4/12/2007' to null(which actually means they still stay)
now the above query ll return '3/15/2001' instead of 'null'. Can anyone help me with this?
I'm new to MSSQL 2005 and want to get a summary of a log table. I want to count all the rows for each date based on a DATETIME field called 'post_date' that holds the date and time of each record's creation.
this is the best I can come up with:
Code:
SELECT DISTINCT(LEFT(post_date,11)) AS post_date, COUNT(DISTINCT(LEFT(post_date,11))) AS total_posts FROM log_directory_contacts GROUP BY post_date
The results show each date but the count column ('total_posts') returns '1' for every row even when I know their are more than 1 record on that date.
The above report has 4 columns, with 1st & last column being a calculated field.
Notice that the Opening Balance description is a sum of all amount under period 01,with software having a total Opening Balance of 4,000.00 (2,000.00 + 2,000.00).
The amount for both Hardware & software are group by period 02, and a Closing Balance is derived by adding the sum of Period 01 & Period 02.Total column is a summation of both Hardware & Software amount.
Guys, hope someone out there can help me with the sql command for the above report?
The above report has 4 columns, with 1st & last column being a calculated field. Notice that the Opening Balance description is a sum of all amount under period 01, with software having a total Opening Balance of 4,000.00 (2,000.00 + 2,000.00). The amount for both Hardware & software are group by period 02, and a Closing Balance is derived by adding the sum of Period 01 & Period 02.Total column is a summation of both Hardware & Software amount.
Guys, hope someone out there can help me with the sql command for the above report?
I have a table with a datetime field and a username. the following gives me a count of the individual users reagrdless of how many entries there are for them.
SELECT COUNT(DISTINCT Log_Username) AS Expr1 FROM tblUserLog
However, I also want to have a total of distinct users for each date - how do I do this ?
my group by clause has to select date.. problem is it differentiate 2006-01-23 15:45:34.000 and 2006-01-23 15:46:55.000 ..
As we all know, for sure we need to group all the 23 jan date into one without bothering the time, so how do i let the query pick only one line for date 23 jan?? thankss in advance...
I want to modify the following SQL Query to replace the hard-coded date with 5 years back from the current month (The Current Month minus 5 years).
SELECT FirstName, LastName, LatestHireDate, COUNT(*) as "Latest Hire Date" FROM dbo.EMPLOYEE WHERE LatestHireDate > '2013-08-01' and ActiveResourceFlag = 'TRUE' GROUP BY LatestHireDate, FirstName, LastName;
Hi,I have a datetime column named dtDateTime.its format is "Oct 27 2006 12:00:00 "I want to group by only date part of it and countmy code is$sql1="SELECT convert(varchar,J1708Data.dtDateTime,120),count(convert(varchar,J1708Data.dtDateTime,120))FROM Vehicle INNER JOIN J1708Data ON Vehicle.iID = J1708Data.iVehicleIdWHERE (J1708Data.iPidId = 303) AND(J1708Date.dtDateTime between '2006-10-25' AND '2006-10-28')AND (Vehicle.sDescription = $VehicleID)GROUP BY convert(varchar,J1708Data.dtDateTime,120)";However, convert part, group by part doesnt' work at all.(i couldn't check count part)can you find where's the problem?Thx.
How can I group my data to show up like the following? I need to get the wr, sample and the most current date in MS Access. 0805393, 01, 5/16/08 0805393, 02, 5/16/08 0805393, 03, 5/17/08
I tried Max(Y), and also grouping them and it's driving me crazy!... Thanks
I am trying to to a transaction count (per day) running a stored procedure that does a group by date. The problem is that since the date has a time stamp (I assume) it sees each date as a different group even if it's the same day. Is there a way to format the date in the stored procedure so that it sees all of the transactions on the same day as one or is there another way to do this. Select count(recordid),transactiondatefrom sometable group by transactiondate Thanks in advance
But the problem i have is i need to filter the records based on Date joined, but i cant include the DateJoined in my Group by clause ( i am making the Groupby As View) and since my view dont contain the date i couldnt use that as view to use in my application.
hi all,i got is table:Id StartDate EndDatea 19/03/2001 18/03/2002a 19/03/2002 18/04/2002*b 13/08/2000 12/08/2001b 13/08/2001 12/08/2002b 13/08/2002 10/07/2002*Sort command and groupins i am ok but i need to select only the records thathas the latest enddate. (See *)any ideas? thanks in advancerashid
I'm trying to show the max date from a field by group. I've tried Max(OrderedDate) but it still shows me all the dates from the group field I only want to see the last date. I'm using report builder FE, which I can't modify the BE. Any ideas I can get this to work?
Looking to see if thier is a better way to find the last record entered in a group of records.
What I'm doing now is finding the max for the secound column and then doing a sub query to find the max of the third column having the second columns equal.
Table example using simplied data.
PolId
CoveragId
EffDate
Status
Limit1
2
1
9/7/2007
a
10000
2
2
9/7/2007
a
150000
2
2
10/1/2007
a
200000
3
1
9/7/2007
a
10000
The parent program addes a row every time the data is changed. To make things worst; the records arn't always in sqenal order like the above table and some time edits the row instead.
The current query returns a single value. from a single table.
Current code used in the select protion on a larger query. bpi = basicpolicyInformation.
( Select c1.limit1 From AFW_Coverage as c1 Where c1.PolId=bpi.PolId and c1.CoverageId = (select max(CoverageId) as CoverageId From AFW_Coverage as c where c.PolId = c1.PolId and c.CoverageCode = 'Dwelling' and status <> 'D' ) and c1.effDate = (select max(Effdate) as Effdate From AFW_Coverage as c where c.PolId = c1.PolId and c.CoverageID = c1.CoverageId )
Explain the current code. It uses the two sub queries to find the correct record ID that has the data needed.
hopefully someone can suggest the best way of implementing the problem i am trying to resolve. We have a table which contains rows relating to tests run on our product. This table is populated from an SSIS job which parses CSV files.
There are multiple rows per serial number relating to multiple tests. The only tests i am interested in are the ones with an ID of T120. Here is the query i have so far which should make it a little easier to explain:
SELECT [SerialNumber] ,Param1 ,[TimeStamp] FROM [Build Efficiency System].[dbo].[SSIS_SCANNERDATA_TBL] WHERE Test = 'T120' GROUP BY SerialNumber, Param1, [TimeStamp] ORDER BY SerialNumber
What i have above is fine to a point. The problem i am encountering is that in test T120 it specifies a part which can be be one of about 6 in field Param1. If during testing there is a problem with the part then it is replaced and the test run a second time up until the whole product passes the test. The query above returns all instances of replacements so i may have the out put as follows:
I really hope that someone can help me or at least point me in the right direction. I am selecting a set of data and using the date values across the X axis. However the needs exists to group these by week, but these weeks are not the normal weeks, for - they exist as follows the month starts on the first Monday of a month, for example December 2007 starts on Monday the 3rd and the week ends on the 6th of December a so on till the fact that the last week of the month December 2007 starts on Monday the 31st and ends on January the 6th is there any way that I can create a group that could group the datetime values together in this way,
This is not best achieved in SSRS where should I be creating these groups. Any help would really be appreciated.
My scenario is: a person has many events, all based on a date. I need to aggregate the person to show min and max dates for a period, the period being defined as ending when there is not an event following the next date.
I have below SQL. When I run it I get the 'Each GROUP BY expression must contain at least one column that is not an outer reference' error. The date and string expressions are generated dynamically and need to be grouped upon if possible. What am I missing?
INSERT INTO tblStaffPayrollHistory (StaffID, FromDate, ToDate, PayrollNo, EventID) SELECT DISTINCT tblStaffBookings.StaffID, CONVERT(DATETIME, '2015-05-17', 102), CONVERT(DATETIME, '2015-05-17', 102), 'tree', tblEvents.ID FROM tblStaffBookings INNER JOIN tblEvents ON tblStaffBookings.EventID = tblEvents.ID WHERE ... GROUP BY tblStaffBookings.StaffID, CONVERT(DATETIME, '2015-05-17', 102), CONVERT(DATETIME, '2015-05-17', 102), 'tree', tblEvents.ID
I have an SSRS 2012 table report with groups; each group is broken ie. one group for one page, and there are multiple groups in multiple pages.
'GroupName' column has multiple values - X,Y,Z,......
I need to group 'GroupName' with X,Y,Z,..... ie value X in page 1,value Y in page 2, value Z in page 3...
Now, I need to display another column (ABC) in this table report (outside the group column 'GroupName'); this outside column itself is another column header (not a group header) in the table (report) and it derives its name partly from the 'GroupName' values:
Example:
Value X for GroupName in page 1 will mean, in page 1, column Name of ABC column must be ABC-X Value Y for GroupName in page 2 will mean, in page 2, column Name of ABC column must be ABC-Y Value Z for GroupName in page 3 will mean, in page 3, column Name of ABC column must be ABC-Z
ie the column name of ABC (Clm ABC) must be dynamic as per the GroupName values (X,Y,Z....)
Page1:
GroupName Clm ABC-X
X
Page2:
GroupName Clm ABC-Y
Y
Page3:
GroupName Clm ABC-Z
Z
I have been able to use First(ReportItems!GroupName.Value) in the Page Header to get GroupNames displayed in each page; I get X in page 1, Y in page 2, Z in page 3.....
However, when I use ReportItems (that refers to a group name) in the Report Body outside the group,
I get the following error:
Report item expressions can only refer to other report items within the same grouping scope or a containing grouping scope
I need to get the X, Y, Z ... in each page for the column ABC.
I have been able to use this - First(Fields!GroupName.Value); however, I get ABC-X, ABC-X, ABC-X in each of the pages for the ABC column, instead of ABC-X in page 1, ABC-Y in page 2, ABC-Z in page 3, ...
Currently I report our monthly fees broken down into 4 weeks per month by using 4 separate datasets with the following code
SELECT SUM(Practice.ibvSalesByJob.JobBilledExVAT) AS Sum_JobBilledExVAT FROM Practice.ibvSalesByJob INNER JOIN Practice.idvJobType
[Code] ....
The second dataset then has the date code changed to
AND Practice.ibvSalesByJob.[Date] >= Cast(@Month AS char(2)) + '/08/' + Cast(@DateYear AS char(4)) + ' 00:00:01' AND Practice.ibvSalesByJob.[Date] <= Cast(@Month AS char(2)) + '/16/' + Cast(@DateYear AS char(4)) + ' 00:00:00'
The third
AND Practice.ibvSalesByJob.[Date] >= Cast(@Month AS char(2)) + '/16/' + Cast(@DateYear AS char(4)) + ' 00:00:01' AND Practice.ibvSalesByJob.[Date] <= Cast(@Month AS char(2)) + '/23/' + Cast(@DateYear AS char(4)) + ' 00:00:00'
The fourth
AND Practice.ibvSalesByJob.[Date] >= Cast(@Month AS char(2)) + '/23/' + Cast(@DateYear AS char(4)) + ' 00:00:01'
Now I was hoping so that I could report the above data in one chart and do an expression on the category and group the dates so I would just have one dataset like below but four separate columns saying Week 1, 2 3 and 4 and then the sum filtered in line.
SELECT SUM(Practice.ibvSalesByJob.JobBilledExVAT) AS Sum_JobBilledExVAT FROM Practice.ibvSalesByJob INNER JOIN Practice.idvJobType
[Code] ....
If I could somehow with SQL tie in all 4 separate datasets and display them as Week 1, 2, 3 and 4 underneath the dataset and selectable.An even simpler solution maybe just understanding how charts work, I can get it so I display the 4 separate weeks in the chart however I can't get the bottom line (Category Group) to display Week 1, 2, 3 and 4.
Hello, I have four diffrent tabels: bo_ Class, bo_Competition, bo_Result, bo_Licence And list all Licence where bo_Class.classRankingNbr >0
ResultLicNbr FirstName SurName ClassName ResultPlace ClassRankingNbr ClassRankingFemaleMale ------------ --------- --------------- ------------- ----------- --------------- ---------- M70891DEN03 Dennis Vrabac U23 SM 2007 - Herrar 1 1 H M050887PON01 Pontus Svensson U23 SM 2007 - Herrar 2 1 H M181188MAR01 Marcus Edlund U23 SM 2007 - Herrar 3 1 H M190291JOH01 Johan Helldén U23 SM 2007 - Herrar 4 1 H M180360GER01 Gert Lindholm Herrar klass A 1 2 H M041062MIC01 Micael Hamberg Herrar klass A 2 2 H K191286SOP01 Sophia Bergvall U23 SM 2007- Damer 1 1 D K030889REB01 Rebecka Larsen U23 SM 2007- Damer 2 1 D K050785CAR01 Carin Johansson U23 SM 2007- Damer 3 1 D
If bo_Class.ClassRowNbr for an Class is 1 get out place 1 from that competition, and if an ClassRowNbr is 2 get out top2 from that competition. And so on.
From that list i want an SubQuery list where all licens order by where bo_Result.ResultPlace = bo_Class.ClassRowNbr.
In text form:
ResultLicNbr FirstName SurName ClassName ResultPlace ClassRankingNbr ClassRankingFemaleMale ------------ --------- --------------- ------------- ----------- --------------- ---------------------- M170891DEN03 Dennis Vrabac U23 SM 2007 - Herrar 1 1 H M180360GER01 Gert Lindholm Herrar klass A 1 2 H M041062MIC01 Micael Hamberg Herrar klass A 2 2 H K191286SOP01 Sophia Bergvall U23 SM 2007- Damer 1 1 D K030889REB01 Rebecka Larsen U23 SM 2007- Damer 2 1 D K180793LIN01 Linnéa Hamberg Damer Klass A 1 1 D
SELECT m.Namn + ' ' + m.ENamn AS Spelare, SUM(r.Serier) AS Ser, SUM(r.Poang) AS Po, ROUND(SUM(r.Resultat) / SUM(r.Serier * 1.0), 2, 1)AS Snitt, ROUND(SUM(r.Poang * 1.0) / SUM(r.Serier), 2) AS [P Snitt], ROUND(SUM(r.Miss * 1.0 / r.Serier), 2, 1) AS Miss, SUM(r.Miss) AS [Sa Miss], MAX(r.Resultat) AS Bästa, MIN(r.Resultat) AS Sämsta FROM Resultat r INNER JOIN Medlemmar m ON r.Spelare = m.ID WHERE (r.Omgang IN (SELECT DISTINCT TOP 3 Omgang FROM Resultat ORDER BY Omgang DESC)) GROUP BY m.Namn + ' ' + m.ENamn ORDER BY 4 DESC
What i want to do is sort out Min(r.Resultat) where serier is mor than 3, Not WHERE (r.Resultat) >3. More like in Access "Min(IIf(r.serier=4,r.resultat,Null)) AS [Sämsta]" But that i cant do in SQL
Have also tryed with "WHERE (SELECT MIN(r.Serier) FROM Resultat Resultat r INNER JOIN medlemmar m ON r.Spelare = m.ID" Get lowest result same on all players.
Hello allI've got this tricky situation that I would like to solve in SQL, butdon't know how to do. This is the table:Id = 3, VId = 2, Time1 = 10:00, Time2 = 14:00Id = 4, VId = 2, Time1 = 16:00, Time2 = 17:00Id = 5, VId = 2, Time1 = 18:00, Time2 = 19:00Id = 6, VId = 2, Time1 = 20:00, Time2 = 21:00Id = 7, VId = 3, Time1 = 11:00, Time2 = 13:00Id = 8, VId = 3, Time1 = 15:00, Time2 = 16:00Id = 9, VId = 3, Time1 = 18:00, Time2 = 20:00GetRows @Time='15:30' will return row with Id=4GetRows @Time='16:30' will return row with Id=4 and row=9Logic behind this:Return row n where Time2 of Id=(n-1) < @Time < Time 1 of Id=(n) and sameVId.Ie. if @Time = '15:30' then Time2 of Id = 3 is lower than @Time, andTime1 of Id = 4 is higher than @Time => return row with Id = 4.This got a bit messy but if someone could decipher this and possiblygive an answer I'd be very glad.regardsJohnny
I have an integer in the database that was saved in reverse byte order (BigEndian). Due to some backward compatibility issues (long story) I can't just convert the number to the normal format and save it that way in the database. Instead, when I read the number in my program, I just reverse its bytes and display the proper value, and translate the number back when it has to be saved back to the database.
Now, the problem is that there are some views that pull this numbers directly from the database and display it.
My question is: can this number be converted from BigEndian to LittleEndian similarly to how I do in my program using T-SQL?