Adding rows searching similar fields - sql

the data is getting logged in the below format .
name count
'EB4_1F_N_MAIN_IN'
'EB4_1F_N_MOD4_IN'
'EB4_1F_N_MOD5_IN'
'EB4_1F_S_MAIN_IN'
'EB4_2F_N_MAIN_IN'
'EB4_2F_S_MAIN_IN'
'EB4_3F_N_MAIN_IN'
'EB4_3F_S_MAIN_IN'
'EB4_3F_SE_TRN_IN'
'EB4_4F_N_MAIN_IN'
'EB4_4F_S_MAIN_IN'
'EB4_4FSE_TRUN_IN'
'EB4_5F_S_MAIN_IN'
'EB4_GF_N_MAIN_IN'
'EB4_GF_S_MAIN_IN'
'EB5_1F_N_MAIN_IN'
'EB5_1F_S_MAIN_IN'
'EB5_2F_N_MAIN_IN'
'EB5_2F_S_MAIN_IN'
'EB5_2F_SW_DMZ_IN'
'EB5_3F_N_MAIN_IN'
'EB5_3F_S_MAIN_IN'
'EB5_4F_N_MAIN_IN'
'EB5_4F_S_MAIN_IN'
'EB5_5F_N_MAIN_IN'
'EB5_5F_S_MAIN_IN'
'EB5_6F_N_MAIN_IN'
'EB5_6F_S_MAIN_IN'
'EB5_DC_EPX_ST_IN'
'EB5_DC_FM_RM1_IN'
'EB5_DC_FM_RM2_IN'
'EB5_DC_NEPAHU_IN'
'EB5_DC_NWPAHU_IN'
'EB5_DC_SO_NWE_IN'
'EB5_GF_N_MAIN_IN'
'EB6_1F_N_MAIN_IN'
'EB6_1F_S_MAIN_IN'
'EB6_1F_SW_ODC_IN'
'EB6_2F_N_MAIN_IN'
'EB6_2F_S_MAIN_IN'
'EB6_3F_N_MAIN_IN'
'EB6_3F_S_MAIN_IN'
'EB6_4F_N_MAIN_IN'
'EB6_4F_S_MAIN_IN'
'EB6_5F_CISCOMAIN'
'EB6_5F_N_MAIN_IN'
'EB6_5F_S_MAIN_IN'
'EB6_GF_N_MAIN_IN'
'EB6_GF_S_MAIN_IN'
Above is the snapshot of the table . count is not shown .My requirement is to add the count of similar names field like adding all 1F_N and similarly .
can u please tell me how do i do this .

Maybe something like this is what you are looking for? I tested using SQL Server as you didn't specify what DBMS you are using. Also you didn't define how you determine similarity... I'm used the four char substring starting at 6 in this query, but it should be easily adapted to suit your needs.
SELECT a.name,
b.cnt AS Count
FROM table1 a
INNER JOIN (SELECT Substring(name, 6, 4) AS substr,
Count(*) AS cnt
FROM table1
GROUP BY Substring(name, 6, 4)) b
ON Substring(a.name, 6, 4) = b.substr
Sample SQL Fiddle

Related

How to include more than one value in IN operator in Big Query

I have the following query that is working fine in Big Query:
SELECT
date,
nombre,
i.identifier,
i.hour
FROM
`table` t, unnest(identifier_s) i
where 2 in unnest(i.hour)
However, I need to include more integers in the search value of the IN operator. Something like this:
...where (2 or 5 or 6) in unnest...
Consider below example - hope yo will be able to adopt it to your specific use-case
select date, nombre, i.identifier, i.hour
from `adsmovil-produccion.analisis_alejandro.100_temp_pers` t,
unnest(identifier_s) i
where exists (
select 1
from unnest(i.hour) x
join unnest([2, 5, 6]) x
using(x)
)

conditional IIF in a JOIN

