T-SQL (SS2K8) :: Group By And Order By - Location Starting With A And B

Jul 7, 2014

I am having below schema.

CREATE TABLE #Turnover (
location varchar(50),
Total int
)

insert into #Turnover (location,Total) values('A', 500)
insert into #Turnover (location,Total) values('AB', 200)
insert into #Turnover (location,Total) values('ABC', 100)
insert into #Turnover (location,Total) values('BA', 100)
insert into #Turnover (location,Total) values('BAC', 500)
insert into #Turnover (location,Total) values('BAM', 100)

Now i want output order by total but same time i want to create two groups. i.e. location starting with A and order by total and after locations starting with B and order by total.

View 5 Replies


ADVERTISEMENT

T-SQL (SS2K8) :: Outer Join Add Non Matching Rows To Each Order Group?

Mar 30, 2015

In Outer join, I would like to add the outer columns that don't exist in the right table for each order number. So currently the columns that don't exist in the right table only appear once for the entire set. How can I go about adding PCity, PState to each order group, so that PCity and PState would be added as null rows to each group of orders?

if OBJECT_ID('tempdb..#left_table') is not null
drop table #left_table;
if OBJECT_ID('tempdb..#right_table') is not null
drop table #right_table;
create table #left_table

[Code]....

View 2 Replies View Related

T-SQL (SS2K8) :: Increment A Column By 5 Starting At 5

Jun 4, 2015

I am trying to increment a column that I am creating in a select by 5's, example 5, 10, 20, 25.... first row starting at 5,

Something like

WITH CTE as
(
SELECT cast(5 as int) as myColumn from table
)

SELECT cte.myColumn + 5 from CTE

View 3 Replies View Related

Best Way To Order Results Sequentially Starting From Somewhere In The Middle

Apr 19, 2007

I'm working with SQL Server 2005, and I'm trying to sort the results based on a user selected letter. Say the user selects 'D' to filter his results. I'd like to return the results starting from D followed by E, F, G...Z, A, B, C. What I'm getting is the results for the D entries at the top of the result set, followed by A, B, C, E...Z.

A solution comes to mind that would be very long and db intensive, by querying on 'like 'D', followed by like 'E', followed by like 'F', etc, but I'm sure that there is a much more efficient way to do this. Below is the code that I'm using now.





' where @SortString = 'd' and @Test is a temp Table



BEGIN

Insert into @Test

Select CompanyName,ContactId, CompanyId

from vContacts where CompanyName like @SortString +'%'

Order by CompanyName



Insert into @Test

Select CompanyName,ContactId, CompanyId

from vContacts where CompanyName not like @SortString +'%'

Order by CompanyName

END



Thanks in advance for your help

View 3 Replies View Related

T-SQL (SS2K8) :: Arrival And Depart Location Query

Apr 7, 2014

I have a question regarding the selection of arrival and departure city.

