Simple Group By And Count With Join Not Matching Between Sql Server And Sql CE
Apr 2, 2007
In Sql Server
Code Snippet
CREATE TABLE t_contact
(
Id uniqueidentifier,
FirstName nvarchar(50),
LastName nvarchar(50),
TaskId uniqueidentifier
)
GO
CREATE TABLE t_task
(
Id uniqueidentifier,
Start datetime
)
GO
INSERT INTO t_task (Start, Id) VALUES ('3/25/2007 12:00:00 AM', '5949b899-3230-4d30-b210-9903015b2c6b')
INSERT INTO t_contact (FirstName, LastName, TaskId, Id) VALUES ('Adam', 'Tybor', '5949b899-3230-4d30-b210-9903015b2c6b', '304fc653-d366-404b-878d-9903015b2c6f');
INSERT INTO t_task (Start, Id) VALUES ('4/1/2007 12:00:00 AM', '4bd2df60-ca6c-493d-8824-9903015b2c6f')
INSERT INTO t_contact (FirstName, LastName, TaskId, Id) VALUES ('John', 'Doe', '4bd2df60-ca6c-493d-8824-9903015b2c6f', '7b91f7d6-d71e-47b4-a7ec-9903015b2c6f')
INSERT INTO t_task (Start, Id) VALUES ('3/29/2007 12:00:00 AM', '05167e74-cf63-452a-8f25-9903015b2c6f')
INSERT INTO t_contact (FirstName, LastName, TaskId, Id) VALUES ('Jane', 'Doe', '05167e74-cf63-452a-8f25-9903015b2c6f', '6871ee8d-bc83-478c-8a7c-9903015b2c6f')
GO
SELECT task1_.Start as y0_, count(this_.FirstName) as y1_ FROM t_contact this_ inner join t_task task1_ on this_.TaskId=task1_.Id GROUP BY task1_.Start
GO
Result (Expected)
2007-03-25 00:00:00.000 1
2007-03-29 00:00:00.000 1
2007-04-01 00:00:00.000 1
Result In Sql CE (UnExpected)
2007-03-25 00:00:00.000 3
2007-03-29 00:00:00.000 3
2007-04-01 00:00:00.000 3
Can SQL CE not count with a join? Seems like this a bug with aggregates or joins. I tried everything to try and get the correct result but no luck.
Thanks Adam
View 3 Replies
ADVERTISEMENT
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
Feb 23, 2006
this shoud be simple, but my brain's hurting.
I have a table with 3 fields...
IDnum,content,application
eg
123,stop,fb
123,start,fb
123,continue,fb
123,fault,fb
123,start,gt
123,stop,gt
123,continue,gt
123,start,ha
321,stop,fb
321,start,fb
321,continue,fb
321,fault,fb
321,start,gt
I need to know All those unique IDs that have "start" against them, but have not got "stop" against them, I'm not interested in the other content strings.
I need to query it based on a specific application ID.
there will be many records from each IDnum, but only one start for each. I need to know all those IDnums that have "started" and not "stopped"
tia
a
View 1 Replies
View Related
Jan 19, 2007
hi all,
I want to know that how can i write a stored procedure that takes an input param, i.e., a string and returns the count of the matching words between the input parameter and the value of a column in a table. e.g. I have a table Person with a column address. now my stored procedure matches and counts the number of words that are common between the address of the person and the input param string. I am looking forward for any help in this matter. I am using Sql server 2005.
View 1 Replies
View Related
Jan 12, 2006
Hello All,I've got a DATETIME field, and it includes hour:minutes:second data. I want to do selects where I can simply match on the month, day and year. For instance, something like this:SELECT * FROM QuizAttempts WHERE DateTimeTaken = '1/12/2006'And have it match anything that was taken that day, regardless of *when* it was taken. Any suggestions?Thanks! -Josh
View 2 Replies
View Related
Mar 31, 2007
And by correctly, I mean the way it *should* match, of course.I've got 2 data sources, using a left outer join, matching 2 columns. Whatever is the right side of my table is never matching and returning data. Here's my basic setup:OLE DB Source 1 (table1)
Sel Name Order JoinKeyno UniqueID 1 yesyes Column1 0 no
...
OLE DB Source 2 (table2)
Sel Name Order JoinKeyyes ID 1 yesyes Columns 0 no
...
There is a link (arrow) between UniqueID and ID, and the join type is "Left Outer Join". When I execute the statement "SELECT * FROM Table1 LEFT JOIN TABLE2 ON TABLE1.UniqueID = Table2.ID", the data returns correctly to me. What am I missing with the properties I've set above with the merge join?
View 6 Replies
View Related
Feb 28, 2008
I have been assigned to write a report that queries 3 tables. Table Store, Store_ID unique index, Cust table Cust_ID non unique index, CUST_Store_ID relates to Store Table, Table Emp, Emp_ID non unique index, Emp_Store_ID relates to Store Table
The problem is that the Employes and the customer don't relate to each other in any way. There can be stores with no Employees and there can be stores with no Customers. There are employees and customers listed multiple times in thier respective tables, and the multiple listings need to show on the report. A result set may look like this.
Store | Emp | Cust
1|1|
2|2|1
2|2|2
2| |3
2| |4
3| |5
3| |5
4|3|6
4|3|7
4|3|7
4|3|8
4|3|
4|3|
I tried a full join, but ended up with this:
Store ID ~~~~~Emp~~~~~Cust
OHCOL-811C ~~~~~10498500|OHCOL-811C|00151.00|448097821|SL0005 ~~~~~448097821| | | |N|AD-TSUACE |SPKG |146921092
OHCOL-811C ~~~~~10498500|OHCOL-811C|00152.00|448097821|SL0006 ~~~~~448097821| | | |N|AD-TSUACE |SPKG |146921092
OHCOL-811C ~~~~~10498500|OHCOL-811C|00153.00|448097821|SL0007 ~~~~~448097821| | | |N|AD-TSUACE |SPKG |146921092
OHCOL-811C ~~~~~10498500|OHCOL-811C|00154.00|448097821|SL9827 ~~~~~448097821| | | |N|AD-TSUACE |SPKG |146921092
OHCOL-811C ~~~~~10498500|OHCOL-811C|00155.00|448097821|SL0150 ~~~~~448097821| | | |N|AD-TSUACE |SPKG |146921092
OHCOL-811C ~~~~~10498500|OHCOL-811C|00151.00|448097821|SL0005 ~~~~~448097821| | | |N|AD-TSUACE |SPKG |146876651
OHCOL-811C ~~~~~10498500|OHCOL-811C|00152.00|448097821|SL0006 ~~~~~448097821| | | |N|AD-TSUACE |SPKG |146876651
OHCOL-811C ~~~~~10498500|OHCOL-811C|00153.00|448097821|SL0007 ~~~~~448097821| | | |N|AD-TSUACE |SPKG |146876651
OHCOL-811C ~~~~~10498500|OHCOL-811C|00154.00|448097821|SL9827 ~~~~~448097821| | | |N|AD-TSUACE |SPKG |146876651
OHCOL-811C ~~~~~10498500|OHCOL-811C|00155.00|448097821|SL0150 ~~~~~448097821| | | |N|AD-TSUACE |SPKG |146876651
As you can see Cust is being duplicated for each Emp. The actual result set should look like this.
Store ID ~~~~~Emp~~~~~Cust
OHCOL-811C ~~~~~10498500|OHCOL-811C|00151.00|448097821|SL0005 ~~~~~448097821| | | |N|AD-TSUACE |SPKG |146921092
OHCOL-811C ~~~~~10498500|OHCOL-811C|00152.00|448097821|SL0006 ~~~~~448097821| | | |N|AD-TSUACE |SPKG |146876651
OHCOL-811C ~~~~~10498500|OHCOL-811C|00153.00|448097821|SL0007 ~~~~~NULL
OHCOL-811C ~~~~~10498500|OHCOL-811C|00154.00|448097821|SL9827 ~~~~~NULL
OHCOL-811C ~~~~~10498500|OHCOL-811C|00155.00|448097821|SL0150 ~~~~~NULL
View 5 Replies
View Related
Dec 5, 2007
Hi, I'm working in a web project.
In our lab, all the PCs are installed with SQL Server Management Studio, and the codes of the site are left by the previous batch of programmers.
Here is briefly how our web works, the web will call the sql server providing a username (userA) and password (aaaaaa) to log into the database, then the web can connect to the database to do various functions like select, insert, update and delete.
Currently anyone can go to thier SQL Server Management Studio on thier PC to edit various things like names and columns of tables by logging in as userA.
But, we now only want a small number of users to have the ability to change things in the database. What are some ways we can do that?
Something that I've thought of is that only allow userA to log in and do functions like select, insert, update and delete and do not have the ability to edit things like names and columns of tables. Have a userB with password only known to me that can have full control of the database which I have done.
View 2 Replies
View Related
May 1, 2014
select top 15 count(*) as cnt, state from table
group by state
order by cnt desc
[code[...
Can the above three queries be combined into one and still be fast, if so how?What i am trying to go is an item count, by group, similar to ones Inbox in Outlook.
View 9 Replies
View Related
Mar 3, 2015
How to return only non matching left join records. Currently I am doing a traffic management database to learn sql.
I am checking for all parishes with no associated drivers. Currently I only have 2 of such.
The regular left join
select parish.name, driver.fname from parish left join driver on driver.parish=parish.name
Returns the all the names of the parishes and the first name of the associated drive, followed by the matches, however the two parishes with no matches have null for the first name.
View 1 Replies
View Related
Aug 9, 2007
Let's say I have a list of IDs called EntryID and each EntryID can belong to ONE table out of a group of six, what is the best way to get a listing of these?
For example:
select r.*
from #Reminders r
left join mytable1 mt1 on (r.EntryID = mt1.EntryID)
left join mytable2 mt2 on (r.EntryID = mt2.EntryID)
left join mytable3 mt3 on (r.EntryID = mt3.EntryID)
left join mytable4 mt4 on (r.EntryID = mt4.EntryID)
As you can see, #Reminders has one field called EntryID (and many rows).
In my example above, only ONE of those tables will actually be able to join but I have no idea which one has the matching EntryID.
What is the best way for me to do this? I want to grab "ReportStatus" from the corresponding "mytable"... (each "mytable" has a ReportStatus column)
View 5 Replies
View Related
Aug 10, 2015
I have two queries as below;
SELECT EventID, Role, EventDuty, Qty, StartTime, EndTime, Hours
FROM dbo.tblEventStaffRequired;
and
SELECT EventID, Role, StartTime, EndTime, Hours, COUNT(ID) AS Booked
FROM tblStaffBookings
GROUP BY EventID, Role, StartTime, EndTime, Hours;
How can I join the results of the two by matching the columns EventID, Role, StartTime and EndTime in the two and have the following columns in output EventID, Role, EventDuty, Qty, StartTime, EndTime, Hours and Booked?
View 4 Replies
View Related
Feb 6, 2008
HiI am new to SQL and am having a problem. I need to fix my query to do the following...2) get a total of the number of rows returned.
DECLARE @StartDate varchar(12)DECLARE @EndDate varchar(12)DECLARE @Region varchar(20)
SET @StartDate = '01/01/2002'SET @EndDate = '12/31/2008'SET @Region = 'Central'
SELECTA.createdon,A.casetypecodename,A.subjectidname,A.title,A.accountid,A.customerid,A.customeridname,B.new_Region,B.new_RegionName
FROM dbo.FilteredIncident AINNER JOIN dbo.FilteredAccount B ON A.customerid = B.accountid
WHERE (A.createdon >=@StartDate AND A.createdon <= @EndDate)AND (B.new_RegionName = @Region)AND (A.casetypecode = 2)
View 1 Replies
View Related
Jan 29, 2007
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.
What am I doing wrong? Thanks!
View 9 Replies
View Related
Jun 23, 2007
Hi;
I know it must be really easy but I couldn't somehow figure it out:
3 columns: pms_ID, pms_UserID, pms_FlagSeen 1 ab1 True 2 ab1 True 3 ab1 True 4 ab1 False 5 ab2 True 6 ab2 False
All I want is to count number of Falses and Total (Trues and Falses) belonging to Users (I mean by pms_UserID)
Any help would be appreciated..
Regards...
View 12 Replies
View Related
Jan 14, 2007
Hi, i am using MSSQL 2005 and i am trying to count the number of rows in a table that is null. The problem is that its returning 0 even doh its several that is null.
SELECT count(*) as nr FROM forum_board WHERE (owner = null)
have also tried
SELECT count(*) as nr FROM forum_board WHERE (owner = 'null')
still returns 0.
View 2 Replies
View Related
May 14, 2008
I know this is simple but my minds gone blank
id desc
12345 Item 1
12345 Item 2
12346 Item 3
12346 Item 4
12347 Item 5
I want to count all the distinct ID's, so for the results here I need
id count
12345 2
12346 2
12347 1
Thanks
View 5 Replies
View Related
Feb 21, 2007
I am receiving funny results from a query. To simplify, I have 2 tables (todayyesterday). Each tbl has the same 8 columns. My query joins the two tables then looks where either of two columns has changed. What is happening is that when checking one of the columns it seems as though sql is flipping the column, causing it to be returned in error.
result set
colA colB colC colD colE colF colG colG (from yesterday)
1 1 a b c d e m
1 1 a b c d m e
So what's happening is that the record above is actually the same record and should not be returned. There is a daily pmt column that changes but I am not using that in the query. Aside from that the two records are identicle.
Any help is appreciated.
View 7 Replies
View Related
Aug 19, 2006
Hi,
I have the following situation (with a site that already works and i cannot modify the database architecture and following CrossRef tables -- you will see what i mean by CrossRef tables below)
I have:
Master table Hotel
table AddressCrossRef (with: RefID = Hotel.ID, RefType = 'Hotel', AddrID)
joins
table Address (key = AddrID)
table MediaCrossRef (with RefID = Hotel.ID, RefType= 'Hotel', MediaID)
joins
table Media (with MediaID,mediaType = 'thumbnail')
foreach hotel, there definitely is a crossRef entry in AddressCrossRef and Address tables respectively (since every hotel has an address)
however not all hotels have thumbnail image
hence i have hotel inner join AddressXReff inner join Address ..... however i must have
left outer join mediaXref left outer join media
the problem is that if there is no entry in Media or mediaXref, I don't get any results
i tried to get over it by using
where (media.mediaTyple like 'thumbnail' or media.mediaType is null)
but then i started getting multiple results for each hotel because media's of type movie or full_image or etc... all got returned
any clue?
thanks
View 5 Replies
View Related
Apr 25, 2015
I have a really basic question. The following SQL query works for me:
Select EnterUserID, Enterdate
from tblCsEventReminders
where EnterDate >= Convert(datetime, '2015-04-01')
I am essentially trying to write a query to count the number of user logins after a certain date, so I need to count 'EnterUserID' but I am having problems getting the count() function to work.
View 3 Replies
View Related
Apr 26, 2005
I have a table with traffic citations. I simply want to select records where one license plate number appears multiple times in the table, and has a customer id of null in at least 1 record and customer id of NOT null in at least 1 other record.
I know this is simple, but obviously this query does NOT work:
SELECT VEHICLE_LICENSE_NUMBER
FROM CITATIONS
WHERE (CUSTOMER_ID IS NULL AND CUSTOMER_ID <> NULL)
GROUP BY VEHICLE_LICENSE_NUMBER
Any advice is much appreciated! Thank you so much for your time.
View 4 Replies
View Related
Jul 20, 2005
Suppose the following tables:parent------parentidaddressphone
View 1 Replies
View Related
Sep 3, 2004
I don't know if it's Friday or what, but I can't for the life of me come up with an easy way to do this:
I have 3 tables I want to join:
Sale Table:
Sale_No Cus_No Sale_Qty
1 Joe01 250
Order Table:
Ord_No Sale_No Order_Qty ShipToCode
1 1 20 DestA
2 1 20 DestA
3 1 20 DestA
4 1 20 DestB
5 1 20 DestB
ShipTo Table:
Cus_No ShipToCode ShipToName
Joe01 DestA Philadelphia
Joe01 DestB Chicago
Bob01 DestA Boston
A sale for say 100 tons would have 5 orders (each for 20 tons) associated with it by Sale_No. Each of those orders can go to a different ShipTo destination. Since only the ShipTo Code is stored in the Orders table, I need to get the ShipToName. However, As demonstrated in the example table above, the key in the ShipTo table is both Cus_No AND ShipToCode.
I want a list of Sales and Orders, which is an inner join on Sale_No, piece of cake. However, I then need to use the ShipTo table to go from the ShipToCode to the ShipToName. Unfortunately, Cus_No is not in the Orders table, it is back in the Sales table (proper normalization is a pain sometimes).
What I came up with is this, but is this correct?:
FROM Sales INNER JOIN
Orders ON Sales.sale_no = Orders.sale_no INNER JOIN
ShipTo ON Orders.ShipToCode = ShipTo.ShipToCode AND
Sales.cus_no = ShipTo.cus_no
View 12 Replies
View Related
Jul 4, 2007
I did:
select city, count(*) from classifieds_address group by city
You would think it would distiguish between "Koh Phangan" and "Koh Phangan ". No!!! It shows them as one row in SQL!!! Is this SQL bug?
Aberdeen 1bayombong 1Brooklyn 4Castle Peak Bay 1Causeway Bay 2Chiang Mai 1Hong Kong 1Hong Kong Wan Chai 1Hong Kong Island 10Hong Kong Sheung Wan 1Karachi 1Koh Phangan 21Koh Samui 37Koh Tao 9Kowloon 13Kowloon, Tsim Sha Tsui 1Lantau Island 1Mid-Levels 3New Territories 2New York 3North Point 1Oligar 1Tsim Sha Tsui 1Wan Chai 5Yau Ma Tei 1Yau Ma Tei, Kowloon 1
View 5 Replies
View Related
Jul 20, 2006
I've been fighting with this all day there has to be an easy way to do this in a single query:
FOR EACH fac_id, get the INSPECTION_ID of the earliest start_date:
INSPECTION_IDFAC_IDSTART_DATE
3007200406280045662004-07-07
3007200306180025662003-06-25
20082002121901114362002-12-19
30072003020600214582003-02-03
30072003020600314582003-02-05
30072003012100214802003-03-25
30072003012100314802005-02-02
20082003123000114362003-12-30
30072004061600615662004-08-26
30072004061600115662001-08-26
30072004061600515662002-08-26
30072004061600215662003-08-26
30072004061600315662004-08-26
30072004061600415662006-08-26
30072004050400415692004-09-10
DESIRED OUTPUT
INSPECTION_IDFAC_IDSTART_DATE
3007200306180025662003-06-25
20082002121901114362002-12-19
30072003020600214582003-02-03
30072003012100214802003-03-25
20082003123000114362003-12-30
30072004061600115662001-08-26
30072004050400415692004-09-10
Am i missing something...
any help would be greatly appreciated...
View 1 Replies
View Related
Jul 20, 2006
I've been fighting with this all day there has to be an easy way to do this in a single query:
FOR EACH fac_id, get the INSPECTION_ID of the earliest start_date:
INSPECTION_IDFAC_IDSTART_DATE
3007200406280045662004-07-07
3007200306180025662003-06-25
20082002121901114362002-12-19
30072003020600214582003-02-03
30072003020600314582003-02-05
30072003012100214802003-03-25
30072003012100314802005-02-02
20082003123000114362003-12-30
30072004061600615662004-08-26
30072004061600115662001-08-26
30072004061600515662002-08-26
30072004061600215662003-08-26
30072004061600315662004-08-26
30072004061600415662006-08-26
30072004050400415692004-09-10
DESIRED OUTPUT
INSPECTION_IDFAC_IDSTART_DATE
3007200306180025662003-06-25
20082002121901114362002-12-19
30072003020600214582003-02-03
30072003012100214802003-03-25
20082003123000114362003-12-30
30072004061600115662001-08-26
30072004050400415692004-09-10
View 2 Replies
View Related
Jun 13, 2006
have a table with sale_id, date, sales_person_id
i need to find out the sales_person_id's who did 1 sales every month
from jan 2003 and another query who did a sales every quarter.
How many sales person have atleast one sale every month (excluding prints) for either 2003, 2004, or 2005?
How many sales person had atleast 25 sales each year 2003-2005
View 1 Replies
View Related
Jul 9, 2013
I want my query to list all SSNS that have more than one record in the table. I have this query:
Code:
SELECT SSN, name4, count(*) from [1099_PER]
group by SSN, name4
having count(SSN) > 1
It does retrieve the right SSNS and tells me how many times the SSN occurs in the table. However, I want my query results to display their full records.
For example
SSN NAME4 COUNT
123445555 WALTER - 4
I want the query to show me all four records for this SSN. I thought removing the count field would do this, but it still gives me only one instance of each SSN.
View 6 Replies
View Related
Dec 21, 2007
I want to know how to merge the following data. I am using 4 queries below. I was hoping to do it with 1 query. Table1Dist Fund VAE AOVAW AOMD CourtMD JudgeCAC AOCAC CourtVAE JudgeVAE JudgeI want to join the following 3 queries:DcountAll DcountAOSelect Dist, Count(Dist)as Count from Table1 GROUP BY Table1.Dist Select Dist, Count(Dist) as Count from Table1 Where Dist='AO' GROUP BY Table1.DistDcountCourtSelect Dist, Count(Dist) as Count from Table1 Where Dist='Court' GROUP BY Table1.DistSELECT DCountAll.Dist, DCountAll.Count, DcountAO.Count AS AO, DcountCourt.CountFROM DcountCourt RIGHT JOIN (DcountAO RIGHT JOIN DCountAll ON DcountAO.Dist = DCountAll.Dist) ON DcountCourt.Dist = DCountAll.Dist;
View 2 Replies
View Related
May 30, 2008
hi everyone, I have a table: Help
A B C05/01/2008 100 1 05/01/2008 100 205/01/2008 100 205/01/2008 200 1 05/01/2008 200 2
SELECT A, COUNT(DISTINCT C) FROM help GROUP BY A
Result:1> 05/01/2008 2I need:1> 05/01/2008 4Thanks !!!
View 4 Replies
View Related
Nov 3, 1998
TO all SQL Gurus,
The following statement returns let say 4 records -
SELECT title, price FROM table GROUP BY title
what is the correct syntax for the statement to return the count of 4 ?
if I put ->
SELECT count (*) FROM table GROUP BY title
it'll return 1.
Thanks,
Frank
View 4 Replies
View Related
May 23, 2008
Hi everybody I have this sql code
SELECT i.infid, i.infname, i.infcalled, p.pubinfid, p.pubpub, p.publang, p.pubcount, p.pubid, n.cdpldesc
FROM info AS i INNER JOIN
pubssubscribe AS p ON i.infid = p.pubinfid INNER JOIN
newpubs AS n ON p.pubid = n.pubid
WHERE (i.infcond IS NULL)AND (p.pubid BETWEEN 30 AND 33) AND (p.pubcount > 1) AND (NOT (p.publang = '.'))
there are many records where infid appears more than once because people could subscribe in different in different publicatons.
ex.
infid pubid
1 30
1 32
1 33
2 30
etc... I want to count the infid appearing once and group it with infid
thanks
View 7 Replies
View Related
Mar 25, 2004
SELECT emp_id,COUNT(*)
FROM emp
GROUP BY empId
THe above query returns the values
empid COUNT
1 4
2 4
3 5
BUT i want sum of the count (13) in the same query. How to achieve this.
Thanks.
View 4 Replies
View Related