I have the next data base:
Table Bill:
Table Bill_Details:
And Table Type:
I want a query to show this result:
The query as far goes like this:
SELECT
Bill.Id_Bill,
Type.Id_Type,
Type.Info,
Bill_Details.Deb,
Bill_Details.Cre,
Bill.NIT,
Bill.Date2,
Bill.Comt
FROM Type
RIGHT JOIN (Bill INNER JOIN Bill_Details
ON Bill.Id_Bill = Bill_Details.Id_Bill)
ON Type.Id_Type = Bill_Details.Id_Type
ORDER BY Bill.Id_Bill, Type.Id_Type;
With this result:
I'm not sure how to deal or how to include this:
Type.600,
Type."TOTAL",
IIF(SUM(Bill_Details.Deb) - Sum(Bill_Details.Cre) >= 0, ABS(SUM(Bill_Details.Deb) - Sum(Bill_Details.Cre)), "" ),
IIF(SUM(Bill_Details.Deb) - Sum(Bill_Details.Cre) <= 0, ABS(SUM(Bill_Details.Deb) - Sum(Bill_Details.Cre)), "" )
The previous code is the responsable of include new data in some fields, since all of the other fields will carry the same data of the upper register. I'll apreciate some sugestions to acomplish this.
Here is a revised version of the UNION which you removed from the question. The original query was a good start, but you just did not provide sufficient details about the error or problem you were experiencing. My comments were not meant to have you remove the problem query, only that you needed to provide more details about the error or problem. In the future if you have a UNION, make sure the each query of the UNION works separately. Then you could debug problems easier, one step at a time.
Problems which I corrected in the second query of the UNION:
Removed reference to table [Type] in the query, since it was not part of the FROM clause. Instead, I replaced it with a literal value.
Fixed FROM clause to join both [Bill] and [Bill_Details] tables. You had fields from both tables, so why would you not join on them just like in the first query of the UNION?
Grouped on all fields from table [Bill] referenced in the SELECT clause. You must either group on all fields, or include them in aggregate expressions like Sum() or First(), etc.
Replaced empty strings with Nulls for the False cases on Iif() statements.
SELECT
Bill.Id_Bill, Type.Id_Type, Type.Info,
Bill_Details.Deb,
Bill_Details.Cre,
Bill.NIT, Bill.Date2, Bill.Comt
FROM
Type RIGHT JOIN (Bill INNER JOIN Bill_Details
ON Bill.Id_Bill = Bill_Details.Id_Bill)
ON Type.Id_Type = Bill_Details.Id_Type;
UNION
SELECT
Bill.Id_Bill, 600 As Id_Type, "TOTAL" As Info,
IIF(SUM(Bill_Details.Deb) - Sum(Bill_Details.Cre) >= 0, ABS(SUM(Bill_Details.Deb) - Sum(Bill_Details.Cre)), Null ) As Deb,
IIF(SUM(Bill_Details.Deb) - Sum(Bill_Details.Cre) <= 0, ABS(SUM(Bill_Details.Deb) - Sum(Bill_Details.Cre)), Null ) As Cre,
Bill.NIT, Bill.Date2, Bill.Comt
FROM Bill INNER JOIN Bill_Details
ON Bill.Id_Bill = Bill_Details.Id_Bill
GROUP BY Bill.Id_Bill, Bill.NIT, Bill.Date2, Bill.Comt;

Questsion about using HAVING in ACCESS SQL

I wanted to get clarity on how the HAVING component in an SQL statement works, particularly with a SQL statement that has multiple joins. Consider the following SQL select statement:
SELECT
p.id,
p.first_name as [First Name],
p.last_name as [Last Name]
FROM
( [tbl_person] as p
INNER JOIN [tbl_person_languages] as pl
ON [p].[id] = [pl].[person_id])
INNER JOIN [tbl_person_crossCuttingSkills] As ccp
ON [p].[id] = [ccp].[person_id]
WHERE
cint(pl.language_id) in (12,14) AND
cint(ccp.skill_id) in (55)
GROUP BY
p.id,
p.first_name,
p.last_name
HAVING
count(pl.language_id) =2 AND
count(ccp.skill_id) = 1
I want to pull out records, from tbl_person, where a record has all of the WHERE requirements. For example: I want to select all users where they speak a languages Italian (with ID 12) and Spanish (ID 15) AND have a skill of cooking (55). They need to have all requirements, not just one or more. I was under the assumption this is where you would use GROUP BY and HAVING. With the HAVING:
count(pl.language_id) =2
I use count = 2 because there are two options in the language WHERE clause (12 and 14)
And I use
count(ccp.skill_id) = 1
Because there is one value in the WHERE clause (55).
Is this the correct way to be doing this? For some reason, this returns no records (I have one record in my DB of a person who fits these requirements exactly). But, if I change my HAVING to:
count(pl.language_id) =2 AND
count(ccp.skill_id) = 2
It works fine. Why is this? Are my assumptions of how this works incorrect or is there something else going on?
Remember what count() does . . . it counts non-NULL values. So, you are counting two non-NULL values then the counts are the same.
In most dialects of SQL, you can fix this by doing:
HAVING count(distinct pl.language_id) = 2 AND count(distinct ccp.skill_id) = 1
But that doesn't work in MS Access, because MS Access does not support COUNT(DISTINCT).
So, you can be more verbose. In your case:
HAVING SUM(iif(cint(pl.language_id) = 12, 1, 0)) > 0 AND
SUM(iif(cint(pl.language_id) = 14, 1, 0)) > 0 AND
SUM(iif(cint(ccp.skill_id) = 55, 1, 0)) > 0
I'm sorry this HAVING clause is not simpler. You could switch to another database (such as SQL Server Express) that more closely aligns with ANSI functionality.