CREATE TABLE #XY123
(
tktamt MONEY,
departcty VARCHAR(5),
arrivalcty VARCHAR(5),
tktnum INT

[Code] ....

There are One way, Round trips and journey with connecting flights in the above data, what I am looking for a report is tktamt, deprt city, arrival city and tktnum in a single line for each trip.

SO if it is one way trip or round trip there is no issue, if it is a trip with connecting flights tktnum has more than four records. In the above case for tktnum 34567 there are four records, for which I have to display only one record, where the depart city is 'JFK and Arrival city is 'SFO' as well as amount and tktnum.

View 9 Replies View Related

Reporting Service Not Starting - The User Or Group Name 'VIDICOMASPNET' Is Not Recognized.

Jun 29, 2006

Hi, I've been trying for the past week to get SQL 2005 Server up and running.The SQL is up and running however I am unable to get the Reporting service to start. The log file is below but the main line is:

ReportingServicesService!library!4!29/06/2006-14:33:45:: e ERROR: Throwing Microsoft.ReportingServices.Diagnostics.Utilities.UnknownUserNameException: The user or group name 'VIDICOMASPNET' is not recognized., ;
Info: Microsoft.ReportingServices.Diagnostics.Utilities.UnknownUserNameException:The user or group name 'VIDICOMASPNET' is not recognized.

But the domain user ASPNET is present. Similar posts across the internet mention about the .NET Framework not be installed correctly. I have reinstalled it many times.

Any help is most appreciated.

Tom

Here is the full log file:
<Header> <Product>Microsoft SQL Server Reporting Services Version 9.00.1399.00</Product> <Locale>en-US</Locale> <TimeZone>GMT Daylight Time</TimeZone> <Path>C:Program FilesMicrosoft SQL ServerMSSQL.4Reporting ServicesLogFilesReportServerService__main_06_29_2006_14_33_39.log</Path> <SystemName>CRM</SystemName> <OSName>Microsoft Windows NT 5.0.2195 Service Pack 4</OSName> <OSVersion>5.0.2195.262144</OSVersion></Header>ReportingServicesService!library!4!29/06/2006-14:33:41:: i INFO: Initializing ConnectionType to '1' as specified in Configuration file.ReportingServicesService!library!4!29/06/2006-14:33:41:: i INFO: Initializing IsSchedulingService to 'True' as specified in Configuration file.ReportingServicesService!library!4!29/06/2006-14:33:41:: i INFO: Initializing IsNotificationService to 'True' as specified in Configuration file.ReportingServicesService!library!4!29/06/2006-14:33:41:: i INFO: Initializing IsEventService to 'True' as specified in Configuration file.ReportingServicesService!library!4!29/06/2006-14:33:41:: i INFO: Initializing PollingInterval to '10' second(s) as specified in Configuration file.ReportingServicesService!library!4!29/06/2006-14:33:41:: i INFO: Initializing WindowsServiceUseFileShareStorage to 'False' as specified in Configuration file.ReportingServicesService!library!4!29/06/2006-14:33:41:: i INFO: Initializing MemoryLimit to '60' percent as specified in Configuration file.ReportingServicesService!library!4!29/06/2006-14:33:41:: i INFO: Initializing RecycleTime to '720' minute(s) as specified in Configuration file.ReportingServicesService!library!4!29/06/2006-14:33:41:: i INFO: Initializing MaximumMemoryLimit to '80' percent as specified in Configuration file.ReportingServicesService!library!4!29/06/2006-14:33:41:: i INFO: Initializing MaxAppDomainUnloadTime to '30' minute(s) as specified in Configuration file.ReportingServicesService!library!4!29/06/2006-14:33:41:: i INFO: Initializing MaxQueueThreads to '0' thread(s) as specified in Configuration file.ReportingServicesService!library!4!29/06/2006-14:33:41:: i INFO: Initializing IsWebServiceEnabled to 'True' as specified in Configuration file.ReportingServicesService!library!4!29/06/2006-14:33:41:: i INFO: Initializing MaxActiveReqForOneUser to '20' requests(s) as specified in Configuration file.ReportingServicesService!library!4!29/06/2006-14:33:41:: i INFO: Initializing MaxScheduleWait to '5' second(s) as specified in Configuration file.ReportingServicesService!library!4!29/06/2006-14:33:41:: i INFO: Initializing DatabaseQueryTimeout to '120' second(s) as specified in Configuration file.ReportingServicesService!library!4!29/06/2006-14:33:41:: i INFO: Initializing ProcessRecycleOptions to '0' as specified in Configuration file.ReportingServicesService!library!4!29/06/2006-14:33:41:: i INFO: Initializing RunningRequestsScavengerCycle to '60' second(s) as specified in Configuration file.ReportingServicesService!library!4!29/06/2006-14:33:41:: i INFO: Initializing RunningRequestsDbCycle to '60' second(s) as specified in Configuration file.ReportingServicesService!library!4!29/06/2006-14:33:41:: i INFO: Initializing RunningRequestsAge to '30' second(s) as specified in Configuration file.ReportingServicesService!library!4!29/06/2006-14:33:41:: i INFO: Initializing CleanupCycleMinutes to '10' minute(s) as specified in Configuration file.ReportingServicesService!library!4!29/06/2006-14:33:41:: i INFO: Initializing DailyCleanupMinuteOfDay to default value of '120' minutes since midnight because it was not specified in Configuration file.ReportingServicesService!library!4!29/06/2006-14:33:41:: i INFO: Initializing WatsonFlags to '1064' as specified in Configuration file.ReportingServicesService!library!4!29/06/2006-14:33:41:: i INFO: Initializing WatsonDumpOnExceptions to 'Microsoft.ReportingServices.Diagnostics.Utilities.InternalCatalogException,Microsoft.ReportingServices.Modeling.InternalModelingException' as specified in Configuration file.ReportingServicesService!library!4!29/06/2006-14:33:41:: i INFO: Initializing WatsonDumpExcludeIfContainsExceptions to 'System.Data.SqlClient.SqlException,System.Threading.ThreadAbortException' as specified in Configuration file.ReportingServicesService!library!4!29/06/2006-14:33:41:: i INFO: Initializing SecureConnectionLevel to '0' as specified in Configuration file.ReportingServicesService!library!4!29/06/2006-14:33:41:: i INFO: Initializing DisplayErrorLink to 'True' as specified in Configuration file.ReportingServicesService!library!4!29/06/2006-14:33:41:: i INFO: Initializing WebServiceUseFileShareStorage to 'False' as specified in Configuration file.ReportingServicesService!servicecontroller!9!29/06/2006-14:33:45:: Total Physical memory: 267894784ReportingServicesService!library!4!29/06/2006-14:33:45:: e ERROR: Throwing Microsoft.ReportingServices.Diagnostics.Utilities.UnknownUserNameException: The user or group name 'VIDICOMASPNET' is not recognized., ; Info: Microsoft.ReportingServices.Diagnostics.Utilities.UnknownUserNameException: The user or group name 'VIDICOMASPNET' is not recognized.ReportingServicesService!servicecontroller!4!29/06/2006-14:33:47:: e ERROR: Exception caught starting RPC server: Microsoft.ReportingServices.Diagnostics.Utilities.UnknownUserNameException: The user or group name 'VIDICOMASPNET' is not recognized. at Microsoft.ReportingServices.Library.Native.NameToSid(String name) at Microsoft.ReportingServices.Library.ServiceAppDomainController.StartRPCServer(Boolean firstTime)

View 5 Replies View Related

T-SQL (SS2K8) :: Generate Working Schedule For Employees For X-days Ahead Based On Starting Date

May 6, 2014

I would like to generate a working schedule for employees for x-days ahead based on a starting date that the user can enter.

I have got 3 relevant tables:

1. Table X with (1) resourcenumber, (2) starting date working schedule and (3) the daynumber representing the starting date (this is ISO so 1 for Monday, 2 for Tuesday etc.)

2. Table Y has the schedule itself and can hold a 7-days schedule or a 14-days schedule. In case of 7 days schedule there a 14 (!) records with (1) resourcenumber, (2) daynumber, (3) starting hour a.m. (4) ending hour a.m (5) starting hour p.m and (6) ending hour p.m. In case of a 14-days schedule there are 28 records (a.m. and p.m. records)

3. Table Z with resource data.

An example to clarify (for fake employee 100):

Table X:
Resource: 100
Starting date: 2012-03-01 (from this date the schedule will be effective)
Daynumber: 4 (2012-03-01 was a Thursday)

Table Y (Resource has a 14 days schedule because per 2 weeks Monday is an off-day):

Record 1 shows: Resource: 100, Daynumber: 1 (= Monday, working day), AM-Starting hour: 09:00, AM-Ending hour: 13:00, PM-starting hour: 13:30, PM-ending hour: 17:30
Record 2: same but daynumber is 2
Record 3: same but daynumber is 3 etc.
...
Record 8 shows: Resource: 100, Daynumber: 8 (= Monday, off-day), AM-Starting hour: 00:00, AM-Ending hour: 00:00, PM-starting hour: 00:00, PM-ending hour: 00:00
Record 9: same as record 2 but daynumber is 9.
etc.
...
Record 14: same as record 7 but day is 14 (= last day)

The weekend days show as 00:00 for the hours (same as day 8 in example)

I generated the working schedule with a CROSS APPLY function based on the starting date and the x-number of days ahead.

I then evaluate the actual daynumber corresponding with that date with the daynumber in table Y. That works fine with a 7-days schedule but I can't get it fixed with a 14-days schedule. Day 8 in that schedule represents an actual day 1 but how do I know what actual date day 8 is ... I think I have to start with the starting date in table X ...

I think ideally I would like to have the generated days as follows (as an example in case of a 14-days schedule starting 2014-05-01 for 30 days ahead):

2014-05-01 = day 4 (= actual daynumber)
2014-05-02 = day 5
2014-05-03 = day 6
...
2014-05-10 = day 13
2014-05-11 = day 14
2014-05-12 = day 1
2014-05-13 = day 2
2014-05-14 = day 3
...
2014-05-24 = day 13
2014-05-25 = day 14
2014-05-26 = day 1
2014-05-27 = day 2
...
2014-05-31 = day 6

With this done I can compare the actual daynumber with the daynumber in Table Y.

The rownumber that the CROSS APPLY function generates has to be reset to 1 after day 14. I tried PARTITION BY in THE ROW_NUMBER function but to no avail ... The only field I can partition by is the maximum value of the daynumber (14 is the example) but that is not allowed in the rownumber function.

View 0 Replies View Related

T-SQL (SS2K8) :: Sort Data By Using ORDER BY

Mar 7, 2013

When We sort the data by using ORDER BY , which sorting algorithm method will be used by SQL optimizer?

1) Bubble Sort
2) Quick Sort
3) Merge Sort
4) Heap Sort
5) Insertion Sort

View 9 Replies View Related

T-SQL (SS2K8) :: Partition And Order Records

Oct 8, 2014

I have the below requirement:

1. Group records according to docno column.
2. Records will sort in desc order. (According to date1 column)
3. In date1 column if more than one date is same than we ll consider the date2 column.
EX: 2008-04-30 00:00:00is same here so sorting will happen based on Date2 column. So internal sorting should happen instead assigning random values.
4. Number column is the expected output column.

docnodate1 date2 Number
d1 2008-08-25 00:00:00 2009-09-08 11:23:41 1
d1 2008-04-30 00:00:00 2008-09-08 14:40:53 2
d1 2008-04-30 00:00:00 2008-09-08 14:29:43 3
d1 2008-04-30 00:00:00 2008-09-08 13:30:04 4

[Code] ....

View 5 Replies View Related

Order Of GROUP BY - Does It Matter?

Oct 23, 2006

Does the order of columns make a difference in the GROUP BY?Woulde GROUP BY A, B, Creturn different results thanGROUP BY C,A,B ?

View 3 Replies View Related

Using Group By And Order By Clause

May 21, 2014

I have a a grid (Fig-1) where i have LineID and corresponding RankValue. I want to sort out the Grid like (Fig-2) where It will be sorted based on Rank Value(Higher to lower) but LineID group should maintain. I am using SqlServer 2008.