SQL "Count (Distinct...)" returns 1 less than actual data shows?

I have some data that doesn't appear to be counting correctly. When I look at the raw data I see 5 distinct values in a given column, but when I run an "Count (Distinct ColA)" it reports 4. This is true for all of the categories I am grouping by, too, not just one. E.g. a 2nd value in the column reports 2 when there are 3, a 3rd value reports 1 when there are 2, etc.
Table A: ID, Type
Table B: ID_FK, WorkID, Date
Here is my query that summarizes:
SELECT COUNT (DISTINCT B.ID_FK), A.Type
FROM A INNER JOIN B ON B.ID_FK = A.ID
WHERE Date > 5/1/2013 and Date < 5/2/2013
GROUP BY Type
ORDER BY Type
And a snippet of the results:
4|Business
2|Design
2|Developer
Here is a sample of my data, non-summarized. Pipe is the separator; I just removed the 'COUNT...' and 'GROUP BY...' parts of the query above to get this:
4507|Business
4515|Business
7882|Business
7889|Business
7889|Business
8004|Business
4761|Design
5594|Design
5594|Design
5594|Design
7736|Design
7736|Design
7736|Design
3132|Developer
3132|Developer
3132|Developer
4826|Developer
5403|Developer
As you can see from the data, Business should be 5, not 4, etc. At least that is what my eyes tell me. :)
I am running this inside a FileMaker 12 solution using it's internal ExecuteSQL call. Don't be concerned by that too much, though: the code should be the same as nearly anything else. :)
Any help would be appreciated.
Thanks,
J
Try using a subquery:
SELECT COUNT(*), Type
FROM (SELECT DISTINCT B.ID_FK, A.Type Type
FROM A
INNER JOIN B ON B.ID_FK = A.ID
WHERE Date > 5/1/2013 and Date < 5/2/2013) x
GROUP BY Type
ORDER BY Type
This could be a FileMaker issue, have you seen this post on the FileMaker forum? It describes the same issue (a count distinct smaller by 1) with 11V3 back in 03/2012 with a plug in, then updated with same issue with 12v3 in 11/2012 with ExecuteSQL. It didn't seem to be resolved in either case.
Other considerations might be if there are any referential integrity constraints on the joined tables, or if you can get a query execution plan, you might find it is executing the query differently than expected. not sure if FileMaker can do this.
I like Barmar's suggestion, it would sort twice.
If you are dealing with a bug, directing the COUNT DISTINCT, Join and/or Group By by structuring the query to make them happen at different times might work around it:
SELECT COUNT (DISTINCT x.ID), x.Type
FROM (SELECT A.ID ID, A.Type Type
FROM A
INNER JOIN B ON B.ID_FK = A.ID
WHERE B.Date > 5/1/2013 and B.Date < 5/2/2013) x
GROUP BY Type
ORDER BY Type
you might also try replacing B.ID_FK with A.ID, who knows what context it applies, such as:
SELECT COUNT (DISTINCT A.ID), A.Type

Using output of one sql query into another

I have 2 SQL queries in single line as follows:
SELECT * FROM (SELECT NameCode,Name FROM tblNames) AS X, (SELECT SUM(Mo+Tu) FROM tblFieldDays WHERE tblFieldDays.NameCode =36)
The first query i.e. (SELECT NameCode,Name FROM tblNames) gives a list of users.
Now I want to calculate sum of Mo+Tu i.e. SUM(Mo+Tu) for each user generated by first query.
i.e. I want to provide NameCode generated in first query instead of current 36 value which static just for example
I also tried to use IN statement as follows:
SELECT * FROM (SELECT NameCode,Name FROM tblNames) AS X, (SELECT SUM(Mo+Tu) FROM tblFieldDays WHERE tblFieldDays.NameCode IN (X.NameCode)) AS Y
But didnt work.
Can anyone help?
Thanks.
SELECT NameCode,
Name,
UserFieldDays = SUM(fieldDays.Mo + fieldDays.Tu)
FROM tblNames users
JOIN tblFieldDays fieldDays ON users.NameCode = fieldDays.NameCode
GROUP BY users.NameCode, users.Name
This is probably what you are looking for:
SELECT NameCode, Name,
(SELECT SUM(MO + TU)
FROM tblFieldDays Y
WHERE Y.NameCode IN (X.NameCode))
FROM tblNames X;
This statement selects all names and code from your table tblNames and adds the sum with a sub select.
Check out this Fiddle.
Hope this helps ... Cheers!