View 3 Replies View Related

Help Needed In Group By With Order By

Apr 15, 2008

hi,

my table data:

Sensor_Serial_No date_time Attribute_Flag
-----------------------------------------------------
2 2008-03-03 01:00:00 5
2 2008-03-03 06:00:00 5

my result should be like this

Sensor_Serial_No no of times occured Attribute_Flag lastoccurence

2 2 5
2008-03-03 06:00:00

please give me query for this please

View 2 Replies View Related

Group By And Order By Problem

Sep 22, 2007

I have the following statement that is working just fine:


SQL = "select* from products GROUP BY name ORDER BY price"

I have many products with the same name but with different prices and I want to display unique records along with other information but I want to display the cheapest product first. It is displaying the first record in the table with that name but not the cheapest distinct record in the list of all distinct product names.
Not sure of the above makes sense so this is the kind of data in the table.

Name= Test1, Price= 25
Name= Test2, Price= 20
Name= Test1, Price= 15
Name= Test3, Price=30


When listing the products it will show the following order:

Test2, 20
Test1, 25
Test3, 30

However I want it to display:

Test1, 15
Test2, 25
Test3, 30

Thanks for any help.

Daniel

View 3 Replies View Related

Using Group By And Order By With The Same Column

Apr 9, 2008

Hi All,

Please let me know if both query fetch the same result

Select Col1, Col2, Col3, Col4 from Table1 Group By Col1, Col2, Col3, Col4
Order By Col1, Col2



Select Col1, Col2, Col3, Col4 from Table1
Order By Col1, Col2

Thanks,
Muthu

View 7 Replies View Related

T-SQL (SS2K8) :: Display Result In Required Order

Jun 17, 2014

I have an issue while display the result in the required order. How to get the required output.

Code :

USE tempdb
GO

IF OBJECT_ID('tempdb..#VersionFormat_tbl') IS NOT NULL
DROP TABLE #VersionFormat_tbl
CREATE TABLE #VersionFormat_tbl
(
[FormatID] [smallint] NOT NULL,
[Description] [varchar](50) NULL,

[Code] ....

Present output :
fileExtension FormatID Description fileExtension versionFormatTypeId
txt 1 Text txt 1
html 2 HTML html 1
xml 3 XML xml 1
pdf 4 PDF pdf 1
xls 5 Excel xls 1
doc 6 Word doc 1

Required output:
fileExtensionFormatIDDescriptionfileExtensionversionFormatTypeId
html2HTMLhtml1
xls5Excelxls1
doc6Worddoc1
pdf4PDFpdf1
txt1Texttxt1
xml3XMLxml1

View 5 Replies View Related

T-SQL (SS2K8) :: Order By On Date Descending In A Column

Jun 18, 2014

Below SQL gives the results of 2 columns and I need the DepartmentandDate column to be ORDER BY on date desc for that CID. Expected result is in the screenshot(attachment)...

IF OBJECT_ID('Tempdb..#tTable') IS NOT NULL
DROP TABLE #tTable
CREATE TABLE #tTable(CID INT, CDate DATETIME, Dept VARCHAR(25))
INSERT INTO #tTable(CID, CDate, Dept)
VALUES
(111, '2014-01-14 00:00:00.000','B is alphabet'),

[Code] ....

View 6 Replies View Related

T-SQL (SS2K8) :: Order By Numeric Doesn't Work

Sep 26, 2014

the select statement in the join orders the numbers asc and if you run everything below the case statement it works, but as you run the whole thing the order is no longer there.

I need the order to work. What am I missing?

--Assign Intern's students

Update tblObjectType3_14
Set [FldNumeric23764]= case when tID = 1 and Q.n <= 100 then 2
when tID = 2 and Q.n <= 150 then 2
when tID = 3 and Q.n <= 150 then 2
when tID = 4 and Q.n <= 130 then 2

[code]....

View 7 Replies View Related

Tricky Group By Order By Query

Jul 23, 2005

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!?

View 5 Replies View Related

Exactly-Once-On-Order - Conversation Group Question

Sep 30, 2007



Hi,

A SSB newbie question:

Looking in a various resources, i see that a conversation group guarantees receiving messages EOID, which means that each message will be received only once, and that messages will be received in the order they were sent.



However, the documentation explicitly states that only dialogs guarantee messages order. So, I€™m a little bit confused, what's the "In-Order" stand for in "Exactly Once In Order"?







Thanks,

Ido

View 3 Replies View Related

T-SQL (SS2K8) :: Change Column Order In Dynamic Pivot?

Sep 17, 2014

I am creating dynamic pivot and my column order is as below

[2015-02],[2015-04] [Prior] ,[2014-08],[2014-11]

but i want to display as below:

[Prior],[2014-08],[2014-11],[2015-02],[2015-04]

View 1 Replies View Related

T-SQL (SS2K8) :: Select Statement With Derived Order Status

Aug 28, 2015

Here is my requirement

Table 1 Order
order ID, Sales order ID order date, order type

Table 2 Order details
Order Details ID, Order ID, Order Stage

Table 3 Related Order details
Order ID(FK to Order ID), Related Order Details ID(FK to Order Details ID), Related Order ID( FK to Order ID)

Here is example

Table 1 Order
1, 1234, 2015-01-01, Refill
2, 1234, 2015-02-02, Extension

Table 2 Order Details
1, 1, Approved
2, 1, Approved
3, 2, Rejected

Table 3 Related Order Details
2, 1, 1
2, 2, 1

I have to Select Order, Order Details and Order Status

Order Status is determined from Order Stage as follows:

If, at least one order detail line(from Order Details and Related Order details table) is approved, that Order status=Approved.

For the example, Order Status of Order ID=2, is Approved based on order status for order details lines 3(from table 2) and order details ID 1 and 2 (from table 3)

How to combined order stage from table 2 and table 3 and then compute order status.

View 3 Replies View Related

T-SQL (SS2K8) :: Group By Performance FK / PK

Jul 10, 2014

We have a medium sized database with the next tables:

- PA: 525000 records
- PR: 780000 records
- R: 1000 records
- B: 45 records

PA: PK = PAARDCODE

PR: PK = PAARDREGISTERCODE
PR: FK = PAARDCODE
PR: FK = REGISTERCODE

R: PK = REGISTERCODE
R: FK = BOEKCODE

B: PK = BOEKCODE

When I group by B.BOEKCODE the query lasts: 10 (or more when 'where' option is added) seconds
When I group by R.BOEKCODE the query lasts less than 2 seconds.

SELECT B.BOEKOMSCHRIJVING, B.BOEKCODE -- or R.BOEKCODE
FROM PA
INNER JOIN PR ON PA.PAARDCODE = PR.PAARDCODE
INNER JOIN R ON R.REGISTERCODE = PR.REGISTERCODE
INNER JOIN B ON R.BOEKCODE = B.BOEKCODE
GROUP BY BOEKOMSCHRIJVING, B.BOEKCODE -- or R.BOEKCODE
ORDER BY BOEKOMSCHRIJVING Why is the option of B.BOEKCODE a lot slower compared to R.BOEKCODE?

View 9 Replies View Related

T-SQL (SS2K8) :: Group By With Nulls?

Jun 12, 2015

Table Clients is master table with all records, joining against Assets table that may or may not have a matching entry.

Trying to sum an asset type against table and no matter what kind of join I do I cannot get SQL to return a NULL match against the Clients ID value. All I get back are matching rows.

Here's the SQL:

select c.cindex,SUM(a.value) AS 'Total Assets' from Clients c
"the join" Assets2012 a on a.clientid=c.cindex
where (c.ClientClass<=7 AND c.ClientClass<>6) AND a.assettype = 2
group by c.cindex

But no matter what type of join I do, left, right, left outer, inner, I am not getting back NULL values for client records that have no matching asset records.

Strange thing is, by removing the "and assettype=2" part I get the whole database back, with NULL's but not the range I'm looking for.

View 2 Replies View Related

T-SQL (SS2K8) :: Select MAX Per Group?

Jul 22, 2015

I want to select all the Rows with Grouping done by PARENTID and Max value in REVNR per GROUP.

I am able to get one MAX row per Group but not all the Rows which has MAX value.

Here is my Table sample.

CREATE TABLE #TableA0 (
iINDEX INT
,PARENTID INT
,DOCUMENTID INT
,QTY INT

[code]....

In the result how do i get just 2 ParentIDs but multiple rows of DocumentID.

View 7 Replies View Related

I Had Set My Windows Service's Startup Type As Automatic But It Is Not Starting Up Automatically When The System Is Starting.

May 18, 2007

Hello,



I had created a windows service using C#. I set its Startup Type as Automatic but it is not getting started automatically when my System Starts.

View 3 Replies View Related

Analysis :: Measure Group Execution Order

Sep 21, 2015

I am executing a CUBE which is having 9 measure groups, i need to know order of the execution of the measure groups.??

ex:
1. fsales
2.ftranssales
3.fitem
4.fstores..............etc...

I know, the dimension are executed first, but not aware of the order of execution of measure group.

View 4 Replies View Related

T-SQL (SS2K8) :: Joining Two Tables - Output Result In The Order By DivID

Mar 25, 2014

I have two tables to be joined

Doc:
ID, DivID

Division:
ID, Div
CREATE TABLE [dbo].[Doc](
[ID] [int] NULL,
[DivID] [int] NULL

[Code] ...

As you can see, some Divisions have no correspondents in Doc, I want to show the count(1) result as 0 for those Division, and output the result in the order by DivID

View 5 Replies View Related

T-SQL (SS2K8) :: Unpivot Sorting Column In Alphabetical Order Automatically

Jan 27, 2015

I am having problem with the unpivot function of sql 2012, i unpivot my column then i get the result that i wanted but the error that i was encountering was the unpivot is automatically sort the column in alphabetically order which is not I desire,

Here is my code

@syear nvarchar(50)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

[Code] ....

View 1 Replies View Related

T-SQL (SS2K8) :: Order Change In Parent To Its Child Tables Using FK Relations?

Apr 20, 2015

I have used Aasim Abdullah's (below link) stored procedure for dynamically generate code for deletion of child tables based on parent with certain filter condition. But I am getting a output which is not proper (Query 1). I would like to have output mentioned in Query 2.

Link:

[URL]

--[Patient] is the Parent table, [Case] is child table and [ChartInstanceCase] is grand child

--When I am deleting a grand child table, it should be linked to child table first followed by Parent

--- query 1

DELETE Top(100000) FROM [dbo].[ChartInstanceCase]
FROM [dbo].[Patient] INNER JOIN [dbo].[Case] ON [Patient].[PatientID] = [Case].[PatientID]
INNER JOIN [dbo].[ChartInstanceCase] ON [Case].[CaseID] = [ChartInstanceCase].[CaseId]
WHERE [Patient].PracticeID = '55';

--Query 2

DELETE Top(100000) [dbo].[ChartInstanceCase]
FROM [dbo].[ChartInstanceCase] INNER JOIN [dbo].[Case] ON [ChartInstanceCase].[CaseId]=[Case].[CaseID]
INNER JOIN [dbo].[Patient] ON [Patient].[PatientID] = [Case].[PatientID]
WHERE [Patient].PracticeID = '55';

how to modify the SP 'dbo.uspCascadeDelete' to get the output as Query 2.

View 1 Replies View Related

T-SQL (SS2K8) :: Group Columns To One Row In A Table

Apr 1, 2014

I have a table with the following columns

Num,ID,Pos,Value
74 ,1,2,beck
74 ,1,2,greg
74 ,1,9,mike
74 ,1,9,laggo
74 ,2,2,beck
74 ,2,2,greg
74 ,2,9,mike
74 ,2,9,laggo

I am trying to get the result as follows.

Num Id Final Value

74 1 2,beck,greg;9,mike,laggo
74 2 2,beck,greg;9,mike,laggo

I tried to use Stuff and XMLpath but it is not giving me distinct results.

View 4 Replies View Related

T-SQL (SS2K8) :: MIN Across Multiple Columns With GROUP BY

Jan 13, 2015

How to get the lowest U.Price along with FX and Rate.

ID-U.Price-FX-Rate

1280 19.1196 EUR 3.85
1280 46.2462 USD 3.63
1280 6.32 RM 1.00

Required output.

ID-U.Price-FX-Rate

1280 6.32 RM 1.00

View 4 Replies View Related

T-SQL (SS2K8) :: Query To Group By Only One Column

Jan 21, 2015

How can we write the query using groupby. I need to have group by only one column. Is it possible through subquery?

select col1, col2, col3
from testtable
group by col1.

View 4 Replies View Related

T-SQL (SS2K8) :: Skipping Group With FOR XML Path

May 18, 2015

I have a table with a text field and a grouping number.

1, A
1, B
1, C
1, D
1, E

2, A
2, C
2, D
2, E

3, A
3, B
3, C
3, F

4, A
4, B
4, D
4, F
4, G

I assemble the text fields into strings by group, using the FOR XML PATH('') function, so that I end up with

1, A-B-C-D-E
2, A-C-D-E
3, A-B-C-F
4, A-B-D-F-G

This all works fine. I now have a requirement to leave out some groups, based on whether the GROUP does or does not contain a certain text. For instance, if I want only GROUPS with 'E', my result set should be

1, A-B-C-D-E
2, A-C-D-E

If I want only those GROUPS that DO NOT have 'E', my result set should be

3, A-B-C-F
4, A-B-D-F-G

I can (and have) put a condition on the assembled string, but it seems to me that it should be possible to make the cut earlier. That is, rather than assembling the string, scanning it with a Like '%E%' condition and discarding what doesn't meet my needs, I would like to stop scanning the incoming GROUP as soon as a mismatch is detected.

If I want all groups that will not contain an 'A', for instance, it is pointless for the query engine to read in 'A', 'B', 'C', 'D', 'E', put them all together into a string, compare it with a wildcard and throw it away. As soon as it sees the 'A', it has enough information to completely skip over the rest of that group. The abbreviated examples here are trivial, but the real code is not.

All I've been able to dream up so far results in skipping the RECORD that contains the 'A', but not the entire group.

View 9 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved